1
0
mirror of https://github.com/django/django.git synced 2025-07-07 11:19:12 +00:00

[soc2009/model-validation] Added first validator with tests

No usefull error message yet, validator isn't used anywhere, just a
mockup basically

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@10898 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Honza Král 2009-06-03 02:35:59 +00:00
parent 2f8f8bf531
commit 14f9493380
4 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,7 @@
from django.core.exceptions import ValidationError
def validate_integer(value, all_values={}, model_instance=None):
try:
int(value)
except (ValueError, TypeError), e:
raise ValidationError('')

View File

View File

View File

@ -0,0 +1,33 @@
from unittest import TestCase
from django.core.exceptions import ValidationError
from django.core.validators import validate_integer
class TestSimpleValidators(TestCase):
pass
SIMPLE_VALIDATORS_VALUES = (
(validate_integer, '42', None),
(validate_integer, '-42', None),
(validate_integer, -42, None),
(validate_integer, -42.5, None),
(validate_integer, None, ValidationError),
(validate_integer, 'a', ValidationError),
)
def get_simple_test_func(expected, value, num):
if isinstance(expected, type) and issubclass(expected, ValidationError):
test_mask = 'test_%s_raises_error_%d'
def test_func(self):
self.assertRaises(expected, validator, value)
else:
test_mask = 'test_%s_%d'
def test_func(self):
self.assertEqual(expected, validator(value))
test_name = test_mask % (validator.__name__, num)
return test_name, test_func
test_counter = {}
for validator, value, expected in SIMPLE_VALIDATORS_VALUES:
num = test_counter[validator] = test_counter.setdefault(validator, 0) + 1
setattr(TestSimpleValidators, *get_simple_test_func(expected, value, num))