Skip to content

Commit

Permalink
Improve cols coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche committed Dec 2, 2024
1 parent 9f0450e commit 53114bf
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions tests/test_tuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,25 @@ class Test(NamedTuple):
def test_simple_dict_nametuples(genconverter: Converter):
"""Namedtuples can be un/structured to/from dicts."""

class TestInner(NamedTuple):
a: int

class Test(NamedTuple):
a: int
b: str = "test"
c: TestInner = TestInner(1)

genconverter.register_unstructure_hook_factory(
lambda t: t is Test, namedtuple_dict_unstructure_factory
lambda t: t in (Test, TestInner), namedtuple_dict_unstructure_factory
)
genconverter.register_structure_hook_factory(
lambda t: t is Test, namedtuple_dict_structure_factory
lambda t: t in (Test, TestInner), namedtuple_dict_structure_factory
)

assert genconverter.unstructure(Test(1)) == {"a": 1, "b": "test"}
assert genconverter.structure({"a": 1, "b": "2"}, Test) == Test(1, "2")
assert genconverter.unstructure(Test(1)) == {"a": 1, "b": "test", "c": {"a": 1}}
assert genconverter.structure({"a": 1, "b": "2"}, Test) == Test(
1, "2", TestInner(1)
)

# Defaults work.
assert genconverter.structure({"a": 1}, Test) == Test(1, "test")
Expand Down

0 comments on commit 53114bf

Please sign in to comment.