1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
"""Tests for functionality from the gen module."""
import linecache
from traceback import format_exc
from attrs import define
from cattrs import Converter
from cattrs.gen import make_dict_structure_fn, make_dict_unstructure_fn
def test_structure_linecache():
"""Linecaching for structuring should work."""
@define
class A:
a: int
c = Converter(detailed_validation=False)
try:
c.structure({"a": "test"}, A)
except ValueError:
res = format_exc()
assert "'a'" in res
def test_unstructure_linecache():
"""Linecaching for unstructuring should work."""
@define
class Inner:
a: int
@define
class Outer:
inner: Inner
c = Converter()
try:
c.unstructure(Outer({}))
except AttributeError:
res = format_exc()
assert "'a'" in res
def test_no_linecache():
"""Linecaching should be disableable."""
@define
class A:
a: int
c = Converter()
before = len(linecache.cache)
c.structure(c.unstructure(A(1)), A)
after = len(linecache.cache)
assert after == before + 2
@define
class B:
a: int
before = len(linecache.cache)
c.register_structure_hook(
B, make_dict_structure_fn(B, c, _cattrs_use_linecache=False)
)
c.register_unstructure_hook(
B, make_dict_unstructure_fn(B, c, _cattrs_use_linecache=False)
)
c.structure(c.unstructure(B(1)), B)
assert len(linecache.cache) == before
def test_linecache_dedup():
"""Linecaching avoids duplicates."""
@define
class LinecacheA:
a: int
c = Converter()
before = len(linecache.cache)
c.structure(c.unstructure(LinecacheA(1)), LinecacheA)
after = len(linecache.cache)
assert after == before + 2
c = Converter()
c.structure(c.unstructure(LinecacheA(1)), LinecacheA)
assert len(linecache.cache) == after
|