2010-01-05 03:56:19 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-07 16:08:47 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-11-03 17:00:18 +00:00
|
|
|
import io
|
|
|
|
import os
|
2010-01-05 03:56:19 +00:00
|
|
|
import re
|
|
|
|
import types
|
2015-01-28 12:35:27 +00:00
|
|
|
from datetime import datetime, timedelta
|
2013-07-01 12:22:27 +00:00
|
|
|
from unittest import TestCase
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2013-10-22 12:28:34 +00:00
|
|
|
from django.core.validators import (
|
2015-04-14 22:11:12 +00:00
|
|
|
BaseValidator, DecimalValidator, EmailValidator, MaxLengthValidator,
|
|
|
|
MaxValueValidator, MinLengthValidator, MinValueValidator, RegexValidator,
|
|
|
|
URLValidator, int_list_validator, validate_comma_separated_integer_list,
|
|
|
|
validate_email, validate_integer, validate_ipv4_address,
|
|
|
|
validate_ipv6_address, validate_ipv46_address, validate_slug,
|
|
|
|
validate_unicode_slug,
|
2013-10-22 12:28:34 +00:00
|
|
|
)
|
2014-07-24 18:01:12 +00:00
|
|
|
from django.test import SimpleTestCase
|
2012-06-07 16:08:47 +00:00
|
|
|
from django.test.utils import str_prefix
|
2014-11-03 17:00:18 +00:00
|
|
|
from django.utils._os import upath
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
NOW = datetime.now()
|
2015-10-29 21:49:10 +00:00
|
|
|
EXTENDED_SCHEMES = ['http', 'https', 'ftp', 'ftps', 'git', 'file', 'git+ssh']
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2014-11-03 17:00:18 +00:00
|
|
|
TEST_DATA = [
|
2010-01-05 03:56:19 +00:00
|
|
|
# (validator, value, expected),
|
|
|
|
(validate_integer, '42', None),
|
|
|
|
(validate_integer, '-42', None),
|
|
|
|
(validate_integer, -42, None),
|
|
|
|
|
2015-06-12 17:49:31 +00:00
|
|
|
(validate_integer, -42.5, ValidationError),
|
2010-01-05 03:56:19 +00:00
|
|
|
(validate_integer, None, ValidationError),
|
|
|
|
(validate_integer, 'a', ValidationError),
|
2015-06-12 17:49:31 +00:00
|
|
|
(validate_integer, '\n42', ValidationError),
|
|
|
|
(validate_integer, '42\n', ValidationError),
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
(validate_email, 'email@here.com', None),
|
|
|
|
(validate_email, 'weirder-email@here.and.there.com', None),
|
2011-06-10 10:22:42 +00:00
|
|
|
(validate_email, 'email@[127.0.0.1]', None),
|
2014-02-22 15:00:23 +00:00
|
|
|
(validate_email, 'email@[2001:dB8::1]', None),
|
|
|
|
(validate_email, 'email@[2001:dB8:0:0:0:0:0:1]', None),
|
|
|
|
(validate_email, 'email@[::fffF:127.0.0.1]', None),
|
2012-10-09 12:56:19 +00:00
|
|
|
(validate_email, 'example@valid-----hyphens.com', None),
|
|
|
|
(validate_email, 'example@valid-with-hyphens.com', None),
|
2012-10-09 13:01:38 +00:00
|
|
|
(validate_email, 'test@domain.with.idn.tld.उदाहरण.परीक्षा', None),
|
2013-01-11 19:42:33 +00:00
|
|
|
(validate_email, 'email@localhost', None),
|
|
|
|
(EmailValidator(whitelist=['localdomain']), 'email@localdomain', None),
|
2013-01-26 11:19:31 +00:00
|
|
|
(validate_email, '"test@test"@example.com', None),
|
2015-04-14 18:42:49 +00:00
|
|
|
(validate_email, 'example@atm.%s' % ('a' * 63), None),
|
|
|
|
(validate_email, 'example@%s.atm' % ('a' * 63), None),
|
|
|
|
(validate_email, 'example@%s.%s.atm' % ('a' * 63, 'b' * 10), None),
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2015-04-14 18:42:49 +00:00
|
|
|
(validate_email, 'example@atm.%s' % ('a' * 64), ValidationError),
|
|
|
|
(validate_email, 'example@%s.atm.%s' % ('b' * 64, 'a' * 63), ValidationError),
|
2010-01-05 03:56:19 +00:00
|
|
|
(validate_email, None, ValidationError),
|
|
|
|
(validate_email, '', ValidationError),
|
|
|
|
(validate_email, 'abc', ValidationError),
|
2012-10-09 12:56:19 +00:00
|
|
|
(validate_email, 'abc@', ValidationError),
|
|
|
|
(validate_email, 'abc@bar', ValidationError),
|
2010-01-05 03:56:19 +00:00
|
|
|
(validate_email, 'a @x.cz', ValidationError),
|
2012-10-09 12:56:19 +00:00
|
|
|
(validate_email, 'abc@.com', ValidationError),
|
2010-01-05 03:56:19 +00:00
|
|
|
(validate_email, 'something@@somewhere.com', ValidationError),
|
2011-06-10 10:22:42 +00:00
|
|
|
(validate_email, 'email@127.0.0.1', ValidationError),
|
2014-02-22 15:00:23 +00:00
|
|
|
(validate_email, 'email@[127.0.0.256]', ValidationError),
|
|
|
|
(validate_email, 'email@[2001:db8::12345]', ValidationError),
|
|
|
|
(validate_email, 'email@[2001:db8:0:0:0:0:1]', ValidationError),
|
|
|
|
(validate_email, 'email@[::ffff:127.0.0.256]', ValidationError),
|
2012-10-09 12:56:19 +00:00
|
|
|
(validate_email, 'example@invalid-.com', ValidationError),
|
|
|
|
(validate_email, 'example@-invalid.com', ValidationError),
|
2014-05-16 13:13:11 +00:00
|
|
|
(validate_email, 'example@invalid.com-', ValidationError),
|
2012-10-09 12:56:19 +00:00
|
|
|
(validate_email, 'example@inv-.alid-.com', ValidationError),
|
|
|
|
(validate_email, 'example@inv-.-alid.com', ValidationError),
|
2013-07-20 16:00:23 +00:00
|
|
|
(validate_email, 'test@example.com\n\n<script src="x.js">', ValidationError),
|
2012-01-07 18:43:46 +00:00
|
|
|
# Quoted-string format (CR not allowed)
|
|
|
|
(validate_email, '"\\\011"@here.com', None),
|
|
|
|
(validate_email, '"\\\012"@here.com', ValidationError),
|
2013-10-23 16:13:17 +00:00
|
|
|
(validate_email, 'trailingdot@shouldfail.com.', ValidationError),
|
2015-04-14 18:42:49 +00:00
|
|
|
# Max length of domain name labels is 63 characters per RFC 1034.
|
|
|
|
(validate_email, 'a@%s.us' % ('a' * 63), None),
|
|
|
|
(validate_email, 'a@%s.us' % ('a' * 64), ValidationError),
|
2015-06-12 17:49:31 +00:00
|
|
|
# Trailing newlines in username or domain not allowed
|
|
|
|
(validate_email, 'a@b.com\n', ValidationError),
|
|
|
|
(validate_email, 'a\n@b.com', ValidationError),
|
|
|
|
(validate_email, '"test@test"\n@example.com', ValidationError),
|
|
|
|
(validate_email, 'a@[127.0.0.1]\n', ValidationError),
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
(validate_slug, 'slug-ok', None),
|
|
|
|
(validate_slug, 'longer-slug-still-ok', None),
|
|
|
|
(validate_slug, '--------', None),
|
|
|
|
(validate_slug, 'nohyphensoranything', None),
|
2015-04-15 22:28:49 +00:00
|
|
|
(validate_slug, 'a', None),
|
|
|
|
(validate_slug, '1', None),
|
|
|
|
(validate_slug, 'a1', None),
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
(validate_slug, '', ValidationError),
|
|
|
|
(validate_slug, ' text ', ValidationError),
|
|
|
|
(validate_slug, ' ', ValidationError),
|
|
|
|
(validate_slug, 'some@mail.com', ValidationError),
|
|
|
|
(validate_slug, '你好', ValidationError),
|
2015-04-15 22:28:49 +00:00
|
|
|
(validate_slug, '你 好', ValidationError),
|
2010-01-05 03:56:19 +00:00
|
|
|
(validate_slug, '\n', ValidationError),
|
2015-06-12 17:49:31 +00:00
|
|
|
(validate_slug, 'trailing-newline\n', ValidationError),
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2015-04-15 22:28:49 +00:00
|
|
|
(validate_unicode_slug, 'slug-ok', None),
|
|
|
|
(validate_unicode_slug, 'longer-slug-still-ok', None),
|
|
|
|
(validate_unicode_slug, '--------', None),
|
|
|
|
(validate_unicode_slug, 'nohyphensoranything', None),
|
|
|
|
(validate_unicode_slug, 'a', None),
|
|
|
|
(validate_unicode_slug, '1', None),
|
|
|
|
(validate_unicode_slug, 'a1', None),
|
|
|
|
(validate_unicode_slug, '你好', None),
|
|
|
|
|
|
|
|
(validate_unicode_slug, '', ValidationError),
|
|
|
|
(validate_unicode_slug, ' text ', ValidationError),
|
|
|
|
(validate_unicode_slug, ' ', ValidationError),
|
|
|
|
(validate_unicode_slug, 'some@mail.com', ValidationError),
|
|
|
|
(validate_unicode_slug, '\n', ValidationError),
|
|
|
|
(validate_unicode_slug, '你 好', ValidationError),
|
|
|
|
(validate_unicode_slug, 'trailing-newline\n', ValidationError),
|
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
(validate_ipv4_address, '1.1.1.1', None),
|
|
|
|
(validate_ipv4_address, '255.0.0.0', None),
|
|
|
|
(validate_ipv4_address, '0.0.0.0', None),
|
|
|
|
|
|
|
|
(validate_ipv4_address, '256.1.1.1', ValidationError),
|
|
|
|
(validate_ipv4_address, '25.1.1.', ValidationError),
|
|
|
|
(validate_ipv4_address, '25,1,1,1', ValidationError),
|
|
|
|
(validate_ipv4_address, '25.1 .1.1', ValidationError),
|
2015-06-12 17:49:31 +00:00
|
|
|
(validate_ipv4_address, '1.1.1.1\n', ValidationError),
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2011-06-11 13:48:24 +00:00
|
|
|
# validate_ipv6_address uses django.utils.ipv6, which
|
2014-04-26 17:18:45 +00:00
|
|
|
# is tested in much greater detail in its own testcase
|
2011-06-11 13:48:24 +00:00
|
|
|
(validate_ipv6_address, 'fe80::1', None),
|
|
|
|
(validate_ipv6_address, '::1', None),
|
|
|
|
(validate_ipv6_address, '1:2:3:4:5:6:7:8', None),
|
|
|
|
|
|
|
|
(validate_ipv6_address, '1:2', ValidationError),
|
|
|
|
(validate_ipv6_address, '::zzz', ValidationError),
|
|
|
|
(validate_ipv6_address, '12345::', ValidationError),
|
|
|
|
|
|
|
|
(validate_ipv46_address, '1.1.1.1', None),
|
|
|
|
(validate_ipv46_address, '255.0.0.0', None),
|
|
|
|
(validate_ipv46_address, '0.0.0.0', None),
|
|
|
|
(validate_ipv46_address, 'fe80::1', None),
|
|
|
|
(validate_ipv46_address, '::1', None),
|
|
|
|
(validate_ipv46_address, '1:2:3:4:5:6:7:8', None),
|
|
|
|
|
|
|
|
(validate_ipv46_address, '256.1.1.1', ValidationError),
|
|
|
|
(validate_ipv46_address, '25.1.1.', ValidationError),
|
|
|
|
(validate_ipv46_address, '25,1,1,1', ValidationError),
|
|
|
|
(validate_ipv46_address, '25.1 .1.1', ValidationError),
|
|
|
|
(validate_ipv46_address, '1:2', ValidationError),
|
|
|
|
(validate_ipv46_address, '::zzz', ValidationError),
|
|
|
|
(validate_ipv46_address, '12345::', ValidationError),
|
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
(validate_comma_separated_integer_list, '1', None),
|
2015-02-14 14:20:43 +00:00
|
|
|
(validate_comma_separated_integer_list, '12', None),
|
|
|
|
(validate_comma_separated_integer_list, '1,2', None),
|
2010-01-05 03:56:19 +00:00
|
|
|
(validate_comma_separated_integer_list, '1,2,3', None),
|
2015-02-14 14:20:43 +00:00
|
|
|
(validate_comma_separated_integer_list, '10,32', None),
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
(validate_comma_separated_integer_list, '', ValidationError),
|
2015-02-14 14:20:43 +00:00
|
|
|
(validate_comma_separated_integer_list, 'a', ValidationError),
|
2010-01-05 03:56:19 +00:00
|
|
|
(validate_comma_separated_integer_list, 'a,b,c', ValidationError),
|
|
|
|
(validate_comma_separated_integer_list, '1, 2, 3', ValidationError),
|
2015-02-14 14:20:43 +00:00
|
|
|
(validate_comma_separated_integer_list, ',', ValidationError),
|
|
|
|
(validate_comma_separated_integer_list, '1,2,3,', ValidationError),
|
|
|
|
(validate_comma_separated_integer_list, '1,2,', ValidationError),
|
|
|
|
(validate_comma_separated_integer_list, ',1', ValidationError),
|
|
|
|
(validate_comma_separated_integer_list, '1,,2', ValidationError),
|
|
|
|
|
|
|
|
(int_list_validator(sep='.'), '1.2.3', None),
|
|
|
|
(int_list_validator(sep='.'), '1,2,3', ValidationError),
|
2015-06-12 17:49:31 +00:00
|
|
|
(int_list_validator(sep='.'), '1.2.3\n', ValidationError),
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
(MaxValueValidator(10), 10, None),
|
|
|
|
(MaxValueValidator(10), -10, None),
|
|
|
|
(MaxValueValidator(10), 0, None),
|
|
|
|
(MaxValueValidator(NOW), NOW, None),
|
|
|
|
(MaxValueValidator(NOW), NOW - timedelta(days=1), None),
|
|
|
|
|
|
|
|
(MaxValueValidator(0), 1, ValidationError),
|
|
|
|
(MaxValueValidator(NOW), NOW + timedelta(days=1), ValidationError),
|
|
|
|
|
|
|
|
(MinValueValidator(-10), -10, None),
|
|
|
|
(MinValueValidator(-10), 10, None),
|
|
|
|
(MinValueValidator(-10), 0, None),
|
|
|
|
(MinValueValidator(NOW), NOW, None),
|
|
|
|
(MinValueValidator(NOW), NOW + timedelta(days=1), None),
|
|
|
|
|
|
|
|
(MinValueValidator(0), -1, ValidationError),
|
|
|
|
(MinValueValidator(NOW), NOW - timedelta(days=1), ValidationError),
|
|
|
|
|
|
|
|
(MaxLengthValidator(10), '', None),
|
2013-11-02 21:34:05 +00:00
|
|
|
(MaxLengthValidator(10), 10 * 'x', None),
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
(MaxLengthValidator(10), 15 * 'x', ValidationError),
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
(MinLengthValidator(10), 15 * 'x', None),
|
|
|
|
(MinLengthValidator(10), 10 * 'x', None),
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
(MinLengthValidator(10), '', ValidationError),
|
|
|
|
|
2013-12-20 23:15:39 +00:00
|
|
|
(URLValidator(EXTENDED_SCHEMES), 'file://localhost/path', None),
|
|
|
|
(URLValidator(EXTENDED_SCHEMES), 'git://example.com/', None),
|
2015-10-29 21:49:10 +00:00
|
|
|
(URLValidator(EXTENDED_SCHEMES), 'git+ssh://git@github.com/example/hg-git.git', None),
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2013-12-20 23:15:39 +00:00
|
|
|
(URLValidator(EXTENDED_SCHEMES), 'git://-invalid.com', ValidationError),
|
2015-06-12 17:49:31 +00:00
|
|
|
# Trailing newlines not accepted
|
|
|
|
(URLValidator(), 'http://www.djangoproject.com/\n', ValidationError),
|
|
|
|
(URLValidator(), 'http://[::ffff:192.9.5.5]\n', ValidationError),
|
2015-06-29 22:09:21 +00:00
|
|
|
# Trailing junk does not take forever to reject
|
|
|
|
(URLValidator(), 'http://www.asdasdasdasdsadfm.com.br ', ValidationError),
|
|
|
|
(URLValidator(), 'http://www.asdasdasdasdsadfm.com.br z', ValidationError),
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
(BaseValidator(True), True, None),
|
|
|
|
(BaseValidator(True), False, ValidationError),
|
|
|
|
|
2011-06-09 23:51:03 +00:00
|
|
|
(RegexValidator(), '', None),
|
|
|
|
(RegexValidator(), 'x1x2', None),
|
|
|
|
(RegexValidator('[0-9]+'), 'xxxxxx', ValidationError),
|
|
|
|
(RegexValidator('[0-9]+'), '1234', None),
|
|
|
|
(RegexValidator(re.compile('[0-9]+')), '1234', None),
|
2010-01-05 03:56:19 +00:00
|
|
|
(RegexValidator('.*'), '', None),
|
|
|
|
(RegexValidator(re.compile('.*')), '', None),
|
|
|
|
(RegexValidator('.*'), 'xxxxx', None),
|
|
|
|
|
|
|
|
(RegexValidator('x'), 'y', ValidationError),
|
|
|
|
(RegexValidator(re.compile('x')), 'y', ValidationError),
|
2013-07-22 00:27:56 +00:00
|
|
|
(RegexValidator('x', inverse_match=True), 'y', None),
|
|
|
|
(RegexValidator(re.compile('x'), inverse_match=True), 'y', None),
|
|
|
|
(RegexValidator('x', inverse_match=True), 'x', ValidationError),
|
|
|
|
(RegexValidator(re.compile('x'), inverse_match=True), 'x', ValidationError),
|
2014-03-13 11:23:12 +00:00
|
|
|
|
|
|
|
(RegexValidator('x', flags=re.IGNORECASE), 'y', ValidationError),
|
|
|
|
(RegexValidator('a'), 'A', ValidationError),
|
|
|
|
(RegexValidator('a', flags=re.IGNORECASE), 'A', None),
|
2014-11-03 17:00:18 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def create_path(filename):
|
|
|
|
return os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), filename))
|
|
|
|
|
|
|
|
# Add valid and invalid URL tests.
|
|
|
|
# This only tests the validator without extended schemes.
|
|
|
|
with io.open(create_path('valid_urls.txt'), encoding='utf8') as f:
|
|
|
|
for url in f:
|
|
|
|
TEST_DATA.append((URLValidator(), url.strip(), None))
|
|
|
|
with io.open(create_path('invalid_urls.txt'), encoding='utf8') as f:
|
|
|
|
for url in f:
|
|
|
|
TEST_DATA.append((URLValidator(), url.strip(), ValidationError))
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
def create_simple_test_method(validator, expected, value, num):
|
2010-01-21 03:02:13 +00:00
|
|
|
if expected is not None and issubclass(expected, Exception):
|
2010-01-05 03:56:19 +00:00
|
|
|
test_mask = 'test_%s_raises_error_%d'
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
def test_func(self):
|
2012-10-09 11:34:03 +00:00
|
|
|
# assertRaises not used, so as to be able to produce an error message
|
|
|
|
# containing the tested value
|
|
|
|
try:
|
|
|
|
validator(value)
|
|
|
|
except expected:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("%s not raised when validating '%s'" % (
|
|
|
|
expected.__name__, value))
|
2010-01-05 03:56:19 +00:00
|
|
|
else:
|
|
|
|
test_mask = 'test_%s_%d'
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
def test_func(self):
|
2012-10-09 11:34:03 +00:00
|
|
|
try:
|
|
|
|
self.assertEqual(expected, validator(value))
|
|
|
|
except ValidationError as e:
|
|
|
|
self.fail("Validation of '%s' failed. Error message was: %s" % (
|
|
|
|
value, str(e)))
|
2010-01-05 03:56:19 +00:00
|
|
|
if isinstance(validator, types.FunctionType):
|
|
|
|
val_name = validator.__name__
|
|
|
|
else:
|
|
|
|
val_name = validator.__class__.__name__
|
|
|
|
test_name = test_mask % (val_name, num)
|
|
|
|
return test_name, test_func
|
|
|
|
|
|
|
|
# Dynamically assemble a test class with the contents of TEST_DATA
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2014-07-24 18:01:12 +00:00
|
|
|
class TestSimpleValidators(SimpleTestCase):
|
2010-04-15 11:19:59 +00:00
|
|
|
def test_single_message(self):
|
|
|
|
v = ValidationError('Not Valid')
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(str(v), str_prefix("[%(_)s'Not Valid']"))
|
|
|
|
self.assertEqual(repr(v), str_prefix("ValidationError([%(_)s'Not Valid'])"))
|
2010-04-15 11:19:59 +00:00
|
|
|
|
|
|
|
def test_message_list(self):
|
|
|
|
v = ValidationError(['First Problem', 'Second Problem'])
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(str(v), str_prefix("[%(_)s'First Problem', %(_)s'Second Problem']"))
|
|
|
|
self.assertEqual(repr(v), str_prefix("ValidationError([%(_)s'First Problem', %(_)s'Second Problem'])"))
|
2010-04-15 11:19:59 +00:00
|
|
|
|
|
|
|
def test_message_dict(self):
|
2013-04-04 19:21:57 +00:00
|
|
|
v = ValidationError({'first': ['First Problem']})
|
2013-06-05 18:55:05 +00:00
|
|
|
self.assertEqual(str(v), str_prefix("{%(_)s'first': [%(_)s'First Problem']}"))
|
|
|
|
self.assertEqual(repr(v), str_prefix("ValidationError({%(_)s'first': [%(_)s'First Problem']})"))
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2014-03-13 11:23:12 +00:00
|
|
|
def test_regex_validator_flags(self):
|
|
|
|
try:
|
|
|
|
RegexValidator(re.compile('a'), flags=re.IGNORECASE)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("TypeError not raised when flags and pre-compiled regex in RegexValidator")
|
|
|
|
|
2014-07-24 18:01:12 +00:00
|
|
|
def test_max_length_validator_message(self):
|
|
|
|
v = MaxLengthValidator(16, message='"%(value)s" has more than %(limit_value)d characters.')
|
|
|
|
with self.assertRaisesMessage(ValidationError, '"djangoproject.com" has more than 16 characters.'):
|
|
|
|
v('djangoproject.com')
|
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
test_counter = 0
|
|
|
|
for validator, value, expected in TEST_DATA:
|
|
|
|
name, method = create_simple_test_method(validator, expected, value, test_counter)
|
|
|
|
setattr(TestSimpleValidators, name, method)
|
|
|
|
test_counter += 1
|
2014-01-19 18:56:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestValidatorEquality(TestCase):
|
|
|
|
"""
|
|
|
|
Tests that validators have valid equality operators (#21638)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_regex_equality(self):
|
|
|
|
self.assertEqual(
|
|
|
|
RegexValidator(r'^(?:[a-z0-9\.\-]*)://'),
|
|
|
|
RegexValidator(r'^(?:[a-z0-9\.\-]*)://'),
|
|
|
|
)
|
|
|
|
self.assertNotEqual(
|
|
|
|
RegexValidator(r'^(?:[a-z0-9\.\-]*)://'),
|
|
|
|
RegexValidator(r'^(?:[0-9\.\-]*)://'),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh noes", "invalid"),
|
|
|
|
RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh noes", "invalid"),
|
|
|
|
)
|
|
|
|
self.assertNotEqual(
|
|
|
|
RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh", "invalid"),
|
|
|
|
RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh noes", "invalid"),
|
|
|
|
)
|
|
|
|
self.assertNotEqual(
|
|
|
|
RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh noes", "invalid"),
|
|
|
|
RegexValidator(r'^(?:[a-z0-9\.\-]*)://'),
|
|
|
|
)
|
|
|
|
|
2014-05-06 20:57:37 +00:00
|
|
|
self.assertNotEqual(
|
|
|
|
RegexValidator('', flags=re.IGNORECASE),
|
|
|
|
RegexValidator(''),
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertNotEqual(
|
|
|
|
RegexValidator(''),
|
|
|
|
RegexValidator('', inverse_match=True),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_regex_equality_nocache(self):
|
|
|
|
pattern = r'^(?:[a-z0-9\.\-]*)://'
|
|
|
|
left = RegexValidator(pattern)
|
|
|
|
re.purge()
|
|
|
|
right = RegexValidator(pattern)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_regex_equality_blank(self):
|
|
|
|
self.assertEqual(
|
|
|
|
RegexValidator(),
|
|
|
|
RegexValidator(),
|
|
|
|
)
|
|
|
|
|
2014-01-19 18:56:16 +00:00
|
|
|
def test_email_equality(self):
|
|
|
|
self.assertEqual(
|
|
|
|
EmailValidator(),
|
|
|
|
EmailValidator(),
|
|
|
|
)
|
|
|
|
self.assertNotEqual(
|
|
|
|
EmailValidator(message="BAD EMAIL"),
|
|
|
|
EmailValidator(),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
EmailValidator(message="BAD EMAIL", code="bad"),
|
|
|
|
EmailValidator(message="BAD EMAIL", code="bad"),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_basic_equality(self):
|
|
|
|
self.assertEqual(
|
|
|
|
MaxValueValidator(44),
|
|
|
|
MaxValueValidator(44),
|
|
|
|
)
|
|
|
|
self.assertNotEqual(
|
|
|
|
MaxValueValidator(44),
|
|
|
|
MinValueValidator(44),
|
|
|
|
)
|
|
|
|
self.assertNotEqual(
|
|
|
|
MinValueValidator(45),
|
|
|
|
MinValueValidator(11),
|
|
|
|
)
|
2015-04-14 22:11:12 +00:00
|
|
|
|
|
|
|
def test_decimal_equality(self):
|
|
|
|
self.assertEqual(
|
|
|
|
DecimalValidator(1, 2),
|
|
|
|
DecimalValidator(1, 2),
|
|
|
|
)
|
|
|
|
self.assertNotEqual(
|
|
|
|
DecimalValidator(1, 2),
|
|
|
|
DecimalValidator(1, 1),
|
|
|
|
)
|
|
|
|
self.assertNotEqual(
|
|
|
|
DecimalValidator(1, 2),
|
|
|
|
DecimalValidator(2, 2),
|
|
|
|
)
|
|
|
|
self.assertNotEqual(
|
|
|
|
DecimalValidator(1, 2),
|
|
|
|
MinValueValidator(11),
|
|
|
|
)
|