From 9568804bcf54a523fb786ee1f353902c2882b3d5 Mon Sep 17 00:00:00 2001 From: Steven B <51370195+sdb9696@users.noreply.github.com> Date: Fri, 3 Jan 2025 13:19:30 +0000 Subject: [PATCH 1/3] Enable testing of child fixture generation for smartcam devices --- tests/fakeprotocol_smart.py | 44 ++++++++++++++++++++++------------ tests/fakeprotocol_smartcam.py | 4 ++-- tests/fixtureinfo.py | 4 ++-- tests/test_devtools.py | 39 ++++++++++++++++++++++++++---- 4 files changed, 68 insertions(+), 23 deletions(-) diff --git a/tests/fakeprotocol_smart.py b/tests/fakeprotocol_smart.py index c0222b995..a30d630fa 100644 --- a/tests/fakeprotocol_smart.py +++ b/tests/fakeprotocol_smart.py @@ -48,13 +48,18 @@ def __init__( ), ) self.fixture_name = fixture_name + + # When True verbatim will bypass any extra processing of missing + # methods and is used to test the fixture creation itself. + self.verbatim = verbatim + # Don't copy the dict if the device is a child so that updates on the # child are then still reflected on the parent's lis of child device in if not is_child: self.info = copy.deepcopy(info) if get_child_fixtures: self.child_protocols = self._get_child_protocols( - self.info, self.fixture_name, "get_child_device_list" + self.info, self.fixture_name, "get_child_device_list", self.verbatim ) else: self.info = info @@ -67,9 +72,6 @@ def __init__( self.warn_fixture_missing_methods = warn_fixture_missing_methods self.fix_incomplete_fixture_lists = fix_incomplete_fixture_lists - # When True verbatim will bypass any extra processing of missing - # methods and is used to test the fixture creation itself. - self.verbatim = verbatim if verbatim: self.warn_fixture_missing_methods = False self.fix_incomplete_fixture_lists = False @@ -124,7 +126,7 @@ def credentials_hash(self): }, ), "get_auto_update_info": ( - "firmware", + ("firmware", 2), {"enable": True, "random_range": 120, "time": 180}, ), "get_alarm_configure": ( @@ -169,6 +171,22 @@ def credentials_hash(self): ), } + def _missing_result(self, method): + if not (missing := self.FIXTURE_MISSING_MAP.get(method)): + return None + condition = missing[0] + if ( + isinstance(condition, tuple) + and (version := self.components.get(condition[0])) + and version >= condition[1] + ): + return copy.deepcopy(missing[1]) + + if condition in self.components: + return copy.deepcopy(missing[1]) + + return None + async def send(self, request: str): request_dict = json_loads(request) method = request_dict["method"] @@ -189,7 +207,7 @@ async def send(self, request: str): @staticmethod def _get_child_protocols( - parent_fixture_info, parent_fixture_name, child_devices_key + parent_fixture_info, parent_fixture_name, child_devices_key, verbatim ): child_infos = parent_fixture_info.get(child_devices_key, {}).get( "child_device_list", [] @@ -251,7 +269,7 @@ def try_get_child_fixture_info(child_dev_info): ) # Replace parent child infos with the infos from the child fixtures so # that updates update both - if child_infos and found_child_fixture_infos: + if not verbatim and child_infos and found_child_fixture_infos: parent_fixture_info[child_devices_key]["child_device_list"] = ( found_child_fixture_infos ) @@ -318,13 +336,11 @@ def _handle_control_child_missing(self, params: dict): elif child_method in child_device_calls: result = copy.deepcopy(child_device_calls[child_method]) return {"result": result, "error_code": 0} - elif ( + elif missing_result := self._missing_result(child_method): # FIXTURE_MISSING is for service calls not in place when # SMART fixtures started to be generated - missing_result := self.FIXTURE_MISSING_MAP.get(child_method) - ) and missing_result[0] in self.components: # Copy to info so it will work with update methods - child_device_calls[child_method] = copy.deepcopy(missing_result[1]) + child_device_calls[child_method] = missing_result result = copy.deepcopy(info[child_method]) retval = {"result": result, "error_code": 0} return retval @@ -529,13 +545,11 @@ async def _send_request(self, request_dict: dict): "method": method, } - if ( + if missing_result := self._missing_result(method): # FIXTURE_MISSING is for service calls not in place when # SMART fixtures started to be generated - missing_result := self.FIXTURE_MISSING_MAP.get(method) - ) and missing_result[0] in self.components: # Copy to info so it will work with update methods - info[method] = copy.deepcopy(missing_result[1]) + info[method] = missing_result result = copy.deepcopy(info[method]) retval = {"result": result, "error_code": 0} elif ( diff --git a/tests/fakeprotocol_smartcam.py b/tests/fakeprotocol_smartcam.py index eee014e8f..17b149792 100644 --- a/tests/fakeprotocol_smartcam.py +++ b/tests/fakeprotocol_smartcam.py @@ -57,11 +57,11 @@ def __init__( # lists if get_child_fixtures: self.child_protocols = FakeSmartTransport._get_child_protocols( - self.info, self.fixture_name, "getChildDeviceList" + self.info, self.fixture_name, "getChildDeviceList", self.verbatim ) else: self.info = info - # self.child_protocols = self._get_child_protocols() + self.list_return_size = list_return_size # Setting this flag allows tests to create dummy transports without diff --git a/tests/fixtureinfo.py b/tests/fixtureinfo.py index 62b712283..8988be1d2 100644 --- a/tests/fixtureinfo.py +++ b/tests/fixtureinfo.py @@ -77,7 +77,7 @@ def idgenerator(paramtuple: FixtureInfo): return None -def get_fixture_info() -> list[FixtureInfo]: +def get_fixture_infos() -> list[FixtureInfo]: """Return raw discovery file contents as JSON. Used for discovery tests.""" fixture_data = [] for file, protocol in SUPPORTED_DEVICES: @@ -99,7 +99,7 @@ def get_fixture_info() -> list[FixtureInfo]: return fixture_data -FIXTURE_DATA: list[FixtureInfo] = get_fixture_info() +FIXTURE_DATA: list[FixtureInfo] = get_fixture_infos() def filter_fixtures( diff --git a/tests/test_devtools.py b/tests/test_devtools.py index 8bdd5746b..d2c1207f5 100644 --- a/tests/test_devtools.py +++ b/tests/test_devtools.py @@ -1,5 +1,7 @@ """Module for dump_devinfo tests.""" +import copy + import pytest from devtools.dump_devinfo import get_legacy_fixture, get_smart_fixtures @@ -11,6 +13,7 @@ from .conftest import ( FixtureInfo, get_device_for_fixture, + get_fixture_info, parametrize, ) @@ -64,22 +67,50 @@ async def test_smart_fixtures(fixture_info: FixtureInfo): assert fixture_info.data == fixture_result.data +def _normalize_child_device_ids(info: dict): + if dev_info := info.get("get_device_info"): + dev_info["device_id"] = "SCRUBBED" + elif ( + dev_info := info.get("getDeviceInfo", {}) + .get("device_info", {}) + .get("basic_info") + ): + dev_info["dev_id"] = "SCRUBBED" + + @smartcam_fixtures async def test_smartcam_fixtures(fixture_info: FixtureInfo): """Test that smartcam fixtures are created the same.""" dev = await get_device_for_fixture(fixture_info, verbatim=True) assert isinstance(dev, SmartCamDevice) - if dev.children: - pytest.skip("Test not currently implemented for devices with children.") - fixtures = await get_smart_fixtures( + # if dev.children: + # pytest.skip("Test not currently implemented for devices with children.") + created_fixtures = await get_smart_fixtures( dev.protocol, discovery_info=fixture_info.data.get("discovery_result"), batch_size=5, ) - fixture_result = fixtures[0] + fixture_result = created_fixtures.pop(0) assert fixture_info.data == fixture_result.data + for created_child_fixture in created_fixtures: + child_fixture_info = get_fixture_info( + created_child_fixture.filename + ".json", + created_child_fixture.protocol_suffix, + ) + + assert child_fixture_info + + _normalize_child_device_ids(created_child_fixture.data) + + saved_fixture_data = copy.deepcopy(child_fixture_info.data) + _normalize_child_device_ids(saved_fixture_data) + saved_fixture_data = { + key: val for key, val in saved_fixture_data.items() if val != -1001 + } + assert saved_fixture_data == created_child_fixture.data + @iot_fixtures async def test_iot_fixtures(fixture_info: FixtureInfo): From 82c884db6b34f481706674aa26211eb1c03bee4c Mon Sep 17 00:00:00 2001 From: Steven B <51370195+sdb9696@users.noreply.github.com> Date: Mon, 6 Jan 2025 15:51:53 +0000 Subject: [PATCH 2/3] Cleanup and add comments --- tests/test_devtools.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_devtools.py b/tests/test_devtools.py index d2c1207f5..3af20035e 100644 --- a/tests/test_devtools.py +++ b/tests/test_devtools.py @@ -68,6 +68,11 @@ async def test_smart_fixtures(fixture_info: FixtureInfo): def _normalize_child_device_ids(info: dict): + """Scrubbed child device ids in hubs may not match ids in child fixtures. + + Different hub fixtures could create the same child fixture so we scrub + them again for the purpose of the test. + """ if dev_info := info.get("get_device_info"): dev_info["device_id"] = "SCRUBBED" elif ( @@ -83,8 +88,7 @@ async def test_smartcam_fixtures(fixture_info: FixtureInfo): """Test that smartcam fixtures are created the same.""" dev = await get_device_for_fixture(fixture_info, verbatim=True) assert isinstance(dev, SmartCamDevice) - # if dev.children: - # pytest.skip("Test not currently implemented for devices with children.") + created_fixtures = await get_smart_fixtures( dev.protocol, discovery_info=fixture_info.data.get("discovery_result"), From cc2e0b46c6e78014d543a13cf8c8f7d7d7ef835b Mon Sep 17 00:00:00 2001 From: Steven B <51370195+sdb9696@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:03:55 +0000 Subject: [PATCH 3/3] Add docstring --- tests/fakeprotocol_smart.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/fakeprotocol_smart.py b/tests/fakeprotocol_smart.py index a30d630fa..a2fc39261 100644 --- a/tests/fakeprotocol_smart.py +++ b/tests/fakeprotocol_smart.py @@ -172,6 +172,14 @@ def credentials_hash(self): } def _missing_result(self, method): + """Check the FIXTURE_MISSING_MAP for responses. + + Fixtures generated prior to a query being supported by dump_devinfo + do not have the response so this method checks whether the component + is supported and fills in the missing response. + If the first value of the lookup value is a tuple it will also check + the version, i.e. (component_name, component_version). + """ if not (missing := self.FIXTURE_MISSING_MAP.get(method)): return None condition = missing[0]