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
This commit is contained in:
Akihiro Motoki 2020-09-13 09:05:05 +09:00
parent 79e505520f
commit 6ba506e281
2 changed files with 20 additions and 18 deletions

View File

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

View File

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