Skip to content

Commit 6bc2851

Browse files
committed
deep compare to ignore list orders in values
1 parent dac51bc commit 6bc2851

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

tests/test_device.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Test ZHA device switch."""
22

33
import asyncio
4+
from collections.abc import Mapping, Sequence
5+
import json
46
import logging
57
import time
68
from unittest import mock
@@ -1032,7 +1034,7 @@ async def test_extended_device_info_ser_deser(zha_gateway: Gateway) -> None:
10321034
assert isinstance(zha_device.extended_device_info.nwk, zigpy.types.NWK)
10331035

10341036
# last_seen changes so we exclude it from the comparison
1035-
json = zha_device.extended_device_info.model_dump_json(
1037+
json_string = zha_device.extended_device_info.model_dump_json(
10361038
exclude=["last_seen", "last_seen_time"]
10371039
)
10381040

@@ -1043,4 +1045,26 @@ async def test_extended_device_info_ser_deser(zha_gateway: Gateway) -> None:
10431045
) as file:
10441046
expected_json = file.read()
10451047

1046-
assert json == expected_json
1048+
assert deep_compare(json.loads(json_string), json.loads(expected_json))
1049+
1050+
1051+
def deep_compare(obj1, obj2):
1052+
"""Recursively compare two objects."""
1053+
if isinstance(obj1, Mapping) and isinstance(obj2, Mapping):
1054+
# Compare dictionaries (order of keys doesn't matter)
1055+
if obj1.keys() != obj2.keys():
1056+
return False
1057+
return all(deep_compare(obj1[key], obj2[key]) for key in obj1)
1058+
1059+
elif (
1060+
isinstance(obj1, Sequence)
1061+
and isinstance(obj2, Sequence)
1062+
and not isinstance(obj1, str)
1063+
):
1064+
# Compare lists or other sequences as sets, ignoring order
1065+
return len(obj1) == len(obj2) and all(
1066+
any(deep_compare(item1, item2) for item2 in obj2) for item1 in obj1
1067+
)
1068+
1069+
# Base case: compare values directly
1070+
return obj1 == obj2

0 commit comments

Comments
 (0)