Skip to content

Commit

Permalink
fixup! fix(tests): attempt at making assertFieldsEqual more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
dairiki committed Sep 16, 2023
1 parent 0a5018e commit ddd3428
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions tests/test_field_for_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,32 @@ def _assertAttrsEqual(self, a, b, msg=None):

attrs_a = {attr for attr in vars(a) if not attr.startswith("_")}
attrs_b = {attr for attr in vars(b) if not attr.startswith("_")}
if attrs_a - attrs_b:
self.fail(
f"Attributes in first value but not in second: {' '.join(map(repr, attrs_a - attrs_b))}"
)
if attrs_b - attrs_a:
self.fail(
f"Attributes in second value but not in first: {' '.join(map(repr, attrs_b - attrs_a))}"
)
if attrs_a != attrs_b:
if msg is None:
msg = "values do not have the same set of attributes"
self.fail(msg)

checked = []

def assert_equal(v1, v2):
if (v1, v2) not in checked:
checked.append((v1, v2))
if (
type(v1) is type(v2)
and isinstance(v1, Sequence)
and isinstance(v1, Sequence)
and len(v1) == len(v1)
):
# TestCase.assertSequenceEqual tests item equality with __eq__.
# (It doesn't dispatch to an assert* method based on the item type.)
# So we'll do the comparison ourself using assertEqual.
for left, right in zip(v1, v2):
assert_equal(left, right)
else:
self.assertEqual(v1, v2, msg)

for attr in attrs_a | attrs_b:
value_a = a.__dict__[attr]
value_b = b.__dict__[attr]
if (
type(value_a) is type(value_b)
and isinstance(value_a, Sequence)
and isinstance(value_b, Sequence)
and len(value_a) == len(value_b)
):
# TestCase.assertSequenceEqual tests item equality with __eq__.
# (It doesn't dispatch to an assert* method based on the item type.)
# So we'll do the comparison ourself using assertEqual.
for v1, v2 in zip(value_a, value_b):
self.assertEqual(v1, v2, msg)
else:
self.assertEqual(value_a, value_b, msg)
assert_equal(vars(a)[attr], vars(b)[attr])

def test_int(self):
self.assertFieldsEqual(
Expand Down

0 comments on commit ddd3428

Please sign in to comment.