From f20e7fcdd22fe3a2181071af1f8460bb6c3d32f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Sat, 15 Aug 2009 13:35:43 +0000 Subject: [PATCH] [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 --- django/core/validators.py | 7 ++--- tests/modeltests/validators/tests.py | 38 ++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/django/core/validators.py b/django/core/validators.py index c5e7c23afb..4b9ab1f602 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -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): diff --git a/tests/modeltests/validators/tests.py b/tests/modeltests/validators/tests.py index 3e185d1fc6..de4d94f1af 100644 --- a/tests/modeltests/validators/tests.py +++ b/tests/modeltests/validators/tests.py @@ -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'