| 1 |
""" |
| 2 |
This tests requests made by bulb 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_bulbs` - Contains API responses |
| 27 |
""" |
| 28 |
|
| 29 |
import logging |
| 30 |
import math |
| 31 |
from dataclasses import asdict |
| 32 |
import pyvesync.const as const |
| 33 |
from pyvesync.utils.helpers import Converters |
| 34 |
from pyvesync.base_devices.bulb_base import VeSyncBulb |
| 35 |
from base_test_cases import TestBase |
| 36 |
from utils import assert_test, parse_args |
| 37 |
from defaults import TestDefaults |
| 38 |
import call_json_bulbs |
| 39 |
|
| 40 |
|
| 41 |
logger = logging.getLogger(__name__) |
| 42 |
logger.setLevel(logging.DEBUG) |
| 43 |
|
| 44 |
DEFAULT_COLOR = TestDefaults.color |
| 45 |
DEFAULT_COLOR_RGB = dict(asdict(DEFAULT_COLOR.rgb)) |
| 46 |
DEFAULT_COLOR_HSV = dict(asdict(DEFAULT_COLOR.hsv)) |
| 47 |
RGB_SET = { |
| 48 |
'red': 50, |
| 49 |
'green': 200, |
| 50 |
'blue': 255, |
| 51 |
|
| 52 |
} |
| 53 |
HSV_SET = { |
| 54 |
'hue': 200, |
| 55 |
'saturation': 50, |
| 56 |
'value': 100, |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
class TestBulbs(TestBase): |
| 61 |
"""Bulbs testing class. |
| 62 |
|
| 63 |
This class tests bulb device details and methods. The methods are |
| 64 |
parametrized from the class variables using `pytest_generate_tests`. |
| 65 |
The call_json_bulbs module contains the responses for the API requests. |
| 66 |
The device is instantiated from the details provided by |
| 67 |
`call_json.DeviceList.device_list_item()`. Inherits from `utils.TestBase`. |
| 68 |
|
| 69 |
Instance Attributes |
| 70 |
------------------- |
| 71 |
self.manager : VeSync |
| 72 |
Instantiated VeSync object |
| 73 |
self.mock_api : Mock |
| 74 |
Mock with patched `helpers.call_api` method |
| 75 |
self.caplog : LogCaptureFixture |
| 76 |
Pytest fixture for capturing logs |
| 77 |
|
| 78 |
Class Attributes |
| 79 |
----------------- |
| 80 |
device : str |
| 81 |
Name of product class - bulbs |
| 82 |
bulbs : list |
| 83 |
List of setup_entry's for bulbs, this variable is named |
| 84 |
after the device class attribute value |
| 85 |
base_methods : List[List[str, Dict[str, Any]]] |
| 86 |
List of common methods for all devices |
| 87 |
device_methods : Dict[List[List[str, Dict[str, Any]]]] |
| 88 |
Dictionary of methods specific to setup_entry's for each |
| 89 |
device. |
| 90 |
|
| 91 |
Methods |
| 92 |
-------- |
| 93 |
test_details() |
| 94 |
Test the device details API request and response |
| 95 |
test_methods() |
| 96 |
Test device methods API request and response |
| 97 |
|
| 98 |
Examples |
| 99 |
-------- |
| 100 |
>>> device = 'bulbs' |
| 101 |
>>> bulbs = call_json_bulbs.bulbs |
| 102 |
>>> base_methods = [['turn_on'], ['turn_off'], ['update']] |
| 103 |
>>> device_methods = { |
| 104 |
'ESWD16': [['method1'], ['method2', {'kwargs': 'value'}]] |
| 105 |
} |
| 106 |
|
| 107 |
""" |
| 108 |
|
| 109 |
device = 'bulbs' |
| 110 |
bulbs = call_json_bulbs.BULBS |
| 111 |
base_methods = [['turn_on'], ['turn_off'], ['set_brightness', {'brightness': 50}]] |
| 112 |
device_methods = { |
| 113 |
'ESL100CW': [['set_color_temp', {'color_temp': 50}]], |
| 114 |
'ESL100MC': [['set_rgb', RGB_SET], |
| 115 |
['set_white_mode']], |
| 116 |
'XYD0001': [['set_hsv', HSV_SET], |
| 117 |
['set_color_temp', {'color_temp': 50}], |
| 118 |
['set_white_mode'] |
| 119 |
] |
| 120 |
} |
| 121 |
|
| 122 |
def test_details(self, setup_entry, method): |
| 123 |
"""Test the device details API request and response. |
| 124 |
|
| 125 |
This method is automatically parametrized by `pytest_generate_tests` |
| 126 |
based on class attribute `device` (name of product class - bulbs), |
| 127 |
device name (bulbs) list of `setup_entry` strings for each object. |
| 128 |
|
| 129 |
Example: |
| 130 |
>>> device = 'bulbs' |
| 131 |
>>> bulbs = call_json_bulbs.BULBS |
| 132 |
|
| 133 |
See Also |
| 134 |
-------- |
| 135 |
`utils.TestBase` class docstring |
| 136 |
`call_json_bulbs` module docstring |
| 137 |
|
| 138 |
Notes |
| 139 |
------ |
| 140 |
The device is instantiated using the `call_json.DeviceList.device_list_item()` |
| 141 |
method. The device details contain the default values set in `utils.Defaults` |
| 142 |
""" |
| 143 |
|
| 144 |
return_dict = call_json_bulbs.DETAILS_RESPONSES[setup_entry] |
| 145 |
return_val = (return_dict, 200) |
| 146 |
self.mock_api.return_value = return_val |
| 147 |
|
| 148 |
|
| 149 |
bulb_obj = self.get_device("bulbs", setup_entry) |
| 150 |
assert isinstance(bulb_obj, VeSyncBulb) |
| 151 |
|
| 152 |
method_call = getattr(bulb_obj, method) |
| 153 |
self.run_in_loop(method_call) |
| 154 |
|
| 155 |
|
| 156 |
all_kwargs = parse_args(self.mock_api) |
| 157 |
|
| 158 |
|
| 159 |
assert assert_test( |
| 160 |
method_call, all_kwargs, setup_entry, self.write_api, self.overwrite |
| 161 |
) |
| 162 |
|
| 163 |
|
| 164 |
assert bulb_obj.state.brightness == TestDefaults.brightness |
| 165 |
if bulb_obj.supports_multicolor: |
| 166 |
assert self._assert_color(bulb_obj) |
| 167 |
if bulb_obj.supports_color_temp: |
| 168 |
assert bulb_obj.state.color_temp == TestDefaults.color_temp |
| 169 |
assert bulb_obj.state.color_temp_kelvin == Converters.color_temp_pct_to_kelvin(TestDefaults.color_temp) |
| 170 |
|
| 171 |
def test_methods(self, setup_entry, method): |
| 172 |
"""Test device methods API request and response. |
| 173 |
|
| 174 |
This method is automatically parametrized by `pytest_generate_tests` |
| 175 |
based on class variables `device` (name of product class - bulbs), |
| 176 |
device name (bulbs) list of `setup_entry`'s, `base_methods` - list of |
| 177 |
methods for all devices, and `device_methods` - list of methods for |
| 178 |
each setup_entry. |
| 179 |
|
| 180 |
Example: |
| 181 |
>>> base_methods = [['turn_on'], ['turn_off'], ['update']] |
| 182 |
>>> device_methods = { |
| 183 |
'setup_entry': [['method1'], ['method2', {'kwargs': 'value'}]] |
| 184 |
} |
| 185 |
|
| 186 |
Notes |
| 187 |
----- |
| 188 |
The response can be a callable that accepts the `kwargs` argument to |
| 189 |
sync the device response with the API response. In some cases the API |
| 190 |
returns data from the method call, such as `get_yearly_energy`, in other cases the |
| 191 |
API returns a simple confirmation the command was successful. |
| 192 |
|
| 193 |
See Also |
| 194 |
-------- |
| 195 |
`TestBase` class method |
| 196 |
`call_json_bulbs` module |
| 197 |
|
| 198 |
""" |
| 199 |
|
| 200 |
method_name = method[0] |
| 201 |
if len(method) == 2 and isinstance(method[1], dict): |
| 202 |
method_kwargs = method[1] |
| 203 |
else: |
| 204 |
method_kwargs = {} |
| 205 |
|
| 206 |
|
| 207 |
method_response = call_json_bulbs.METHOD_RESPONSES[setup_entry][method_name] |
| 208 |
if callable(method_response): |
| 209 |
if method_kwargs: |
| 210 |
self.mock_api.return_value = method_response(method_kwargs), 200 |
| 211 |
else: |
| 212 |
self.mock_api.return_value = method_response(), 200 |
| 213 |
else: |
| 214 |
self.mock_api.return_value = method_response, 200 |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
bulb_obj = self.get_device("bulbs", setup_entry) |
| 219 |
assert isinstance(bulb_obj, VeSyncBulb) |
| 220 |
|
| 221 |
|
| 222 |
method_call = getattr(bulb_obj, method[0]) |
| 223 |
|
| 224 |
|
| 225 |
if method[0] == 'turn_on': |
| 226 |
bulb_obj.state.device_status = const.DeviceStatus.OFF |
| 227 |
elif method[0] == 'turn_off': |
| 228 |
bulb_obj.state.device_status = const.DeviceStatus.ON |
| 229 |
|
| 230 |
|
| 231 |
if method_kwargs: |
| 232 |
self.run_in_loop(method_call, **method_kwargs) |
| 233 |
else: |
| 234 |
self.run_in_loop(method_call) |
| 235 |
|
| 236 |
|
| 237 |
all_kwargs = parse_args(self.mock_api) |
| 238 |
|
| 239 |
|
| 240 |
assert assert_test( |
| 241 |
method_call, all_kwargs, setup_entry, self.write_api, self.overwrite |
| 242 |
) |
| 243 |
|
| 244 |
def _assert_color(self, bulb_obj): |
| 245 |
assert math.isclose(bulb_obj.state.color.rgb.red, DEFAULT_COLOR.rgb.red, rel_tol=1) |
| 246 |
assert math.isclose(bulb_obj.state.color.rgb.green, DEFAULT_COLOR.rgb.green, rel_tol=1) |
| 247 |
assert math.isclose(bulb_obj.state.color.rgb.blue, DEFAULT_COLOR.rgb.blue, rel_tol=1) |
| 248 |
assert math.isclose(bulb_obj.state.color.hsv.hue, DEFAULT_COLOR.hsv.hue, rel_tol=1) |
| 249 |
assert math.isclose(bulb_obj.state.color.hsv.saturation, |
| 250 |
DEFAULT_COLOR.hsv.saturation, |
| 251 |
rel_tol=1) |
| 252 |
assert math.isclose(bulb_obj.state.color.hsv.value, DEFAULT_COLOR.hsv.value, rel_tol=1) |
| 253 |
return True |