def test_validate(self): type_field = TypeField('first_type', validate=True) type_field.set_value('second_type') self.assertRaises(SerializerFieldValueError, type_field.validate) try: type_field.validate() except SerializerFieldValueError as e: self.assertEqual(e.errors, 'Value is not first_type.')
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 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 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 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 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 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
class TestSerializer(Serializer): _type = TypeField('test_type') name = StringField(required=True) email = EmailField(required=True)
class TestSerializer(Serializer): _type = TypeField('test_object') street = StringField(required=True)
class LazyFieldSerializer(Serializer): _type = TypeField('test_object') id = IntegerField(required=True, identity=True) nest = SerializerField('LazyFieldNestSerializer', required=True)
def test_fixed_validate(self): type_field = TypeField('first_type', fixed=True, validate=True) type_field.set_value('second_type') type_field.validate() self.assertEqual(type_field.to_python(), 'first_type') self.assertEqual(type_field.to_native(), 'first_type')