2013-07-01 12:22:27 +00:00
|
|
|
from unittest import TestCase
|
|
|
|
|
2011-08-22 00:48:53 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
|
|
|
|
class ValidationMessagesTest(TestCase):
|
2012-05-31 14:24:37 +00: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 00:48:53 +00:00
|
|
|
def test_autofield_field_raises_error_message(self):
|
|
|
|
f = models.AutoField(primary_key=True)
|
2019-06-27 16:39:47 +00:00
|
|
|
self._test_validation_messages(f, "fõo", ["“fõo” value must be an integer."])
|
2011-08-22 00:48:53 +00:00
|
|
|
|
|
|
|
def test_integer_field_raises_error_message(self):
|
|
|
|
f = models.IntegerField()
|
2019-06-27 16:39:47 +00:00
|
|
|
self._test_validation_messages(f, "fõo", ["“fõo” value must be an integer."])
|
2011-08-22 00:48:53 +00:00
|
|
|
|
|
|
|
def test_boolean_field_raises_error_message(self):
|
|
|
|
f = models.BooleanField()
|
2019-06-27 16:39:47 +00:00
|
|
|
self._test_validation_messages(
|
|
|
|
f, "fõo", ["“fõo” value must be either True or False."]
|
|
|
|
)
|
2011-08-22 00:48:53 +00:00
|
|
|
|
2017-05-06 14:56:28 +00:00
|
|
|
def test_nullable_boolean_field_raises_error_message(self):
|
|
|
|
f = models.BooleanField(null=True)
|
2019-06-27 16:39:47 +00:00
|
|
|
self._test_validation_messages(
|
|
|
|
f, "fõo", ["“fõo” value must be either True, False, or None."]
|
|
|
|
)
|
2017-05-06 14:56:28 +00:00
|
|
|
|
2011-08-22 00:48:53 +00:00
|
|
|
def test_float_field_raises_error_message(self):
|
|
|
|
f = models.FloatField()
|
2019-06-27 16:39:47 +00:00
|
|
|
self._test_validation_messages(f, "fõo", ["“fõo” value must be a float."])
|
2011-08-22 00:48:53 +00:00
|
|
|
|
|
|
|
def test_decimal_field_raises_error_message(self):
|
|
|
|
f = models.DecimalField()
|
2019-06-27 16:39:47 +00:00
|
|
|
self._test_validation_messages(
|
|
|
|
f, "fõo", ["“fõo” value must be a decimal number."]
|
|
|
|
)
|
2011-08-22 00:48:53 +00:00
|
|
|
|
|
|
|
def test_null_boolean_field_raises_error_message(self):
|
2021-01-13 20:28:09 +00:00
|
|
|
f = models.BooleanField(null=True)
|
|
|
|
self._test_validation_messages(
|
|
|
|
f, "fõo", ["“fõo” value must be either True, False, or None."]
|
|
|
|
)
|
2011-10-13 08:11:00 +00:00
|
|
|
|
|
|
|
def test_date_field_raises_error_message(self):
|
|
|
|
f = models.DateField()
|
2016-04-08 02:04:45 +00:00
|
|
|
self._test_validation_messages(
|
|
|
|
f,
|
|
|
|
"fõo",
|
2019-06-27 16:39:47 +00:00
|
|
|
[
|
|
|
|
"“fõo” value has an invalid date format. It must be in YYYY-MM-DD "
|
|
|
|
"format."
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
|
|
|
self._test_validation_messages(
|
|
|
|
f,
|
|
|
|
"aaaa-10-10",
|
2019-06-27 16:39:47 +00:00
|
|
|
[
|
|
|
|
"“aaaa-10-10” value has an invalid date format. It must be in "
|
|
|
|
"YYYY-MM-DD format."
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
|
|
|
self._test_validation_messages(
|
|
|
|
f,
|
|
|
|
"2011-13-10",
|
2019-06-27 16:39:47 +00:00
|
|
|
[
|
|
|
|
"“2011-13-10” value has the correct format (YYYY-MM-DD) but it is an "
|
|
|
|
"invalid date."
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
|
|
|
self._test_validation_messages(
|
|
|
|
f,
|
|
|
|
"2011-10-32",
|
2019-06-27 16:39:47 +00:00
|
|
|
[
|
|
|
|
"“2011-10-32” value has the correct format (YYYY-MM-DD) but it is an "
|
|
|
|
"invalid date."
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
2011-10-13 08:11:00 +00:00
|
|
|
|
|
|
|
def test_datetime_field_raises_error_message(self):
|
|
|
|
f = models.DateTimeField()
|
|
|
|
# Wrong format
|
2016-04-08 02:04:45 +00:00
|
|
|
self._test_validation_messages(
|
|
|
|
f,
|
|
|
|
"fõo",
|
2019-06-27 16:39:47 +00:00
|
|
|
[
|
|
|
|
"“fõo” value has an invalid format. It must be in "
|
|
|
|
"YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
2011-11-18 13:01:06 +00:00
|
|
|
# Correct format but invalid date
|
2016-04-08 02:04:45 +00:00
|
|
|
self._test_validation_messages(
|
|
|
|
f,
|
|
|
|
"2011-10-32",
|
2019-06-27 16:39:47 +00:00
|
|
|
[
|
|
|
|
"“2011-10-32” value has the correct format (YYYY-MM-DD) but it is an "
|
|
|
|
"invalid date."
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
2011-10-13 08:11:00 +00:00
|
|
|
# Correct format but invalid date/time
|
2016-04-08 02:04:45 +00:00
|
|
|
self._test_validation_messages(
|
|
|
|
f,
|
|
|
|
"2011-10-32 10:10",
|
2019-06-27 16:39:47 +00:00
|
|
|
[
|
|
|
|
"“2011-10-32 10:10” value has the correct format "
|
|
|
|
"(YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]) but it is an invalid date/time."
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
2011-11-18 13:01:06 +00:00
|
|
|
|
|
|
|
def test_time_field_raises_error_message(self):
|
|
|
|
f = models.TimeField()
|
|
|
|
# Wrong format
|
2016-04-08 02:04:45 +00:00
|
|
|
self._test_validation_messages(
|
|
|
|
f,
|
|
|
|
"fõo",
|
2019-06-27 16:39:47 +00:00
|
|
|
[
|
|
|
|
"“fõo” value has an invalid format. It must be in HH:MM[:ss[.uuuuuu]] "
|
|
|
|
"format."
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
2011-11-18 13:01:06 +00:00
|
|
|
# Correct format but invalid time
|
2016-04-08 02:04:45 +00:00
|
|
|
self._test_validation_messages(
|
|
|
|
f,
|
|
|
|
"25:50",
|
2019-06-27 16:39:47 +00:00
|
|
|
[
|
|
|
|
"“25:50” value has the correct format (HH:MM[:ss[.uuuuuu]]) but it is "
|
|
|
|
"an invalid time."
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|