def test_deserialize_simple_class_with_default_factory(self): @dataclass class SimpleClass: int_field: int str_field: List[str] = field(default_factory=list) value = {'int_field': 42} self.assertEqual(instance_from_dict(SimpleClass, value), SimpleClass(42, []))
def test_deserialize_simple_class(self): @dataclass class SimpleClass: int_field: int str_field: str value = {'int_field': 42, 'str_field': "a string"} self.assertEqual(instance_from_dict(SimpleClass, value), SimpleClass(42, "a string"))
def test_deserialize_simple_class_with_default_value(self): @dataclass class SimpleClass: int_field: int str_field: str = "a default value" value = {'int_field': 42} self.assertEqual(instance_from_dict(SimpleClass, value), SimpleClass(42, "a default value"))
def test_deserialize_missing_set_fields_empty(self): @dataclass class SimpleClassWithSet: str_field: str = "foo" int_field: Set[int] = field(default_factory=set) instance = SimpleClassWithSet() self.assertEqual( instance_from_dict(SimpleClassWithSet, {'str_field': "foo"}), instance)
def test_deserialize_set_fields(self): @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_from_dict(SimpleClassWithSet, { 'int_field': [1, 2, 3, 4, 5], 'str_field': "foo" }), instance)
def test_deserialize_empty_list_fields(self): @dataclass class SimpleClassWithList: int_field: List[int] = field(default_factory=list) str_field: str = "foo" instance = SimpleClassWithList() self.assertEqual( instance_from_dict(SimpleClassWithList, { 'int_field': [], 'str_field': "foo" }), instance)
def test_deserialize_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_from_dict( 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" }), instance)
def test_deserialize_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_from_dict( SimpleChildClass, { 'int_field_base': 42, 'str_field_base': 'base', 'int_field_child': 84, 'str_field_child': "child" }), instance)