Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deep unordered for nested data structures #17

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pytest_unordered/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ def unordered(*args: Any, check_type: Optional[bool] = None) -> UnorderedList:
return UnorderedList(args, check_type=False)


def unordered_deep(obj: Any) -> Any:
if isinstance(obj, dict):
return dict((k, unordered_deep(v)) for k, v in obj.items())
if isinstance(obj, list) or isinstance(obj, tuple):
return unordered((unordered_deep(x) for x in obj))
return obj


def _compare_eq_unordered(left: Iterable, right: Iterable) -> Tuple[List, List]:
extra_left = []
extra_right = list(right)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_unordered.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pytest_unordered import UnorderedList
from pytest_unordered import _compare_eq_unordered
from pytest_unordered import unordered
from pytest_unordered import unordered_deep


@pytest.mark.parametrize(
Expand Down Expand Up @@ -255,3 +256,21 @@ def test_mock_any() -> None:
assert p_unordered == test_1
assert p_unordered == test_2
assert p_unordered == test_3


@pytest.mark.parametrize(
["expected", "actual"],
[
(unordered_deep([1, 2, 3]), [3, 2, 1]),
(unordered_deep((1, 2, 3)), (3, 2, 1)),
(unordered_deep({1, 2, 3}), {3, 2, 1}),
(unordered_deep([1, 2, {"a": (4, 5, 6)}]), [{"a": [6, 5, 4]}, 2, 1]), # fmt: skip
(unordered_deep([{1: (["a", "b"])}, 2, 3]), [3, 2, {1: ["b", "a"]}]), # fmt: skip
(unordered_deep(("a", "b", "c")), ["b", "a", "c"]),
],
)
def test_unordered_deep(expected: UnorderedList, actual: Iterable) -> None:
assert expected == actual
assert actual == expected
assert not (expected != actual)
assert not (actual != expected)