Added test for custom fields with min_value and max_value validators

This commit is contained in:
Mark Hughes 2013-06-12 17:36:16 +01:00
parent 5d0aeef69e
commit 82c515c19c

View File

@ -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())