From 6ba506e281d606f74b49d7d6a53471b493c0b9d9 Mon Sep 17 00:00:00 2001 From: Akihiro Motoki Date: Sun, 13 Sep 2020 09:05:05 +0900 Subject: [PATCH] test: use safer way to override settings Django provides safer ways to override settings. We should avoid direct replacement of existing setting. Change-Id: I1fbcb79d18baaae037536b180989cf69c198fa7d --- .../dashboards/settings/user/tests.py | 9 ++++-- .../test/unit/api/test_base.py | 29 +++++++++---------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/openstack_dashboard/dashboards/settings/user/tests.py b/openstack_dashboard/dashboards/settings/user/tests.py index 54c49424d5..d5284add08 100644 --- a/openstack_dashboard/dashboards/settings/user/tests.py +++ b/openstack_dashboard/dashboards/settings/user/tests.py @@ -12,7 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -from django.conf import settings from django.urls import reverse from openstack_dashboard.test import helpers as test @@ -32,9 +31,13 @@ class UserSettingsTest(test.TestCase): def test_display_language(self): # Add an unknown language to LANGUAGES list - settings.LANGUAGES += (('unknown', 'Unknown Language'),) + with self.modify_settings( + # NOTE: modify_settings take a list of values to append. + # We add a single entry but it is a tuple, + # so we need to wrap it as a list. + LANGUAGES={'append': [('unknown', 'Unknown Language')]}): + res = self.client.get(INDEX_URL) - res = self.client.get(INDEX_URL) # Known language self.assertContains(res, 'English') # Unknown language diff --git a/openstack_dashboard/test/unit/api/test_base.py b/openstack_dashboard/test/unit/api/test_base.py index b3ba74d581..443e684c18 100644 --- a/openstack_dashboard/test/unit/api/test_base.py +++ b/openstack_dashboard/test/unit/api/test_base.py @@ -16,8 +16,6 @@ # License for the specific language governing permissions and limitations # under the License. -from django.conf import settings - from horizon import exceptions from openstack_dashboard.api import base as api_base @@ -232,21 +230,22 @@ class ApiVersionTests(test.TestCase): def setUp(self): super(ApiVersionTests, self).setUp() - self.previous_settings = settings.OPENSTACK_API_VERSIONS - settings.OPENSTACK_API_VERSIONS = { - "data-processing": 1.1, - "identity": "3", - "volume": 1 - } - # Make sure cached data from other tests doesn't interfere - cinder.VERSIONS.clear_active_cache() - keystone.VERSIONS.clear_active_cache() - glance.VERSIONS.clear_active_cache() + override = self.settings( + OPENSTACK_API_VERSIONS={ + "data-processing": 1.1, + "identity": "3", + "volume": 1 + } + ) + override.enable() + self.addCleanup(override.disable) - def tearDown(self): - super(ApiVersionTests, self).tearDown() - settings.OPENSTACK_API_VERSIONS = self.previous_settings + # Make sure cached data from other tests doesn't interfere + self._clear_version_cache() # Clear out our bogus data so it doesn't interfere + self.addCleanup(self._clear_version_cache) + + def _clear_version_cache(self): cinder.VERSIONS.clear_active_cache() keystone.VERSIONS.clear_active_cache() glance.VERSIONS.clear_active_cache()