def test_deserialize_polyfield(self, schema): original = self.ContrivedShapeClass( Rectangle('blue', 1, 100), [Rectangle('pink', 4, 93), Rectangle('red', 3, 90)], 'rectangle') data = schema().load({ 'main': { 'color': 'blue', 'length': 1, 'width': 100 }, 'others': [{ 'color': 'pink', 'length': 4, 'width': 93 }, { 'color': 'red', 'length': 3, 'width': 90 }], 'type': 'rectangle' }) assert data == original
def test_serializing_polyfield_by_parent_type(field): rect = Rectangle("blue", 4, 10) Sticker = namedtuple('Sticker', ['shape', 'image', 'type']) marshmallow_sticker = Sticker(rect, "marshmallow.png", 'rectangle') rect_dict = field.serialize('shape', marshmallow_sticker) assert rect_dict == {"length": 4, "width": 10, "color": "blue"}
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_by_parent_type(): rect = Rectangle("blue", 4, 10) Sticker = namedtuple('Sticker', ['shape', 'image', 'type']) marshmallow_sticker = Sticker(rect, "marshmallow.png", 'rectangle') field = PolyField(serialization_schema_selector= shape_property_schema_serialization_disambiguation, deserialization_schema_selector= shape_property_schema_deserialization_disambiguation) rect_dict = field.serialize('shape', marshmallow_sticker) assert rect_dict == {"length": 4, "width": 10, "color": "blue"}
def test_deserialize_polyfield_none(self, schema): original = self.ContrivedShapeClass(Rectangle("blue", 1, 100), None) data = schema().load({ 'main': { 'color': 'blue', 'length': 1, 'width': 100 }, 'others': None }) assert data == original
def test_deserialize_polyfield(self): original = self.ContrivedShapeClass(Rectangle('blue', 1, 100), [Rectangle('pink', 4, 93)], 'rectangle') data, errors = self.ContrivedShapeClassSchema(strict=True).load({ 'main': { 'color': 'blue', 'length': 1, 'width': 100 }, 'others': [{ 'color': 'pink', 'length': 4, 'width': 93 }], 'type': 'rectangle' }) assert not errors assert data == original
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_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