mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-27 03:54:01 +03:00
Added test for custom fields with min_value and max_value validators
This commit is contained in:
parent
5d0aeef69e
commit
82c515c19c
|
@ -866,3 +866,33 @@ class FieldCallableDefault(TestCase):
|
||||||
into = {}
|
into = {}
|
||||||
field.field_from_native({}, {}, 'field', into)
|
field.field_from_native({}, {}, 'field', into)
|
||||||
self.assertEqual(into, {'field': 'foo bar'})
|
self.assertEqual(into, {'field': 'foo bar'})
|
||||||
|
|
||||||
|
|
||||||
|
class CustomIntegerField(TestCase):
|
||||||
|
"""
|
||||||
|
Test that custom fields apply min_value and max_value constraints
|
||||||
|
"""
|
||||||
|
def test_custom_fields_can_be_validated_for_value(self):
|
||||||
|
|
||||||
|
class MoneyField(models.PositiveIntegerField):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class EntryModel(models.Model):
|
||||||
|
bank = MoneyField(validators=[validators.MaxValueValidator(100)])
|
||||||
|
|
||||||
|
class EntrySerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = EntryModel
|
||||||
|
|
||||||
|
entry = EntryModel(bank=1)
|
||||||
|
|
||||||
|
serializer = EntrySerializer(entry, data={"bank": 11})
|
||||||
|
self.assertTrue(serializer.is_valid())
|
||||||
|
|
||||||
|
serializer = EntrySerializer(entry, data={"bank": -1})
|
||||||
|
self.assertFalse(serializer.is_valid())
|
||||||
|
|
||||||
|
serializer = EntrySerializer(entry, data={"bank": 101})
|
||||||
|
self.assertFalse(serializer.is_valid())
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user