def test_default(self): int_field = IntegerField(required=True, max_value=24, min_value=22, default=23) self.assertEqual(int_field.to_python(), 23) self.assertEqual(int_field.to_native(), 23)
def test_required(self): field = IntegerField(required=True) try: field.validate() except SerializerFieldValueError as e: self.assertEqual(repr(e), 'This field is required.') self.assertEqual(str(e), '[field]: This field is required.') else: self.fail('SerializerFieldValueError not raised.')
def test_max_min(self): int_field = IntegerField(required=True, max_value=24, min_value=22) int_field.set_value(23) int_field.validate() self.assertEqual(int_field.to_python(), 23) self.assertEqual(int_field.to_native(), 23) int_field = IntegerField(required=True, max_value=24, min_value=22) int_field.set_value(100) self.assertRaises(SerializerFieldValueError, int_field.validate)
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 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 LazyFieldNestSerializer(Serializer): name = StringField(required=True) id = IntegerField(required=True, identity=True)
class LazyFieldSerializer(Serializer): _type = TypeField('test_object') id = IntegerField(required=True, identity=True) nest = SerializerField('LazyFieldNestSerializer', required=True)
class NotRequired(Serializer): number = IntegerField(required=False) name = DecimalField(required=False)
class KnownTestSerialzer(Serializer): code = StringField(required=True, max_length=3) number = IntegerField(required=True) address = SerializerField(UnknownTestSerialzer, required=True)
def test_hide_on_null(self): int_field = IntegerField(required=False, on_null=HIDE_FIELD) self.assertRaises(IgnoreField, int_field.to_native) self.assertIsNone(int_field.to_python())
def test_validate_raises(self): int_field = IntegerField(required=True) int_field.set_value('int') self.assertRaises(SerializerFieldValueError, int_field.validate)
class TestSerializer(Serializer): uuid = UUIDField(required=True) name = StringField(required=True) foo_number = IntegerField(required=False)
def test_set_value_string(self): int_field = IntegerField(required=True) int_field.set_value('24') int_field.validate() self.assertEqual(int_field.to_python(), 24) self.assertEqual(int_field.to_native(), 24)
class TestSerializer(Serializer): name = StringField(required=True) number = IntegerField(required=True, max_value=10, min_value=5)
class NestSerializer(Serializer): name = StringField(required=True) id = IntegerField(required=True, identity=True) planet = StringField(required=True) os = StringField(required=True)
class KnownTestSerialzer(Serializer): code = StringField(required=True, max_length=3) number = IntegerField(required=True) address = SerializerField(NestKnownTestSerialzer, required=True, fields=['street'])