Skip to content

Commit 4f312b5

Browse files
committed
Provide custom rendering for DandiBaseModel inequalities to simplify handling
1 parent 4026e84 commit 4f312b5

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

dandi/pytest_plugin.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from dandischema.models import DandiBaseModel
34
from pytest import Config, Item, Parser
45

56
from .tests.fixtures import * # noqa: F401, F403 # lgtm [py/polluting-import]
@@ -28,9 +29,7 @@ def pytest_configure(config):
2829
"ai_generated",
2930
]
3031
for marker in markers:
31-
config.addinivalue_line(
32-
"markers", marker
33-
)
32+
config.addinivalue_line("markers", marker)
3433

3534

3635
def pytest_collection_modifyitems(items: list[Item], config: Config) -> None:
@@ -46,3 +45,25 @@ def pytest_collection_modifyitems(items: list[Item], config: Config) -> None:
4645
deselected_items.append(item)
4746
config.hook.pytest_deselected(items=deselected_items)
4847
items[:] = selected_items
48+
49+
50+
def pytest_assertrepr_compare(op, left, right):
51+
"""Custom comparison representation for your classes."""
52+
if (
53+
isinstance(left, DandiBaseModel)
54+
and isinstance(right, DandiBaseModel)
55+
and op == "=="
56+
):
57+
ldict, rdict = dict(left), dict(right)
58+
if ldict == rdict:
59+
return [
60+
"dict representations of models are equal, but values aren't!",
61+
f"Left: {left!r}",
62+
f"Right: {right!r}",
63+
]
64+
else:
65+
# Rely on pytest just "recursing" into interpreting the dict fails
66+
# TODO: could be further improved by account for ANY values etc
67+
assert ldict == rdict # for easier comprehension of diffs
68+
return None
69+
return None

0 commit comments

Comments
 (0)