mirror of
https://github.com/django/django.git
synced 2025-07-07 19:29: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:
parent
2f8f8bf531
commit
14f9493380
7
django/core/validators.py
Normal file
7
django/core/validators.py
Normal 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('')
|
0
tests/modeltests/validators/__init__.py
Normal file
0
tests/modeltests/validators/__init__.py
Normal file
0
tests/modeltests/validators/models.py
Normal file
0
tests/modeltests/validators/models.py
Normal file
33
tests/modeltests/validators/tests.py
Normal file
33
tests/modeltests/validators/tests.py
Normal 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))
|
Loading…
x
Reference in New Issue
Block a user