Skip to content

Commit f56798e

Browse files
committed
✅ increase test coverage for Utils.compact and related functions by adding tests for undefined entry removal and cycle detection
1 parent 8b07704 commit f56798e

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

tests/unit/utils_test.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,65 @@ def test_combine_neither_is_an_array(self) -> None:
628628
assert b is not combined
629629
assert combined == [1, 2]
630630

631+
def test_compact_removes_undefined_entries_and_avoids_cycles(self) -> None:
632+
root: t.Dict[str, t.Any] = {
633+
"keep": 1,
634+
"drop": Undefined(),
635+
"nested": [
636+
Undefined(),
637+
{"inner": Undefined(), "keep": "ok"},
638+
],
639+
}
640+
root["self"] = root
641+
642+
Utils.compact(root)
643+
644+
assert "drop" not in root
645+
assert root["nested"][0] == {"keep": "ok"}
646+
647+
def test_remove_undefined_from_list_handles_nested_structures(self) -> None:
648+
data: t.List[t.Any] = [
649+
Undefined(),
650+
{"inner": Undefined(), "tuple": (Undefined(), 3)},
651+
[Undefined(), 4],
652+
]
653+
654+
Utils._remove_undefined_from_list(data)
655+
656+
assert data == [{"tuple": [3]}, [4]]
657+
658+
def test_dicts_are_equal_short_circuits_on_cycles(self) -> None:
659+
a: t.Dict[str, t.Any] = {}
660+
a["self"] = a
661+
662+
assert Utils._dicts_are_equal(a, a) is True
663+
664+
def test_dicts_are_equal_detects_missing_keys(self) -> None:
665+
left: t.Dict[str, t.Any] = {"a": 1}
666+
right: t.Dict[str, t.Any] = {"b": 1}
667+
668+
assert Utils._dicts_are_equal(left, right) is False
669+
670+
def test_dicts_are_equal_detects_nested_value_mismatch(self) -> None:
671+
left: t.Dict[str, t.Any] = {"a": {"b": 1}}
672+
right: t.Dict[str, t.Any] = {"a": {"b": 2}}
673+
674+
assert Utils._dicts_are_equal(left, right) is False
675+
676+
def test_is_non_nullish_primitive_falls_back_when_object_check_false(self, monkeypatch: pytest.MonkeyPatch) -> None:
677+
import builtins
678+
679+
original_isinstance = builtins.isinstance
680+
681+
def fake_isinstance(obj: t.Any, typ: t.Any) -> bool:
682+
if typ is object:
683+
return False
684+
return original_isinstance(obj, typ)
685+
686+
monkeypatch.setitem(Utils.is_non_nullish_primitive.__globals__, "isinstance", fake_isinstance)
687+
688+
assert Utils.is_non_nullish_primitive(object()) is False
689+
631690
def test_remove_undefined_from_list(self) -> None:
632691
map_with_undefined: t.Dict[str, t.Any] = {
633692
"a": [

0 commit comments

Comments
 (0)