1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

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): def setUpClass(cls):
super().setUpClass() super().setUpClass()
if cls._overridden_settings: if cls._overridden_settings:
cls._cls_overridden_context = override_settings(**cls._overridden_settings) cls.enterClassContext(override_settings(**cls._overridden_settings))
cls._cls_overridden_context.enable()
cls.addClassCleanup(cls._cls_overridden_context.disable)
if cls._modified_settings: if cls._modified_settings:
cls._cls_modified_context = modify_settings(cls._modified_settings) cls.enterClassContext(modify_settings(cls._modified_settings))
cls._cls_modified_context.enable()
cls.addClassCleanup(cls._cls_modified_context.disable)
cls._add_databases_failures() cls._add_databases_failures()
cls.addClassCleanup(cls._remove_databases_failures) cls.addClassCleanup(cls._remove_databases_failures)
@ -1732,11 +1728,9 @@ class LiveServerTestCase(TransactionTestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
super().setUpClass() super().setUpClass()
cls._live_server_modified_settings = modify_settings( cls.enterClassContext(
ALLOWED_HOSTS={"append": cls.allowed_host}, 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() cls._start_server_thread()
@classmethod @classmethod

View File

@ -94,15 +94,17 @@ class BaseModelBackendTest:
backend = "django.contrib.auth.backends.ModelBackend" backend = "django.contrib.auth.backends.ModelBackend"
@classmethod
def setUpClass(cls):
cls.enterClassContext(
modify_settings(AUTHENTICATION_BACKENDS={"append": cls.backend})
)
super().setUpClass()
def setUp(self): def setUp(self):
# The custom_perms test messes with ContentTypes, which will be cached. # The custom_perms test messes with ContentTypes, which will be cached.
# Flush the cache to ensure there are no side effects. # Flush the cache to ensure there are no side effects.
self.addCleanup(ContentType.objects.clear_cache) 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() self.create_users()
def test_has_perm(self): def test_has_perm(self):

View File

@ -20,13 +20,15 @@ class RemoteUserTest(TestCase):
known_user = "knownuser" known_user = "knownuser"
known_user2 = "knownuser2" known_user2 = "knownuser2"
def setUp(self): @classmethod
patched_settings = modify_settings( def setUpClass(cls):
AUTHENTICATION_BACKENDS={"append": self.backend}, cls.enterClassContext(
MIDDLEWARE={"append": self.middleware}, modify_settings(
AUTHENTICATION_BACKENDS={"append": cls.backend},
MIDDLEWARE={"append": cls.middleware},
)
) )
patched_settings.enable() super().setUpClass()
self.addCleanup(patched_settings.disable)
def test_no_remote_user(self): def test_no_remote_user(self):
"""Users are not created when remote user is not specified.""" """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.core.exceptions import ValidationError
from django.test import SimpleTestCase, override_settings from django.test import SimpleTestCase, override_settings
from django.utils import translation from django.utils import translation
from django.utils.translation import activate, deactivate
class LocalizedTimeTests(SimpleTestCase): class LocalizedTimeTests(SimpleTestCase):
def setUp(self): @classmethod
def setUpClass(cls):
# nl/formats.py has customized TIME_INPUT_FORMATS: # nl/formats.py has customized TIME_INPUT_FORMATS:
# ['%H:%M:%S', '%H.%M:%S', '%H.%M', '%H:%M'] # ['%H:%M:%S', '%H.%M:%S', '%H.%M', '%H:%M']
activate("nl") cls.enterClassContext(translation.override("nl"))
self.addCleanup(deactivate) super().setUpClass()
def test_timeField(self): def test_timeField(self):
"TimeFields can parse dates in the default format" "TimeFields can parse dates in the default format"
@ -319,9 +319,10 @@ class SimpleTimeFormatTests(SimpleTestCase):
class LocalizedDateTests(SimpleTestCase): class LocalizedDateTests(SimpleTestCase):
def setUp(self): @classmethod
activate("de") def setUpClass(cls):
self.addCleanup(deactivate) cls.enterClassContext(translation.override("de"))
super().setUpClass()
def test_dateField(self): def test_dateField(self):
"DateFields can parse dates in the default format" "DateFields can parse dates in the default format"
@ -631,9 +632,10 @@ class SimpleDateFormatTests(SimpleTestCase):
class LocalizedDateTimeTests(SimpleTestCase): class LocalizedDateTimeTests(SimpleTestCase):
def setUp(self): @classmethod
activate("de") def setUpClass(cls):
self.addCleanup(deactivate) cls.enterClassContext(translation.override("de"))
super().setUpClass()
def test_dateTimeField(self): def test_dateTimeField(self):
"DateTimeFields can parse dates in the default format" "DateTimeFields can parse dates in the default format"

View File

@ -1172,10 +1172,10 @@ class PythonGlobalState(SimpleTestCase):
class BaseEmailBackendTests(HeadersCheckMixin): class BaseEmailBackendTests(HeadersCheckMixin):
email_backend = None email_backend = None
def setUp(self): @classmethod
settings_override = override_settings(EMAIL_BACKEND=self.email_backend) def setUpClass(cls):
settings_override.enable() cls.enterClassContext(override_settings(EMAIL_BACKEND=cls.email_backend))
self.addCleanup(settings_override.disable) super().setUpClass()
def assertStartsWith(self, first, second): def assertStartsWith(self, first, second):
if not first.startswith(second): if not first.startswith(second):

View File

@ -31,29 +31,32 @@ class BaseTests:
"error": constants.ERROR, "error": constants.ERROR,
} }
def setUp(self): @classmethod
settings_override = override_settings( def setUpClass(cls):
TEMPLATES=[ cls.enterClassContext(
{ override_settings(
"BACKEND": "django.template.backends.django.DjangoTemplates", TEMPLATES=[
"DIRS": [], {
"APP_DIRS": True, "BACKEND": "django.template.backends.django.DjangoTemplates",
"OPTIONS": { "DIRS": [],
"context_processors": ( "APP_DIRS": True,
"django.contrib.auth.context_processors.auth", "OPTIONS": {
"django.contrib.messages.context_processors.messages", "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" ROOT_URLCONF="messages_tests.urls",
% (self.storage_class.__module__, self.storage_class.__name__), MESSAGE_TAGS={},
SESSION_SERIALIZER="django.contrib.sessions.serializers.JSONSerializer", MESSAGE_STORAGE=(
f"{cls.storage_class.__module__}.{cls.storage_class.__name__}"
),
SESSION_SERIALIZER="django.contrib.sessions.serializers.JSONSerializer",
)
) )
settings_override.enable() super().setUpClass()
self.addCleanup(settings_override.disable)
def get_request(self): def get_request(self):
return HttpRequest() return HttpRequest()

View File

@ -25,10 +25,7 @@ class LiveServerBase(StaticLiveServerTestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
# Override settings cls.enterClassContext(override_settings(**TEST_SETTINGS))
cls.settings_override = override_settings(**TEST_SETTINGS)
cls.settings_override.enable()
cls.addClassCleanup(cls.settings_override.disable)
super().setUpClass() 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") @override_settings(TIME_ZONE="Europe/Copenhagen")
class DateFormatTests(SimpleTestCase): class DateFormatTests(SimpleTestCase):
def setUp(self): @classmethod
_orig_lang = translation.get_language() def setUpClass(cls):
translation.activate("en-us") cls.enterClassContext(translation.override("en-us"))
self.addCleanup(translation.activate, _orig_lang) super().setUpClass()
def test_date(self): def test_date(self):
d = date(2009, 5, 16) d = date(2009, 5, 16)