Skip to content

Commit 3c3bc1b

Browse files
committed
Added a circular reference test to cover a missed branch
1 parent e589e45 commit 3c3bc1b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

tests/test_converter_inheritance.py

+26
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ class NonUnionContainer:
5050
a: typing.List[Parent]
5151

5252

53+
@attr.define
54+
class CircularA:
55+
a: int
56+
other: "typing.List[CircularA]"
57+
58+
59+
@attr.define
60+
class CircularB(CircularA):
61+
b: int
62+
63+
5364
IDS_TO_STRUCT_UNSTRUCT = {
5465
"parent-only": (Parent(1), dict(p=1)),
5566
"child1-only": (Child1(1, 2), dict(p=1, c1=2)),
@@ -163,6 +174,21 @@ def test_structure_as_union():
163174
assert res == [Child1(1, 2)]
164175

165176

177+
def test_circular_reference():
178+
c = Converter(include_subclasses=True)
179+
struct = CircularB(a=1, other=[CircularB(a=2, other=[], b=3)], b=4)
180+
unstruct = dict(a=1, other=[dict(a=2, other=[], b=3)], b=4)
181+
182+
res = c.unstructure(struct)
183+
assert res == unstruct
184+
185+
res = c.unstructure(struct, CircularA)
186+
assert res == unstruct
187+
188+
res = c.structure(unstruct, CircularA)
189+
assert res == struct
190+
191+
166192
@pytest.mark.parametrize(
167193
"struct_unstruct", IDS_TO_STRUCT_UNSTRUCT.values(), ids=IDS_TO_STRUCT_UNSTRUCT
168194
)

0 commit comments

Comments
 (0)