def test_should_serialize_complex_object(self): person = ComplicatedPerson(name='John', age=51, salary=Salary(10000.50, 5000.75), address=Address(line1='foo', line2='bar', postal_code='123456')) expected_json = { "name": "John", "age": 51, "salary": { "basic": 10000.50, "allowance": 5000.75, }, "address": { "line1": "foo", "line2": "bar", "postal_code": "123456" }} json_string = pyobj_serializer.serialize(person) self.assertEqual(json.loads(json_string), expected_json)
def test_should_serialize_when_list_attrs_are_empty(self): person_list = PersonList([]) expected_json = { 'objects': [] } json_string = pyobj_serializer.serialize(person_list) self.assertEqual(json.loads(json_string), expected_json)
def test_should_serialize_object_with_lists_in_it(self): person_list = PersonList([SimplePerson('John'), SimplePerson('Doe')]) expected_json = { 'objects': [{'name': 'John'}, {'name': 'Doe'}] } json_string = pyobj_serializer.serialize(person_list) self.assertEqual(json.loads(json_string), expected_json)
def test_should_serialize_object_with_tuples_in_it(self): animal_list = AnimalList((Animal('cujo', 'Canis lupus'), Animal('jinxy', 'Canis lupus'))) expected_json = { 'objects': [{'name': 'cujo', 'species': 'Canis lupus'}, {'name': 'jinxy', 'species': 'Canis lupus'}] } json_string = pyobj_serializer.serialize(animal_list) self.assertEqual(json.loads(json_string), expected_json)
def test_should_serialize_to_dict_if_no_type_info_given_and_is_complex_type(self): generic_person = GenericPerson('John Doe', {'age': 10, 'nationality': 'unkown'}) expected_json = { 'name': 'John Doe', 'generic_attrs': { 'age': 10, 'nationality': 'unkown' } } json_string = pyobj_serializer.serialize(generic_person) self.assertEqual(json.loads(json_string), expected_json)
def test_should_serialize_object_with_simple_inheritance(self): dog = Dog(name='cujo', species='Canis lupus', breed=Breed('Labrador', 'Retriever')) expected_json = { 'name': 'cujo', 'species': 'Canis lupus', 'breed': { 'name': 'Labrador', 'type': 'Retriever' } } json_string = pyobj_serializer.serialize(dog) self.assertEqual(json.loads(json_string), expected_json)
def test_should_serialize_when_attrs_are_none(self): simple_person = SimplePerson(None) json_string = pyobj_serializer.serialize(simple_person) self.assertEqual(json.loads(json_string), {'name': None})
def test_should_serialize_simple_object(self): person = SimplePerson('John') json_string = pyobj_serializer.serialize(person) self.assertEqual(json_string, '{"name": "John"}')