1
0
mirror of https://github.com/django/django.git synced 2024-12-23 09:36:06 +00:00
django/tests/validation/test_error_messages.py
Russell Keith-Magee d818e0c9b2 Fixed #16905 -- Added extensible checks (nee validation) framework
This is the result of Christopher Medrela's 2013 Summer of Code project.

Thanks also to Preston Holmes, Tim Graham, Anssi Kääriäinen, Florian
Apolloner, and Alex Gaynor for review notes along the way.

Also: Fixes #8579, fixes #3055, fixes #19844.
2014-01-20 10:45:21 +08:00

94 lines
3.6 KiB
Python

# -*- encoding: utf-8 -*-
from __future__ import unicode_literals
from unittest import TestCase
from django.core.exceptions import ValidationError
from django.db import models
class ValidationMessagesTest(TestCase):
def _test_validation_messages(self, field, value, expected):
with self.assertRaises(ValidationError) as cm:
field.clean(value, None)
self.assertEqual(cm.exception.messages, expected)
def test_autofield_field_raises_error_message(self):
f = models.AutoField(primary_key=True)
self._test_validation_messages(f, 'fõo',
["'fõo' value must be an integer."])
def test_integer_field_raises_error_message(self):
f = models.IntegerField()
self._test_validation_messages(f, 'fõo',
["'fõo' value must be an integer."])
def test_boolean_field_raises_error_message(self):
f = models.BooleanField()
self._test_validation_messages(f, 'fõo',
["'fõo' value must be either True or False."])
def test_float_field_raises_error_message(self):
f = models.FloatField()
self._test_validation_messages(f, 'fõo',
["'fõo' value must be a float."])
def test_decimal_field_raises_error_message(self):
f = models.DecimalField()
self._test_validation_messages(f, 'fõo',
["'fõo' value must be a decimal number."])
def test_null_boolean_field_raises_error_message(self):
f = models.NullBooleanField()
self._test_validation_messages(f, 'fõo',
["'fõo' value must be either None, True or False."])
def test_date_field_raises_error_message(self):
f = models.DateField()
self._test_validation_messages(f, 'fõo',
["'fõo' value has an invalid date format. "
"It must be in YYYY-MM-DD format."])
self._test_validation_messages(f, 'aaaa-10-10',
["'aaaa-10-10' value has an invalid date format. "
"It must be in YYYY-MM-DD format."])
self._test_validation_messages(f, '2011-13-10',
["'2011-13-10' value has the correct format (YYYY-MM-DD) "
"but it is an invalid date."])
self._test_validation_messages(f, '2011-10-32',
["'2011-10-32' value has the correct format (YYYY-MM-DD) "
"but it is an invalid date."])
def test_datetime_field_raises_error_message(self):
f = models.DateTimeField()
# Wrong format
self._test_validation_messages(f, 'fõo',
["'fõo' value has an invalid format. It must be "
"in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."])
# Correct format but invalid date
self._test_validation_messages(f, '2011-10-32',
["'2011-10-32' value has the correct format "
"(YYYY-MM-DD) but it is an invalid date."])
# Correct format but invalid date/time
self._test_validation_messages(f, '2011-10-32 10:10',
["'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."])
def test_time_field_raises_error_message(self):
f = models.TimeField()
# Wrong format
self._test_validation_messages(f, 'fõo',
["'fõo' value has an invalid format. It must be in "
"HH:MM[:ss[.uuuuuu]] format."])
# Correct format but invalid time
self._test_validation_messages(f, '25:50',
["'25:50' value has the correct format "
"(HH:MM[:ss[.uuuuuu]]) but it is an invalid time."])