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_deserialize_none(self): self.assertIsNone(models.deserialize(Vehicle, None))
def test_deserialize_with_map(self): data = [ { "_owner_id": 2, "created_at": None, "id": 1, "is_used": True, "paint": "red", "type": VehicleType.car, "name": "vehicle", "owner": { "id": 2, "first_name": "first_name", "last_name": "last_name" }, "options": [{ "id": 3, "name": "option 1" }, { "id": 4, "name": "option 2" }], "parts": [{ "id": 5, "name": "part 1" }, { "id": 6, "name": "part 2" }], }, { "_owner_id": 2, "created_at": None, "id": 7, "is_used": True, "paint": "red", "type": VehicleType.car, "name": "vehicle", # missing owner to test back population of many to ones "options": [{ "id": 3, "name": "option 1" }, { "id": 4, "name": "option 2" }], "parts": [{ "id": 5, "name": "part 1" }, { "id": 6, "name": "part 2" }], }, ] vehicle1, vehicle2 = models.deserialize(Vehicle, data) self.assertEqual(vehicle1.owner, vehicle2.owner)
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), )