Coverage for jumpstarter_driver_energenie/driver_test.py: 83%

18 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-06 10:21 +0200

1import pytest 

2from pytest_httpserver import HTTPServer 

3 

4from jumpstarter.common.utils import serve 

5from .driver import EnerGenie 

6 

7def test_drivers_energenie(httpserver: HTTPServer): 

8 # Configure mock responses 

9 # 1. Login response - Match raw data string 

10 httpserver.expect_request( 

11 "/login.html", 

12 method="POST", 

13 data="pw=1" 

14 ).respond_with_data("Login successful") # Defaults to status 200 

15 

16 # 2. Response for turning ON switch 1 - Match raw data string 

17 httpserver.expect_request( 

18 "/", 

19 method="POST", 

20 data="cte1=1" 

21 ).respond_with_data("Switch turned ON") # Defaults to status 200 

22 

23 # 3. Response for turning OFF switch 1 - Match raw data string 

24 httpserver.expect_request( 

25 "/", 

26 method="POST", 

27 data="cte1=0" 

28 ).respond_with_data("Switch turned OFF") # Defaults to status 200 

29 

30 # Get the mock server's host and port 

31 host = f"{httpserver.host}:{httpserver.port}" 

32 

33 # Create EnerGenie instance with the mock server's URL 

34 instance = EnerGenie(host=host) 

35 

36 with serve(instance) as client: 

37 client.on() 

38 client.off() 

39 

40 # check_assertions will verify that all expected requests were received 

41 # in the correct order and that no unexpected requests arrived. 

42 try: 

43 httpserver.check_assertions() 

44 except AssertionError as e: 

45 print(f"httpserver assertions FAILED: {e}") 

46 raise # Re-raise the assertion error to fail the test