def test_serialize_class_with_multiple_superclasses(self): @dataclass class RootClass: int_field_root: int @dataclass class SimpleBaseClass(RootClass): int_field_base: int str_field_base: str @dataclass class AnotherBaseClass: str_field_another: str @dataclass class SimpleChildClass(SimpleBaseClass, AnotherBaseClass): int_field_child: int str_field_child: str instance = SimpleChildClass(int_field_root=89, int_field_base=42, str_field_base="base", str_field_another="another", int_field_child=84, str_field_child="child") self.assertEqual( instance_to_dict(instance), { 'int_field_root': 89, 'int_field_base': 42, 'str_field_base': 'base', 'str_field_another': 'another', 'int_field_child': 84, 'str_field_child': "child" })
def test_serialize_optional_field_without_value(self): @dataclass class SimpleClassWithOptionalField: int_field: Optional[int] str_field: str instance = SimpleClassWithOptionalField(None, "some value") self.assertEqual(instance_to_dict(instance), {'str_field': "some value"})
def test_serialize_list_fields(self): @dataclass class SimpleClassWithList: int_field: List[int] = field(default_factory=list) str_field: str = "foo" instance = SimpleClassWithList([1, 2, 3, 4, 5]) self.assertEqual(instance_to_dict(instance), { 'int_field': [1, 2, 3, 4, 5], 'str_field': "foo" })
def test_serialize_int_value_0(self): @dataclass class SimpleClass: int_field: int str_field: str instance = SimpleClass(0, "a string") self.assertEqual(instance_to_dict(instance), { 'int_field': 0, 'str_field': "a string" })
def test_serialize_simple_class(self): @dataclass class SimpleClass: int_field: int str_field: str instance = SimpleClass(42, "a string") self.assertEqual(instance_to_dict(instance), { 'int_field': 42, 'str_field': "a string" })
def test_serialize_empty_set_fields(self): @dataclass class SimpleClassWithSet: int_field: Set[int] = field(default_factory=set) str_field: str = "foo" instance = SimpleClassWithSet() self.assertEqual(instance_to_dict(instance), { 'int_field': [], 'str_field': "foo" })
def test_serialize_fields_as(self): @dataclass @serialize_fields_as(int_field=str) class SimpleClassWithMetdata: int_field: int str_field: str instance = SimpleClassWithMetdata(7, "a string") self.assertEqual(instance_to_dict(instance), { 'int_field': "7", 'str_field': "a string", })
def test_serialize_dict_fields(self): @dataclass class SimpleClassWithDict: dict_field: Dict[str, int] instance = SimpleClassWithDict({'one': 1, 'two': 2, 'three': 3}) self.assertEqual(instance_to_dict(instance), {'dict_field': { 'one': 1, 'two': 2, 'three': 3 }})
def test_serialize_set_fields(self): # Note that this also tests that sets are serialized into sorted-order @dataclass class SimpleClassWithSet: int_field: Set[int] = field(default_factory=set) str_field: str = "foo" instance = SimpleClassWithSet(set([5, 4, 3, 2, 1])) self.assertEqual(instance_to_dict(instance), { 'int_field': [1, 2, 3, 4, 5], 'str_field': "foo" })
def test_serialize_fields_as_with_callable(self): def my_serializer(value: int) -> str: return f'The value is {value}.' @dataclass @serialize_fields_as(int_field=my_serializer) class SimpleClassWithMetdata: int_field: int str_field: str instance = SimpleClassWithMetdata(7, "a string") self.assertEqual(instance_to_dict(instance), { 'int_field': "The value is 7.", 'str_field': "a string", })
def test_serialize_class_with_superclass(self): @dataclass class SimpleBaseClass: int_field_base: int str_field_base: str @dataclass class SimpleChildClass(SimpleBaseClass): int_field_child: int str_field_child: str instance = SimpleChildClass(int_field_base=42, str_field_base="base", int_field_child=84, str_field_child="child") self.assertEqual( instance_to_dict(instance), { 'int_field_base': 42, 'str_field_base': 'base', 'int_field_child': 84, 'str_field_child': "child" })