def test_serializing_polyfield_many(field): rect = Rectangle("blue", 4, 10) tri = Triangle("red", 1, 100) StickerCollection = namedtuple('StickerCollection', ['shapes', 'image']) marshmallow_sticker_collection = StickerCollection([rect, tri], "marshmallow.png") shapes = field.serialize('shapes', marshmallow_sticker_collection) expected_shapes = [{ "length": 4, "width": 10, "color": "blue" }, { "base": 1, "height": 100, "color": "red" }] assert shapes == expected_shapes
def test_deserialize_polyfield(self): original = self.ContrivedShapeClass( Rectangle('blue', 1, 100), [Rectangle('pink', 4, 93), Triangle('red', 8, 45)] ) data = self.ContrivedShapeClassSchema().load( {'main': {'color': 'blue', 'length': 1, 'width': 100}, 'others': [ {'color': 'pink', 'length': 4, 'width': 93}, {'color': 'red', 'base': 8, 'height': 45}]} ) assert data == original
def test_serializing_polyfield_many(): rect = Rectangle("blue", 4, 10) tri = Triangle("red", 1, 100) StickerCollection = namedtuple('StickerCollection', ['shapes', 'image']) marshmallow_sticker_collection = StickerCollection([rect, tri], "marshmallow.png") field = PolyField( serialization_schema_selector=shape_schema_serialization_disambiguation, deserialization_schema_selector= shape_schema_deserialization_disambiguation, many=True) shapes = field.serialize('shapes', marshmallow_sticker_collection) expected_shapes = [{ "length": 4, "width": 10, "color": "blue" }, { "base": 1, "height": 100, "color": "red" }] assert shapes == expected_shapes