From 0c5456ef37a22e2ce0d31e7ebf99d9fa04a5b9c4 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Thu, 4 Jan 2024 05:55:29 +0100 Subject: [PATCH] Used enterClassContext() where appropriate. --- django/test/testcases.py | 14 ++---- tests/auth_tests/test_auth_backends.py | 12 +++-- tests/auth_tests/test_remote_user.py | 14 +++--- tests/forms_tests/tests/test_input_formats.py | 22 +++++---- tests/mail/tests.py | 8 ++-- tests/messages_tests/base.py | 47 ++++++++++--------- tests/staticfiles_tests/test_liveserver.py | 5 +- tests/utils_tests/test_dateformat.py | 8 ++-- 8 files changed, 65 insertions(+), 65 deletions(-) diff --git a/django/test/testcases.py b/django/test/testcases.py index 7382f7f0f0..ce681f287c 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -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 diff --git a/tests/auth_tests/test_auth_backends.py b/tests/auth_tests/test_auth_backends.py index a7005de8a4..3b4f40e6e0 100644 --- a/tests/auth_tests/test_auth_backends.py +++ b/tests/auth_tests/test_auth_backends.py @@ -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): diff --git a/tests/auth_tests/test_remote_user.py b/tests/auth_tests/test_remote_user.py index 6066ab96e9..d3cf4b9da5 100644 --- a/tests/auth_tests/test_remote_user.py +++ b/tests/auth_tests/test_remote_user.py @@ -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.""" diff --git a/tests/forms_tests/tests/test_input_formats.py b/tests/forms_tests/tests/test_input_formats.py index 7a0dfca8a7..c5023d8d10 100644 --- a/tests/forms_tests/tests/test_input_formats.py +++ b/tests/forms_tests/tests/test_input_formats.py @@ -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" diff --git a/tests/mail/tests.py b/tests/mail/tests.py index 73eceafa46..cd981b6e9e 100644 --- a/tests/mail/tests.py +++ b/tests/mail/tests.py @@ -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): diff --git a/tests/messages_tests/base.py b/tests/messages_tests/base.py index 34582b5462..ce4b2acac8 100644 --- a/tests/messages_tests/base.py +++ b/tests/messages_tests/base.py @@ -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() diff --git a/tests/staticfiles_tests/test_liveserver.py b/tests/staticfiles_tests/test_liveserver.py index 714ebd1a8a..0575d684c5 100644 --- a/tests/staticfiles_tests/test_liveserver.py +++ b/tests/staticfiles_tests/test_liveserver.py @@ -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() diff --git a/tests/utils_tests/test_dateformat.py b/tests/utils_tests/test_dateformat.py index 33e2c57333..dc330a710b 100644 --- a/tests/utils_tests/test_dateformat.py +++ b/tests/utils_tests/test_dateformat.py @@ -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)