Added missing custom validation method test

This commit is contained in:
Lukasz Balcerzak 2013-12-20 17:16:24 +01:00
parent 02ae1682b5
commit 71aa5f3c45

View File

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