class TestFlatSerializer(Serializer): _type = TypeField('test_object') id = IntegerField(required=True, identity=True) name = StringField(required=True) street = StringField(required=False, on_null=HIDE_FIELD) uuid_var = UUIDField(required=True) maxmin = IntegerField(max_value=10, min_value=6, required=True) datetime_var = DatetimeField(required=True) date_var = DateField(required=True) time_var = TimeField(required=True) haus = StringField(required=True, map_field='house') url = UrlField(required=True, base='http://www.base.com', default='api') action = StringField(required=False, action_field=True)
class RecursionRefSerializerTwo(Serializer): name = StringField() ref_one = SerializerField('RecursionRefSerializerOne', exclude=['sers'], required=True) ref_two = SerializerField('RecursionRefSerializerOne', exclude=['sers'], required=True)
class TestSerializerOne(Serializer): _type = TypeField('test_object_one') street = StringField(required=True) def street_clean_value(self, value): if py2to3._unicode(value).startswith('sesame'): return 'Street for kids' else: return value
class CustomValidationSerializer(Serializer): code = StringField(required=True, max_length=3) name = StringField(required=True, map_field='foo') pk = UUIDField(required=False) def code_validate(self, value): if value == 'ABC': raise SerializerFieldValueError(message='ABC is not allowed.') def foo_validate(self, value): if value == 'FOOBAR': raise SerializerFieldValueError(message='FOOBAR is not allowed.') def pk_validate(self, value): if not isinstance(value, uuid.UUID): raise SerializerFieldValueError('NEVER RAISE') elif str(value).upper() == 'BD0623E8-27A6-4A94-9B57-2A6D833D55F7': raise SerializerFieldValueError( message='BD0623E8-27A6-4A94-9B57-2A6D833D55F7 is not allowed.')
class TestNestSerializer(Serializer): class NestSerializer(Serializer): name = StringField(required=True) id = IntegerField(required=True, identity=True) planet = StringField(required=True) os = StringField(required=True) _type = TypeField('test_object') id = IntegerField(required=True, identity=True) name = StringField(required=True) nest = SerializerField(NestSerializer, fields=['name'], required=True)
class TestSerializerTwo(Serializer): _type = TypeField('test_object_two') street = StringField(required=True) def street_to_python(self, field): value = field.to_python() return 'Changed for python: {}'.format(value) def street_to_native(self, field): value = field.to_native() return 'Changed for native: {}'.format(value)
class MySerializer(Serializer): class MyNestSerializer(Serializer): name = StringField(required=True) id = IntegerField(required=True, identity=True) _type = TypeField('test_object') id = IntegerField(required=True, identity=True) name = StringField(required=True) date_var = DateField(required=True, map_field='dt') time_var = TimeField(required=True, map_field='t') url = UrlField(required=True, base='http://www.base.com', default='api') nest = SerializerField(MyNestSerializer, required=True)
class TestSerializerThree(Serializer): _type = TypeField('test_object_three') street = StringField(required=True) def street_to_python(self, field): value = field.to_python() if value.startswith('ignore'): raise IgnoreField() return value def street_to_native(self, field): value = field.to_native() if value.startswith('ignore'): raise IgnoreField() return value
def test_max_min_length(self): field = StringField(required=True, max_length=7, min_length=4) field.set_value('foobar') field.validate() self.assertEqual(field.to_python(), 'foobar') self.assertEqual(field.to_native(), 'foobar') field = StringField(required=True, max_length=7, min_length=4) field.set_value('foo') self.assertRaises(SerializerFieldValueError, field.validate) field = StringField(required=True, max_length=7, min_length=4) field.set_value('foobarfoobar') self.assertRaises(SerializerFieldValueError, field.validate)
class TestSerializer(Serializer): _type = TypeField('test_type') name = StringField(required=True) email = EmailField(required=True)
def test_default(self): field = StringField(required=True, default='a string') field.validate() self.assertEqual(field.to_python(), 'a string') self.assertEqual(field.to_native(), 'a string')
class KnownTestSerialzer(Serializer): code = StringField(required=True, max_length=3) number = IntegerField(required=True) address = SerializerField(NestKnownTestSerialzer, required=True, fields=['street'])
class KnownTestSerialzer(Serializer): code = StringField(required=True, max_length=3) number = IntegerField(required=True) address = SerializerField(UnknownTestSerialzer, required=True)
class ListItemSerialzer(Serializer): code = StringField(required=True, max_length=3) uuid = UUIDField(required=True)
def test_set_value(self): field = StringField(required=True) field.set_value('string') field.validate() self.assertEqual(field.to_python(), 'string') self.assertEqual(field.to_native(), 'string')
class SE(Serializer): name = StringField() def the_method(self, field): return 'test {}'.format(field.to_python())
class MetaTestSerializer(Serializer): name = StringField() last_name = StringField() street = StringField() city = StringField() country = StringField()
class NotRequiredSerializerField(Serializer): name = StringField() nest = SerializerField(NotRequiredNest, required=False)
class NotRequiredNest(Serializer): name = StringField(required=True) foo = StringField(required=False)
class TestSerializer(Serializer): uuid = UUIDField(required=True) name = StringField(required=True) foo_number = IntegerField(required=False)
def test_hide_on_null(self): field = StringField(required=False, on_null=HIDE_FIELD) self.assertRaises(IgnoreField, field.to_native) self.assertIsNone(field.to_python())
class NestSerializer(Serializer): name = StringField(required=True) id = IntegerField(required=True, identity=True) planet = StringField(required=True) os = StringField(required=True)
def test_validate_raises(self): field = StringField(required=True) field.set_value(12) self.assertRaises(SerializerFieldValueError, field.validate)
class RecursionRefSerializerOne(Serializer): name = StringField() sers = ListSerializerField('RecursionRefSerializerTwo', exclude=['ser'], required=False)
class LazyFieldNestSerializer(Serializer): name = StringField(required=True) id = IntegerField(required=True, identity=True)
class UnknownTestSerialzer(Serializer): street = StringField(required=True)
class TestSerializer(Serializer): _type = TypeField('test_object') street = StringField(required=True)
class NestKnownTestSerialzer(Serializer): street = StringField(required=True) city = StringField(required=True)
class TestSerializer(Serializer): name = StringField(required=True) number = IntegerField(required=True, max_value=10, min_value=5)