mirror of
https://github.com/django/django.git
synced 2025-07-06 18:59:13 +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:
parent
7a6a71dd0c
commit
f20e7fcdd2
@ -95,7 +95,7 @@ validate_comma_separated_integer_list = RegexValidator(comma_separated_int_list_
|
|||||||
|
|
||||||
|
|
||||||
class BaseValidator(object):
|
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
|
clean = lambda self, x: x
|
||||||
message = _(u'Ensure this value is %(limit_value)s (it is %(show_value)s).')
|
message = _(u'Ensure this value is %(limit_value)s (it is %(show_value)s).')
|
||||||
code = 'limit_value'
|
code = 'limit_value'
|
||||||
@ -105,11 +105,12 @@ class BaseValidator(object):
|
|||||||
|
|
||||||
def __call__(self, value):
|
def __call__(self, value):
|
||||||
cleaned = self.clean(value)
|
cleaned = self.clean(value)
|
||||||
|
params = {'limit_value': self.limit_value, 'show_value': cleaned}
|
||||||
if self.compare(cleaned, self.limit_value):
|
if self.compare(cleaned, self.limit_value):
|
||||||
raise ValidationError(
|
raise ValidationError(
|
||||||
self.message,
|
self.message % params,
|
||||||
code=self.code,
|
code=self.code,
|
||||||
params={'limit_value': self.limit_value, 'show_value': cleaned}
|
params=params,
|
||||||
)
|
)
|
||||||
|
|
||||||
class MaxValueValidator(BaseValidator):
|
class MaxValueValidator(BaseValidator):
|
||||||
|
@ -2,14 +2,16 @@
|
|||||||
import types
|
import types
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
import re
|
||||||
|
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.core.validators import (
|
from django.core.validators import (
|
||||||
validate_integer, validate_email, validate_slug, validate_ipv4_address,
|
validate_integer, validate_email, validate_slug, validate_ipv4_address,
|
||||||
validate_comma_separated_integer_list, MaxValueValidator,
|
validate_comma_separated_integer_list, MaxValueValidator,
|
||||||
MinValueValidator, MaxLengthValidator, MinLengthValidator,
|
MinValueValidator, MaxLengthValidator, MinLengthValidator,
|
||||||
RequiredIfOtherFieldBlank,
|
RequiredIfOtherFieldBlank, URLValidator, BaseValidator,
|
||||||
)
|
RegexValidator,
|
||||||
|
)
|
||||||
|
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
class TestSimpleValidators(TestCase):
|
class TestSimpleValidators(TestCase):
|
||||||
@ -94,8 +96,40 @@ SIMPLE_VALIDATORS_VALUES = (
|
|||||||
|
|
||||||
(MinLengthValidator(10), '', ValidationError),
|
(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):
|
def get_simple_test_func(validator, expected, value, num):
|
||||||
if isinstance(expected, type) and issubclass(expected, Exception):
|
if isinstance(expected, type) and issubclass(expected, Exception):
|
||||||
test_mask = 'test_%s_raises_error_%d'
|
test_mask = 'test_%s_raises_error_%d'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user