| 1 |
from unittest import TestCase |
| 2 |
|
| 3 |
from django.core.exceptions import ValidationError |
| 4 |
from django.db import models |
| 5 |
|
| 6 |
|
| 7 |
class ValidationMessagesTest(TestCase): |
| 8 |
def _test_validation_messages(self, field, value, expected): |
| 9 |
with self.assertRaises(ValidationError) as cm: |
| 10 |
field.clean(value, None) |
| 11 |
self.assertEqual(cm.exception.messages, expected) |
| 12 |
|
| 13 |
def test_autofield_field_raises_error_message(self): |
| 14 |
f = models.AutoField(primary_key=True) |
| 15 |
self._test_validation_messages(f, "fõo", ["“fõo” value must be an integer."]) |
| 16 |
|
| 17 |
def test_integer_field_raises_error_message(self): |
| 18 |
f = models.IntegerField() |
| 19 |
self._test_validation_messages(f, "fõo", ["“fõo” value must be an integer."]) |
| 20 |
|
| 21 |
def test_boolean_field_raises_error_message(self): |
| 22 |
f = models.BooleanField() |
| 23 |
self._test_validation_messages( |
| 24 |
f, "fõo", ["“fõo” value must be either True or False."] |
| 25 |
) |
| 26 |
|
| 27 |
def test_nullable_boolean_field_raises_error_message(self): |
| 28 |
f = models.BooleanField(null=True) |
| 29 |
self._test_validation_messages( |
| 30 |
f, "fõo", ["“fõo” value must be either True, False, or None."] |
| 31 |
) |
| 32 |
|
| 33 |
def test_float_field_raises_error_message(self): |
| 34 |
f = models.FloatField() |
| 35 |
self._test_validation_messages(f, "fõo", ["“fõo” value must be a float."]) |
| 36 |
|
| 37 |
def test_decimal_field_raises_error_message(self): |
| 38 |
f = models.DecimalField() |
| 39 |
self._test_validation_messages( |
| 40 |
f, "fõo", ["“fõo” value must be a decimal number."] |
| 41 |
) |
| 42 |
|
| 43 |
def test_null_boolean_field_raises_error_message(self): |
| 44 |
f = models.BooleanField(null=True) |
| 45 |
self._test_validation_messages( |
| 46 |
f, "fõo", ["“fõo” value must be either True, False, or None."] |
| 47 |
) |
| 48 |
|
| 49 |
def test_date_field_raises_error_message(self): |
| 50 |
f = models.DateField() |
| 51 |
self._test_validation_messages( |
| 52 |
f, |
| 53 |
"fõo", |
| 54 |
[ |
| 55 |
"“fõo” value has an invalid date format. It must be in YYYY-MM-DD " |
| 56 |
"format." |
| 57 |
], |
| 58 |
) |
| 59 |
self._test_validation_messages( |
| 60 |
f, |
| 61 |
"aaaa-10-10", |
| 62 |
[ |
| 63 |
"“aaaa-10-10” value has an invalid date format. It must be in " |
| 64 |
"YYYY-MM-DD format." |
| 65 |
], |
| 66 |
) |
| 67 |
self._test_validation_messages( |
| 68 |
f, |
| 69 |
"2011-13-10", |
| 70 |
[ |
| 71 |
"“2011-13-10” value has the correct format (YYYY-MM-DD) but it is an " |
| 72 |
"invalid date." |
| 73 |
], |
| 74 |
) |
| 75 |
self._test_validation_messages( |
| 76 |
f, |
| 77 |
"2011-10-32", |
| 78 |
[ |
| 79 |
"“2011-10-32” value has the correct format (YYYY-MM-DD) but it is an " |
| 80 |
"invalid date." |
| 81 |
], |
| 82 |
) |
| 83 |
|
| 84 |
def test_datetime_field_raises_error_message(self): |
| 85 |
f = models.DateTimeField() |
| 86 |
# Wrong format |
| 87 |
self._test_validation_messages( |
| 88 |
f, |
| 89 |
"fõo", |
| 90 |
[ |
| 91 |
"“fõo” value has an invalid format. It must be in " |
| 92 |
"YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format." |
| 93 |
], |
| 94 |
) |
| 95 |
# Correct format but invalid date |
| 96 |
self._test_validation_messages( |
| 97 |
f, |
| 98 |
"2011-10-32", |
| 99 |
[ |
| 100 |
"“2011-10-32” value has the correct format (YYYY-MM-DD) but it is an " |
| 101 |
"invalid date." |
| 102 |
], |
| 103 |
) |
| 104 |
# Correct format but invalid date/time |
| 105 |
self._test_validation_messages( |
| 106 |
f, |
| 107 |
"2011-10-32 10:10", |
| 108 |
[ |
| 109 |
"“2011-10-32 10:10” value has the correct format " |
| 110 |
"(YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]) but it is an invalid date/time." |
| 111 |
], |
| 112 |
) |
| 113 |
|
| 114 |
def test_time_field_raises_error_message(self): |
| 115 |
f = models.TimeField() |
| 116 |
# Wrong format |
| 117 |
self._test_validation_messages( |
| 118 |
f, |
| 119 |
"fõo", |
| 120 |
[ |
| 121 |
"“fõo” value has an invalid format. It must be in HH:MM[:ss[.uuuuuu]] " |
| 122 |
"format." |
| 123 |
], |
| 124 |
) |
| 125 |
# Correct format but invalid time |
| 126 |
self._test_validation_messages( |
| 127 |
f, |
| 128 |
"25:50", |
| 129 |
[ |
| 130 |
"“25:50” value has the correct format (HH:MM[:ss[.uuuuuu]]) but it is " |
| 131 |
"an invalid time." |
| 132 |
], |
| 133 |
) |