Refactored validators tests to use subtests.

This commit is contained in:
Tom Forbes 2018-08-18 19:26:20 +01:00 committed by Tim Graham
parent 3e09b37f80
commit 8c70ba92dd

View File

@ -3,7 +3,7 @@ import re
import types import types
from datetime import datetime, timedelta from datetime import datetime, timedelta
from decimal import Decimal from decimal import Decimal
from unittest import TestCase, skipUnless from unittest import TestCase
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
@ -306,43 +306,21 @@ with open(create_path('invalid_urls.txt'), encoding='utf8') as f:
TEST_DATA.append((URLValidator(), url.strip(), ValidationError)) TEST_DATA.append((URLValidator(), url.strip(), ValidationError))
def create_simple_test_method(validator, expected, value, num): class TestValidators(SimpleTestCase):
if expected is not None and issubclass(expected, Exception):
test_mask = 'test_%s_raises_error_%d'
def test_func(self): def test_validators(self):
# assertRaises not used, so as to be able to produce an error message for validator, value, expected in TEST_DATA:
# containing the tested value name = validator.__name__ if isinstance(validator, types.FunctionType) else validator.__class__.__name__
try: exception_expected = expected is not None and issubclass(expected, Exception)
validator(value) with self.subTest(name, value=value):
except expected: if validator is validate_image_file_extension and not PILLOW_IS_INSTALLED:
pass self.skipTest('Pillow is required to test validate_image_file_extension.')
else: if exception_expected:
self.fail("%s not raised when validating '%s'" % ( with self.assertRaises(expected):
expected.__name__, value)) validator(value)
else: else:
test_mask = 'test_%s_%d' self.assertEqual(expected, validator(value))
def test_func(self):
try:
self.assertEqual(expected, validator(value))
except ValidationError as e:
self.fail("Validation of '%s' failed. Error message was: %s" % (
value, str(e)))
if isinstance(validator, types.FunctionType):
val_name = validator.__name__
else:
val_name = validator.__class__.__name__
test_name = test_mask % (val_name, num)
if validator is validate_image_file_extension:
SKIP_MSG = "Pillow is required to test validate_image_file_extension"
test_func = skipUnless(PILLOW_IS_INSTALLED, SKIP_MSG)(test_func)
return test_name, test_func
# Dynamically assemble a test class with the contents of TEST_DATA
class TestSimpleValidators(SimpleTestCase):
def test_single_message(self): def test_single_message(self):
v = ValidationError('Not Valid') v = ValidationError('Not Valid')
self.assertEqual(str(v), "['Not Valid']") self.assertEqual(str(v), "['Not Valid']")
@ -369,13 +347,6 @@ class TestSimpleValidators(SimpleTestCase):
v('djangoproject.com') v('djangoproject.com')
test_counter = 0
for validator, value, expected in TEST_DATA:
name, method = create_simple_test_method(validator, expected, value, test_counter)
setattr(TestSimpleValidators, name, method)
test_counter += 1
class TestValidatorEquality(TestCase): class TestValidatorEquality(TestCase):
""" """
Validators have valid equality operators (#21638) Validators have valid equality operators (#21638)