Debian Codegraph @

src/tests/test_all_devices.py

raw symbols SCIP
1 """
2 This tests all requests made by the pyvesync library with pytest.
3
4 All tests inherit from the TestBase class which contains the fixtures
5 and methods needed to run the tests.
6
7 The `helpers.call_api` method is patched to return a mock response.
8 The method, endpoint, headers and json arguments are recorded
9 in YAML files in the api directory, categorized in folders by
10 module and files by the class name.
11
12 The default is to record requests that do not exist and compare requests
13 that already exist. If the API changes, set the overwrite argument to True
14 in order to overwrite the existing YAML file with the new request.
15 """
16 import logging
17 from base_test_cases import TestBase
18 import call_json
19 import call_json_outlets
20 import call_json_bulbs
21 import call_json_fans
22 import call_json_purifiers
23 import call_json_humidifiers
24 import call_json_switches
25 from utils import assert_test, parse_args
26
27 logger = logging.getLogger(__name__)
28 logger.setLevel(logging.DEBUG)
29
30
31 def test_device_tests():
32 """Test to ensure all devices have are defined for testing.
33
34 All devices should have an entry in the DETAILS_RESPONSES dict
35 with response for get_details() method. This test ensures that
36 all devices have been configured for testing. The details response
37 should be located in `{device_type}Details` class of the respective
38 call_json_{device_type} module and the DETAILS_RESPONSE module variable.
39 The class variable with the details response does not matter, the dictionary
40 key of DETAILS_RESPONSES should match the device type.
41
42 Examples
43 ---------
44 class FanDetails:
45 "Core200SResponse": {'speed': 1, 'device_status': 'on'}
46
47 DETAILS_RESPONSES = {
48 'Core200S': FanDetails.Core200SResponse
49 }
50
51 Asserts
52 -------
53 Number of devices for each type has a response defined in the
54 respective `call_json` module.
55
56 See Also
57 --------
58 src/tests/README.md - README located in the tests directory
59 """
60 assert call_json_fans.FANS_NUM == len(call_json_fans.DETAILS_RESPONSES)
61 assert call_json_bulbs.BULBS_NUM == len(call_json_bulbs.DETAILS_RESPONSES)
62 assert call_json_outlets.OUTLETS_NUM == len(call_json_outlets.DETAILS_RESPONSES)
63 assert call_json_switches.SWITCHES_NUM == len(call_json_switches.DETAILS_RESPONSES)
64 assert call_json_purifiers.PURIFIERS_NUM == len(call_json_purifiers.DETAILS_RESPONSES)
65 assert call_json_humidifiers.HUMIDIFIERS_NUM == len(call_json_humidifiers.DETAILS_RESPONSES)
66
67
68 class TestGeneralAPI(TestBase):
69 """General API testing class for login() and get_devices()."""
70
71 def test_get_devices(self):
72 """Test get_devices() method request and API response."""
73 print("Test Device List")
74 self.mock_api.return_value = call_json.DeviceList.device_list_response(), 200
75 self.run_in_loop(self.manager.get_devices)
76 all_kwargs = parse_args(self.mock_api)
77 assert assert_test(self.manager.get_devices, all_kwargs, None,
78 self.write_api, self.overwrite)
79 assert len(self.manager.devices.bulbs) == call_json_bulbs.BULBS_NUM
80 assert len(self.manager.devices.outlets) == call_json_outlets.OUTLETS_NUM
81 assert len(self.manager.devices.fans) == call_json_fans.FANS_NUM
82 assert len(self.manager.devices.switches) == call_json_switches.SWITCHES_NUM
83 assert len(self.manager.devices.humidifiers) == call_json_humidifiers.HUMIDIFIERS_NUM
84 assert len(self.manager.devices.air_purifiers) == call_json_purifiers.PURIFIERS_NUM