Used enterClassContext() where appropriate.

This commit is contained in:
Mariusz Felisiak 2024-01-04 05:55:29 +01:00 committed by GitHub
parent d89a465e62
commit 0c5456ef37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 65 additions and 65 deletions

View File

@ -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)
@ -1732,11 +1728,9 @@ class LiveServerTestCase(TransactionTestCase):
@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

View File

@ -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):

View File

@ -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."""

View File

@ -4,15 +4,15 @@ from django import forms
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"
@ -319,9 +319,10 @@ class SimpleTimeFormatTests(SimpleTestCase):
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"
@ -631,9 +632,10 @@ class SimpleDateFormatTests(SimpleTestCase):
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"

View File

@ -1172,10 +1172,10 @@ class PythonGlobalState(SimpleTestCase):
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):

View File

@ -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()

View File

@ -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()

View File

@ -9,10 +9,10 @@ from django.utils.timezone import get_default_timezone, get_fixed_timezone, make
@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)