mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
Added missing custom validation method test
This commit is contained in:
parent
02ae1682b5
commit
71aa5f3c45
|
@ -47,6 +47,12 @@ class ShouldValidateModel(models.Model):
|
|||
class ShouldValidateModelSerializer(serializers.ModelSerializer):
|
||||
renamed = serializers.CharField(source='should_validate_field', required=False)
|
||||
|
||||
def validate_renamed(self, attrs, source):
|
||||
value = attrs[source]
|
||||
if len(value) < 3:
|
||||
raise serializers.ValidationError('Minimum 3 characters.')
|
||||
return attrs
|
||||
|
||||
class Meta:
|
||||
model = ShouldValidateModel
|
||||
fields = ('renamed',)
|
||||
|
@ -63,6 +69,13 @@ class TestPreSaveValidationExclusions(TestCase):
|
|||
self.assertEqual(serializer.is_valid(), False)
|
||||
|
||||
|
||||
class TestCustomValidationMethods(TestCase):
|
||||
def test_custom_validation_method_is_executed(self):
|
||||
serializer = ShouldValidateModelSerializer(data={'renamed': 'fo'})
|
||||
self.assertFalse(serializer.is_valid())
|
||||
self.assertIn('renamed', serializer.errors)
|
||||
|
||||
|
||||
class ValidationSerializer(serializers.Serializer):
|
||||
foo = serializers.CharField()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user