diff options
| author | Julien Phalip <jphalip@gmail.com> | 2011-10-13 08:11:00 +0000 |
|---|---|---|
| committer | Julien Phalip <jphalip@gmail.com> | 2011-10-13 08:11:00 +0000 |
| commit | 3b22c683438e59ebe27a2d21ebd6d33fbf1644bd (patch) | |
| tree | 1e0d60a587955cd64927ebd69745f26405aee753 /tests | |
| parent | 9a5262b037c0b5c4270a11b19d7ef0ab59b6aef5 (diff) | |
Fixed #12467 -- Made the model data validation for `DateField` and `DateTimeField` more useful by actually telling what was the value that failed. Also did a bit of PEP8 cleanup in the area. Thanks to knutin for the report, to raulcd for the initial patch and to charettes for the review.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16966 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modeltests/validation/test_error_messages.py | 56 | ||||
| -rw-r--r-- | tests/modeltests/validation/tests.py | 2 |
2 files changed, 57 insertions, 1 deletions
diff --git a/tests/modeltests/validation/test_error_messages.py b/tests/modeltests/validation/test_error_messages.py index 45d03277f4..43b707e632 100644 --- a/tests/modeltests/validation/test_error_messages.py +++ b/tests/modeltests/validation/test_error_messages.py @@ -55,3 +55,59 @@ class ValidationMessagesTest(TestCase): except ValidationError, e: self.assertEqual(e.messages, [u"'foo' value must be either None, True or False."]) + + def test_date_field_raises_error_message(self): + f = models.DateField() + self.assertRaises(ValidationError, f.clean, 'foo', None) + try: + f.clean('foo', None) + except ValidationError, e: + self.assertEqual(e.messages, [ + u"'foo' value has an invalid date format. " + u"It must be in YYYY-MM-DD format."]) + + self.assertRaises(ValidationError, f.clean, 'aaaa-10-10', None) + try: + f.clean('aaaa-10-10', None) + except ValidationError, e: + self.assertEqual(e.messages, [ + u"'aaaa-10-10' value has an invalid date format. " + u"It must be in YYYY-MM-DD format."]) + + self.assertRaises(ValidationError, f.clean, '2011-13-10', None) + try: + f.clean('2011-13-10', None) + except ValidationError, e: + self.assertEqual(e.messages, [ + u"'2011-13-10' value has the correct format (YYYY-MM-DD) " + u"but it is an invalid date."]) + + self.assertRaises(ValidationError, f.clean, '2011-10-32', None) + try: + f.clean('2011-10-32', None) + except ValidationError, e: + self.assertEqual(e.messages, [ + u"'2011-10-32' value has the correct format (YYYY-MM-DD) " + u"but it is an invalid date."]) + + def test_datetime_field_raises_error_message(self): + f = models.DateTimeField() + # Wrong format + self.assertRaises(ValidationError, f.clean, 'foo', None) + try: + f.clean('foo', None) + except ValidationError, e: + self.assertEqual(e.messages, [ + u"'foo' value either has an invalid valid format " + u"(The format must be YYYY-MM-DD HH:MM[:ss[.uuuuuu]]) " + u"or is an invalid date/time."]) + self.assertRaises(ValidationError, f.clean, + '2011-10-32 10:10', None) + # Correct format but invalid date/time + try: + f.clean('2011-10-32 10:10', None) + except ValidationError, e: + self.assertEqual(e.messages, [ + u"'2011-10-32 10:10' value either has an invalid valid format " + u"(The format must be YYYY-MM-DD HH:MM[:ss[.uuuuuu]]) " + u"or is an invalid date/time."])
\ No newline at end of file diff --git a/tests/modeltests/validation/tests.py b/tests/modeltests/validation/tests.py index e6e5d97971..5b20df4ef3 100644 --- a/tests/modeltests/validation/tests.py +++ b/tests/modeltests/validation/tests.py @@ -12,7 +12,7 @@ from modeltests.validation.validators import TestModelsWithValidators from modeltests.validation.test_unique import (GetUniqueCheckTests, PerformUniqueChecksTest) from modeltests.validation.test_custom_messages import CustomMessagesTest - +from modeltests.validation.test_error_messages import ValidationMessagesTest class BaseModelValidationTests(ValidationTestCase): |
