| 1 |
""" |
| 2 |
This tests requests for Humidifiers. |
| 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_fans` - Contains API responses |
| 27 |
""" |
| 28 |
|
| 29 |
import logging |
| 30 |
from dataclasses import asdict |
| 31 |
import pytest |
| 32 |
import pyvesync.const as const |
| 33 |
from pyvesync.base_devices.humidifier_base import VeSyncHumidifier |
| 34 |
from base_test_cases import TestBase |
| 35 |
from utils import assert_test, parse_args |
| 36 |
from defaults import TestDefaults |
| 37 |
import call_json_humidifiers |
| 38 |
|
| 39 |
|
| 40 |
logger = logging.getLogger(__name__) |
| 41 |
logger.setLevel(logging.DEBUG) |
| 42 |
|
| 43 |
DEFAULT_COLOR = TestDefaults.color |
| 44 |
DEFAULT_COLOR_RGB = dict(asdict(DEFAULT_COLOR.rgb)) |
| 45 |
DEFAULT_COLOR_HSV = dict(asdict(DEFAULT_COLOR.hsv)) |
| 46 |
RGB_SET = { |
| 47 |
"red": 50, |
| 48 |
"green": 200, |
| 49 |
"blue": 255, |
| 50 |
} |
| 51 |
HSV_SET = { |
| 52 |
"hue": 200, |
| 53 |
"saturation": 50, |
| 54 |
"value": 100, |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
class TestHumidifiers(TestBase): |
| 59 |
"""Humidifier testing class. |
| 60 |
|
| 61 |
This class tests Humidifier product details and methods. The methods are |
| 62 |
parametrized from the class variables using `pytest_generate_tests`. |
| 63 |
The call_json_fans module contains the responses for the API requests. |
| 64 |
The device is instantiated from the details provided by |
| 65 |
`call_json.DeviceList.device_list_item()`. Inherits from `utils.TestBase`. |
| 66 |
|
| 67 |
Instance Attributes |
| 68 |
------------------- |
| 69 |
self.manager : VeSync |
| 70 |
Instantiated VeSync object |
| 71 |
self.mock_api : Mock |
| 72 |
Mock with patched `helpers.call_api` method |
| 73 |
self.caplog : LogCaptureFixture |
| 74 |
Pytest fixture for capturing logs |
| 75 |
|
| 76 |
Class Variables |
| 77 |
--------------- |
| 78 |
device : str |
| 79 |
Name of product class - humidifiers |
| 80 |
humidifers : list |
| 81 |
List of setup_entry's for humidifiers, this variable is named |
| 82 |
after the device variable value |
| 83 |
base_methods : List[List[str, Dict[str, Any]]] |
| 84 |
List of common methods for all devices |
| 85 |
device_methods : Dict[List[List[str, Dict[str, Any]]]] |
| 86 |
Dictionary of methods specific to device types |
| 87 |
|
| 88 |
Methods |
| 89 |
-------- |
| 90 |
test_details() |
| 91 |
Test the device details API request and response |
| 92 |
test_methods() |
| 93 |
Test device methods API request and response |
| 94 |
|
| 95 |
Examples |
| 96 |
-------- |
| 97 |
>>> device = 'humidifiers' |
| 98 |
>>> humidifiers = call_json_fans.HUMID_MODELS |
| 99 |
>>> base_methods = [['turn_on'], ['turn_off'], ['update']] |
| 100 |
>>> device_methods = { |
| 101 |
'ESWD16': [['method1'], ['method2', {'kwargs': 'value'}]] |
| 102 |
} |
| 103 |
|
| 104 |
""" |
| 105 |
|
| 106 |
device = "humidifiers" |
| 107 |
humidifiers = call_json_humidifiers.HUMIDIFIERS |
| 108 |
base_methods = [ |
| 109 |
["turn_on"], |
| 110 |
["turn_off"], |
| 111 |
["turn_on_display"], |
| 112 |
["turn_off_display"], |
| 113 |
["turn_on_automatic_stop"], |
| 114 |
["turn_off_automatic_stop"], |
| 115 |
["set_humidity", {"humidity": 50}], |
| 116 |
["set_auto_mode"], |
| 117 |
["set_manual_mode"], |
| 118 |
["set_mist_level", {"level": 2}], |
| 119 |
] |
| 120 |
device_methods = { |
| 121 |
"LUH-A602S-WUS": [["set_warm_level", {"warm_level": 3}]], |
| 122 |
"LUH-A603S-WUS": [["set_warm_level", {"warm_level": 3}]], |
| 123 |
"LEH-S601S": [["turn_off_drying_mode"]], |
| 124 |
} |
| 125 |
|
| 126 |
def test_details(self, setup_entry, method): |
| 127 |
"""Test the device details API request and response. |
| 128 |
|
| 129 |
This method is automatically parametrized by `pytest_generate_tests` |
| 130 |
based on class variables `device` (name of product class - fans), |
| 131 |
device name (fans) list of setup_entry's. |
| 132 |
|
| 133 |
Example: |
| 134 |
>>> device = 'air_purifiers' |
| 135 |
>>> air_purifiers = call_json_fans.AIR_MODELS |
| 136 |
|
| 137 |
See Also |
| 138 |
-------- |
| 139 |
`utils.TestBase` class docstring |
| 140 |
`call_json_fans` module docstring |
| 141 |
|
| 142 |
Notes |
| 143 |
------ |
| 144 |
The device is instantiated using the `call_json.DeviceList.device_list_item()` |
| 145 |
method. The device details contain the default values set in `utils.Defaults` |
| 146 |
""" |
| 147 |
|
| 148 |
return_dict = call_json_humidifiers.DETAILS_RESPONSES[setup_entry] |
| 149 |
return_val = (return_dict, 200) |
| 150 |
self.mock_api.return_value = return_val |
| 151 |
|
| 152 |
|
| 153 |
fan_obj = self.get_device("humidifiers", setup_entry) |
| 154 |
assert isinstance(fan_obj, VeSyncHumidifier) |
| 155 |
|
| 156 |
method_call = getattr(fan_obj, method) |
| 157 |
self.run_in_loop(method_call) |
| 158 |
|
| 159 |
|
| 160 |
assert fan_obj.state.device_status == const.DeviceStatus.ON |
| 161 |
assert fan_obj.state.connection_status == const.ConnectionStatus.ONLINE |
| 162 |
assert fan_obj.state.mode == call_json_humidifiers.HumidifierDefaults.humidifier_mode |
| 163 |
assert fan_obj.state.humidity == call_json_humidifiers.HumidifierDefaults.humidity |
| 164 |
assert ( |
| 165 |
fan_obj.state.target_humidity |
| 166 |
== call_json_humidifiers.HumidifierDefaults.target_humidity |
| 167 |
) |
| 168 |
assert fan_obj.state.mist_level == call_json_humidifiers.HumidifierDefaults.mist_level |
| 169 |
assert fan_obj.state.display_status == const.DeviceStatus.from_bool(call_json_humidifiers.HumidifierDefaults.display) |
| 170 |
|
| 171 |
if fan_obj.supports_nightlight: |
| 172 |
assert fan_obj.state.nightlight_status == call_json_humidifiers.HumidifierDefaults.nightlight_status |
| 173 |
if fan_obj.supports_nightlight_brightness: |
| 174 |
assert ( |
| 175 |
fan_obj.state.nightlight_brightness |
| 176 |
== call_json_humidifiers.HumidifierDefaults.nightlight_brightness |
| 177 |
) |
| 178 |
if fan_obj.supports_drying_mode: |
| 179 |
assert fan_obj.state.drying_mode_auto_switch == call_json_humidifiers.HumidifierDefaults.drying_mode_switch |
| 180 |
assert fan_obj.state.drying_mode_state == call_json_humidifiers.HumidifierDefaults.drying_state |
| 181 |
|
| 182 |
|
| 183 |
all_kwargs = parse_args(self.mock_api) |
| 184 |
|
| 185 |
|
| 186 |
assert assert_test( |
| 187 |
method_call, all_kwargs, setup_entry, self.write_api, self.overwrite |
| 188 |
) |
| 189 |
|
| 190 |
def test_methods(self, setup_entry, method): |
| 191 |
"""Test device methods API request and response. |
| 192 |
|
| 193 |
This method is automatically parametrized by `pytest_generate_tests` |
| 194 |
based on class variables `device` (name of product class - humidifiers), |
| 195 |
device name (humidifiers) list of setup_entry's, `base_methods` - list of |
| 196 |
methods for all devices, and `device_methods` - list of methods for |
| 197 |
each device type. |
| 198 |
|
| 199 |
Example: |
| 200 |
>>> base_methods = [['turn_on'], ['turn_off'], ['update']] |
| 201 |
>>> device_methods = { |
| 202 |
'setup_entry': [['method1'], ['method2', {'kwargs': 'value'}]] |
| 203 |
} |
| 204 |
|
| 205 |
Notes |
| 206 |
----- |
| 207 |
The response can be a callable that accepts the `kwargs` argument to |
| 208 |
sync the device response with the API response. In some cases the API |
| 209 |
returns data from the method call, such as `get_yearly_energy`, in other cases the |
| 210 |
API returns a simple confirmation the command was successful. |
| 211 |
|
| 212 |
See Also |
| 213 |
-------- |
| 214 |
`TestBase` class method |
| 215 |
`call_json_fans` module |
| 216 |
|
| 217 |
""" |
| 218 |
|
| 219 |
method_name = method[0] |
| 220 |
if len(method) == 2 and isinstance(method[1], dict): |
| 221 |
method_kwargs = method[1] |
| 222 |
else: |
| 223 |
method_kwargs = {} |
| 224 |
|
| 225 |
|
| 226 |
method_response = call_json_humidifiers.METHOD_RESPONSES[setup_entry][method_name] |
| 227 |
|
| 228 |
fan_obj = self.get_device("humidifiers", setup_entry) |
| 229 |
assert isinstance(fan_obj, VeSyncHumidifier) |
| 230 |
|
| 231 |
if method_name in ["turn_on_automatic_stop", "turn_off_automatic_stop"] \ |
| 232 |
and const.HumidifierFeatures.AUTO_STOP not in fan_obj.features: |
| 233 |
pytest.skip("Automatic stop not supported for this device.") |
| 234 |
|
| 235 |
if callable(method_response): |
| 236 |
if method_kwargs: |
| 237 |
self.mock_api.return_value = method_response(method_kwargs), 200 |
| 238 |
else: |
| 239 |
self.mock_api.return_value = method_response(), 200 |
| 240 |
else: |
| 241 |
self.mock_api.return_value = method_response, 200 |
| 242 |
|
| 243 |
|
| 244 |
method_call = getattr(fan_obj, method[0]) |
| 245 |
|
| 246 |
|
| 247 |
if method[0] == "turn_on": |
| 248 |
fan_obj.state.device_status = const.DeviceStatus.OFF |
| 249 |
elif method[0] == "turn_off": |
| 250 |
fan_obj.state.device_status = const.DeviceStatus.ON |
| 251 |
elif method[0] == "set_auto_mode": |
| 252 |
fan_obj.state.mode = const.HumidifierModes.MANUAL |
| 253 |
elif method[0] == "set_manual_mode": |
| 254 |
fan_obj.state.mode = const.HumidifierModes.AUTO |
| 255 |
|
| 256 |
|
| 257 |
if method_kwargs: |
| 258 |
self.run_in_loop(method_call, **method_kwargs) |
| 259 |
else: |
| 260 |
self.run_in_loop(method_call) |
| 261 |
|
| 262 |
if method[0] == "set_auto_mode": |
| 263 |
assert fan_obj.state.mode == const.HumidifierModes.AUTO |
| 264 |
elif method[0] == "set_manual_mode": |
| 265 |
assert fan_obj.state.mode == const.HumidifierModes.MANUAL |
| 266 |
elif method[0] == "set_humidity": |
| 267 |
assert fan_obj.state.target_humidity == method_kwargs["humidity"] |
| 268 |
elif method[0] == "set_mist_level": |
| 269 |
assert fan_obj.state.mist_level == method_kwargs["level"] |
| 270 |
elif method[0] == "set_warm_level": |
| 271 |
assert fan_obj.state.warm_mist_level == method_kwargs["warm_level"] |
| 272 |
elif method[0] == "turn_off_drying_mode": |
| 273 |
assert fan_obj.state.drying_mode_auto_switch == const.DeviceStatus.OFF |
| 274 |
elif method[0] == "turn_on_automatic_stop": |
| 275 |
assert fan_obj.state.automatic_stop_config is True |
| 276 |
elif method[0] == "turn_off_automatic_stop": |
| 277 |
assert fan_obj.state.automatic_stop_config is False |
| 278 |
|
| 279 |
all_kwargs = parse_args(self.mock_api) |
| 280 |
|
| 281 |
|
| 282 |
assert assert_test( |
| 283 |
method_call, all_kwargs, setup_entry, self.write_api, self.overwrite |
| 284 |
) |