Esempio n. 1
0
 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.')
Esempio n. 2
0
    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
Esempio n. 3
0
    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)
Esempio n. 4
0
    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)
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
 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.')
Esempio n. 8
0
    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
Esempio n. 9
0
 class TestSerializer(Serializer):
     _type = TypeField('test_type')
     name = StringField(required=True)
     email = EmailField(required=True)
Esempio n. 10
0
 class TestSerializer(Serializer):
     _type = TypeField('test_object')
     street = StringField(required=True)
Esempio n. 11
0
 class LazyFieldSerializer(Serializer):
     _type = TypeField('test_object')
     id = IntegerField(required=True, identity=True)
     nest = SerializerField('LazyFieldNestSerializer', required=True)
Esempio n. 12
0
 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')
Esempio n. 13
0
 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')