def serde(self) -> SerdeStruct: ss = SerdeSet.walk(SERVER_SERDE_INST, datetime.datetime) ss = ss | SerdeSet.walk(SERVER_SERDE_INST, Optional[str]) for k, v in self.items(): if v.conf.exc: continue ssreq = SerdeSet.walk(SERVER_SERDE_INST, v.req) ssres = SerdeSet.walk(SERVER_SERDE_INST, v.res) ss = ss | ssreq | ssres return ss.struct(SERVER_SERDE_INST)
def _generic(self, tp, tp_2, val): with self.subTest('init'): serde = SerdeSet.walk(SERVER_SERDE_INST, tp).struct(SERVER_SERDE_INST) serde_2 = SerdeSet.walk(SERVER_SERDE_INST, tp_2).struct(SERVER_SERDE_INST) i = val with self.subTest('ser'): x = serde.serialize(tp, i) with self.subTest('des'): y = serde_2.deserialize(tp_2, x) self.assertEqual(i, y)
def test_forward_ref_cyclic(self): with self.subTest('is_creatable'): WorkerSerde = SerdeSet.walk(SERVER_SERDE_INST, ObjR1) \ .merge(SerdeSet.walk(SERVER_SERDE_INST, ObjR2)).struct(SERVER_SERDE_INST) if sys.version_info >= (3, 7): with self.subTest('is_picklable'): pickle.dumps(WorkerSerde) xo = {'i': {'i': {'i': None}}} with self.subTest('des'): x = WorkerSerde.deserialize(ObjR1, xo) with self.subTest('ser'): y = WorkerSerde.serialize(ObjR1, x) with self.subTest('res'): self.assertEqual(y, xo)
def test_empty(self): i = SERVER_SERDE_INST x = SerdeSet.walk(i, Simple, SerdeStepContext(mod=sys.modules[__name__])) y = x.struct(i) z = y.deserialize(Simple, {'x': 5, 'z': {'y': 'abc'}})
def test_caller(self): i = SerdeInst(CALL_TYPES) class Simpleton(NamedTuple): x: int def a(a: int, b: str = 'abc', *cs: Dict[str, str], g: str, **kwargs: int): pass class A: def a(self, a: int, c: Simpleton, b: str = 'abc', *cs: Dict[str, str], g: str, **kwargs: int): pass obj = A() wrapper = CallableArgsWrapper.from_func(a) wrapper2 = CallableArgsWrapper.from_func_cls(A, A.a) x1 = SerdeSet.walk(i, wrapper) x2 = SerdeSet.walk(i, wrapper2) x = x1.merge(x2) y = x.struct(i) z = y.deserialize(wrapper, [[5, 'asd', {'a': 'a'}, {'b': 'c'}], {'g': 'abc', 'd': 5}]) args, kwargs = z a(*args, **kwargs) # z = y.deserialize(wrapper2, [[5, {'x': 5}, 'asd', {'a': 'a'}, {'b': 'c'}], {'g': 'abc', 'd': 5}]) zb = y.serialize(wrapper2, z) args, kwargs = z obj.a(*args, **kwargs)
def test_union_versioning_fail(self): tp = Union[A, B, C] tp_2 = Union[A, C] with self.subTest('init'): serde = SerdeSet.walk(SERVER_SERDE_INST, tp).struct(SERVER_SERDE_INST) serde_2 = SerdeSet.walk(SERVER_SERDE_INST, tp_2).struct(SERVER_SERDE_INST) i = B(b'asd') with self.subTest('ser'): x = serde.serialize(tp, i) with self.subTest('des'): try: y = serde_2.deserialize(tp_2, x) self.assertEqual(i, y) except SerdeException as e: self.assertEqual('u_idx', e.resolve().code)
def test_pickle_simple(self): with self.subTest('is_creatable'): WorkerSerde = SerdeSet.walk(SERVER_SERDE_INST, Obj).struct(SERVER_SERDE_INST) with self.subTest('is_picklable'): import pickle pickle.dumps(WorkerSerde) xo = {'i': 5} with self.subTest('des'): x = WorkerSerde.deserialize(Obj, xo) with self.subTest('ser'): y = WorkerSerde.serialize(Obj, x) with self.subTest('res'): self.assertEqual(y, xo)
def test_union_optional_none(self): tp = Optional[Union[A, B]] with self.subTest('init'): serde = SerdeSet.walk(SERVER_SERDE_INST, tp).struct(SERVER_SERDE_INST) i = None with self.subTest('ser'): x = serde.serialize(tp, i) with self.subTest('des'): y = serde.deserialize(tp, x) self.assertEqual(i, y)
def test_list_generic_0(self): type_ = Obj[int] with self.subTest('walk'): serde = SerdeSet.walk(SERVER_SERDE_INST, type_).struct(SERVER_SERDE_INST) initial = Obj([1, 2, 3]) with self.subTest('ser'): x = serde.serialize(type_, initial) with self.subTest('des'): y = serde.deserialize(type_, x) with self.subTest('eq'): self.assertEqual(initial, y)
def test_tuple_0(self): type_ = Tuple[int, int, int] with self.subTest('walk'): serde = SerdeSet.walk(SERVER_SERDE_INST, type_).struct(SERVER_SERDE_INST) initial = (1, 2, 3) with self.subTest('ser'): x = serde.serialize(type_, initial) with self.subTest('des'): y = serde.deserialize(type_, x) with self.subTest('eq'): self.assertEqual(initial, y)
def test_tuple_generic_1(self): type_ = Obj3[int] with self.subTest('walk'): serde = SerdeSet.walk(SERVER_SERDE_INST, type_).struct(SERVER_SERDE_INST) initial = Obj3((Obj2(1), Obj2(2))) with self.subTest('ser'): x = serde.serialize(type_, initial) with self.subTest('des'): y = serde.deserialize(type_, x) with self.subTest('eq'): self.assertEqual(initial, y)
def test_forward_ref_foreign_mod(self): with self.subTest('is_creatable'): WorkerSerde = SerdeSet.walk(SERVER_SERDE_INST, ObjR4).struct(SERVER_SERDE_INST) with self.subTest('is_picklable'): import pickle pickle.dumps(WorkerSerde) xo = {'y': {'i': 6}} with self.subTest('des'): x = WorkerSerde.deserialize(ObjR4, xo) with self.subTest('ser'): y = WorkerSerde.serialize(ObjR4, x) with self.subTest('res'): self.assertEqual(y, xo)
def test_generic_method_ret(self): fa = CallableRetWrapper.from_func_cls(Runner[int], Runner.method) a = SerdeSet.walk( SERVER_SERDE_INST, fa ) xo = 8 x = a.struct(SERVER_SERDE_INST).serialize(fa, xo) y = a.struct(SERVER_SERDE_INST).deserialize(fa, xo) self.assertEqual(x, y) try: a.struct(SERVER_SERDE_INST).serialize(fa, 'str') except SerdeException as e: self.assertEqual('str', e.resolve().val) self.assertEqual('int', e.resolve().kwargs['t'])
def test_generic_method(self): fa = CallableArgsWrapper.from_func_cls(Runner[int], Runner.method2) a = SerdeSet.walk( SERVER_SERDE_INST, fa ) xo = [(5, 7), {}] x = a.struct(SERVER_SERDE_INST).serialize(fa, xo) y = a.struct(SERVER_SERDE_INST).deserialize(fa, xo) self.assertEqual(x, y) try: a.struct(SERVER_SERDE_INST).serialize(fa, [(5, 'asd'), {}]) except SerdeException as e: self.assertEqual('asd', e.resolve().val) self.assertEqual('int', e.resolve().kwargs['t'])
def test_generic(self): for ty, tv in zip(GEN_TYPES, GEN_VALS): n = ty.__class__.__name__ gt = Gen[ty] tv = {'i': tv} with self.subTest(f'{n}.is_creatable'): WorkerSerde = SerdeSet.walk(SERVER_SERDE_INST, gt).struct(SERVER_SERDE_INST) if sys.version_info >= (3, 7): with self.subTest(f'{n}.is_picklable'): pickle.dumps(WorkerSerde) with self.subTest(f'{n}.des'): x = WorkerSerde.deserialize(gt, tv) with self.subTest(f'{n}.ser'): y = WorkerSerde.serialize(gt, x) with self.subTest(f'{n}.res'): self.assertEqual(tv, y)
def build_worker_serde(): a = SerdeSet.walk(SERVER_SERDE_INST, Obj) s = a.merge(a) return s.struct(SERVER_SERDE_INST)
def test_caller(self): i = SERVER_SERDE_INST class Simpleton(NamedTuple): x: int def a(a: int, b: str = 'abc', *cs: Dict[str, str], g: str, **kwargs: int): pass class A: def a(self, a: int, c: Simpleton, b: str = 'abc', *cs: Dict[str, str], g: str, **kwargs: int): pass obj = A() wrapper = CallableArgsWrapper.from_func(a) wrapper2 = CallableArgsWrapper.from_func_cls(obj, A.a) x1 = SerdeSet.walk(i, wrapper, SerdeStepContext(mod=sys.modules[__name__])) x2 = SerdeSet.walk(i, wrapper2, SerdeStepContext(mod=sys.modules[__name__])) x = x1.merge(x2) y = x.struct(i) z = y.deserialize(wrapper, [[5, 'asd', { 'a': 'a' }, { 'b': 'c' }], { 'g': 'abc', 'd': 5 }]) args, kwargs = z a(*args, **kwargs) # z = y.deserialize(wrapper2, [[5, { 'x': 5 }, 'asd', { 'a': 'a' }, { 'b': 'c' }], { 'g': 'abc', 'd': 5 }]) zb = y.serialize(wrapper2, z) args, kwargs = z obj.a(*args, **kwargs)
def setUp(self): self.serde = SerdeSet.walk(SERVER_SERDE_INST, ObjV1) self.serde = self.serde.merge( SerdeSet.walk(SERVER_SERDE_INST, ObjV2Err)) self.serde = self.serde.merge(SerdeSet.walk(SERVER_SERDE_INST, ObjV2)) self.serde = self.serde.struct(SERVER_SERDE_INST)
def test_union_same_name(self): with self.subTest('init'): with self.assertRaises(AssertionError): serde = SerdeSet.walk(SERVER_SERDE_INST, Union[_A, A, B]).struct(SERVER_SERDE_INST)