Skip to content

Commit 994adda

Browse files
fix: match
1 parent 6104f45 commit 994adda

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

packages/common-library/src/common_library/dict_tools.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,15 @@ def update_dict(obj: dict, **updates):
5858
{key: update_value(obj[key]) if callable(update_value) else update_value}
5959
)
6060
return obj
61+
62+
63+
def assert_equal_ignoring_none(expected: dict, actual: dict):
64+
for key, exp_value in expected.items():
65+
if exp_value is None:
66+
continue
67+
assert key in actual, f"Missing key {key}"
68+
act_value = actual[key]
69+
if isinstance(exp_value, dict) and isinstance(act_value, dict):
70+
assert_equal_ignoring_none(exp_value, act_value)
71+
else:
72+
assert act_value == exp_value, f"Mismatch in {key}: {act_value} != {exp_value}"

packages/common-library/tests/test_dict_tools.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Any
77

88
import pytest
9+
from common_library.dict_tools import assert_equal_ignoring_none
910
from common_library.dict_tools import (
1011
copy_from_dict,
1112
get_from_dict,
@@ -160,3 +161,31 @@ def test_copy_from_dict(data: dict[str, Any]):
160161
assert selected_data["Status"]["State"] == data["Status"]["State"]
161162
assert "Message" not in selected_data["Status"]["State"]
162163
assert "running" in data["Status"]["State"]
164+
165+
166+
@pytest.mark.parametrize(
167+
"expected, actual",
168+
[
169+
({"a": 1, "b": 2}, {"a": 1, "b": 2, "c": 3}),
170+
({"a": 1, "b": None}, {"a": 1, "b": 42}),
171+
({"a": {"x": 10, "y": None}}, {"a": {"x": 10, "y": 99}}),
172+
({"a": {"x": 10, "y": 20}}, {"a": {"x": 10, "y": 20, "z": 30}}),
173+
({}, {"foo": "bar"}),
174+
],
175+
)
176+
def test_assert_equal_ignoring_none_passes(expected, actual):
177+
assert_equal_ignoring_none(expected, actual)
178+
179+
@pytest.mark.parametrize(
180+
"expected, actual, error_msg",
181+
[
182+
({"a": 1, "b": 2}, {"a": 1}, "Missing key b"),
183+
({"a": 1, "b": 2}, {"a": 1, "b": 3}, "Mismatch in b: 3 != 2"),
184+
({"a": {"x": 10, "y": 20}}, {"a": {"x": 10, "y": 99}}, "Mismatch in y: 99 != 20"),
185+
({"a": {"x": 10}}, {"a": {}}, "Missing key x"),
186+
],
187+
)
188+
def test_assert_equal_ignoring_none_fails(expected, actual, error_msg):
189+
with pytest.raises(AssertionError) as exc_info:
190+
assert_equal_ignoring_none(expected, actual)
191+
assert error_msg in str(exc_info.value)

0 commit comments

Comments
 (0)