def test_serialize_with_composites(self): business = Business( name="test", location=Address(street="street 1", state="state 1", zip="zip 1"), other_location=Address(street="street 2", state="state 2", zip="zip 2"), ) self.assertDictEqual( { "id": None, "name": "test", "employees": 5, "location": { "state": "state 1", "street": "street 1", "zip": "zip 1" }, "other_location": { "state": "state 2", "street": "street 2", "zip": "zip 2" }, }, models.serialize(business), )
def test_serialize_with_relations(self): vehicle = Vehicle( name="vehicle", owner=Owner(first_name="first_name", last_name="last_name"), is_used=True, paint="red", type=VehicleType.car, options=[Option(name="option 1"), Option(name="option 2")], parts=[Part(name="part 1"), Part(name="part 2")], ) self.assertDictEqual( { "_owner_id": None, "created_at": None, "id": None, "is_used": True, "paint": "red", "type": VehicleType.car, "name": "vehicle", "owner": {"id": None, "first_name": "first_name", "last_name": "last_name"}, "options": [{"id": None, "name": "option 1"}, {"id": None, "name": "option 2"}], "parts": [{"id": None, "name": "part 1"}, {"id": None, "name": "part 2"}], }, models.serialize(vehicle, Vehicle.owner, Vehicle.options, Vehicle.parts), )
def test_shallow_serialize(self): vehicle = Vehicle(owner=Owner(first_name="first_name", last_name="last_name"), type=VehicleType.car) self.assertDictEqual( { "_owner_id": None, "created_at": None, "id": None, "is_used": None, "name": None, "paint": None, "type": VehicleType.car, }, models.serialize(vehicle), )
def test_deserialize_composites(self): data = { "id": None, "name": "test", "location": { "state": "state 1", "street": "street 1", "zip": "zip 1" }, "other_location": { "state": "state 2", "street": "street 2", "zip": "zip 2" }, } business = models.deserialize(Business, data) self.assertDictEqual( { "id": None, "name": "test", "employees": 5, "location": { "state": "state 1", "street": "street 1", "zip": "zip 1" }, "other_location": { "state": "state 2", "street": "street 2", "zip": "zip 2" }, }, models.serialize(business), )
def test_serialize_none(self): self.assertIsNone(models.serialize(None))
def test_deserialize(self): data = { "_owner_id": None, "created_at": None, "id": 1, "is_used": True, "name": "vehicle", "options": [{ "id": 3, "name": "option 1" }, { "id": 4, "name": "option 2" }], "owner": { "id": 2, "first_name": "first_name", "last_name": "last_name" }, "paint": "red", "parts": [{ "id": 5, "name": "part 1" }, { "id": 6, "name": "part 2" }], "type": VehicleType.car, } vehicle = models.deserialize(Vehicle, data) self.assertDictEqual( { "_owner_id": None, "created_at": None, "id": 1, "is_used": True, "msrp": None, "name": "vehicle", "options": [{ "id": 3, "name": "option 1" }, { "id": 4, "name": "option 2" }], "owner": { "id": 2, "first_name": "first_name", "last_name": "last_name" }, "paint": "red", "parts": [{ "id": 5, "name": "part 1" }, { "id": 6, "name": "part 2" }], "type": VehicleType.car, }, models.serialize(vehicle, Vehicle.owner, Vehicle.options, Vehicle.parts), )