Skip to content

Commit

Permalink
Do not ignore Union argument ordering in tests (#248)
Browse files Browse the repository at this point in the history
* Revert "Ignore union ordering"

This reverts commit b61cd50.

* test: mark expected failure, add separate test of its non-failing bits

* ci(tests): report skipped/xfailed tests

* docs(test): add link for context

Co-authored-by: Ophir LOJKINE <contact@ophir.dev>

---------

Co-authored-by: Ophir LOJKINE <contact@ophir.dev>
  • Loading branch information
dairiki and lovasoa committed Sep 17, 2023
1 parent 260c4fe commit f315d71
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ jobs:
if: ${{ matrix.python_version != 'pypy3' && matrix.python_version != '3.6' }}
run: pre-commit run --all-files
- name: Test with pytest
run: pytest
run: pytest -ra
38 changes: 28 additions & 10 deletions tests/test_field_for_schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import inspect
import sys
import typing
Expand Down Expand Up @@ -26,18 +27,11 @@ class TestFieldForSchema(unittest.TestCase):
def assertFieldsEqual(self, a: fields.Field, b: fields.Field):
self.assertEqual(a.__class__, b.__class__, "field class")

def canonical(k, v):
if k == "union_fields":
# See https://github.com/lovasoa/marshmallow_dataclass/pull/246#issuecomment-1722291806
return k, sorted(map(repr, v))
elif inspect.isclass(v):
return k, f"{v!r} ({v.__mro__!r})"
else:
return k, repr(v)

def attrs(x):
return sorted(
canonical(k, v) for k, v in vars(x).items() if not k.startswith("_")
(k, f"{v!r} ({v.__mro__!r})" if inspect.isclass(v) else repr(v))
for k, v in x.__dict__.items()
if not k.startswith("_")
)

self.assertEqual(attrs(a), attrs(b))
Expand Down Expand Up @@ -189,7 +183,11 @@ def test_union_multiple_types_with_none(self):
),
)

@unittest.expectedFailure
def test_optional_multiple_types(self):
# excercise bug (see #247)
Optional[Union[str, int]]

self.assertFieldsEqual(
field_for_schema(Optional[Union[int, str]]),
union_field.Union(
Expand All @@ -203,6 +201,26 @@ def test_optional_multiple_types(self):
),
)

def test_optional_multiple_types_ignoring_union_field_order(self):
# see https://github.com/lovasoa/marshmallow_dataclass/pull/246#issuecomment-1722204048
result = field_for_schema(Optional[Union[int, str]])
expected = union_field.Union(
[
(int, fields.Integer(required=True)),
(str, fields.String(required=True)),
],
required=False,
dump_default=None,
load_default=None,
)

def sort_union_fields(field):
rv = copy.copy(field)
rv.union_fields = sorted(field.union_fields, key=repr)
return rv

self.assertFieldsEqual(sort_union_fields(result), sort_union_fields(expected))

def test_newtype(self):
self.assertFieldsEqual(
field_for_schema(typing.NewType("UserId", int), default=0),
Expand Down

0 comments on commit f315d71

Please sign in to comment.