diff options
| author | Joseph Kocherhans <joseph@jkocherhans.com> | 2010-01-05 03:56:19 +0000 |
|---|---|---|
| committer | Joseph Kocherhans <joseph@jkocherhans.com> | 2010-01-05 03:56:19 +0000 |
| commit | 471596fc1afcb9c6258d317c619eaf5fd394e797 (patch) | |
| tree | 193767161be3cc23dc2e6be5e4f16d8fd21a2925 /tests/regressiontests/forms | |
| parent | 4e89105d64bb9e04c409139a41e9c7aac263df4c (diff) | |
Merged soc2009/model-validation to trunk. Thanks, Honza!
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12098 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/forms')
| -rw-r--r-- | tests/regressiontests/forms/error_messages.py | 24 | ||||
| -rw-r--r-- | tests/regressiontests/forms/fields.py | 6 | ||||
| -rw-r--r-- | tests/regressiontests/forms/localflavor/ar.py | 8 | ||||
| -rw-r--r-- | tests/regressiontests/forms/localflavor/is_.py | 10 | ||||
| -rw-r--r-- | tests/regressiontests/forms/tests.py | 1 | ||||
| -rw-r--r-- | tests/regressiontests/forms/util.py | 17 | ||||
| -rw-r--r-- | tests/regressiontests/forms/validators.py | 17 |
7 files changed, 53 insertions, 30 deletions
diff --git a/tests/regressiontests/forms/error_messages.py b/tests/regressiontests/forms/error_messages.py index b7224dbde0..038fa39f6b 100644 --- a/tests/regressiontests/forms/error_messages.py +++ b/tests/regressiontests/forms/error_messages.py @@ -6,8 +6,8 @@ tests = r""" # CharField ################################################################### >>> e = {'required': 'REQUIRED'} ->>> e['min_length'] = 'LENGTH %(length)s, MIN LENGTH %(min)s' ->>> e['max_length'] = 'LENGTH %(length)s, MAX LENGTH %(max)s' +>>> e['min_length'] = 'LENGTH %(show_value)s, MIN LENGTH %(limit_value)s' +>>> e['max_length'] = 'LENGTH %(show_value)s, MAX LENGTH %(limit_value)s' >>> f = CharField(min_length=5, max_length=10, error_messages=e) >>> f.clean('') Traceback (most recent call last): @@ -26,8 +26,8 @@ ValidationError: [u'LENGTH 11, MAX LENGTH 10'] >>> e = {'required': 'REQUIRED'} >>> e['invalid'] = 'INVALID' ->>> e['min_value'] = 'MIN VALUE IS %s' ->>> e['max_value'] = 'MAX VALUE IS %s' +>>> e['min_value'] = 'MIN VALUE IS %(limit_value)s' +>>> e['max_value'] = 'MAX VALUE IS %(limit_value)s' >>> f = IntegerField(min_value=5, max_value=10, error_messages=e) >>> f.clean('') Traceback (most recent call last): @@ -50,8 +50,8 @@ ValidationError: [u'MAX VALUE IS 10'] >>> e = {'required': 'REQUIRED'} >>> e['invalid'] = 'INVALID' ->>> e['min_value'] = 'MIN VALUE IS %s' ->>> e['max_value'] = 'MAX VALUE IS %s' +>>> e['min_value'] = 'MIN VALUE IS %(limit_value)s' +>>> e['max_value'] = 'MAX VALUE IS %(limit_value)s' >>> f = FloatField(min_value=5, max_value=10, error_messages=e) >>> f.clean('') Traceback (most recent call last): @@ -74,8 +74,8 @@ ValidationError: [u'MAX VALUE IS 10'] >>> e = {'required': 'REQUIRED'} >>> e['invalid'] = 'INVALID' ->>> e['min_value'] = 'MIN VALUE IS %s' ->>> e['max_value'] = 'MAX VALUE IS %s' +>>> e['min_value'] = 'MIN VALUE IS %(limit_value)s' +>>> e['max_value'] = 'MAX VALUE IS %(limit_value)s' >>> e['max_digits'] = 'MAX DIGITS IS %s' >>> e['max_decimal_places'] = 'MAX DP IS %s' >>> e['max_whole_digits'] = 'MAX DIGITS BEFORE DP IS %s' @@ -156,8 +156,8 @@ ValidationError: [u'INVALID'] >>> e = {'required': 'REQUIRED'} >>> e['invalid'] = 'INVALID' ->>> e['min_length'] = 'LENGTH %(length)s, MIN LENGTH %(min)s' ->>> e['max_length'] = 'LENGTH %(length)s, MAX LENGTH %(max)s' +>>> e['min_length'] = 'LENGTH %(show_value)s, MIN LENGTH %(limit_value)s' +>>> e['max_length'] = 'LENGTH %(show_value)s, MAX LENGTH %(limit_value)s' >>> f = RegexField(r'^\d+$', min_length=5, max_length=10, error_messages=e) >>> f.clean('') Traceback (most recent call last): @@ -180,8 +180,8 @@ ValidationError: [u'LENGTH 11, MAX LENGTH 10'] >>> e = {'required': 'REQUIRED'} >>> e['invalid'] = 'INVALID' ->>> e['min_length'] = 'LENGTH %(length)s, MIN LENGTH %(min)s' ->>> e['max_length'] = 'LENGTH %(length)s, MAX LENGTH %(max)s' +>>> e['min_length'] = 'LENGTH %(show_value)s, MIN LENGTH %(limit_value)s' +>>> e['max_length'] = 'LENGTH %(show_value)s, MAX LENGTH %(limit_value)s' >>> f = EmailField(min_length=8, max_length=10, error_messages=e) >>> f.clean('') Traceback (most recent call last): diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py index c9736d38e1..87330904df 100644 --- a/tests/regressiontests/forms/fields.py +++ b/tests/regressiontests/forms/fields.py @@ -386,7 +386,7 @@ class FieldsTests(TestCase): def test_regexfield_31(self): f = RegexField('^\d+$', min_length=5, max_length=10) self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).']", f.clean, '123') - self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).']", f.clean, 'abc') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).', u'Enter a valid value.']", f.clean, 'abc') self.assertEqual(u'12345', f.clean('12345')) self.assertEqual(u'1234567890', f.clean('1234567890')) self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '12345678901') @@ -548,6 +548,10 @@ class FieldsTests(TestCase): self.assertEqual(u'http://example.com/', f.clean('http://example.com')) self.assertEqual(u'http://example.com/test', f.clean('http://example.com/test')) + def test_urlfield_ticket11826(self): + f = URLField() + self.assertEqual(u'http://example.com/?some_param=some_value', f.clean('http://example.com?some_param=some_value')) + # BooleanField ################################################################ def test_booleanfield_44(self): diff --git a/tests/regressiontests/forms/localflavor/ar.py b/tests/regressiontests/forms/localflavor/ar.py index e1c827c4a0..c7967c84da 100644 --- a/tests/regressiontests/forms/localflavor/ar.py +++ b/tests/regressiontests/forms/localflavor/ar.py @@ -28,7 +28,7 @@ u'C1064AAB' >>> f.clean('C1064AABB') Traceback (most recent call last): ... -ValidationError: [u'Ensure this value has at most 8 characters (it has 9).'] +ValidationError: [u'Ensure this value has at most 8 characters (it has 9).', u'Enter a postal code in the format NNNN or ANNNNAAA.'] >>> f.clean('C1064AA') Traceback (most recent call last): ... @@ -44,7 +44,7 @@ ValidationError: [u'Enter a postal code in the format NNNN or ANNNNAAA.'] >>> f.clean('500') Traceback (most recent call last): ... -ValidationError: [u'Ensure this value has at least 4 characters (it has 3).'] +ValidationError: [u'Ensure this value has at least 4 characters (it has 3).', u'Enter a postal code in the format NNNN or ANNNNAAA.'] >>> f.clean('5PPP') Traceback (most recent call last): ... @@ -78,7 +78,7 @@ u'C1064AAB' >>> f.clean('C1064AABB') Traceback (most recent call last): ... -ValidationError: [u'Ensure this value has at most 8 characters (it has 9).'] +ValidationError: [u'Ensure this value has at most 8 characters (it has 9).', u'Enter a postal code in the format NNNN or ANNNNAAA.'] >>> f.clean('C1064AA') Traceback (most recent call last): ... @@ -94,7 +94,7 @@ ValidationError: [u'Enter a postal code in the format NNNN or ANNNNAAA.'] >>> f.clean('500') Traceback (most recent call last): ... -ValidationError: [u'Ensure this value has at least 4 characters (it has 3).'] +ValidationError: [u'Ensure this value has at least 4 characters (it has 3).', u'Enter a postal code in the format NNNN or ANNNNAAA.'] >>> f.clean('5PPP') Traceback (most recent call last): ... diff --git a/tests/regressiontests/forms/localflavor/is_.py b/tests/regressiontests/forms/localflavor/is_.py index 6851441a79..e71c2dd8de 100644 --- a/tests/regressiontests/forms/localflavor/is_.py +++ b/tests/regressiontests/forms/localflavor/is_.py @@ -15,11 +15,11 @@ u'230880-3449' >>> f.clean('230880343') Traceback (most recent call last): ... -ValidationError: [u'Ensure this value has at least 10 characters (it has 9).'] +ValidationError: [u'Ensure this value has at least 10 characters (it has 9).', u'Enter a valid Icelandic identification number. The format is XXXXXX-XXXX.'] >>> f.clean('230880343234') Traceback (most recent call last): ... -ValidationError: [u'Ensure this value has at most 11 characters (it has 12).'] +ValidationError: [u'Ensure this value has at most 11 characters (it has 12).', u'Enter a valid Icelandic identification number. The format is XXXXXX-XXXX.'] >>> f.clean('abcdefghijk') Traceback (most recent call last): ... @@ -61,18 +61,18 @@ ValidationError: [u'Enter a valid value.'] >>> f.clean('123456') Traceback (most recent call last): ... -ValidationError: [u'Ensure this value has at least 7 characters (it has 6).'] +ValidationError: [u'Ensure this value has at least 7 characters (it has 6).', u'Enter a valid value.'] >>> f.clean('123456555') Traceback (most recent call last): ... -ValidationError: [u'Ensure this value has at most 8 characters (it has 9).'] +ValidationError: [u'Ensure this value has at most 8 characters (it has 9).', u'Enter a valid value.'] >>> f.clean('abcdefg') Traceback (most recent call last): ValidationError: [u'Enter a valid value.'] >>> f.clean(' 1234567 ') Traceback (most recent call last): ... -ValidationError: [u'Ensure this value has at most 8 characters (it has 9).'] +ValidationError: [u'Ensure this value has at most 8 characters (it has 9).', u'Enter a valid value.'] >>> f.clean(' 12367 ') Traceback (most recent call last): ... diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 89140f04b1..db70500909 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -38,6 +38,7 @@ from formsets import tests as formset_tests from media import media_tests from fields import FieldsTests +from validators import TestFieldWithValidators __test__ = { 'extra_tests': extra_tests, diff --git a/tests/regressiontests/forms/util.py b/tests/regressiontests/forms/util.py index 845ddeaadb..f365c8c1ae 100644 --- a/tests/regressiontests/forms/util.py +++ b/tests/regressiontests/forms/util.py @@ -5,6 +5,7 @@ Tests for forms/util.py module. tests = r""" >>> from django.forms.util import * +>>> from django.core.exceptions import ValidationError >>> from django.utils.translation import ugettext_lazy ########### @@ -24,36 +25,36 @@ u'' ################### # Can take a string. ->>> print ValidationError("There was an error.").messages +>>> print ErrorList(ValidationError("There was an error.").messages) <ul class="errorlist"><li>There was an error.</li></ul> # Can take a unicode string. ->>> print ValidationError(u"Not \u03C0.").messages +>>> print ErrorList(ValidationError(u"Not \u03C0.").messages) <ul class="errorlist"><li>Not π.</li></ul> # Can take a lazy string. ->>> print ValidationError(ugettext_lazy("Error.")).messages +>>> print ErrorList(ValidationError(ugettext_lazy("Error.")).messages) <ul class="errorlist"><li>Error.</li></ul> # Can take a list. ->>> print ValidationError(["Error one.", "Error two."]).messages +>>> print ErrorList(ValidationError(["Error one.", "Error two."]).messages) <ul class="errorlist"><li>Error one.</li><li>Error two.</li></ul> # Can take a mixture in a list. ->>> print ValidationError(["First error.", u"Not \u03C0.", ugettext_lazy("Error.")]).messages +>>> print ErrorList(ValidationError(["First error.", u"Not \u03C0.", ugettext_lazy("Error.")]).messages) <ul class="errorlist"><li>First error.</li><li>Not π.</li><li>Error.</li></ul> >>> class VeryBadError: ... def __unicode__(self): return u"A very bad error." # Can take a non-string. ->>> print ValidationError(VeryBadError()).messages +>>> print ErrorList(ValidationError(VeryBadError()).messages) <ul class="errorlist"><li>A very bad error.</li></ul> # Escapes non-safe input but not input marked safe. >>> example = 'Example of link: <a href="http://www.example.com/">example</a>' ->>> print ValidationError(example).messages +>>> print ErrorList([example]) <ul class="errorlist"><li>Example of link: <a href="http://www.example.com/">example</a></li></ul> ->>> print ValidationError(mark_safe(example)).messages +>>> print ErrorList([mark_safe(example)]) <ul class="errorlist"><li>Example of link: <a href="http://www.example.com/">example</a></li></ul> """ diff --git a/tests/regressiontests/forms/validators.py b/tests/regressiontests/forms/validators.py new file mode 100644 index 0000000000..ed8e8fbd9a --- /dev/null +++ b/tests/regressiontests/forms/validators.py @@ -0,0 +1,17 @@ +from unittest import TestCase + +from django import forms +from django.core import validators +from django.core.exceptions import ValidationError + + +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)) |
