def transform_foo_to_bar(foo: Foo) -> Bar: mapper = { 'i': FunctionCall(func=lambda f: [int(f)], args=['i']), 'f': FunctionCall(func=lambda x: str(x), args=['f']) } deserializer = Deserializer(Bar, {'numbers': 'i', 's': 'f'}) serializer = Serializer(source=foo, mapper=mapper) return deserializer.deserialize(serializer.serialize(), keep_undefined=False)
def test_serialization_of_classreference_should_work(): class Bar(Structure): x = Integer y = Integer class Foo(Structure): a = Integer bar1 = Bar bar2 = Bar _required = [] input_dict = {'a': 3, 'bar1': {'x': 3, 'y': 4, 'z': 5}} foo = deserialize_structure(Foo, input_dict) assert Serializer(source=foo.bar1).serialize() == {'x': 3, 'y': 4, 'z': 5} s = Serializer(source=foo.bar2) assert s.serialize() == None
def test_convert_camel_case(): class Foo(Structure): first_name: String last_name: String age_years: PositiveInt _additionalProperties = False original = Foo(first_name="joe", last_name="smith", age_years=5) res = Serializer(source=original).serialize(camel_case_convert=True) assert res == {"firstName": "joe", "lastName": "smith", "ageYears": 5}
def test_serializer_with_invalid_mapper_value_type(): class Foo(Structure): f = Float i = Integer foo = Foo(f=5.5, i=999) mapper = {'f': 123, 'i': FunctionCall(func=lambda x: str(x), args=['f'])} with raises(ValueError) as excinfo: Serializer(foo, mapper=mapper) assert 'mapper_value: Got 123; Did not match any field option' in str( excinfo.value)
def test_enum_serialization_returns_string_name(): class Values(enum.Enum): ABC = enum.auto() DEF = enum.auto() GHI = enum.auto() class Example(Structure): arr = Array[Enum[Values]] e = Example(arr=[Values.GHI, Values.DEF, 'GHI']) assert Serializer(e).serialize() == {'arr': ['GHI', 'DEF', 'GHI']}
def test_serializer_with_invalid_mapper_key_type(): class Foo(Structure): f = Float i = Integer foo = Foo(f=5.5, i=999) mapper = { 123: FunctionCall(func=lambda f: [int(f)], args=['i']), 'i': FunctionCall(func=lambda x: str(x), args=['f']) } with raises(TypeError) as excinfo: Serializer(foo, mapper=mapper) assert 'mapper_key: Got 123; Expected a string' in str(excinfo.value)
def test_pickle_with_map_without_any_type_definition(): class Bar(Structure): m = Map() a = Integer original = Bar(a=3, m={'abc': Bar(a=2, m={"x": "xx"}), 'bcd': 2}) serialized = serialize(original) unpickeled = pickle.loads(pickle.dumps(serialized)) deserialized = Deserializer(target_class=Bar).deserialize(unpickeled) # there is no info on the fact that deserialized.m['abc'] should be converted to a Bar instance, so # we convert it to a simple dict, to make it straight forward to compare original.m['abc'] = Serializer(original.m['abc']).serialize() assert deserialized == original
def test_serializer_with_invalid_function_call_arg(): class Foo(Structure): f = Float i = Integer foo = Foo(f=5.5, i=999) mapper = { 'f': FunctionCall(func=lambda f: [int(f)], args=['i', 'x']), 'i': FunctionCall(func=lambda x: str(x), args=['f']) } with raises(ValueError) as excinfo: Serializer(foo, mapper=mapper) assert 'Mapper[f] has a function call with an invalid argument: x' in str( excinfo.value)
def test_serializer_with_invalid_mapper_key(): class Foo(Structure): f = Float i = Integer foo = Foo(f=5.5, i=999) mapper = { 'x': FunctionCall(func=lambda f: [int(f)], args=['i']), 'i': FunctionCall(func=lambda x: str(x), args=['f']) } with raises(ValueError) as excinfo: Serializer(foo, mapper=mapper) assert 'Invalid key in mapper for class Foo: x. Keys must be one of the class fields.' in str( excinfo.value)
def test_serializer_with_mapper_with_function_with_args(): class Foo(Structure): f = Float i = Integer foo = Foo(f=5.5, i=999) mapper = { 'f': FunctionCall(func=lambda f: [int(f)], args=['i']), 'i': FunctionCall(func=lambda x: str(x), args=['f']) } assert Serializer(source=foo, mapper=mapper).serialize() == { 'f': [999], 'i': '5.5' }