def test_dict_field(): serializer = dict_field('a') test_obj = {'a': 'b'} target = {} serializer.serialize(test_obj, target) assert {'a': 'b'} == target from_json = {'a': 'b'} target = {} serializer.deserialize(from_json, target) assert {'a': 'b'} == target serializer = dict_field('empty') test_obj = {"empty": None} target = {} serializer.serialize(test_obj, target) assert {"empty": None} == target serializer = dict_field('empty') test_obj = {"empty": None} target = {} serializer.serialize(test_obj, target, serialization_context) assert {} == target
def test_many(): child_serializer = serializer( field('c1') ) a_serializer = serializer( field('a'), many('c', serializer=child_serializer) ) a_dict_serializer = serializer( dict_field('a'), many('c', serializer=child_serializer, attr_getter=lambda x: x.get('c')) ) test_obj = TestObject() target = a_serializer.serialize(test_obj) target2 = a_dict_serializer.serialize(TestObject.__dict__) reference = { 'a': 1, 'c': [{ 'c1': 'a', }, { 'c1': 'a', }] } assert reference == target == target2 target = a_serializer.deserialize(reference) assert target == reference
def test_child(): child_serializer = serializer(field('b1')) a_serializer = serializer(field('a'), child('b', serializer=child_serializer)) a_dict_serializer = serializer( dict_field('a'), child('b', serializer=child_serializer, attr_getter=lambda x: x.get('b'))) test_obj = TestObject() target = a_serializer.serialize(test_obj) target2 = a_dict_serializer.serialize(test_obj.__class__.__dict__) assert { 'a': 1, 'b': { 'b1': 2, } } == target == target2 from_json = { 'a': 1, 'b': { 'b1': 2, } } target = a_serializer.deserialize(from_json) assert from_json == target
def test_dict_field(): serializer = dict_field('a') test_obj = {'a': 'b'} target = {} serializer.serialize(test_obj, target) assert {'a': 'b'} == target from_json = {'a': 'b'} target = {} serializer.deserialize(from_json, target) assert {'a': 'b'} == target
def test_many_full_validation(): size_serializer = serializer(dict_field('size')) test_serializer = many('e', serializer=size_serializer, validators=[raise_validator]) test_obj = {'e': [{"size": 'a'}, {"size": 'bb'}]} target = {} errors = None try: test_serializer.deserialize(test_obj, target) except ValidationException as e: errors = e.errors assert errors == {'e': {'_full_errors': ['Invalid']}}