| 1 |
""" |
| 2 |
This tests requests made by switch devices. |
| 3 |
|
| 4 |
All tests inherit from the TestBase class which contains the fixtures |
| 5 |
and methods needed to run the tests. |
| 6 |
|
| 7 |
The tests are automatically parametrized by `pytest_generate_tests` in |
| 8 |
conftest.py. The two methods that are parametrized are `test_details` |
| 9 |
and `test_methods`. The class variables are used to build the list of |
| 10 |
devices, test methods and arguments. |
| 11 |
|
| 12 |
The `helpers.call_api` method is patched to return a mock response. |
| 13 |
The method, endpoint, headers and json arguments are recorded |
| 14 |
in YAML files in the api directory, categorized in folders by |
| 15 |
module and files by the class name. |
| 16 |
|
| 17 |
The default is to record requests that do not exist and compare requests |
| 18 |
that already exist. If the API changes, set the overwrite argument to True |
| 19 |
in order to overwrite the existing YAML file with the new request. |
| 20 |
|
| 21 |
See Also |
| 22 |
-------- |
| 23 |
`utils.TestBase` - Base class for all tests, containing mock objects |
| 24 |
`confest.pytest_generate_tests` - Parametrizes tests based on |
| 25 |
method names & class attributes |
| 26 |
`call_json_switches` - Contains API responses |
| 27 |
""" |
| 28 |
|
| 29 |
import logging |
| 30 |
from pyvesync.base_devices.switch_base import VeSyncSwitch |
| 31 |
import pyvesync.const as const |
| 32 |
from base_test_cases import TestBase |
| 33 |
from utils import assert_test, parse_args |
| 34 |
from defaults import TestDefaults |
| 35 |
import call_json |
| 36 |
import call_json_switches |
| 37 |
|
| 38 |
|
| 39 |
logger = logging.getLogger(__name__) |
| 40 |
logger.setLevel(logging.DEBUG) |
| 41 |
|
| 42 |
DEFAULT_COLOR = TestDefaults.color.rgb |
| 43 |
COLOR_DICT = { |
| 44 |
'red': DEFAULT_COLOR.red, |
| 45 |
'blue': DEFAULT_COLOR.blue, |
| 46 |
'green': DEFAULT_COLOR.green, |
| 47 |
} |
| 48 |
|
| 49 |
NEW_BRIGHTNESS = 75 |
| 50 |
NEW_COLOR_TEMP = 40 |
| 51 |
|
| 52 |
|
| 53 |
class TestSwitches(TestBase): |
| 54 |
"""Switches testing class. |
| 55 |
|
| 56 |
This class tests switch device details and methods. The methods are |
| 57 |
parametrized from the class variables using `pytest_generate_tests`. |
| 58 |
The call_json_switches module contains the responses for the API requests. |
| 59 |
The device is instantiated from the details provided by |
| 60 |
`call_json.DeviceList.device_list_item()`. Inherits from `utils.TestBase`. |
| 61 |
|
| 62 |
Instance Attributes |
| 63 |
------------------- |
| 64 |
self.manager : VeSync |
| 65 |
Instantiated VeSync object |
| 66 |
self.mock_api : Mock |
| 67 |
Mock with patched `helpers.call_api` method |
| 68 |
self.caplog : LogCaptureFixture |
| 69 |
Pytest fixture for capturing logs |
| 70 |
|
| 71 |
|
| 72 |
Class Variables |
| 73 |
--------------- |
| 74 |
device : str |
| 75 |
Name of device type - switches |
| 76 |
switches : list |
| 77 |
List of device types for switches, this variable is named |
| 78 |
after the device variable value |
| 79 |
base_methods : List[List[str, Dict[str, Any]]] |
| 80 |
List of common methods for all devices |
| 81 |
device_methods : Dict[List[List[str, Dict[str, Any]]]] |
| 82 |
Dictionary of methods specific to device types |
| 83 |
|
| 84 |
Methods |
| 85 |
-------- |
| 86 |
test_details() |
| 87 |
Test the device details API request and response |
| 88 |
test_methods() |
| 89 |
Test device methods API request and response |
| 90 |
|
| 91 |
Examples |
| 92 |
-------- |
| 93 |
>>> device = 'switches' |
| 94 |
>>> switches = call_json_switches.SWITCHES |
| 95 |
>>> base_methods = [['turn_on'], ['turn_off'], ['update']] |
| 96 |
>>> device_methods = { |
| 97 |
'ESWD16': [['method1'], ['method2', {'kwargs': 'value'}]] |
| 98 |
} |
| 99 |
|
| 100 |
""" |
| 101 |
|
| 102 |
device = 'switches' |
| 103 |
switches = call_json_switches.SWITCHES |
| 104 |
base_methods = [['turn_on'], ['turn_off']] |
| 105 |
device_methods = { |
| 106 |
'ESWD16': [['turn_on_indicator_light'], |
| 107 |
['turn_on_rgb_backlight'], |
| 108 |
['set_backlight_color', COLOR_DICT], |
| 109 |
['set_brightness', {'brightness': NEW_BRIGHTNESS}]], |
| 110 |
} |
| 111 |
|
| 112 |
def test_details(self, setup_entry, method): |
| 113 |
"""Test the device details API request and response. |
| 114 |
|
| 115 |
This method is automatically parametrized by `pytest_generate_tests` |
| 116 |
based on class variables `device` (name of device type - switches), |
| 117 |
device name (switches) list of device types. |
| 118 |
|
| 119 |
Example: |
| 120 |
>>> device = 'switches' |
| 121 |
>>> switches = call_json_switches.SWITCHES |
| 122 |
|
| 123 |
See Also |
| 124 |
-------- |
| 125 |
`utils.TestBase` class docstring |
| 126 |
`call_json_switches` module docstring |
| 127 |
|
| 128 |
Notes |
| 129 |
------ |
| 130 |
The device is instantiated using the `call_json.DeviceList.device_list_item()` |
| 131 |
method. The device details contain the default values set in `utils.Defaults` |
| 132 |
""" |
| 133 |
|
| 134 |
resp_dict = call_json_switches.DETAILS_RESPONSES[setup_entry] |
| 135 |
self.mock_api.return_value = resp_dict, 200 |
| 136 |
|
| 137 |
|
| 138 |
switch_obj = self.get_device("switches", setup_entry) |
| 139 |
assert isinstance(switch_obj, VeSyncSwitch) |
| 140 |
|
| 141 |
method_call = getattr(switch_obj, method) |
| 142 |
self.run_in_loop(method_call) |
| 143 |
|
| 144 |
|
| 145 |
all_kwargs = parse_args(self.mock_api) |
| 146 |
|
| 147 |
|
| 148 |
assert assert_test( |
| 149 |
method_call, |
| 150 |
all_kwargs, |
| 151 |
setup_entry, |
| 152 |
self.write_api, |
| 153 |
self.overwrite, |
| 154 |
) |
| 155 |
|
| 156 |
|
| 157 |
if switch_obj.supports_dimmable: |
| 158 |
assert switch_obj.state.brightness == TestDefaults.brightness |
| 159 |
assert switch_obj.state.indicator_status == 'on' |
| 160 |
assert switch_obj.state.backlight_status == 'on' |
| 161 |
assert switch_obj.state.backlight_color is not None |
| 162 |
assert switch_obj.state.backlight_color.rgb.to_dict() == COLOR_DICT |
| 163 |
self.mock_api.reset_mock() |
| 164 |
bad_dict = call_json.DETAILS_BADCODE |
| 165 |
self.mock_api.return_value = bad_dict, 200 |
| 166 |
self.run_in_loop(switch_obj.get_details) |
| 167 |
assert 'Unknown error' in self.caplog.records[-1].message |
| 168 |
|
| 169 |
def test_methods(self, setup_entry, method): |
| 170 |
"""Test switch methods API request and response. |
| 171 |
|
| 172 |
This method is automatically parametrized by `pytest_generate_tests` |
| 173 |
based on class variables `device` (name of device type - switches), |
| 174 |
device name (switches) list of device types, `base_methods` - list of |
| 175 |
methods for all devices, and `device_methods` - list of methods for |
| 176 |
each device type. |
| 177 |
|
| 178 |
Example: |
| 179 |
>>> base_methods = [['turn_on'], ['turn_off'], ['update']] |
| 180 |
>>> device_methods = { |
| 181 |
'dev_type': [['method1'], ['method2', {'kwargs': 'value'}]] |
| 182 |
} |
| 183 |
|
| 184 |
Notes |
| 185 |
----- |
| 186 |
The response can be a callable that accepts the `kwargs` argument to |
| 187 |
sync the device response with the API response. In some cases the API |
| 188 |
returns data from the method call, such as `get_yearly_energy`, in other cases the |
| 189 |
API returns a simple confirmation the command was successful. |
| 190 |
|
| 191 |
See Also |
| 192 |
-------- |
| 193 |
`TestBase` class method |
| 194 |
`call_json_switches` module |
| 195 |
|
| 196 |
""" |
| 197 |
|
| 198 |
method_name = method[0] |
| 199 |
if len(method) == 2 and isinstance(method[1], dict): |
| 200 |
method_kwargs = method[1] |
| 201 |
else: |
| 202 |
method_kwargs = {} |
| 203 |
|
| 204 |
|
| 205 |
method_response = call_json_switches.METHOD_RESPONSES[setup_entry][method_name] |
| 206 |
if callable(method_response): |
| 207 |
if method_kwargs: |
| 208 |
resp_dict = method_response(**method_kwargs) |
| 209 |
else: |
| 210 |
resp_dict = method_response() |
| 211 |
else: |
| 212 |
resp_dict = method_response |
| 213 |
self.mock_api.return_value = resp_dict, 200 |
| 214 |
|
| 215 |
switch_obj = self.get_device("switches", setup_entry) |
| 216 |
assert isinstance(switch_obj, VeSyncSwitch) |
| 217 |
|
| 218 |
|
| 219 |
method_call = getattr(switch_obj, method[0]) |
| 220 |
|
| 221 |
|
| 222 |
if method[0] == 'turn_on': |
| 223 |
switch_obj.state.device_status = const.DeviceStatus.OFF |
| 224 |
elif method[0] == 'turn_off': |
| 225 |
switch_obj.state.device_status = const.DeviceStatus.ON |
| 226 |
|
| 227 |
|
| 228 |
if method_kwargs: |
| 229 |
self.run_in_loop(method_call, **method_kwargs) |
| 230 |
else: |
| 231 |
self.run_in_loop(method_call) |
| 232 |
|
| 233 |
|
| 234 |
all_kwargs = parse_args(self.mock_api) |
| 235 |
|
| 236 |
|
| 237 |
assert assert_test( |
| 238 |
method_call, all_kwargs, setup_entry, self.write_api, self.overwrite |
| 239 |
) |
| 240 |
|
| 241 |
self.mock_api.reset_mock() |
| 242 |
resp_dict = call_json.DETAILS_BADCODE |
| 243 |
self.mock_api.return_value = resp_dict, 200 |
| 244 |
if method_kwargs: |
| 245 |
return_val = self.run_in_loop(method_call, **method_kwargs) |
| 246 |
assert return_val is False |
| 247 |
else: |
| 248 |
return_val = self.run_in_loop(method_call) |
| 249 |
assert return_val is False |