def test_0_simple(self): for obj in (False, True, 0, 1, -1, 1.2, 1E9 + 7, '', 'test', None, [1, None, 'a'], { 'x': 2, 1.3: None }, { 'a': 1, 'b': 'xx', 'c': [1, 2, 3], 'd': { 'w': 'ww' } }): self.assertEqual(obj, serialize(obj)) self.assertEqual(obj, deserialize(obj, type(obj))) # special case for int value converting to float self.assertEqual(1.0, deserialize(1, float)) # tuple deserialized into list self.assertEqual([], serialize(())) self.assertEqual([1, 2, [3]], serialize((1, 2, (3, )))) # not supported types self.assertRaises(TypeError, serialize, {1, 2}) self.assertRaises(TypeError, serialize, int) # list & dict # no inner element recursively deserializing due to no type hint for obj in ([lambda: 'rua', iter([1, 2])], { 1: set({}), 'a': range(10) }): self.assertEqual(obj, deserialize(obj, type(obj)))
def test_2_complex_class(self): a = ConfigImpl() self.assertEqual(a, deserialize(serialize(a), ConfigImpl)) b = deserialize( { 'a': 11, 'b': 'bb', 'c': { 'x': 3, 'y': 4 }, 'd': [{ 'x': 10, 'y': 23 }, { 'x': 11, 'y': 24 }], 'e': { 'home': { 'x': 33, 'y': 24 }, 'park': { 'x': -3, 'y': 2.4 } } }, ConfigImpl) self.assertEqual(b, deserialize(serialize(b), ConfigImpl)) self.assertRaises(TypeError, deserialize, {'e': object()}, ConfigImpl)
def test_2_complex(self): a = ConfigImpl() self.assertEqual(a, deserialize(serialize(a), ConfigImpl)) b = deserialize( { 'a': 11, 'b': 'bb', 'c': { 'x': 3, 'y': 4 }, 'd': [{ 'x': 10, 'y': 23 }, { 'x': 11, 'y': 24 }], 'e': { 'home': { 'x': 33, 'y': 24 }, 'park': { 'x': -3, 'y': 2.4 } } }, ConfigImpl) self.assertEqual(b, deserialize(serialize(b), ConfigImpl))
def test_7_enum(self): class Gender(Enum): male = 'man' female = 'woman' class MyData(Serializable): name: str = 'zhang_san' gender: Gender = Gender.male data = MyData.get_default() self.assertEqual({ 'name': 'zhang_san', 'gender': 'male' }, data.serialize()) data.gender = Gender.female self.assertEqual({ 'name': 'zhang_san', 'gender': 'female' }, data.serialize()) self.assertEqual( Gender.female, MyData.deserialize({ 'name': 'li_si', 'gender': 'female' }).gender) self.assertRaises(KeyError, MyData.deserialize, {'gender': 'none'}) # auto() for base_class in (Enum, IntEnum, Flag, IntFlag): class TestEnum(base_class): RED = auto() BLUE = auto() GREEN = auto() class TestData(Serializable): data: TestEnum try: # noinspection PyTypeChecker for my_enum in TestEnum: serialized = TestData(data=my_enum).serialize() # self.assertEqual(my_enum.value, serialized['data'], msg='w') self.assertEqual(my_enum, TestData.deserialize(serialized).data) except: print( 'Error when testing with base class {}'.format(base_class)) raise # special enum value class TestEnum(Enum): A = auto() SET = set() RANGE = range(5) for enum in TestEnum: self.assertIsInstance(serialize(enum), str) self.assertEqual(enum, deserialize(serialize(enum), TestEnum))
def test_1_simple_class(self): point = Point() point.x = 0.1 point.y = 0.2 self.assertEqual(serialize(point), {'x': 0.1, 'y': 0.2}) self.assertEqual(deserialize({'x': 0.1, 'y': 0.2}, Point), point) self.assertEqual( deserialize({ 'x': 0.1, 'y': 0.2 }, Point, error_at_missing=True, error_at_redundancy=True), point) self.assertEqual(deserialize({}, Point), Point()) self.assertRaises(ValueError, deserialize, {'x': 1}, Point, error_at_missing=True) self.assertRaises(ValueError, deserialize, {'z': 1}, Point, error_at_redundancy=True) self.assertRaises(TypeError, deserialize, {'x': []}, Point) self.assertRaises(TypeError, deserialize, {'x': set()}, Point)
def test_8_subclass(self): class A: a: int = 1 class B: b: str = 'bb' class C(A, B): c: bool = False o = C() o.a, o.b, o.c = 2, 'BB', True o2 = deserialize(serialize(o), type(o)) self.assertEqual(o2.a, o.a) self.assertEqual(o2.b, o.b) self.assertEqual(o2.c, o.c)
def test_1_dict(self): data = {'a': 1, 'b': 'xx', 'c': [1, 2, 3], 'd': {'w': 'ww'}} self.assertEqual(data, serialize(data)) self.assertEqual(data, deserialize(data, dict))