Permalink
Browse files

Use the class decorator syntax available in Python >= 2.6. Refs #17965.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17829 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 9383a27 commit eb163f37cb62268443d592f8ce08bd8a0ab9631d @aaugustin aaugustin committed Mar 30, 2012
@@ -8,6 +8,12 @@
from django.test.utils import override_settings
+@override_settings(
+ TEMPLATE_DIRS=(
+ os.path.join(os.path.dirname(__file__), 'templates'),
+ ),
+ USE_TZ=False, # required for loading the fixture
+)
class AuthContextProcessorTests(TestCase):
"""
Tests for the ``django.contrib.auth.context_processors.auth`` processor
@@ -95,10 +101,3 @@ def test_user_attrs(self):
# See bug #12060
self.assertEqual(response.context['user'], user)
self.assertEqual(user, response.context['user'])
-
-AuthContextProcessorTests = override_settings(
- TEMPLATE_DIRS=(
- os.path.join(os.path.dirname(__file__), 'templates'),
- ),
- USE_TZ=False, # required for loading the fixture
-)(AuthContextProcessorTests)
@@ -11,6 +11,7 @@
from django.utils.translation import ugettext as _
+@override_settings(USE_TZ=False)
class UserCreationFormTest(TestCase):
fixtures = ['authtestdata.json']
@@ -75,9 +76,8 @@ def test_success(self):
u = form.save()
self.assertEqual(repr(u), '<User: jsmith@example.com>')
-UserCreationFormTest = override_settings(USE_TZ=False)(UserCreationFormTest)
-
+@override_settings(USE_TZ=False)
class AuthenticationFormTest(TestCase):
fixtures = ['authtestdata.json']
@@ -128,9 +128,8 @@ def test_success(self):
self.assertTrue(form.is_valid())
self.assertEqual(form.non_field_errors(), [])
-AuthenticationFormTest = override_settings(USE_TZ=False)(AuthenticationFormTest)
-
+@override_settings(USE_TZ=False)
class SetPasswordFormTest(TestCase):
fixtures = ['authtestdata.json']
@@ -156,9 +155,8 @@ def test_success(self):
form = SetPasswordForm(user, data)
self.assertTrue(form.is_valid())
-SetPasswordFormTest = override_settings(USE_TZ=False)(SetPasswordFormTest)
-
+@override_settings(USE_TZ=False)
class PasswordChangeFormTest(TestCase):
fixtures = ['authtestdata.json']
@@ -205,9 +203,8 @@ def test_field_order(self):
self.assertEqual(PasswordChangeForm(user, {}).fields.keys(),
['old_password', 'new_password1', 'new_password2'])
-PasswordChangeFormTest = override_settings(USE_TZ=False)(PasswordChangeFormTest)
-
+@override_settings(USE_TZ=False)
class UserChangeFormTest(TestCase):
fixtures = ['authtestdata.json']
@@ -254,9 +251,7 @@ def test_bug_17944_unknown_password_algorithm(self):
form.as_table()
-UserChangeFormTest = override_settings(USE_TZ=False)(UserChangeFormTest)
-
-
+@override_settings(USE_TZ=False)
class PasswordResetFormTest(TestCase):
fixtures = ['authtestdata.json']
@@ -334,5 +329,3 @@ def test_unusable_password(self):
self.assertFalse(form.is_valid())
self.assertEqual(form["email"].errors,
[_(u"The user account associated with this e-mail address cannot reset the password.")])
-
-PasswordResetFormTest = override_settings(USE_TZ=False)(PasswordResetFormTest)
@@ -4,7 +4,6 @@
PBKDF2SHA1PasswordHasher, get_hasher, UNUSABLE_PASSWORD)
from django.utils import unittest
from django.utils.unittest import skipUnless
-from django.test.utils import override_settings
try:
@@ -19,6 +18,7 @@
class TestUtilsHashPass(unittest.TestCase):
+
def setUp(self):
load_hashers(password_hashers=default_hashers)
@@ -5,6 +5,7 @@
SiteProfileNotAvailable, UserManager)
+@override_settings(USE_TZ=False)
class ProfileTestCase(TestCase):
fixtures = ['authtestdata.json']
@@ -38,9 +39,8 @@ def test_site_profile_not_available(self):
settings.AUTH_PROFILE_MODULE = 'foo.bar'
self.assertRaises(SiteProfileNotAvailable, user.get_profile)
-ProfileTestCase = override_settings(USE_TZ=False)(ProfileTestCase)
-
+@override_settings(USE_TZ=False)
class NaturalKeysTestCase(TestCase):
fixtures = ['authtestdata.json']
@@ -53,9 +53,8 @@ def test_group_natural_key(self):
users_group = Group.objects.create(name='users')
self.assertEquals(Group.objects.get_by_natural_key('users'), users_group)
-NaturalKeysTestCase = override_settings(USE_TZ=False)(NaturalKeysTestCase)
-
+@override_settings(USE_TZ=False)
class LoadDataWithoutNaturalKeysTestCase(TestCase):
fixtures = ['regular.json']
@@ -64,9 +63,8 @@ def test_user_is_created_and_added_to_group(self):
group = Group.objects.get(name='my_group')
self.assertEquals(group, user.groups.get())
-LoadDataWithoutNaturalKeysTestCase = override_settings(USE_TZ=False)(LoadDataWithoutNaturalKeysTestCase)
-
+@override_settings(USE_TZ=False)
class LoadDataWithNaturalKeysTestCase(TestCase):
fixtures = ['natural.json']
@@ -75,8 +73,6 @@ def test_user_is_created_and_added_to_group(self):
group = Group.objects.get(name='my_group')
self.assertEquals(group, user.groups.get())
-LoadDataWithNaturalKeysTestCase = override_settings(USE_TZ=False)(LoadDataWithNaturalKeysTestCase)
-
class UserManagerTestCase(TestCase):
@@ -3,6 +3,7 @@
from django.contrib.auth import signals
+@override_settings(USE_TZ=False)
class SignalTestCase(TestCase):
urls = 'django.contrib.auth.tests.urls'
fixtures = ['authtestdata.json']
@@ -46,5 +47,3 @@ def test_logout(self):
self.client.get('/logout/next_page/')
self.assertEqual(len(self.logged_out), 1)
self.assertEqual(self.logged_out[0].username, 'testclient')
-
-SignalTestCase = override_settings(USE_TZ=False)(SignalTestCase)
@@ -18,6 +18,7 @@
SetPasswordForm, PasswordResetForm)
+@override_settings(USE_TZ=False)
class AuthViewsTestCase(TestCase):
"""
Helper base class for all the follow test cases.
@@ -52,8 +53,6 @@ def login(self, password='password'):
def assertContainsEscaped(self, response, text, **kwargs):
return self.assertContains(response, escape(force_unicode(text)), **kwargs)
-AuthViewsTestCase = override_settings(USE_TZ=False)(AuthViewsTestCase)
-
class AuthViewNamedURLTests(AuthViewsTestCase):
urls = 'django.contrib.auth.urls'
@@ -38,6 +38,7 @@ def stored_cookie_messages_count(storage, response):
return len(data)
+@override_settings(SESSION_COOKIE_DOMAIN='.lawrence.com')
class CookieTest(BaseTest):
storage_class = CookieStorage
@@ -130,6 +131,3 @@ def test_json_encoder_decoder(self):
value = encoder.encode(messages)
decoded_messages = json.loads(value, cls=MessageDecoder)
self.assertEqual(messages, decoded_messages)
-
-CookieTest = override_settings(
- SESSION_COOKIE_DOMAIN='.lawrence.com')(CookieTest)
@@ -287,7 +287,9 @@ def test_sessionmanager_save(self):
self.assertEqual(self.session['y'], 2)
-DatabaseSessionWithTimeZoneTests = override_settings(USE_TZ=True)(DatabaseSessionTests)
+@override_settings(USE_TZ=True)
+class DatabaseSessionWithTimeZoneTests(DatabaseSessionTests):
+ pass
class CacheDBSessionTests(SessionTestsMixin, TestCase):
@@ -308,7 +310,9 @@ def test_load_overlong_key(self):
restore_warnings_state(warnings_state)
-CacheDBSessionWithTimeZoneTests = override_settings(USE_TZ=True)(CacheDBSessionTests)
+@override_settings(USE_TZ=True)
+class CacheDBSessionWithTimeZoneTests(CacheDBSessionTests):
+ pass
# Don't need DB flushing for these tests, so can use unittest.TestCase as base class
@@ -26,7 +26,8 @@ def test_secure_sitemap_section(self):
</urlset>
""" % (self.base_url, date.today()))
-#@override_settings(SECURE_PROXY_SSL_HEADER=False)
+
+@override_settings(SECURE_PROXY_SSL_HEADER=False)
class HTTPSDetectionSitemapTests(SitemapTestsBase):
extra = {'wsgi.url_scheme': 'https'}
@@ -47,5 +48,3 @@ def test_sitemap_section_with_https_request(self):
<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
</urlset>
""" % (self.base_url.replace('http://', 'https://'), date.today()))
-
-HTTPSDetectionSitemapTests = override_settings(SECURE_PROXY_SSL_HEADER=False)(HTTPSDetectionSitemapTests)
@@ -1450,14 +1450,13 @@ The decorator can also be applied to test case classes::
from django.test import TestCase
from django.test.utils import override_settings
+ @override_settings(LOGIN_URL='/other/login/')
class LoginTestCase(TestCase):
def test_login(self):
response = self.client.get('/sekrit/')
self.assertRedirects(response, '/other/login/?next=/sekrit/')
- LoginTestCase = override_settings(LOGIN_URL='/other/login/')(LoginTestCase)
-
.. note::
When given a class, the decorator modifies the class directly and
@@ -1467,19 +1466,6 @@ The decorator can also be applied to test case classes::
the original ``LoginTestCase`` is still equally affected by the
decorator.
-On Python 2.6 and higher you can also use the well known decorator syntax to
-decorate the class::
-
- from django.test import TestCase
- from django.test.utils import override_settings
-
- @override_settings(LOGIN_URL='/other/login/')
- class LoginTestCase(TestCase):
-
- def test_login(self):
- response = self.client.get('/sekrit/')
- self.assertRedirects(response, '/other/login/?next=/sekrit/')
-
.. note::
When overriding settings, make sure to handle the cases in which your app's
@@ -18,7 +18,7 @@
from django.test.utils import override_settings
-# @override_settings(INSTALLED_APPS=('app1', 'app2'))
+@override_settings(INSTALLED_APPS=('app1', 'app2'))
class ProxyModelInheritanceTests(TransactionTestCase):
def setUp(self):
@@ -41,5 +41,3 @@ def test_table_exists(self):
from .app2.models import NiceModel
self.assertEqual(NiceModel.objects.all().count(), 0)
self.assertEqual(ProxyModel.objects.all().count(), 0)
-
-ProxyModelInheritanceTests = override_settings(INSTALLED_APPS=('app1', 'app2'))(ProxyModelInheritanceTests)
Oops, something went wrong.

0 comments on commit eb163f3

Please sign in to comment.