Exemplo n.º 1
0
 def test_invalid(self):
     field = UUIDField(required=True)
     field.set_value('no_uuid')
     try:
         field.validate()
     except SerializerFieldValueError as e:
         self.assertEqual(repr(e), 'Invalid value.')
         self.assertEqual(str(e), '[field]: Invalid value.')
     else:
         self.fail('SerializerFieldValueError not raised.')
Exemplo n.º 2
0
    def test_default(self):
        field = UUIDField(required=True, default=uuid.UUID('8005ea5e-60b7-4b2a-ab41-a773b8b72e84'))
        field.validate()
        self.assertEqual(field.to_python(),  uuid.UUID('8005ea5e-60b7-4b2a-ab41-a773b8b72e84'))
        self.assertEqual(field.to_native(), '8005ea5e-60b7-4b2a-ab41-a773b8b72e84'.upper())

        field = UUIDField(required=True, default='8005ea5e-60b7-4b2a-ab41-a773b8b72e84')
        field.validate()
        self.assertEqual(field.to_python(),  uuid.UUID('8005ea5e-60b7-4b2a-ab41-a773b8b72e84'))
        self.assertEqual(field.to_native(), '8005ea5e-60b7-4b2a-ab41-a773b8b72e84'.upper())
Exemplo n.º 3
0
 def test_set_value_string(self):
     field = UUIDField(required=True)
     field.set_value('8005ea5e-60b7-4b2a-ab41-a773b8b72e84'.upper())
     field.validate()
     self.assertIsInstance(field.to_python(), uuid.UUID)
     self.assertEqual(field.to_python(),
                      uuid.UUID('8005ea5e-60b7-4b2a-ab41-a773b8b72e84'))
     self.assertEqual(field.to_native(),
                      '8005ea5e-60b7-4b2a-ab41-a773b8b72e84'.upper())
Exemplo n.º 4
0
 def test_no_binary(self):
     field = UUIDField(required=True, binary=False)
     field.set_value('8005ea5e-60b7-4b2a-ab41-a773b8b72e84'.upper())
     field.validate()
     self.assertIsInstance(field.to_python(), py2to3.string)
     self.assertEqual(field.to_python(),
                      '8005ea5e-60b7-4b2a-ab41-a773b8b72e84')
     self.assertEqual(field.to_native(),
                      '8005ea5e-60b7-4b2a-ab41-a773b8b72e84'.upper())
Exemplo n.º 5
0
 def test_no_binary(self):
     field = UUIDField(required=True, binary=False)
     field.set_value('8005ea5e-60b7-4b2a-ab41-a773b8b72e84'.upper())
     field.validate()
     self.assertIsInstance(field.to_python(), py2to3.string)
     self.assertEqual(field.to_python(), '8005ea5e-60b7-4b2a-ab41-a773b8b72e84')
     self.assertEqual(field.to_native(), '8005ea5e-60b7-4b2a-ab41-a773b8b72e84'.upper())
Exemplo n.º 6
0
 def test_set_value_string(self):
     field = UUIDField(required=True)
     field.set_value('8005ea5e-60b7-4b2a-ab41-a773b8b72e84'.upper())
     field.validate()
     self.assertIsInstance(field.to_python(), uuid.UUID)
     self.assertEqual(field.to_python(),  uuid.UUID('8005ea5e-60b7-4b2a-ab41-a773b8b72e84'))
     self.assertEqual(field.to_native(), '8005ea5e-60b7-4b2a-ab41-a773b8b72e84'.upper())
Exemplo n.º 7
0
    def test_required(self):
        field = UUIDField(required=True)
        self.assertRaises(SerializerFieldValueError, field.validate)

        field = UUIDField(required=False)
        field.validate()
        self.assertIsNone(field.to_python())
        self.assertEqual(field.to_native(), '')
Exemplo n.º 8
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)
Exemplo n.º 9
0
    def test_required(self):
        field = UUIDField(required=True)
        self.assertRaises(SerializerFieldValueError, field.validate)

        field = UUIDField(required=False)
        field.validate()
        self.assertIsNone(field.to_python())
        self.assertEqual(field.to_native(), '')
Exemplo n.º 10
0
 def test_invalid(self):
     field = UUIDField(required=True)
     field.set_value('no_uuid')
     try:
         field.validate()
     except SerializerFieldValueError as e:
         self.assertEqual(repr(e), 'Invalid value.')
         self.assertEqual(str(e), '[field]: Invalid value.')
     else:
         self.fail('SerializerFieldValueError not raised.')
Exemplo n.º 11
0
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.')
Exemplo n.º 12
0
 class ListItemSerialzer(Serializer):
     code = StringField(required=True, max_length=3)
     uuid = UUIDField(required=True)
Exemplo n.º 13
0
 def test_set_value(self):
     field = UUIDField(required=True)
     field.set_value(uuid.uuid4())
     field.validate()
     self.assertIsInstance(field.to_python(), uuid.UUID)
Exemplo n.º 14
0
 def test_hide_on_null(self):
     field = UUIDField(required=False, on_null=HIDE_FIELD)
     self.assertRaises(IgnoreField, field.to_native)
     self.assertIsNone(field.to_python())
Exemplo n.º 15
0
 def test_validate_raises(self):
     field = UUIDField(required=True)
     field.set_value('nono')
     self.assertRaises(SerializerFieldValueError, field.validate)
Exemplo n.º 16
0
 class TestSerializer(Serializer):
     uuid = UUIDField(required=True)
     name = StringField(required=True)
     foo_number = IntegerField(required=False)
Exemplo n.º 17
0
 def test_hide_on_null(self):
     field = UUIDField(required=False, on_null=HIDE_FIELD)
     self.assertRaises(IgnoreField, field.to_native)
     self.assertIsNone(field.to_python())
Exemplo n.º 18
0
    def test_default(self):
        field = UUIDField(
            required=True,
            default=uuid.UUID('8005ea5e-60b7-4b2a-ab41-a773b8b72e84'))
        field.validate()
        self.assertEqual(field.to_python(),
                         uuid.UUID('8005ea5e-60b7-4b2a-ab41-a773b8b72e84'))
        self.assertEqual(field.to_native(),
                         '8005ea5e-60b7-4b2a-ab41-a773b8b72e84'.upper())

        field = UUIDField(required=True,
                          default='8005ea5e-60b7-4b2a-ab41-a773b8b72e84')
        field.validate()
        self.assertEqual(field.to_python(),
                         uuid.UUID('8005ea5e-60b7-4b2a-ab41-a773b8b72e84'))
        self.assertEqual(field.to_native(),
                         '8005ea5e-60b7-4b2a-ab41-a773b8b72e84'.upper())
Exemplo n.º 19
0
 def test_set_value(self):
     field = UUIDField(required=True)
     field.set_value(uuid.uuid4())
     field.validate()
     self.assertIsInstance(field.to_python(), uuid.UUID)
Exemplo n.º 20
0
 def test_validate_raises(self):
     field = UUIDField(required=True)
     field.set_value('nono')
     self.assertRaises(SerializerFieldValueError, field.validate)