1
0
mirror of https://github.com/django/django.git synced 2025-07-06 10:49:17 +00:00

[soc2009/model-validation] simple test for URLValidator, RegexValidator and BaseValidator

URL tests copied from tests/regressiontests/forms/fields.py

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11460 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Honza Král 2009-08-15 13:35:43 +00:00
parent 7a6a71dd0c
commit f20e7fcdd2
2 changed files with 40 additions and 5 deletions

View File

@ -95,7 +95,7 @@ validate_comma_separated_integer_list = RegexValidator(comma_separated_int_list_
class BaseValidator(object):
compare = lambda self, a, b: a is b
compare = lambda self, a, b: a is not b
clean = lambda self, x: x
message = _(u'Ensure this value is %(limit_value)s (it is %(show_value)s).')
code = 'limit_value'
@ -105,11 +105,12 @@ class BaseValidator(object):
def __call__(self, value):
cleaned = self.clean(value)
params = {'limit_value': self.limit_value, 'show_value': cleaned}
if self.compare(cleaned, self.limit_value):
raise ValidationError(
self.message,
self.message % params,
code=self.code,
params={'limit_value': self.limit_value, 'show_value': cleaned}
params=params,
)
class MaxValueValidator(BaseValidator):

View File

@ -2,14 +2,16 @@
import types
from unittest import TestCase
from datetime import datetime, timedelta
import re
from django.core.exceptions import ValidationError
from django.core.validators import (
validate_integer, validate_email, validate_slug, validate_ipv4_address,
validate_comma_separated_integer_list, MaxValueValidator,
MinValueValidator, MaxLengthValidator, MinLengthValidator,
RequiredIfOtherFieldBlank,
)
RequiredIfOtherFieldBlank, URLValidator, BaseValidator,
RegexValidator,
)
now = datetime.now()
class TestSimpleValidators(TestCase):
@ -94,8 +96,40 @@ SIMPLE_VALIDATORS_VALUES = (
(MinLengthValidator(10), '', ValidationError),
(URLValidator(), 'http://www.djangoproject.com/', None),
(URLValidator(), 'http://localhost/', None),
(URLValidator(), 'http://example.com/', None),
(URLValidator(), 'http://www.example.com/', None),
(URLValidator(), 'http://www.example.com:8000/test', None),
(URLValidator(), 'http://valid-with-hyphens.com/', None),
(URLValidator(), 'http://subdomain.domain.com/', None),
(URLValidator(), 'http://200.8.9.10/', None),
(URLValidator(), 'http://200.8.9.10:8000/test', None),
(URLValidator(), 'http://valid-----hyphens.com/', None),
(URLValidator(), 'foo', ValidationError),
(URLValidator(), 'http://', ValidationError),
(URLValidator(), 'http://example', ValidationError),
(URLValidator(), 'http://example.', ValidationError),
(URLValidator(), 'http://.com', ValidationError),
(URLValidator(), 'http://invalid-.com', ValidationError),
(URLValidator(), 'http://-invalid.com', ValidationError),
(URLValidator(), 'http://inv-.alid-.com', ValidationError),
(URLValidator(), 'http://inv-.-alid.com', ValidationError),
(BaseValidator(True), True, None),
(BaseValidator(True), False, ValidationError),
(RegexValidator('.*'), '', None),
(RegexValidator(re.compile('.*')), '', None),
(RegexValidator('.*'), 'xxxxx', None),
(RegexValidator('x'), 'y', ValidationError),
(RegexValidator(re.compile('x')), 'y', ValidationError),
)
def get_simple_test_func(validator, expected, value, num):
if isinstance(expected, type) and issubclass(expected, Exception):
test_mask = 'test_%s_raises_error_%d'