From 9dee3b26e58aec526f83c3c81a370764e8fc7bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Mon, 8 Jun 2009 00:56:01 +0000 Subject: [PATCH] [soc2009/model-validation] Added tests for ComplexValidators Also the tests for validators can now verify other exceptions and not just ValidationError git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@10949 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/modeltests/validators/tests.py | 35 ++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/tests/modeltests/validators/tests.py b/tests/modeltests/validators/tests.py index 3a6e223310..7f84475149 100644 --- a/tests/modeltests/validators/tests.py +++ b/tests/modeltests/validators/tests.py @@ -1,12 +1,13 @@ from unittest import TestCase from django.core.exceptions import ValidationError -from django.core.validators import validate_integer, validate_email +from django.core.validators import validate_integer, validate_email, RequiredIfOtherFieldBlank class TestSimpleValidators(TestCase): pass SIMPLE_VALIDATORS_VALUES = ( + # (validator, value, expected), (validate_integer, '42', None), (validate_integer, '-42', None), (validate_integer, -42, None), @@ -23,7 +24,7 @@ SIMPLE_VALIDATORS_VALUES = ( ) def get_simple_test_func(validator, expected, value, num): - if isinstance(expected, type) and issubclass(expected, ValidationError): + if isinstance(expected, type) and issubclass(expected, Exception): test_mask = 'test_%s_raises_error_%d' def test_func(self): self.assertRaises(expected, validator, value) @@ -38,3 +39,33 @@ 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(validator, expected, value, num)) + +class TestComplexValidators(TestCase): + pass + +COMPLEX_VALIDATORS_VALUES = ( + #(validator, value, all_values, obj, expected), + (RequiredIfOtherFieldBlank('other'), 'given', {'other': 'given'}, None, None), + (RequiredIfOtherFieldBlank('other'), '', {'other': 'given'}, None, None), + (RequiredIfOtherFieldBlank('other'), 'given', {}, None, AssertionError), + (RequiredIfOtherFieldBlank('other'), '', {}, None, AssertionError), + (RequiredIfOtherFieldBlank('other'), '', {'other': ''}, None, ValidationError), +) + +def get_complex_test_func(validator, expected, value, all_values, obj, num): + if isinstance(expected, type) and issubclass(expected, Exception): + test_mask = 'test_%s_raises_error_%d' + def test_func(self): + self.assertRaises(expected, validator, value, all_values=all_values, obj=obj) + else: + test_mask = 'test_%s_%d' + def test_func(self): + self.assertEqual(expected, validator(value, all_values=all_values, obj=obj)) + test_name = test_mask % (validator.__class__.__name__, num) + return test_name, test_func + +test_counter = {} +for validator, value, all_values, obj, expected in COMPLEX_VALIDATORS_VALUES: + num = test_counter[validator.__class__.__name__] = test_counter.setdefault(validator.__class__.__name__, 0) + 1 + setattr(TestComplexValidators, *get_complex_test_func(validator, expected, value, all_values, obj, num)) +