def test_hierarchy_field(): obj = WithHierarchyAsField(SecondChild('a')) serde_and_compare(obj, true_payload={'field': { 'type': 'second', 'field': 'a' }})
def test_unsized_tuple_hint(): @make_string class CClass(Comparable): def __init__(self, value: Tuple[int, ...]): self.value = value serde_and_compare(CClass((3, 2)), true_payload={'value': [3, 2]})
def test_primitive_serializer(): @type_field('type') class Root(Serializer): pass class PrimitiveDatasetType(Root): type = 'primitive' def __init__(self, ptype: str): self.ptype = ptype @property def to_type(self): import builtins return getattr(builtins, self.ptype) def deserialize(self, obj): return self.to_type(obj) def serialize(self, instance): return instance IntSerializer = PrimitiveDatasetType('int') serde_and_compare(1, IntSerializer)
def test_typed_tuple_hint(): @make_string class CClass(Comparable): def __init__(self, value: Tuple[str, int]): self.value = value serde_and_compare(CClass(('a', 1)), true_payload={'value': ['a', 1]})
def test_typed_tuple(): serde_and_compare((AClass('a'), BClass(1)), Tuple[AClass, BClass], [{ 'value': 'a' }, { 'value': 1 }])
def test_hierarchy_skip(): obj = SecondChild('test') serde_and_compare(obj, Child, true_payload={ 'type': 'second', 'field': 'test' })
def test_set_hint(): @make_string class CClass(Comparable): def __init__(self, value: Set[str]): self.value = value value = CClass({'a', 'b'}) serde_and_compare(value) assert serialize(value) in [{'value': ['a', 'b']}, {'value': ['b', 'a']}]
def test_static_serializer(): @make_string class AContainer(Comparable): def __init__(self, test: AClass): self.test = test t1 = AContainer(AClass([5 for _ in range(10)])) serde_and_compare(t1)
def test_set(): value = {AClass('a'), AClass('b')} serde_and_compare(value, Set[AClass]) assert serialize(value, Set[AClass]) in [[{ 'value': 'a' }, { 'value': 'b' }], [{ 'value': 'b' }, { 'value': 'a' }]]
def test_any(): class WithAny(Comparable): def __init__(self, field: Any): self.field = field obj = WithAny(1) serde_and_compare(obj, true_payload={'field': 1}) obj = WithAny('1') serde_and_compare(obj, true_payload={'field': '1'}) obj = WithAny({'a', 1}) serde_and_compare(obj, true_payload={'field': {'a', 1}})
def test_seq(): obj1 = TupleTest((1, 2)) serde_and_compare(obj1, true_payload={'field': [1, 2]}) obj1 = SetTest({1, 2}) serde_and_compare(obj1, true_payload={'field': [1, 2]})
def test_optional_none(): obj1 = OptionalNone([1]) obj2 = OptionalNone([None]) serde_and_compare(obj1, true_payload={'field': [1]}) serde_and_compare(obj2, true_payload={'field': [None]})
def test_dict_keys(): class IntDict(Comparable): def __init__(self, d: Dict[int, int]): self.d = d serde_and_compare(IntDict({1: 1}))
def test_hierarchy_ser(): obj = ChildAL('b') serde_and_compare(obj, true_payload=['child_al', 'b'])
def test_type_hint_with_default_not_exists(): serde_and_compare(HintAndDefault())
def test_unserializable(): class A(Unserializable): pass with pytest.raises(UnserializableError): serde_and_compare(A())
def test_serializer_logic_add(): ser1 = ASerializer('a') ser2 = ASerializer('b') sum_ser = ser1 + ser2 serde_and_compare('', sum_ser, 'ab')
def test_parametrized(): t1 = ContainerSized(CClass([5 for _ in range(10)])) serde_and_compare(t1, true_payload={'test': {'value': 5}})
def test_camel_case(): obj = CamelCase('a', 'b') serde_and_compare(obj, true_payload={'fieldOne': 'a', 'fieldTwo': 'b'})
def test_rename__child_extend(): obj = ChildExtend('aaaa', 'bbbb') serde_and_compare(obj, true_payload={'field_1': 'aaaa', 'field_2': 'bbbb'})
def test_rename__child_override(): obj = ChildOverride('aaaa') serde_and_compare(obj, true_payload={'1field': 'aaaa'})
def test_rename__child(): obj = Child('aaaa') serde_and_compare(obj, true_payload={'field_1': 'aaaa'})
def test_rename__parent(): obj = Parent('aaaa') serde_and_compare(obj, true_payload={'field_1': 'aaaa'})
def test_explicit_child(): obj = SecondChild('aaa') serde_and_compare(obj, SecondChild, {'field': 'aaa'}, check_payload=False)
def test_unsized_tuple(): serde_and_compare((1, 2, 3), Tuple[int, ...], [1, 2, 3])
def test_serializer_logic(): serde_and_compare('a', ASerializer('b'), 'ab')
def test_type_hint_with_default_exists(): serde_and_compare(HintAndDefault(Foo('kek')), true_payload={'foo': { 'bar': 'kek' }})
def test_hierarchy(): obj = Child() serde_and_compare(obj, Parent)
def test_type_hint_with_default_dict_exists(): serde_and_compare(HintAndDefaultDict({'test': Foo('kek')}))
def test_simple(): obj = SimpleAL(1, 'a') serde_and_compare(obj, true_payload=[1, 'a'])