def test_default(self): field = UrlField(required=True, default='https://www.onyg.de') field.validate() self.assertEqual(field.to_python(), 'https://www.onyg.de') self.assertEqual(field.to_native(), 'https://www.onyg.de') field = UrlField(required=True, default='no url') self.assertRaises(SerializerFieldValueError, field.validate)
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)
def test_hide_on_null(self): field = UrlField(required=False, on_null=HIDE_FIELD) self.assertRaises(IgnoreField, field.to_native) self.assertIsNone(field.to_python())
def test_validate_raises(self): field = UrlField(required=True) field.set_value('api') self.assertRaises(SerializerFieldValueError, field.validate)
def test_base(self): field = UrlField(required=True, base='http://www.onyg.de') field.set_value('api') field.validate() self.assertEqual(field.to_python(), 'http://www.onyg.de/api') self.assertEqual(field.to_native(), 'http://www.onyg.de/api')