1
0
mirror of https://github.com/django/django.git synced 2025-10-25 22:56:12 +00:00
Files
django/tests/modeltests/validators/tests.py
Honza Král 14f9493380 [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
2009-06-03 02:35:59 +00:00

34 lines
1.1 KiB
Python

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))