Skip to content

Commit

Permalink
Used enterClassContext() where appropriate.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixxm authored Jan 4, 2024
1 parent d89a465 commit 0c5456e
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 65 deletions.
14 changes: 4 additions & 10 deletions django/test/testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,9 @@ class SimpleTestCase(unittest.TestCase):
def setUpClass(cls):
super().setUpClass()
if cls._overridden_settings:
cls._cls_overridden_context = override_settings(**cls._overridden_settings)
cls._cls_overridden_context.enable()
cls.addClassCleanup(cls._cls_overridden_context.disable)
cls.enterClassContext(override_settings(**cls._overridden_settings))
if cls._modified_settings:
cls._cls_modified_context = modify_settings(cls._modified_settings)
cls._cls_modified_context.enable()
cls.addClassCleanup(cls._cls_modified_context.disable)
cls.enterClassContext(modify_settings(cls._modified_settings))
cls._add_databases_failures()
cls.addClassCleanup(cls._remove_databases_failures)

Expand Down Expand Up @@ -1732,11 +1728,9 @@ def _make_connections_override(cls):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls._live_server_modified_settings = modify_settings(
ALLOWED_HOSTS={"append": cls.allowed_host},
cls.enterClassContext(
modify_settings(ALLOWED_HOSTS={"append": cls.allowed_host})
)
cls._live_server_modified_settings.enable()
cls.addClassCleanup(cls._live_server_modified_settings.disable)
cls._start_server_thread()

@classmethod
Expand Down
12 changes: 7 additions & 5 deletions tests/auth_tests/test_auth_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,17 @@ class BaseModelBackendTest:

backend = "django.contrib.auth.backends.ModelBackend"

@classmethod
def setUpClass(cls):
cls.enterClassContext(
modify_settings(AUTHENTICATION_BACKENDS={"append": cls.backend})
)
super().setUpClass()

def setUp(self):
# The custom_perms test messes with ContentTypes, which will be cached.
# Flush the cache to ensure there are no side effects.
self.addCleanup(ContentType.objects.clear_cache)
patched_settings = modify_settings(
AUTHENTICATION_BACKENDS={"append": self.backend},
)
patched_settings.enable()
self.addCleanup(patched_settings.disable)
self.create_users()

def test_has_perm(self):
Expand Down
14 changes: 8 additions & 6 deletions tests/auth_tests/test_remote_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ class RemoteUserTest(TestCase):
known_user = "knownuser"
known_user2 = "knownuser2"

def setUp(self):
patched_settings = modify_settings(
AUTHENTICATION_BACKENDS={"append": self.backend},
MIDDLEWARE={"append": self.middleware},
@classmethod
def setUpClass(cls):
cls.enterClassContext(
modify_settings(
AUTHENTICATION_BACKENDS={"append": cls.backend},
MIDDLEWARE={"append": cls.middleware},
)
)
patched_settings.enable()
self.addCleanup(patched_settings.disable)
super().setUpClass()

def test_no_remote_user(self):
"""Users are not created when remote user is not specified."""
Expand Down
22 changes: 12 additions & 10 deletions tests/forms_tests/tests/test_input_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
from django.core.exceptions import ValidationError
from django.test import SimpleTestCase, override_settings
from django.utils import translation
from django.utils.translation import activate, deactivate


class LocalizedTimeTests(SimpleTestCase):
def setUp(self):
@classmethod
def setUpClass(cls):
# nl/formats.py has customized TIME_INPUT_FORMATS:
# ['%H:%M:%S', '%H.%M:%S', '%H.%M', '%H:%M']
activate("nl")
self.addCleanup(deactivate)
cls.enterClassContext(translation.override("nl"))
super().setUpClass()

def test_timeField(self):
"TimeFields can parse dates in the default format"
Expand Down Expand Up @@ -319,9 +319,10 @@ def test_localized_timeField_with_inputformat(self):


class LocalizedDateTests(SimpleTestCase):
def setUp(self):
activate("de")
self.addCleanup(deactivate)
@classmethod
def setUpClass(cls):
cls.enterClassContext(translation.override("de"))
super().setUpClass()

def test_dateField(self):
"DateFields can parse dates in the default format"
Expand Down Expand Up @@ -631,9 +632,10 @@ def test_localized_dateField_with_inputformat(self):


class LocalizedDateTimeTests(SimpleTestCase):
def setUp(self):
activate("de")
self.addCleanup(deactivate)
@classmethod
def setUpClass(cls):
cls.enterClassContext(translation.override("de"))
super().setUpClass()

def test_dateTimeField(self):
"DateTimeFields can parse dates in the default format"
Expand Down
8 changes: 4 additions & 4 deletions tests/mail/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1172,10 +1172,10 @@ def test_8bit_non_latin(self):
class BaseEmailBackendTests(HeadersCheckMixin):
email_backend = None

def setUp(self):
settings_override = override_settings(EMAIL_BACKEND=self.email_backend)
settings_override.enable()
self.addCleanup(settings_override.disable)
@classmethod
def setUpClass(cls):
cls.enterClassContext(override_settings(EMAIL_BACKEND=cls.email_backend))
super().setUpClass()

def assertStartsWith(self, first, second):
if not first.startswith(second):
Expand Down
47 changes: 25 additions & 22 deletions tests/messages_tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,32 @@ class BaseTests:
"error": constants.ERROR,
}

def setUp(self):
settings_override = override_settings(
TEMPLATES=[
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": (
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
),
},
}
],
ROOT_URLCONF="messages_tests.urls",
MESSAGE_TAGS={},
MESSAGE_STORAGE="%s.%s"
% (self.storage_class.__module__, self.storage_class.__name__),
SESSION_SERIALIZER="django.contrib.sessions.serializers.JSONSerializer",
@classmethod
def setUpClass(cls):
cls.enterClassContext(
override_settings(
TEMPLATES=[
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": (
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
),
},
}
],
ROOT_URLCONF="messages_tests.urls",
MESSAGE_TAGS={},
MESSAGE_STORAGE=(
f"{cls.storage_class.__module__}.{cls.storage_class.__name__}"
),
SESSION_SERIALIZER="django.contrib.sessions.serializers.JSONSerializer",
)
)
settings_override.enable()
self.addCleanup(settings_override.disable)
super().setUpClass()

def get_request(self):
return HttpRequest()
Expand Down
5 changes: 1 addition & 4 deletions tests/staticfiles_tests/test_liveserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ class LiveServerBase(StaticLiveServerTestCase):

@classmethod
def setUpClass(cls):
# Override settings
cls.settings_override = override_settings(**TEST_SETTINGS)
cls.settings_override.enable()
cls.addClassCleanup(cls.settings_override.disable)
cls.enterClassContext(override_settings(**TEST_SETTINGS))
super().setUpClass()


Expand Down
8 changes: 4 additions & 4 deletions tests/utils_tests/test_dateformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

@override_settings(TIME_ZONE="Europe/Copenhagen")
class DateFormatTests(SimpleTestCase):
def setUp(self):
_orig_lang = translation.get_language()
translation.activate("en-us")
self.addCleanup(translation.activate, _orig_lang)
@classmethod
def setUpClass(cls):
cls.enterClassContext(translation.override("en-us"))
super().setUpClass()

def test_date(self):
d = date(2009, 5, 16)
Expand Down

0 comments on commit 0c5456e

Please sign in to comment.