1
0
mirror of https://github.com/django/django.git synced 2025-10-27 07:36:08 +00:00

[soc2009/model-validation] Make sure that all validators in the same group (simple/complex) get run even if they all fail and we get all their messages.

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11185 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Honza Král
2009-07-05 13:26:57 +00:00
parent 8424c8ae6c
commit 63f244f144
3 changed files with 39 additions and 3 deletions

View File

@@ -0,0 +1,34 @@
from unittest import TestCase
from django import forms
from django.core import validators
from django.core.exceptions import ValidationError
class AlwaysFailingValidator(validators.ComplexValidator):
def __call__(self, value, all_values={}, obj=None):
raise ValidationError('AlwaysFailingValidator')
class TestFieldWithValidators(TestCase):
def test_all_errors_get_reported(self):
field = forms.CharField(
validators=[validators.validate_integer, validators.validate_email,]
)
self.assertRaises(ValidationError, field.clean, 'not int nor mail')
try:
field.clean('not int nor mail')
except ValidationError, e:
self.assertEqual(2, len(e.messages))
class TestFormWithValidators(TestCase):
def test_all_complex_validators_get_run_even_if_they_fail(self):
class MyForm(forms.Form):
validator_field = forms.CharField(
validators=[
AlwaysFailingValidator(),
AlwaysFailingValidator(),
]
)
form = MyForm({'validator_field': 'some value'})
self.assertFalse(form.is_valid())
self.assertEqual(['validator_field'], form.errors.keys())
self.assertEqual(['AlwaysFailingValidator', 'AlwaysFailingValidator'], form.errors['validator_field'])