Refs #31806 -- Added tests for including the value in some ValidationErrors.

This commit is contained in:
Jon Dufresne 2020-07-27 12:34:23 +02:00 committed by Mariusz Felisiak
parent cc3d24d7d5
commit 013e06bb37
1 changed files with 41 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import re import re
import types
from unittest import TestCase from unittest import TestCase
from django import forms from django import forms
@ -62,3 +63,43 @@ class TestFieldWithValidators(TestCase):
form = UserForm({'full_name': 'not int nor mail'}) form = UserForm({'full_name': 'not int nor mail'})
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
self.assertEqual(form.errors['full_name'], ['Enter a valid integer.', 'Enter a valid email address.']) self.assertEqual(form.errors['full_name'], ['Enter a valid integer.', 'Enter a valid email address.'])
class ValidatorCustomMessageTests(TestCase):
def test_value_placeholder_with_char_field(self):
cases = [
(validators.MaxLengthValidator(10), 11 * 'x', 'max_length'),
(validators.MinLengthValidator(10), 9 * 'x', 'min_length'),
]
for validator, value, code in cases:
if isinstance(validator, types.FunctionType):
name = validator.__name__
else:
name = type(validator).__name__
with self.subTest(name, value=value):
class MyForm(forms.Form):
field = forms.CharField(
validators=[validator],
error_messages={code: '%(value)s'},
)
form = MyForm({'field': value})
self.assertIs(form.is_valid(), False)
self.assertEqual(form.errors, {'field': [value]})
def test_value_placeholder_with_integer_field(self):
cases = [
(validators.MaxValueValidator(0), 1, 'max_value'),
(validators.MinValueValidator(0), -1, 'min_value'),
]
for validator, value, code in cases:
with self.subTest(type(validator).__name__, value=value):
class MyForm(forms.Form):
field = forms.IntegerField(
validators=[validator],
error_messages={code: '%(value)s'},
)
form = MyForm({'field': value})
self.assertIs(form.is_valid(), False)
self.assertEqual(form.errors, {'field': [str(value)]})