Permalink
Browse files

Removed some direct settings manipulations in tests; refs #21230.

  • Loading branch information...
1 parent 499cd91 commit 3565efaa451db6eb735a085ea6aae3fe86e6d283 @Bouke Bouke committed with timgraham Oct 14, 2013
@@ -56,7 +56,8 @@
for a staff account. Note that both fields may be case-sensitive."
-@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
+@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
+ USE_I18N=True, USE_L10N=False, LANGUAGE_CODE='en')
class AdminViewBasicTestCase(TestCase):
fixtures = ['admin-views-users.xml', 'admin-views-colors.xml',
'admin-views-fabrics.xml', 'admin-views-books.xml']
@@ -69,16 +70,9 @@ class AdminViewBasicTestCase(TestCase):
urls = "admin_views.urls"
def setUp(self):
- self.old_USE_I18N = settings.USE_I18N
- self.old_USE_L10N = settings.USE_L10N
- self.old_LANGUAGE_CODE = settings.LANGUAGE_CODE
self.client.login(username='super', password='secret')
- settings.USE_I18N = True
def tearDown(self):
- settings.USE_I18N = self.old_USE_I18N
- settings.USE_L10N = self.old_USE_L10N
- settings.LANGUAGE_CODE = self.old_LANGUAGE_CODE
self.client.logout()
formats.reset_format_cache()
@@ -2410,15 +2404,12 @@ def test_model_admin_default_delete_action(self):
self.client.post('/test_admin/admin/admin_views/subscriber/', delete_confirmation_data)
self.assertEqual(Subscriber.objects.count(), 0)
+ @override_settings(USE_THOUSAND_SEPARATOR=True, USE_L10N=True)
def test_non_localized_pk(self):
"""If USE_THOUSAND_SEPARATOR is set, make sure that the ids for
the objects selected for deletion are rendered without separators.
Refs #14895.
"""
- self.old_USE_THOUSAND_SEPARATOR = settings.USE_THOUSAND_SEPARATOR
- self.old_USE_L10N = settings.USE_L10N
- settings.USE_THOUSAND_SEPARATOR = True
- settings.USE_L10N = True
subscriber = Subscriber.objects.get(id=1)
subscriber.id = 9999
subscriber.save()
@@ -2431,8 +2422,6 @@ def test_non_localized_pk(self):
self.assertTemplateUsed(response, 'admin/delete_selected_confirmation.html')
self.assertContains(response, 'value="9999"') # Instead of 9,999
self.assertContains(response, 'value="2"')
- settings.USE_THOUSAND_SEPARATOR = self.old_USE_THOUSAND_SEPARATOR
- settings.USE_L10N = self.old_USE_L10N
def test_model_admin_default_delete_action_protected(self):
"""
@@ -4127,21 +4116,16 @@ def testLangNamePresent(self):
self.assertNotContains(response, ' xml:lang=""')
-@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
+@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
+ USE_THOUSAND_SEPARATOR=True, USE_L10N=True)
class DateHierarchyTests(TestCase):
urls = "admin_views.urls"
fixtures = ['admin-views-users.xml']
def setUp(self):
self.client.login(username='super', password='secret')
- self.old_USE_THOUSAND_SEPARATOR = settings.USE_THOUSAND_SEPARATOR
- self.old_USE_L10N = settings.USE_L10N
- settings.USE_THOUSAND_SEPARATOR = True
- settings.USE_L10N = True
def tearDown(self):
- settings.USE_THOUSAND_SEPARATOR = self.old_USE_THOUSAND_SEPARATOR
- settings.USE_L10N = self.old_USE_L10N
formats.reset_format_cache()
def assert_non_localized_year(self, response, year):
@@ -3,6 +3,7 @@
from django.conf import settings
from django.contrib.comments.forms import CommentForm
from django.contrib.comments.models import Comment
+from django.test.utils import override_settings
from . import CommentTestCase
from ..models import Article
@@ -58,26 +59,18 @@ def testGetCommentObject(self):
c.save()
self.assertEqual(Comment.objects.count(), 1)
+ @override_settings(PROFANITIES_LIST=["rooster"])
def testProfanities(self):
"""Test COMMENTS_ALLOW_PROFANITIES and PROFANITIES_LIST settings"""
a = Article.objects.get(pk=1)
d = self.getValidData(a)
- # Save settings in case other tests need 'em
- saved = settings.PROFANITIES_LIST, settings.COMMENTS_ALLOW_PROFANITIES
-
- # Don't wanna swear in the unit tests if we don't have to...
- settings.PROFANITIES_LIST = ["rooster"]
-
# Try with COMMENTS_ALLOW_PROFANITIES off
- settings.COMMENTS_ALLOW_PROFANITIES = False
- f = CommentForm(a, data=dict(d, comment="What a rooster!"))
- self.assertFalse(f.is_valid())
+ with self.settings(COMMENTS_ALLOW_PROFANITIES=False):
+ f = CommentForm(a, data=dict(d, comment="What a rooster!"))
+ self.assertFalse(f.is_valid())
# Now with COMMENTS_ALLOW_PROFANITIES on
- settings.COMMENTS_ALLOW_PROFANITIES = True
- f = CommentForm(a, data=dict(d, comment="What a rooster!"))
- self.assertTrue(f.is_valid())
-
- # Restore settings
- settings.PROFANITIES_LIST, settings.COMMENTS_ALLOW_PROFANITIES = saved
+ with self.settings(COMMENTS_ALLOW_PROFANITIES=True):
+ f = CommentForm(a, data=dict(d, comment="What a rooster!"))
+ self.assertTrue(f.is_valid())
@@ -83,22 +83,19 @@ def testHashTampering(self):
def testDebugCommentErrors(self):
"""The debug error template should be shown only if DEBUG is True"""
- olddebug = settings.DEBUG
-
- settings.DEBUG = True
a = Article.objects.get(pk=1)
data = self.getValidData(a)
data["security_hash"] = "Nobody expects the Spanish Inquisition!"
- response = self.client.post("/post/", data)
- self.assertEqual(response.status_code, 400)
- self.assertTemplateUsed(response, "comments/400-debug.html")
- settings.DEBUG = False
- response = self.client.post("/post/", data)
- self.assertEqual(response.status_code, 400)
- self.assertTemplateNotUsed(response, "comments/400-debug.html")
+ with self.settings(DEBUG=True):
+ response = self.client.post("/post/", data)
+ self.assertEqual(response.status_code, 400)
+ self.assertTemplateUsed(response, "comments/400-debug.html")
- settings.DEBUG = olddebug
+ with self.settings(DEBUG=False):
+ response = self.client.post("/post/", data)
+ self.assertEqual(response.status_code, 400)
+ self.assertTemplateNotUsed(response, "comments/400-debug.html")
def testCreateValidComment(self):
address = "1.2.3.4"
@@ -18,7 +18,8 @@
EpisodePermanent, Category)
-@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
+@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
+ TEMPLATE_DEBUG=True)
class GenericAdminViewTest(TestCase):
urls = "generic_inline_admin.urls"
fixtures = ['users.xml']
@@ -27,8 +28,7 @@ def setUp(self):
# set TEMPLATE_DEBUG to True to ensure {% include %} will raise
# exceptions since that is how inlines are rendered and #9498 will
# bubble up if it is an issue.
- self.original_template_debug = settings.TEMPLATE_DEBUG
- settings.TEMPLATE_DEBUG = True
+
self.client.login(username='super', password='secret')
# Can't load content via a fixture (since the GenericForeignKey
@@ -46,7 +46,6 @@ def setUp(self):
def tearDown(self):
self.client.logout()
- settings.TEMPLATE_DEBUG = self.original_template_debug
def testBasicAddGet(self):
"""
View
@@ -968,17 +968,9 @@ def assertUgettext(self, msgid, msgstr):
self.assertTrue(msgstr in result, ("The string '%s' isn't in the "
"translation of '%s'; the actual result is '%s'." % (msgstr, msgid, result)))
+@override_settings(INSTALLED_APPS=['i18n.resolution'] + list(settings.INSTALLED_APPS))
class AppResolutionOrderI18NTests(ResolutionOrderI18NTests):
- def setUp(self):
- self.old_installed_apps = settings.INSTALLED_APPS
- settings.INSTALLED_APPS = ['i18n.resolution'] + list(settings.INSTALLED_APPS)
- super(AppResolutionOrderI18NTests, self).setUp()
-
- def tearDown(self):
- settings.INSTALLED_APPS = self.old_installed_apps
- super(AppResolutionOrderI18NTests, self).tearDown()
-
def test_app_translation(self):
self.assertUgettext('Date/time', 'APP')
@@ -6,6 +6,7 @@
from django.db.models.loading import cache
from django.template import Context, Template
from django.test import TestCase
+from django.test.utils import override_settings
from django.utils.encoding import force_text
from .models import (
@@ -108,6 +109,7 @@ def test_explicit_abstract_manager(self):
except AttributeError as e:
self.assertEqual(str(e), "Manager isn't available; AbstractBase1 is abstract")
+ @override_settings(TEST_SWAPPABLE_MODEL='managers_regress.Parent')
def test_swappable_manager(self):
try:
# This test adds dummy models to the app cache. These
@@ -116,8 +118,6 @@ def test_swappable_manager(self):
old_app_models = copy.deepcopy(cache.app_models)
old_app_store = copy.deepcopy(cache.app_store)
- settings.TEST_SWAPPABLE_MODEL = 'managers_regress.Parent'
-
class SwappableModel(models.Model):
class Meta:
swappable = 'TEST_SWAPPABLE_MODEL'
@@ -131,10 +131,10 @@ class Meta:
self.assertEqual(str(e), "Manager isn't available; SwappableModel has been swapped for 'managers_regress.Parent'")
finally:
- del settings.TEST_SWAPPABLE_MODEL
cache.app_models = old_app_models
cache.app_store = old_app_store
+ @override_settings(TEST_SWAPPABLE_MODEL='managers_regress.Parent')
def test_custom_swappable_manager(self):
try:
# This test adds dummy models to the app cache. These
@@ -143,8 +143,6 @@ def test_custom_swappable_manager(self):
old_app_models = copy.deepcopy(cache.app_models)
old_app_store = copy.deepcopy(cache.app_store)
- settings.TEST_SWAPPABLE_MODEL = 'managers_regress.Parent'
-
class SwappableModel(models.Model):
stuff = models.Manager()
@@ -162,10 +160,10 @@ class Meta:
self.assertEqual(str(e), "Manager isn't available; SwappableModel has been swapped for 'managers_regress.Parent'")
finally:
- del settings.TEST_SWAPPABLE_MODEL
cache.app_models = old_app_models
cache.app_store = old_app_store
+ @override_settings(TEST_SWAPPABLE_MODEL='managers_regress.Parent')
def test_explicit_swappable_manager(self):
try:
# This test adds dummy models to the app cache. These
@@ -174,8 +172,6 @@ def test_explicit_swappable_manager(self):
old_app_models = copy.deepcopy(cache.app_models)
old_app_store = copy.deepcopy(cache.app_store)
- settings.TEST_SWAPPABLE_MODEL = 'managers_regress.Parent'
-
class SwappableModel(models.Model):
objects = models.Manager()
@@ -193,7 +189,6 @@ class Meta:
self.assertEqual(str(e), "Manager isn't available; SwappableModel has been swapped for 'managers_regress.Parent'")
finally:
- del settings.TEST_SWAPPABLE_MODEL
cache.app_models = old_app_models
cache.app_store = old_app_store
@@ -6,6 +6,7 @@
from django.template.response import TemplateResponse
from django.template import Template
from django.test import TestCase
+from django.test.utils import override_settings
class TestException(Exception):
pass
@@ -772,15 +773,11 @@ def test_process_exception_bad_middleware_permission_denied(self):
class RootUrlconfTests(TestCase):
urls = 'middleware_exceptions.urls'
+ @override_settings(ROOT_URLCONF=None)
def test_missing_root_urlconf(self):
- try:
- original_ROOT_URLCONF = settings.ROOT_URLCONF
- del settings.ROOT_URLCONF
- except AttributeError:
- original_ROOT_URLCONF = _missing
+ # Removing ROOT_URLCONF is safe, as override_settings will restore
+ # the previously defined settings.
+ del settings.ROOT_URLCONF
self.assertRaises(AttributeError,
self.client.get, "/middleware_exceptions/view/"
)
-
- if original_ROOT_URLCONF is not _missing:
- settings.ROOT_URLCONF = original_ROOT_URLCONF
@@ -15,7 +15,7 @@
from django.forms.models import BaseModelFormSet
from django.forms.widgets import Select
from django.test import TestCase
-from django.test.utils import str_prefix
+from django.test.utils import str_prefix, override_settings
from django.utils import six
from .models import Band, Concert, ValidationTestModel, ValidationTestInlineModel
@@ -540,9 +540,7 @@ class BandAdmin(ModelAdmin):
class ValidationTests(unittest.TestCase):
def test_validation_only_runs_in_debug(self):
# Ensure validation only runs when DEBUG = True
- try:
- settings.DEBUG = True
-
+ with override_settings(DEBUG=True):
class ValidationTestModelAdmin(ModelAdmin):
raw_id_fields = 10
@@ -555,11 +553,10 @@ class ValidationTestModelAdmin(ModelAdmin):
ValidationTestModel,
ValidationTestModelAdmin,
)
- finally:
- settings.DEBUG = False
- site = AdminSite()
- site.register(ValidationTestModel, ValidationTestModelAdmin)
+ with override_settings(DEBUG=False):
+ site = AdminSite()
+ site.register(ValidationTestModel, ValidationTestModelAdmin)
def test_raw_id_fields_validation(self):
@@ -150,6 +150,7 @@ class Meta:
proxy = True
self.assertRaises(FieldError, build_new_fields)
+ @override_settings(TEST_SWAPPABLE_MODEL='proxy_models.AlternateModel')
def test_swappable(self):
try:
# This test adds dummy applications to the app cache. These
@@ -158,8 +159,6 @@ def test_swappable(self):
old_app_models = copy.deepcopy(cache.app_models)
old_app_store = copy.deepcopy(cache.app_store)
- settings.TEST_SWAPPABLE_MODEL = 'proxy_models.AlternateModel'
-
class SwappableModel(models.Model):
class Meta:
@@ -175,7 +174,6 @@ class ProxyModel(SwappableModel):
class Meta:
proxy = True
finally:
- del settings.TEST_SWAPPABLE_MODEL
cache.app_models = old_app_models
cache.app_store = old_app_store
@@ -9,6 +9,7 @@
from django.db.utils import ConnectionHandler, DEFAULT_DB_ALIAS, DatabaseError
from django.test import (TransactionTestCase, skipIfDBFeature,
skipUnlessDBFeature)
+from django.test.utils import override_settings
from multiple_database.routers import TestRouter
@@ -23,6 +24,9 @@
requires_threading = unittest.skipUnless(threading, 'requires threading')
+# We need to set settings.DEBUG to True so we can capture the output SQL
+# to examine.
+@override_settings(DEBUG=True)
class SelectForUpdateTests(TransactionTestCase):
available_apps = ['select_for_update']
@@ -41,11 +45,6 @@ def setUp(self):
self.new_connection = new_connections[DEFAULT_DB_ALIAS]
self.new_connection.enter_transaction_management()
- # We need to set settings.DEBUG to True so we can capture
- # the output SQL to examine.
- self._old_debug = settings.DEBUG
- settings.DEBUG = True
-
def tearDown(self):
try:
# We don't really care if this fails - some of the tests will set
@@ -55,7 +54,6 @@ def tearDown(self):
except transaction.TransactionManagementError:
pass
self.new_connection.close()
- settings.DEBUG = self._old_debug
try:
self.end_blocking_transaction()
except (DatabaseError, AttributeError):
Oops, something went wrong.

0 comments on commit 3565efa

Please sign in to comment.