|
| 1 | +import itertools |
| 2 | +from typing import Union |
| 3 | + |
| 4 | +import pytest |
| 5 | +from attrs import define |
| 6 | +from hypothesis import given |
| 7 | +from hypothesis.strategies import integers |
| 8 | + |
| 9 | +from cattrs import BaseConverter |
| 10 | +from cattrs.strategies import use_class_methods |
| 11 | + |
| 12 | + |
| 13 | +@define |
| 14 | +class Base: |
| 15 | + a: int |
| 16 | + |
| 17 | + |
| 18 | +class Structure(Base): |
| 19 | + @classmethod |
| 20 | + def _structure(cls, data: dict): |
| 21 | + return cls(data["b"]) # expecting "b", not "a" |
| 22 | + |
| 23 | + |
| 24 | +class Unstructure(Base): |
| 25 | + def _unstructure(self): |
| 26 | + return {"c": self.a} # unstructuring as "c", not "a" |
| 27 | + |
| 28 | + |
| 29 | +class Both(Structure, Unstructure): |
| 30 | + pass |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def get_converter(converter: BaseConverter): |
| 35 | + def aux(structure: str, unstructure: str) -> BaseConverter: |
| 36 | + use_class_methods(converter, structure, unstructure) |
| 37 | + return converter |
| 38 | + |
| 39 | + return aux |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "cls,structure_method,unstructure_method", |
| 44 | + itertools.product( |
| 45 | + [Structure, Unstructure, Both], |
| 46 | + ["_structure", "_undefined", None], |
| 47 | + ["_unstructure", "_undefined", None], |
| 48 | + ), |
| 49 | +) |
| 50 | +def test_not_nested(get_converter, structure_method, unstructure_method, cls) -> None: |
| 51 | + converter = get_converter(structure_method, unstructure_method) |
| 52 | + |
| 53 | + assert converter.structure( |
| 54 | + { |
| 55 | + "b" |
| 56 | + if structure_method == "_structure" and hasattr(cls, "_structure") |
| 57 | + else "a": 42 |
| 58 | + }, |
| 59 | + cls, |
| 60 | + ) == cls(42) |
| 61 | + |
| 62 | + assert converter.unstructure(cls(42)) == { |
| 63 | + "c" |
| 64 | + if unstructure_method == "_unstructure" and hasattr(cls, "_unstructure") |
| 65 | + else "a": 42 |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | +@given(integers(1, 5)) |
| 70 | +def test_nested_roundtrip(depth): |
| 71 | + @define |
| 72 | + class Nested: |
| 73 | + a: Union["Nested", None] |
| 74 | + c: int |
| 75 | + |
| 76 | + @classmethod |
| 77 | + def _structure(cls, data, conv): |
| 78 | + b = data["b"] |
| 79 | + return cls(None if b is None else conv.structure(b, cls), data["c"]) |
| 80 | + |
| 81 | + def _unstructure(self, conv): |
| 82 | + return {"b": conv.unstructure(self.a), "c": self.c} |
| 83 | + |
| 84 | + @staticmethod |
| 85 | + def create(depth: int) -> Union["Nested", None]: |
| 86 | + return None if depth == 0 else Nested(Nested.create(depth - 1), 42) |
| 87 | + |
| 88 | + structured = Nested.create(depth) |
| 89 | + |
| 90 | + converter = BaseConverter() |
| 91 | + use_class_methods(converter, "_structure", "_unstructure") |
| 92 | + assert structured == converter.structure(converter.unstructure(structured), Nested) |
0 commit comments