2011-08-22 08:48:53 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.db import models
|
2011-10-14 02:04:12 +08:00
|
|
|
from django.utils.unittest import TestCase
|
2011-08-22 08:48:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ValidationMessagesTest(TestCase):
|
|
|
|
|
2012-05-31 22:24:37 +08:00
|
|
|
def _test_validation_messages(self, field, value, expected):
|
|
|
|
with self.assertRaises(ValidationError) as cm:
|
|
|
|
field.clean(value, None)
|
|
|
|
self.assertEqual(cm.exception.messages, expected)
|
|
|
|
|
2011-08-22 08:48:53 +08:00
|
|
|
def test_autofield_field_raises_error_message(self):
|
|
|
|
f = models.AutoField(primary_key=True)
|
2012-05-31 22:24:37 +08:00
|
|
|
self._test_validation_messages(f, 'foo',
|
|
|
|
[u"'foo' value must be an integer."])
|
2011-10-13 19:53:59 +08:00
|
|
|
# primary_key must be True. Refs #12467.
|
2012-05-31 22:24:37 +08:00
|
|
|
with self.assertRaisesRegexp(AssertionError,
|
|
|
|
"AutoFields must have primary_key=True."):
|
2011-10-13 19:53:59 +08:00
|
|
|
models.AutoField(primary_key=False)
|
2011-08-22 08:48:53 +08:00
|
|
|
|
|
|
|
def test_integer_field_raises_error_message(self):
|
|
|
|
f = models.IntegerField()
|
2012-05-31 22:24:37 +08:00
|
|
|
self._test_validation_messages(f, 'foo',
|
|
|
|
[u"'foo' value must be an integer."])
|
2011-08-22 08:48:53 +08:00
|
|
|
|
|
|
|
def test_boolean_field_raises_error_message(self):
|
|
|
|
f = models.BooleanField()
|
2012-05-31 22:24:37 +08:00
|
|
|
self._test_validation_messages(f, 'foo',
|
|
|
|
[u"'foo' value must be either True or False."])
|
2011-08-22 08:48:53 +08:00
|
|
|
|
|
|
|
def test_float_field_raises_error_message(self):
|
|
|
|
f = models.FloatField()
|
2012-05-31 22:24:37 +08:00
|
|
|
self._test_validation_messages(f, 'foo',
|
|
|
|
[u"'foo' value must be a float."])
|
2011-08-22 08:48:53 +08:00
|
|
|
|
|
|
|
def test_decimal_field_raises_error_message(self):
|
|
|
|
f = models.DecimalField()
|
2012-05-31 22:24:37 +08:00
|
|
|
self._test_validation_messages(f, 'foo',
|
|
|
|
[u"'foo' value must be a decimal number."])
|
2011-08-22 08:48:53 +08:00
|
|
|
|
|
|
|
def test_null_boolean_field_raises_error_message(self):
|
|
|
|
f = models.NullBooleanField()
|
2012-05-31 22:24:37 +08:00
|
|
|
self._test_validation_messages(f, 'foo',
|
|
|
|
[u"'foo' value must be either None, True or False."])
|
2011-10-13 16:11:00 +08:00
|
|
|
|
|
|
|
def test_date_field_raises_error_message(self):
|
|
|
|
f = models.DateField()
|
2012-05-31 22:24:37 +08:00
|
|
|
self._test_validation_messages(f, 'foo',
|
|
|
|
[u"'foo' value has an invalid date format. "
|
|
|
|
u"It must be in YYYY-MM-DD format."])
|
|
|
|
|
|
|
|
self._test_validation_messages(f, 'aaaa-10-10',
|
|
|
|
[u"'aaaa-10-10' value has an invalid date format. "
|
|
|
|
u"It must be in YYYY-MM-DD format."])
|
|
|
|
|
|
|
|
self._test_validation_messages(f, '2011-13-10',
|
|
|
|
[u"'2011-13-10' value has the correct format (YYYY-MM-DD) "
|
|
|
|
u"but it is an invalid date."])
|
|
|
|
|
|
|
|
self._test_validation_messages(f, '2011-10-32',
|
|
|
|
[u"'2011-10-32' value has the correct format (YYYY-MM-DD) "
|
|
|
|
u"but it is an invalid date."])
|
2011-10-13 16:11:00 +08:00
|
|
|
|
|
|
|
def test_datetime_field_raises_error_message(self):
|
|
|
|
f = models.DateTimeField()
|
|
|
|
# Wrong format
|
2012-05-31 22:24:37 +08:00
|
|
|
self._test_validation_messages(f, 'foo',
|
|
|
|
[u"'foo' value has an invalid format. It must be "
|
|
|
|
u"in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."])
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
# Correct format but invalid date
|
2012-05-31 22:24:37 +08:00
|
|
|
self._test_validation_messages(f, '2011-10-32',
|
|
|
|
[u"'2011-10-32' value has the correct format "
|
|
|
|
u"(YYYY-MM-DD) but it is an invalid date."])
|
2011-11-18 21:01:06 +08:00
|
|
|
|
2011-10-13 16:11:00 +08:00
|
|
|
# Correct format but invalid date/time
|
2012-05-31 22:24:37 +08:00
|
|
|
self._test_validation_messages(f, '2011-10-32 10:10',
|
|
|
|
[u"'2011-10-32 10:10' value has the correct format "
|
|
|
|
u"(YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]) "
|
|
|
|
u"but it is an invalid date/time."])
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
def test_time_field_raises_error_message(self):
|
|
|
|
f = models.TimeField()
|
|
|
|
# Wrong format
|
2012-05-31 22:24:37 +08:00
|
|
|
self._test_validation_messages(f, 'foo',
|
|
|
|
[u"'foo' value has an invalid format. It must be in "
|
|
|
|
u"HH:MM[:ss[.uuuuuu]] format."])
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
# Correct format but invalid time
|
2012-05-31 22:24:37 +08:00
|
|
|
self._test_validation_messages(f, '25:50',
|
|
|
|
[u"'25:50' value has the correct format "
|
|
|
|
u"(HH:MM[:ss[.uuuuuu]]) but it is an invalid time."])
|