From 7a21ea5464b61a8fcd43cd1b9b5f99c94687600a Mon Sep 17 00:00:00 2001 From: nessita <124304+nessita@users.noreply.github.com> Date: Mon, 13 Nov 2023 05:35:57 -0300 Subject: [PATCH] [5.0.x] Fixed #34958 -- Fixed isolation of messages_tests.tests.TestLevelTags.test_lazy. Backport of 1b56b24f81a2e64b4bd3059abad9b6fd0c801c66 from main --- tests/messages_tests/tests.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/messages_tests/tests.py b/tests/messages_tests/tests.py index 17eb8639e8..19aeee9a08 100644 --- a/tests/messages_tests/tests.py +++ b/tests/messages_tests/tests.py @@ -1,3 +1,5 @@ +import importlib +import sys from unittest import mock from django.conf import settings @@ -65,8 +67,13 @@ class TestLevelTags(SimpleTestCase): self.assertEqual(base.LEVEL_TAGS, self.message_tags) def test_lazy(self): + storage_base_import_path = "django.contrib.messages.storage.base" + in_use_base = sys.modules.pop(storage_base_import_path) + self.addCleanup(sys.modules.__setitem__, storage_base_import_path, in_use_base) # Don't use @override_settings to avoid calling the setting_changed - # signal. + # signal, but ensure that base.LEVEL_TAGS hasn't been read yet (this + # means that we need to ensure the `base` module is freshly imported). + base = importlib.import_module(storage_base_import_path) old_message_tags = getattr(settings, "MESSAGE_TAGS", None) settings.MESSAGE_TAGS = {constants.ERROR: "bad"} try: