def test_serialize_nonnullable(self): class Model(psm.SchemaModel): field = psm.FloatField(nullable=False) m1 = Model(field=None) with self.assertRaises(psm.ValidationError): psm.serialize(m1) m2 = Model(field=12.12) m2_str = psm.serialize(m2) self.assertEqual('{"field": 12.12}', m2_str)
def test_serialize_nullable_bool(self): class Model(psm.SchemaModel): field = psm.BoolField(nullable=True) m1 = Model(field=None) m1_str = psm.serialize(m1) self.assertEqual('{"field": null}', m1_str) m2 = Model(field=False) m2_str = psm.serialize(m2) self.assertEqual('{"field": false}', m2_str) m3 = Model(field=True) m3_str = psm.serialize(m3) self.assertEqual('{"field": true}', m3_str)
def test_serialize_nullable(self): class Model(psm.SchemaModel): field = psm.FloatField(nullable=True) m1 = Model() m1_str = psm.serialize(m1) self.assertEqual('{}', m1_str) m2 = Model(field=None) m2_str = psm.serialize(m2) self.assertEqual('{"field": null}', m2_str) m3 = Model(field=12.12) m3_str = psm.serialize(m3) self.assertEqual('{"field": 12.12}', m3_str)
def test_serialize_nonnullable_string(self): class Model(psm.SchemaModel): field = psm.StringField(nullable=False) m1 = Model(field=None) with self.assertRaises(psm.ValidationError): psm.serialize(m1) m2 = Model(field='str') m2_str = psm.serialize(m2) self.assertEqual('{"field": "str"}', m2_str) m3 = Model(field='') m3_str = psm.serialize(m3) self.assertEqual('{"field": ""}', m3_str)
def test_serialize_nullable_string(self): class Model(psm.SchemaModel): field = psm.StringField(nullable=True) m1 = Model(field=None) m1_str = psm.serialize(m1) self.assertEqual('{"field": null}', m1_str) m2 = Model(field='str') m2_str = psm.serialize(m2) self.assertEqual('{"field": "str"}', m2_str) m3 = Model(field='') m3_str = psm.serialize(m3) self.assertEqual('{"field": ""}', m3_str)
def test_serialize_bool(self): class Model(psm.SchemaModel): field = psm.BoolField(nullable=False) m1 = Model(field=None) with self.assertRaises(psm.ValidationError): psm.serialize(m1) m2 = Model(field=False) m2_str = psm.serialize(m2) self.assertEqual('{"field": false}', m2_str) m3 = Model(field=True) m3_str = psm.serialize(m3) self.assertEqual('{"field": true}', m3_str)
def test_serialize_list_of_lists(self): class Transformation(psm.SchemaModel): m = psm.ListField([psm.ListField([psm.IntegerField()])]) m1 = Transformation(m=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]) m1_str = psm.serialize(m1) self.assertEqual('{"m": [[1, 0, 0], [0, 1, 0], [0, 0, 1]]}', m1_str)
def test_serialize_list_of_integers(self): class Model(psm.SchemaModel): indices = psm.ListField([psm.IntegerField()]) m1 = Model(indices=[1, 8, 9, 101]) m1_str = psm.serialize(m1) self.assertEqual('{"indices": [1, 8, 9, 101]}', m1_str)
def test_serialize_obj_of_objs(self): class SubModel(psm.SchemaModel): field = psm.StringField() class Model(psm.SchemaModel): obj_field = psm.ObjectField(SubModel) m1 = Model(obj_field=SubModel(field='greetings')) m1_str = psm.serialize(m1) self.assertEqual('{"obj_field": {"field": "greetings"}}', m1_str)
def test_serialize_list_of_objects_with_list(self): class Vector(psm.SchemaModel): u = psm.ListField([psm.IntegerField()], min_length=3, max_length=3) class Model(psm.SchemaModel): vectors = psm.ListField([psm.ObjectField(Vector)]) m1 = Model(vectors=[ Vector(u=[1, 0, 0]), Vector(u=[0, 1, 0]), Vector(u=[0, 0, 1]) ]) m1_str = psm.serialize(m1) self.assertEqual( '{"vectors": [{"u": [1, 0, 0]}, {"u": [0, 1, 0]}, {"u": [0, 0, 1]}]}', m1_str)
def test_serialize_list_of_objects(self): class Vector(psm.SchemaModel): x = psm.IntegerField(required=True, nullable=False) y = psm.IntegerField(required=True, nullable=False) z = psm.IntegerField(required=True, nullable=False) class Model(psm.SchemaModel): vectors = psm.ListField([psm.ObjectField(Vector)]) m1 = Model(vectors=[ Vector(x=1, y=0, z=0), Vector(x=0, y=1, z=0), Vector(x=0, y=0, z=1) ]) m1_str = psm.serialize(m1) self.assertEqual( '{"vectors": [{"x": 1, "y": 0, "z": 0}, {"x": 0, "y": 1, "z": 0}, {"x": 0, "y": 0, "z": 1}]}', m1_str)
def test_serialize_mixed_list(self): class SubModel(psm.SchemaModel): data = psm.ListField([psm.BoolField()]) class Model(psm.SchemaModel): bag = psm.ListField([ psm.IntegerField(), psm.ListField([psm.ObjectField(SubModel)]) ]) m1 = Model(bag=[ 1, [ SubModel(data=[True, False]), SubModel(data=[True, True, False, False]) ] ]) m1_str = psm.serialize(m1) self.assertEqual( '{"bag": [1, [{"data": [true, false]}, {"data": [true, true, false, false]}]]}', m1_str)