Skip to content

Commit

Permalink
Added a circular reference test to cover a missed branch
Browse files Browse the repository at this point in the history
  • Loading branch information
matmel committed Oct 23, 2022
1 parent 4e25330 commit 8f5009b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/test_converter_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ class NonUnionContainer:
a: typing.List[Parent]


@attr.define
class CircularA:
a: int
other: "typing.List[CircularA]"


@attr.define
class CircularB(CircularA):
b: int


IDS_TO_STRUCT_UNSTRUCT = {
"parent-only": (Parent(1), dict(p=1)),
"child1-only": (Child1(1, 2), dict(p=1, c1=2)),
Expand Down Expand Up @@ -163,6 +174,21 @@ def test_structure_as_union():
assert res == [Child1(1, 2)]


def test_circular_reference():
c = Converter(include_subclasses=True)
struct = CircularB(a=1, other=[CircularB(a=2, other=[], b=3)], b=4)
unstruct = dict(a=1, other=[dict(a=2, other=[], b=3)], b=4)

res = c.unstructure(struct)
assert res == unstruct

res = c.unstructure(struct, CircularA)
assert res == unstruct

res = c.structure(unstruct, CircularA)
assert res == struct


@pytest.mark.parametrize(
"struct_unstruct", IDS_TO_STRUCT_UNSTRUCT.values(), ids=IDS_TO_STRUCT_UNSTRUCT
)
Expand Down

0 comments on commit 8f5009b

Please sign in to comment.