Refs #27804 -- Used subTest() in several tests.

This commit is contained in:
Bruno Alla 2017-03-07 21:00:43 +00:00 committed by Tim Graham
parent 91b2bc3e70
commit 6092ea8fa6
19 changed files with 324 additions and 262 deletions

View File

@ -339,4 +339,5 @@ class AdminDocViewFunctionsTests(SimpleTestCase):
(r'^a/?$', '/a/'), (r'^a/?$', '/a/'),
) )
for pattern, output in tests: for pattern, output in tests:
self.assertEqual(simplify_regex(pattern), output) with self.subTest(pattern=pattern):
self.assertEqual(simplify_regex(pattern), output)

View File

@ -47,32 +47,35 @@ class MultiDatabaseTests(TestCase):
@mock.patch('django.contrib.admin.options.transaction') @mock.patch('django.contrib.admin.options.transaction')
def test_add_view(self, mock): def test_add_view(self, mock):
for db in connections: for db in connections:
Router.target_db = db with self.subTest(db=db):
self.client.force_login(self.superusers[db]) Router.target_db = db
self.client.post( self.client.force_login(self.superusers[db])
reverse('test_adminsite:admin_views_book_add'), self.client.post(
{'name': 'Foobar: 5th edition'}, reverse('test_adminsite:admin_views_book_add'),
) {'name': 'Foobar: 5th edition'},
mock.atomic.assert_called_with(using=db) )
mock.atomic.assert_called_with(using=db)
@mock.patch('django.contrib.admin.options.transaction') @mock.patch('django.contrib.admin.options.transaction')
def test_change_view(self, mock): def test_change_view(self, mock):
for db in connections: for db in connections:
Router.target_db = db with self.subTest(db=db):
self.client.force_login(self.superusers[db]) Router.target_db = db
self.client.post( self.client.force_login(self.superusers[db])
reverse('test_adminsite:admin_views_book_change', args=[self.test_book_ids[db]]), self.client.post(
{'name': 'Test Book 2: Test more'}, reverse('test_adminsite:admin_views_book_change', args=[self.test_book_ids[db]]),
) {'name': 'Test Book 2: Test more'},
mock.atomic.assert_called_with(using=db) )
mock.atomic.assert_called_with(using=db)
@mock.patch('django.contrib.admin.options.transaction') @mock.patch('django.contrib.admin.options.transaction')
def test_delete_view(self, mock): def test_delete_view(self, mock):
for db in connections: for db in connections:
Router.target_db = db with self.subTest(db=db):
self.client.force_login(self.superusers[db]) Router.target_db = db
self.client.post( self.client.force_login(self.superusers[db])
reverse('test_adminsite:admin_views_book_delete', args=[self.test_book_ids[db]]), self.client.post(
{'post': 'yes'}, reverse('test_adminsite:admin_views_book_delete', args=[self.test_book_ids[db]]),
) {'post': 'yes'},
mock.atomic.assert_called_with(using=db) )
mock.atomic.assert_called_with(using=db)

View File

@ -42,11 +42,12 @@ class MultiDatabaseTests(TestCase):
@mock.patch('django.contrib.auth.admin.transaction') @mock.patch('django.contrib.auth.admin.transaction')
def test_add_view(self, mock): def test_add_view(self, mock):
for db in connections: for db in connections:
Router.target_db = db with self.subTest(db_connection=db):
self.client.force_login(self.superusers[db]) Router.target_db = db
self.client.post(reverse('test_adminsite:auth_user_add'), { self.client.force_login(self.superusers[db])
'username': 'some_user', self.client.post(reverse('test_adminsite:auth_user_add'), {
'password1': 'helloworld', 'username': 'some_user',
'password2': 'helloworld', 'password1': 'helloworld',
}) 'password2': 'helloworld',
mock.atomic.assert_called_with(using=db) })
mock.atomic.assert_called_with(using=db)

View File

@ -300,13 +300,14 @@ class TestUtilsHashPass(SimpleTestCase):
def test_upgrade(self): def test_upgrade(self):
self.assertEqual('pbkdf2_sha256', get_hasher('default').algorithm) self.assertEqual('pbkdf2_sha256', get_hasher('default').algorithm)
for algo in ('sha1', 'md5'): for algo in ('sha1', 'md5'):
encoded = make_password('lètmein', hasher=algo) with self.subTest(algo=algo):
state = {'upgraded': False} encoded = make_password('lètmein', hasher=algo)
state = {'upgraded': False}
def setter(password): def setter(password):
state['upgraded'] = True state['upgraded'] = True
self.assertTrue(check_password('lètmein', encoded, setter)) self.assertTrue(check_password('lètmein', encoded, setter))
self.assertTrue(state['upgraded']) self.assertTrue(state['upgraded'])
def test_no_upgrade(self): def test_no_upgrade(self):
encoded = make_password('lètmein') encoded = make_password('lètmein')
@ -327,13 +328,14 @@ class TestUtilsHashPass(SimpleTestCase):
def test_no_upgrade_on_incorrect_pass(self): def test_no_upgrade_on_incorrect_pass(self):
self.assertEqual('pbkdf2_sha256', get_hasher('default').algorithm) self.assertEqual('pbkdf2_sha256', get_hasher('default').algorithm)
for algo in ('sha1', 'md5'): for algo in ('sha1', 'md5'):
encoded = make_password('lètmein', hasher=algo) with self.subTest(algo=algo):
state = {'upgraded': False} encoded = make_password('lètmein', hasher=algo)
state = {'upgraded': False}
def setter(): def setter():
state['upgraded'] = True state['upgraded'] = True
self.assertFalse(check_password('WRONG', encoded, setter)) self.assertFalse(check_password('WRONG', encoded, setter))
self.assertFalse(state['upgraded']) self.assertFalse(state['upgraded'])
def test_pbkdf2_upgrade(self): def test_pbkdf2_upgrade(self):
hasher = get_hasher('default') hasher = get_hasher('default')

View File

@ -161,7 +161,7 @@ class AbstractBaseUserTests(TestCase):
# The normalization happens in AbstractBaseUser.clean() # The normalization happens in AbstractBaseUser.clean()
ohm_username = 'iamtheΩ' # U+2126 OHM SIGN ohm_username = 'iamtheΩ' # U+2126 OHM SIGN
for model in ('auth.User', 'auth_tests.CustomUser'): for model in ('auth.User', 'auth_tests.CustomUser'):
with self.settings(AUTH_USER_MODEL=model): with self.subTest(model=model), self.settings(AUTH_USER_MODEL=model):
User = get_user_model() User = get_user_model()
user = User(**{User.USERNAME_FIELD: ohm_username, 'password': 'foo'}) user = User(**{User.USERNAME_FIELD: ohm_username, 'password': 'foo'})
user.clean() user.clean()

View File

@ -214,17 +214,21 @@ class UsernameValidatorsTests(TestCase):
] ]
v = validators.UnicodeUsernameValidator() v = validators.UnicodeUsernameValidator()
for valid in valid_usernames: for valid in valid_usernames:
v(valid) with self.subTest(valid=valid):
v(valid)
for invalid in invalid_usernames: for invalid in invalid_usernames:
with self.assertRaises(ValidationError): with self.subTest(invalid=invalid):
v(invalid) with self.assertRaises(ValidationError):
v(invalid)
def test_ascii_validator(self): def test_ascii_validator(self):
valid_usernames = ['glenn', 'GLEnN', 'jean-marc'] valid_usernames = ['glenn', 'GLEnN', 'jean-marc']
invalid_usernames = ["o'connell", 'Éric', 'jean marc', "أحمد"] invalid_usernames = ["o'connell", 'Éric', 'jean marc', "أحمد"]
v = validators.ASCIIUsernameValidator() v = validators.ASCIIUsernameValidator()
for valid in valid_usernames: for valid in valid_usernames:
v(valid) with self.subTest(valid=valid):
v(valid)
for invalid in invalid_usernames: for invalid in invalid_usernames:
with self.assertRaises(ValidationError): with self.subTest(invalid=invalid):
v(invalid) with self.assertRaises(ValidationError):
v(invalid)

View File

@ -108,10 +108,11 @@ class AuthViewNamedURLTests(AuthViewsTestCase):
('password_reset_complete', [], {}), ('password_reset_complete', [], {}),
] ]
for name, args, kwargs in expected_named_urls: for name, args, kwargs in expected_named_urls:
try: with self.subTest(name=name):
reverse(name, args=args, kwargs=kwargs) try:
except NoReverseMatch: reverse(name, args=args, kwargs=kwargs)
self.fail("Reversal of url named '%s' failed with NoReverseMatch" % name) except NoReverseMatch:
self.fail("Reversal of url named '%s' failed with NoReverseMatch" % name)
class PasswordResetTest(AuthViewsTestCase): class PasswordResetTest(AuthViewsTestCase):
@ -559,48 +560,54 @@ class LoginTest(AuthViewsTestCase):
def test_security_check(self): def test_security_check(self):
login_url = reverse('login') login_url = reverse('login')
# Those URLs should not pass the security check # These URLs should not pass the security check.
for bad_url in ('http://example.com', bad_urls = (
'http:///example.com', 'http://example.com',
'https://example.com', 'http:///example.com',
'ftp://example.com', 'https://example.com',
'///example.com', 'ftp://example.com',
'//example.com', '///example.com',
'javascript:alert("XSS")'): '//example.com',
'javascript:alert("XSS")',
)
for bad_url in bad_urls:
with self.subTest(bad_url=bad_url):
nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
'url': login_url,
'next': REDIRECT_FIELD_NAME,
'bad_url': quote(bad_url),
}
response = self.client.post(nasty_url, {
'username': 'testclient',
'password': 'password',
})
self.assertEqual(response.status_code, 302)
self.assertNotIn(bad_url, response.url, '%s should be blocked' % bad_url)
nasty_url = '%(url)s?%(next)s=%(bad_url)s' % { # These URLs should pass the security check.
'url': login_url, good_urls = (
'next': REDIRECT_FIELD_NAME, '/view/?param=http://example.com',
'bad_url': quote(bad_url), '/view/?param=https://example.com',
} '/view?param=ftp://example.com',
response = self.client.post(nasty_url, { 'view/?param=//example.com',
'username': 'testclient', 'https://testserver/',
'password': 'password', 'HTTPS://testserver/',
}) '//testserver/',
self.assertEqual(response.status_code, 302) '/url%20with%20spaces/',
self.assertNotIn(bad_url, response.url, )
"%s should be blocked" % bad_url) for good_url in good_urls:
with self.subTest(good_url=good_url):
# These URLs *should* still pass the security check safe_url = '%(url)s?%(next)s=%(good_url)s' % {
for good_url in ('/view/?param=http://example.com', 'url': login_url,
'/view/?param=https://example.com', 'next': REDIRECT_FIELD_NAME,
'/view?param=ftp://example.com', 'good_url': quote(good_url),
'view/?param=//example.com', }
'https://testserver/', response = self.client.post(safe_url, {
'HTTPS://testserver/', 'username': 'testclient',
'//testserver/', 'password': 'password',
'/url%20with%20spaces/'): # see ticket #12534 })
safe_url = '%(url)s?%(next)s=%(good_url)s' % { self.assertEqual(response.status_code, 302)
'url': login_url, self.assertIn(good_url, response.url, '%s should be allowed' % good_url)
'next': REDIRECT_FIELD_NAME,
'good_url': quote(good_url),
}
response = self.client.post(safe_url, {
'username': 'testclient',
'password': 'password',
})
self.assertEqual(response.status_code, 302)
self.assertIn(good_url, response.url, "%s should be allowed" % good_url)
def test_security_check_https(self): def test_security_check_https(self):
login_url = reverse('login') login_url = reverse('login')
@ -988,45 +995,52 @@ class LogoutTest(AuthViewsTestCase):
def test_security_check(self): def test_security_check(self):
logout_url = reverse('logout') logout_url = reverse('logout')
# Those URLs should not pass the security check # These URLs should not pass the security check.
for bad_url in ('http://example.com', bad_urls = (
'http:///example.com', 'http://example.com',
'https://example.com', 'http:///example.com',
'ftp://example.com', 'https://example.com',
'///example.com', 'ftp://example.com',
'//example.com', '///example.com',
'javascript:alert("XSS")'): '//example.com',
nasty_url = '%(url)s?%(next)s=%(bad_url)s' % { 'javascript:alert("XSS")',
'url': logout_url, )
'next': REDIRECT_FIELD_NAME, for bad_url in bad_urls:
'bad_url': quote(bad_url), with self.subTest(bad_url=bad_url):
} nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
self.login() 'url': logout_url,
response = self.client.get(nasty_url) 'next': REDIRECT_FIELD_NAME,
self.assertEqual(response.status_code, 302) 'bad_url': quote(bad_url),
self.assertNotIn(bad_url, response.url, }
"%s should be blocked" % bad_url) self.login()
self.confirm_logged_out() response = self.client.get(nasty_url)
self.assertEqual(response.status_code, 302)
self.assertNotIn(bad_url, response.url, '%s should be blocked' % bad_url)
self.confirm_logged_out()
# These URLs *should* still pass the security check # These URLs should pass the security check.
for good_url in ('/view/?param=http://example.com', good_urls = (
'/view/?param=https://example.com', '/view/?param=http://example.com',
'/view?param=ftp://example.com', '/view/?param=https://example.com',
'view/?param=//example.com', '/view?param=ftp://example.com',
'https://testserver/', 'view/?param=//example.com',
'HTTPS://testserver/', 'https://testserver/',
'//testserver/', 'HTTPS://testserver/',
'/url%20with%20spaces/'): # see ticket #12534 '//testserver/',
safe_url = '%(url)s?%(next)s=%(good_url)s' % { '/url%20with%20spaces/',
'url': logout_url, )
'next': REDIRECT_FIELD_NAME, for good_url in good_urls:
'good_url': quote(good_url), with self.subTest(good_url=good_url):
} safe_url = '%(url)s?%(next)s=%(good_url)s' % {
self.login() 'url': logout_url,
response = self.client.get(safe_url) 'next': REDIRECT_FIELD_NAME,
self.assertEqual(response.status_code, 302) 'good_url': quote(good_url),
self.assertIn(good_url, response.url, "%s should be allowed" % good_url) }
self.confirm_logged_out() self.login()
response = self.client.get(safe_url)
self.assertEqual(response.status_code, 302)
self.assertIn(good_url, response.url, '%s should be allowed' % good_url)
self.confirm_logged_out()
def test_security_check_https(self): def test_security_check_https(self):
logout_url = reverse('logout') logout_url = reverse('logout')

67
tests/cache/tests.py vendored
View File

@ -169,8 +169,9 @@ class DummyCacheTests(SimpleTestCase):
'ascii2': {'x': 1} 'ascii2': {'x': 1}
} }
for (key, value) in stuff.items(): for (key, value) in stuff.items():
cache.set(key, value) with self.subTest(key=key):
self.assertIsNone(cache.get(key)) cache.set(key, value)
self.assertIsNone(cache.get(key))
def test_set_many(self): def test_set_many(self):
"set_many does nothing for the dummy cache backend" "set_many does nothing for the dummy cache backend"
@ -420,21 +421,24 @@ class BaseCacheTests:
} }
# Test `set` # Test `set`
for (key, value) in stuff.items(): for (key, value) in stuff.items():
cache.set(key, value) with self.subTest(key=key):
self.assertEqual(cache.get(key), value) cache.set(key, value)
self.assertEqual(cache.get(key), value)
# Test `add` # Test `add`
for (key, value) in stuff.items(): for (key, value) in stuff.items():
cache.delete(key) with self.subTest(key=key):
cache.add(key, value) cache.delete(key)
self.assertEqual(cache.get(key), value) cache.add(key, value)
self.assertEqual(cache.get(key), value)
# Test `set_many` # Test `set_many`
for (key, value) in stuff.items(): for (key, value) in stuff.items():
cache.delete(key) cache.delete(key)
cache.set_many(stuff) cache.set_many(stuff)
for (key, value) in stuff.items(): for (key, value) in stuff.items():
self.assertEqual(cache.get(key), value) with self.subTest(key=key):
self.assertEqual(cache.get(key), value)
def test_binary_string(self): def test_binary_string(self):
# Binary strings should be cacheable # Binary strings should be cacheable
@ -1151,9 +1155,10 @@ class BaseMemcachedTests(BaseCacheTests):
'server1.tld,server2:11211', 'server1.tld,server2:11211',
] ]
for location in locations: for location in locations:
params = {'BACKEND': self.base_params['BACKEND'], 'LOCATION': location} with self.subTest(location=location):
with self.settings(CACHES={'default': params}): params = {'BACKEND': self.base_params['BACKEND'], 'LOCATION': location}
self.assertEqual(cache._servers, ['server1.tld', 'server2:11211']) with self.settings(CACHES={'default': params}):
self.assertEqual(cache._servers, ['server1.tld', 'server2:11211'])
def test_invalid_key_characters(self): def test_invalid_key_characters(self):
""" """
@ -1250,7 +1255,8 @@ class MemcachedCacheTests(BaseMemcachedTests, TestCase):
def test_memcached_uses_highest_pickle_version(self): def test_memcached_uses_highest_pickle_version(self):
# Regression test for #19810 # Regression test for #19810
for cache_key in settings.CACHES: for cache_key in settings.CACHES:
self.assertEqual(caches[cache_key]._cache.pickleProtocol, pickle.HIGHEST_PROTOCOL) with self.subTest(cache_key=cache_key):
self.assertEqual(caches[cache_key]._cache.pickleProtocol, pickle.HIGHEST_PROTOCOL)
@override_settings(CACHES=caches_setting_for_tests( @override_settings(CACHES=caches_setting_for_tests(
base=MemcachedCache_params, base=MemcachedCache_params,
@ -1521,11 +1527,12 @@ class CacheUtils(SimpleTestCase):
('Cookie , Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'), ('Cookie , Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'),
) )
for initial_vary, newheaders, resulting_vary in headers: for initial_vary, newheaders, resulting_vary in headers:
response = HttpResponse() with self.subTest(initial_vary=initial_vary, newheaders=newheaders):
if initial_vary is not None: response = HttpResponse()
response['Vary'] = initial_vary if initial_vary is not None:
patch_vary_headers(response, newheaders) response['Vary'] = initial_vary
self.assertEqual(response['Vary'], resulting_vary) patch_vary_headers(response, newheaders)
self.assertEqual(response['Vary'], resulting_vary)
def test_get_cache_key(self): def test_get_cache_key(self):
request = self.factory.get(self.path) request = self.factory.get(self.path)
@ -1605,12 +1612,13 @@ class CacheUtils(SimpleTestCase):
cc_delim_re = re.compile(r'\s*,\s*') cc_delim_re = re.compile(r'\s*,\s*')
for initial_cc, newheaders, expected_cc in tests: for initial_cc, newheaders, expected_cc in tests:
response = HttpResponse() with self.subTest(initial_cc=initial_cc, newheaders=newheaders):
if initial_cc is not None: response = HttpResponse()
response['Cache-Control'] = initial_cc if initial_cc is not None:
patch_cache_control(response, **newheaders) response['Cache-Control'] = initial_cc
parts = set(cc_delim_re.split(response['Cache-Control'])) patch_cache_control(response, **newheaders)
self.assertEqual(parts, expected_cc) parts = set(cc_delim_re.split(response['Cache-Control']))
self.assertEqual(parts, expected_cc)
@override_settings( @override_settings(
@ -2179,12 +2187,13 @@ class TestWithTemplateResponse(SimpleTestCase):
('Cookie , Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'), ('Cookie , Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'),
) )
for initial_vary, newheaders, resulting_vary in headers: for initial_vary, newheaders, resulting_vary in headers:
template = engines['django'].from_string("This is a test") with self.subTest(initial_vary=initial_vary, newheaders=newheaders):
response = TemplateResponse(HttpRequest(), template) template = engines['django'].from_string("This is a test")
if initial_vary is not None: response = TemplateResponse(HttpRequest(), template)
response['Vary'] = initial_vary if initial_vary is not None:
patch_vary_headers(response, newheaders) response['Vary'] = initial_vary
self.assertEqual(response['Vary'], resulting_vary) patch_vary_headers(response, newheaders)
self.assertEqual(response['Vary'], resulting_vary)
def test_get_cache_key(self): def test_get_cache_key(self):
request = self.factory.get(self.path) request = self.factory.get(self.path)

View File

@ -45,9 +45,10 @@ class ContentTypesViewsTests(TestCase):
def test_shortcut_with_absolute_url(self): def test_shortcut_with_absolute_url(self):
"Can view a shortcut for an Author object that has a get_absolute_url method" "Can view a shortcut for an Author object that has a get_absolute_url method"
for obj in Author.objects.all(): for obj in Author.objects.all():
short_url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, obj.pk) with self.subTest(obj=obj):
response = self.client.get(short_url) short_url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, obj.pk)
self.assertRedirects(response, 'http://testserver%s' % obj.get_absolute_url(), target_status_code=404) response = self.client.get(short_url)
self.assertRedirects(response, 'http://testserver%s' % obj.get_absolute_url(), target_status_code=404)
def test_shortcut_with_absolute_url_including_scheme(self): def test_shortcut_with_absolute_url_including_scheme(self):
""" """
@ -55,9 +56,10 @@ class ContentTypesViewsTests(TestCase):
the tested URLs are: "http://...", "https://..." and "//..." the tested URLs are: "http://...", "https://..." and "//..."
""" """
for obj in SchemeIncludedURL.objects.all(): for obj in SchemeIncludedURL.objects.all():
short_url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(SchemeIncludedURL).id, obj.pk) with self.subTest(obj=obj):
response = self.client.get(short_url) short_url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(SchemeIncludedURL).id, obj.pk)
self.assertRedirects(response, obj.get_absolute_url(), fetch_redirect_response=False) response = self.client.get(short_url)
self.assertRedirects(response, obj.get_absolute_url(), fetch_redirect_response=False)
def test_shortcut_no_absolute_url(self): def test_shortcut_no_absolute_url(self):
""" """
@ -65,9 +67,10 @@ class ContentTypesViewsTests(TestCase):
404. 404.
""" """
for obj in Article.objects.all(): for obj in Article.objects.all():
short_url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Article).id, obj.pk) with self.subTest(obj=obj):
response = self.client.get(short_url) short_url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Article).id, obj.pk)
self.assertEqual(response.status_code, 404) response = self.client.get(short_url)
self.assertEqual(response.status_code, 404)
def test_wrong_type_pk(self): def test_wrong_type_pk(self):
short_url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, 'nobody/expects') short_url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, 'nobody/expects')

View File

@ -42,41 +42,44 @@ class CustomManagerTests(TestCase):
default Manager. default Manager.
""" """
for manager_name in self.custom_manager_names: for manager_name in self.custom_manager_names:
manager = getattr(Person, manager_name) with self.subTest(manager_name=manager_name):
manager = getattr(Person, manager_name)
# Public methods are copied # Public methods are copied
manager.public_method() manager.public_method()
# Private methods are not copied # Private methods are not copied
with self.assertRaises(AttributeError): with self.assertRaises(AttributeError):
manager._private_method() manager._private_method()
def test_manager_honors_queryset_only(self): def test_manager_honors_queryset_only(self):
for manager_name in self.custom_manager_names: for manager_name in self.custom_manager_names:
manager = getattr(Person, manager_name) with self.subTest(manager_name=manager_name):
# Methods with queryset_only=False are copied even if they are private. manager = getattr(Person, manager_name)
manager._optin_private_method() # Methods with queryset_only=False are copied even if they are private.
# Methods with queryset_only=True aren't copied even if they are public. manager._optin_private_method()
with self.assertRaises(AttributeError): # Methods with queryset_only=True aren't copied even if they are public.
manager.optout_public_method() with self.assertRaises(AttributeError):
manager.optout_public_method()
def test_manager_use_queryset_methods(self): def test_manager_use_queryset_methods(self):
""" """
Custom manager will use the queryset methods Custom manager will use the queryset methods
""" """
for manager_name in self.custom_manager_names: for manager_name in self.custom_manager_names:
manager = getattr(Person, manager_name) with self.subTest(manager_name=manager_name):
queryset = manager.filter() manager = getattr(Person, manager_name)
self.assertQuerysetEqual(queryset, ["Bugs Bunny"], str) queryset = manager.filter()
self.assertIs(queryset._filter_CustomQuerySet, True) self.assertQuerysetEqual(queryset, ["Bugs Bunny"], str)
self.assertIs(queryset._filter_CustomQuerySet, True)
# Specialized querysets inherit from our custom queryset. # Specialized querysets inherit from our custom queryset.
queryset = manager.values_list('first_name', flat=True).filter() queryset = manager.values_list('first_name', flat=True).filter()
self.assertEqual(list(queryset), ["Bugs"]) self.assertEqual(list(queryset), ["Bugs"])
self.assertIs(queryset._filter_CustomQuerySet, True) self.assertIs(queryset._filter_CustomQuerySet, True)
self.assertIsInstance(queryset.values(), CustomQuerySet) self.assertIsInstance(queryset.values(), CustomQuerySet)
self.assertIsInstance(queryset.values().values(), CustomQuerySet) self.assertIsInstance(queryset.values().values(), CustomQuerySet)
self.assertIsInstance(queryset.values_list().values(), CustomQuerySet) self.assertIsInstance(queryset.values_list().values(), CustomQuerySet)
def test_init_args(self): def test_init_args(self):
""" """

View File

@ -54,9 +54,9 @@ class DBTypeCasts(unittest.TestCase):
def test_typeCasts(self): def test_typeCasts(self):
for k, v in TEST_CASES.items(): for k, v in TEST_CASES.items():
for inpt, expected in v: for inpt, expected in v:
got = getattr(typecasts, k)(inpt) with self.subTest(k=k, inpt=inpt):
self.assertEqual( got = getattr(typecasts, k)(inpt)
got, self.assertEqual(
expected, got, expected,
"In %s: %r doesn't match %r. Got %r instead." % (k, inpt, expected, got) "In %s: %r doesn't match %r. Got %r instead." % (k, inpt, expected, got)
) )

View File

@ -38,8 +38,9 @@ class FilePathFieldTest(SimpleTestCase):
('/django/forms/widgets.py', 'widgets.py') ('/django/forms/widgets.py', 'widgets.py')
] ]
for exp, got in zip(expected, fix_os_paths(f.choices)): for exp, got in zip(expected, fix_os_paths(f.choices)):
self.assertEqual(exp[1], got[1]) with self.subTest(expected=exp):
self.assertTrue(got[0].endswith(exp[0])) self.assertEqual(exp[1], got[1])
self.assertTrue(got[0].endswith(exp[0]))
msg = "'Select a valid choice. fields.py is not one of the available choices.'" msg = "'Select a valid choice. fields.py is not one of the available choices.'"
with self.assertRaisesMessage(ValidationError, msg): with self.assertRaisesMessage(ValidationError, msg):
f.clean('fields.py') f.clean('fields.py')
@ -61,8 +62,9 @@ class FilePathFieldTest(SimpleTestCase):
('/django/forms/widgets.py', 'widgets.py') ('/django/forms/widgets.py', 'widgets.py')
] ]
for exp, got in zip(expected, fix_os_paths(f.choices)): for exp, got in zip(expected, fix_os_paths(f.choices)):
self.assertEqual(exp[1], got[1]) with self.subTest(expected=exp):
self.assertTrue(got[0].endswith(exp[0])) self.assertEqual(exp[1], got[1])
self.assertTrue(got[0].endswith(exp[0]))
def test_filepathfield_4(self): def test_filepathfield_4(self):
path = os.path.dirname(os.path.abspath(forms.__file__)) + '/' path = os.path.dirname(os.path.abspath(forms.__file__)) + '/'
@ -80,8 +82,9 @@ class FilePathFieldTest(SimpleTestCase):
('/django/forms/widgets.py', 'widgets.py') ('/django/forms/widgets.py', 'widgets.py')
] ]
for exp, got in zip(expected, fix_os_paths(f.choices)): for exp, got in zip(expected, fix_os_paths(f.choices)):
self.assertEqual(exp[1], got[1]) with self.subTest(expected=exp):
self.assertTrue(got[0].endswith(exp[0])) self.assertEqual(exp[1], got[1])
self.assertTrue(got[0].endswith(exp[0]))
def test_filepathfield_folders(self): def test_filepathfield_folders(self):
path = os.path.abspath(os.path.join(__file__, '..', '..')) + '/tests/filepath_test_files/' path = os.path.abspath(os.path.join(__file__, '..', '..')) + '/tests/filepath_test_files/'
@ -93,8 +96,9 @@ class FilePathFieldTest(SimpleTestCase):
actual = fix_os_paths(f.choices) actual = fix_os_paths(f.choices)
self.assertEqual(len(expected), len(actual)) self.assertEqual(len(expected), len(actual))
for exp, got in zip(expected, actual): for exp, got in zip(expected, actual):
self.assertEqual(exp[1], got[1]) with self.subTest(expected=exp):
self.assertTrue(got[0].endswith(exp[0])) self.assertEqual(exp[1], got[1])
self.assertTrue(got[0].endswith(exp[0]))
f = FilePathField(path=path, allow_folders=True, allow_files=True) f = FilePathField(path=path, allow_folders=True, allow_files=True)
f.choices.sort() f.choices.sort()
@ -110,5 +114,6 @@ class FilePathFieldTest(SimpleTestCase):
actual = fix_os_paths(f.choices) actual = fix_os_paths(f.choices)
self.assertEqual(len(expected), len(actual)) self.assertEqual(len(expected), len(actual))
for exp, got in zip(expected, actual): for exp, got in zip(expected, actual):
self.assertEqual(exp[1], got[1]) with self.subTest(expected=exp):
self.assertTrue(got[0].endswith(exp[0])) self.assertEqual(exp[1], got[1])
self.assertTrue(got[0].endswith(exp[0]))

View File

@ -127,8 +127,9 @@ class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
'http://العربية.idn.icann.org/', 'http://العربية.idn.icann.org/',
) )
for url in urls: for url in urls:
# Valid IDN with self.subTest(url=url):
self.assertEqual(url, f.clean(url)) # Valid IDN
self.assertEqual(url, f.clean(url))
def test_urlfield_10(self): def test_urlfield_10(self):
"""URLField correctly validates IPv6 (#18779).""" """URLField correctly validates IPv6 (#18779)."""
@ -138,7 +139,8 @@ class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
'http://[a34:9238::]:8080/', 'http://[a34:9238::]:8080/',
) )
for url in urls: for url in urls:
self.assertEqual(url, f.clean(url)) with self.subTest(url=url):
self.assertEqual(url, f.clean(url))
def test_urlfield_not_string(self): def test_urlfield_not_string(self):
f = URLField(required=False) f = URLField(required=False)

View File

@ -3008,7 +3008,8 @@ Good luck picking a username that doesn&#39;t already exist.</p>
] ]
for args, kwargs, expected in testcases: for args, kwargs, expected in testcases:
self.assertHTMLEqual(boundfield.label_tag(*args, **kwargs), expected) with self.subTest(args=args, kwargs=kwargs):
self.assertHTMLEqual(boundfield.label_tag(*args, **kwargs), expected)
def test_boundfield_label_tag_no_id(self): def test_boundfield_label_tag_no_id(self):
""" """

View File

@ -335,12 +335,12 @@ class EmptyLabelTestCase(TestCase):
] ]
for form, key, expected in tests: for form, key, expected in tests:
f = form({'name': 'some-key', key: ''}) with self.subTest(form=form):
self.assertTrue(f.is_valid()) f = form({'name': 'some-key', key: ''})
m = f.save() self.assertTrue(f.is_valid())
self.assertEqual(expected, getattr(m, key)) m = f.save()
self.assertEqual('No Preference', self.assertEqual(expected, getattr(m, key))
getattr(m, 'get_{}_display'.format(key))()) self.assertEqual('No Preference', getattr(m, 'get_{}_display'.format(key))())
def test_empty_field_integer(self): def test_empty_field_integer(self):
f = EmptyIntegerLabelChoiceForm() f = EmptyIntegerLabelChoiceForm()

View File

@ -68,32 +68,35 @@ class DistanceTest(TestCase):
qs1 = SouthTexasCity.objects.filter(point__dwithin=(self.stx_pnt, dist1)) qs1 = SouthTexasCity.objects.filter(point__dwithin=(self.stx_pnt, dist1))
qs2 = SouthTexasCityFt.objects.filter(point__dwithin=(self.stx_pnt, dist2)) qs2 = SouthTexasCityFt.objects.filter(point__dwithin=(self.stx_pnt, dist2))
for qs in qs1, qs2: for qs in qs1, qs2:
self.assertEqual(tx_cities, self.get_names(qs)) with self.subTest(dist=dist, qs=qs):
self.assertEqual(tx_cities, self.get_names(qs))
# Now performing the `dwithin` queries on a geodetic coordinate system. # Now performing the `dwithin` queries on a geodetic coordinate system.
for dist in au_dists: for dist in au_dists:
if isinstance(dist, D) and not oracle: with self.subTest(dist=dist):
type_error = True if isinstance(dist, D) and not oracle:
else: type_error = True
type_error = False
if isinstance(dist, tuple):
if oracle or spatialite:
# Result in meters
dist = dist[1]
else: else:
# Result in units of the field type_error = False
dist = dist[0]
# Creating the query set. if isinstance(dist, tuple):
qs = AustraliaCity.objects.order_by('name') if oracle or spatialite:
if type_error: # Result in meters
# A ValueError should be raised on PostGIS when trying to pass dist = dist[1]
# Distance objects into a DWithin query using a geodetic field. else:
with self.assertRaises(ValueError): # Result in units of the field
AustraliaCity.objects.filter(point__dwithin=(self.au_pnt, dist)).count() dist = dist[0]
else:
self.assertEqual(au_cities, self.get_names(qs.filter(point__dwithin=(self.au_pnt, dist)))) # Creating the query set.
qs = AustraliaCity.objects.order_by('name')
if type_error:
# A ValueError should be raised on PostGIS when trying to
# pass Distance objects into a DWithin query using a
# geodetic field.
with self.assertRaises(ValueError):
AustraliaCity.objects.filter(point__dwithin=(self.au_pnt, dist)).count()
else:
self.assertEqual(au_cities, self.get_names(qs.filter(point__dwithin=(self.au_pnt, dist))))
@skipUnlessDBFeature("supports_distances_lookups") @skipUnlessDBFeature("supports_distances_lookups")
def test_distance_lookups(self): def test_distance_lookups(self):
@ -298,8 +301,9 @@ class DistanceFunctionsTests(TestCase):
# Ensuring expected distances are returned for each distance queryset. # Ensuring expected distances are returned for each distance queryset.
for qs in dist_qs: for qs in dist_qs:
for i, c in enumerate(qs): for i, c in enumerate(qs):
self.assertAlmostEqual(m_distances[i], c.distance.m, tol) with self.subTest(c=c):
self.assertAlmostEqual(ft_distances[i], c.distance.survey_ft, tol) self.assertAlmostEqual(m_distances[i], c.distance.m, tol)
self.assertAlmostEqual(ft_distances[i], c.distance.survey_ft, tol)
@skipUnlessDBFeature("has_Distance_function", "supports_distance_geodetic") @skipUnlessDBFeature("has_Distance_function", "supports_distance_geodetic")
def test_distance_geodetic(self): def test_distance_geodetic(self):
@ -318,9 +322,10 @@ class DistanceFunctionsTests(TestCase):
40435.4335201384, 0, 68272.3896586844, 12375.0643697706, 0] 40435.4335201384, 0, 68272.3896586844, 12375.0643697706, 0]
qs = AustraliaCity.objects.annotate(distance=Distance('point', ls)).order_by('name') qs = AustraliaCity.objects.annotate(distance=Distance('point', ls)).order_by('name')
for city, distance in zip(qs, distances): for city, distance in zip(qs, distances):
# Testing equivalence to within a meter (kilometer on SpatiaLite). with self.subTest(city=city, distance=distance):
tol = -3 if spatialite else 0 # Testing equivalence to within a meter (kilometer on SpatiaLite).
self.assertAlmostEqual(distance, city.distance.m, tol) tol = -3 if spatialite else 0
self.assertAlmostEqual(distance, city.distance.m, tol)
@skipUnlessDBFeature("has_Distance_function", "supports_distance_geodetic") @skipUnlessDBFeature("has_Distance_function", "supports_distance_geodetic")
def test_distance_geodetic_spheroid(self): def test_distance_geodetic_spheroid(self):
@ -349,14 +354,16 @@ class DistanceFunctionsTests(TestCase):
distance=Distance('point', hillsdale.point, spheroid=True) distance=Distance('point', hillsdale.point, spheroid=True)
).order_by('id') ).order_by('id')
for i, c in enumerate(qs): for i, c in enumerate(qs):
self.assertAlmostEqual(spheroid_distances[i], c.distance.m, tol) with self.subTest(c=c):
self.assertAlmostEqual(spheroid_distances[i], c.distance.m, tol)
if postgis or spatialite: if postgis or spatialite:
# PostGIS uses sphere-only distances by default, testing these as well. # PostGIS uses sphere-only distances by default, testing these as well.
qs = AustraliaCity.objects.exclude(id=hillsdale.id).annotate( qs = AustraliaCity.objects.exclude(id=hillsdale.id).annotate(
distance=Distance('point', hillsdale.point) distance=Distance('point', hillsdale.point)
).order_by('id') ).order_by('id')
for i, c in enumerate(qs): for i, c in enumerate(qs):
self.assertAlmostEqual(sphere_distances[i], c.distance.m, tol) with self.subTest(c=c):
self.assertAlmostEqual(sphere_distances[i], c.distance.m, tol)
@skipIfDBFeature("supports_distance_geodetic") @skipIfDBFeature("supports_distance_geodetic")
@skipUnlessDBFeature("has_Distance_function") @skipUnlessDBFeature("has_Distance_function")

View File

@ -15,8 +15,9 @@ class GeometryFieldTest(SimpleTestCase):
"Testing GeometryField initialization with defaults." "Testing GeometryField initialization with defaults."
fld = forms.GeometryField() fld = forms.GeometryField()
for bad_default in ('blah', 3, 'FoO', None, 0): for bad_default in ('blah', 3, 'FoO', None, 0):
with self.assertRaises(ValidationError): with self.subTest(bad_default=bad_default):
fld.clean(bad_default) with self.assertRaises(ValidationError):
fld.clean(bad_default)
def test_srid(self): def test_srid(self):
"Testing GeometryField with a SRID set." "Testing GeometryField with a SRID set."
@ -50,9 +51,10 @@ class GeometryFieldTest(SimpleTestCase):
# By default, all geometry types are allowed. # By default, all geometry types are allowed.
fld = forms.GeometryField() fld = forms.GeometryField()
for wkt in ('POINT(5 23)', 'MULTIPOLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'LINESTRING(0 0, 1 1)'): for wkt in ('POINT(5 23)', 'MULTIPOLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'LINESTRING(0 0, 1 1)'):
# `to_python` uses the SRID of OpenLayersWidget if the converted with self.subTest(wkt=wkt):
# value doesn't have an SRID itself. # to_python() uses the SRID of OpenLayersWidget if the
self.assertEqual(GEOSGeometry(wkt, srid=fld.widget.map_srid), fld.clean(wkt)) # converted value doesn't have an SRID.
self.assertEqual(GEOSGeometry(wkt, srid=fld.widget.map_srid), fld.clean(wkt))
pnt_fld = forms.GeometryField(geom_type='POINT') pnt_fld = forms.GeometryField(geom_type='POINT')
self.assertEqual(GEOSGeometry('POINT(5 23)', srid=pnt_fld.widget.map_srid), pnt_fld.clean('POINT(5 23)')) self.assertEqual(GEOSGeometry('POINT(5 23)', srid=pnt_fld.widget.map_srid), pnt_fld.clean('POINT(5 23)'))
@ -73,11 +75,13 @@ class GeometryFieldTest(SimpleTestCase):
fld = forms.GeometryField() fld = forms.GeometryField()
# to_python returns the same GEOSGeometry for a WKT # to_python returns the same GEOSGeometry for a WKT
for wkt in ('POINT(5 23)', 'MULTIPOLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'LINESTRING(0 0, 1 1)'): for wkt in ('POINT(5 23)', 'MULTIPOLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'LINESTRING(0 0, 1 1)'):
self.assertEqual(GEOSGeometry(wkt, srid=fld.widget.map_srid), fld.to_python(wkt)) with self.subTest(wkt=wkt):
self.assertEqual(GEOSGeometry(wkt, srid=fld.widget.map_srid), fld.to_python(wkt))
# but raises a ValidationError for any other string # but raises a ValidationError for any other string
for wkt in ('POINT(5)', 'MULTI POLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'BLAH(0 0, 1 1)'): for wkt in ('POINT(5)', 'MULTI POLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'BLAH(0 0, 1 1)'):
with self.assertRaises(forms.ValidationError): with self.subTest(wkt=wkt):
fld.to_python(wkt) with self.assertRaises(forms.ValidationError):
fld.to_python(wkt)
def test_field_with_text_widget(self): def test_field_with_text_widget(self):
class PointForm(forms.Form): class PointForm(forms.Form):

View File

@ -143,7 +143,8 @@ class DistanceTest(unittest.TestCase):
unit_tuple = [('Yard', 'yd'), ('Nautical Mile', 'nm'), ('German legal metre', 'german_m'), unit_tuple = [('Yard', 'yd'), ('Nautical Mile', 'nm'), ('German legal metre', 'german_m'),
('Indian yard', 'indian_yd'), ('Chain (Sears)', 'chain_sears'), ('Chain', 'chain')] ('Indian yard', 'indian_yd'), ('Chain (Sears)', 'chain_sears'), ('Chain', 'chain')]
for nm, att in unit_tuple: for nm, att in unit_tuple:
self.assertEqual(att, D.unit_attname(nm)) with self.subTest(nm=nm):
self.assertEqual(att, D.unit_attname(nm))
class AreaTest(unittest.TestCase): class AreaTest(unittest.TestCase):

View File

@ -76,17 +76,19 @@ class TestPostGISVersionCheck(unittest.TestCase):
] ]
for version in versions: for version in versions:
ops = FakePostGISOperations(version[0]) with self.subTest(version=version):
actual = ops.spatial_version ops = FakePostGISOperations(version[0])
self.assertEqual(version[1:], actual) actual = ops.spatial_version
self.assertEqual(version[1:], actual)
def test_invalid_version_numbers(self): def test_invalid_version_numbers(self):
versions = ['nope', '123'] versions = ['nope', '123']
for version in versions: for version in versions:
ops = FakePostGISOperations(version) with self.subTest(version=version):
with self.assertRaises(Exception): ops = FakePostGISOperations(version)
ops.spatial_version with self.assertRaises(Exception):
ops.spatial_version
def test_no_version_number(self): def test_no_version_number(self):
ops = FakePostGISOperations() ops = FakePostGISOperations()