From 51eda877f48faed59c282a7bfcdae9751e7a56d6 Mon Sep 17 00:00:00 2001 From: adrian-turjak Date: Mon, 22 May 2017 18:52:45 +1200 Subject: [PATCH] Fix class level settings modification * Fixing issues with class level modification of settings file in tests. Change-Id: I58d6d2d493ca8cd9f56947c0ec48f8c546ce1729 --- adjutant/api/v1/tests/__init__.py | 40 +++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/adjutant/api/v1/tests/__init__.py b/adjutant/api/v1/tests/__init__.py index a60a5ae..5a5df60 100644 --- a/adjutant/api/v1/tests/__init__.py +++ b/adjutant/api/v1/tests/__init__.py @@ -322,7 +322,7 @@ class modify_dict_settings(override_settings): super(override_settings, self).__init__() def save_options(self, test_func): - if test_func._modified_dict_settings is None: + if getattr(test_func, "_modified_dict_settings", None) is None: test_func._modified_dict_settings = self.operations else: # Duplicate list to prevent subclasses from altering their parent. @@ -400,20 +400,22 @@ class modify_dict_settings(override_settings): class TestCaseMixin(object): """ Mixin to add modify_dict_settings functions to test classes """ - @classmethod - def setUpClass(cls): - super(AdjutantAPITestCase, cls).setUpClass() - if cls._modified_dict_settings: - cls._cls_modifyied_dict_context = override_settings( - **cls._overridden_settings) - cls._cls_modifyied_dict_context.enable() @classmethod - def tearDownClass(cls): + def _apply_settings_changes(cls): + if getattr(cls, '_modified_dict_settings', None): + operations = {} + for key, value in cls._modified_dict_settings: + operations[key] = value + cls._cls_modified_dict_context = modify_dict_settings( + **operations) + cls._cls_modified_dict_context.enable() + + @classmethod + def _remove_settings_changes(cls): if hasattr(cls, '_cls_modified_dict_context'): cls._cls_modified_dict_context.disable() delattr(cls, '_cls_modified_dict_context') - super(AdjutantAPITestCase, cls).tearDownClass() def modify_dict_settings(self, **kwargs): return modify_dict_settings(**kwargs) @@ -424,6 +426,15 @@ class AdjutantTestCase(TestCase, TestCaseMixin): TestCase override that has support for @modify_dict_settings as a class decorator and internal function """ + @classmethod + def setUpClass(cls): + super(AdjutantTestCase, cls).setUpClass() + cls._apply_settings_changes() + + @classmethod + def tearDownClass(cls): + cls._remove_settings_changes() + super(AdjutantTestCase, cls).tearDownClass() class AdjutantAPITestCase(APITestCase, TestCaseMixin): @@ -431,3 +442,12 @@ class AdjutantAPITestCase(APITestCase, TestCaseMixin): APITestCase override that has support for @modify_dict_settings as a class decorator, and internal function """ + @classmethod + def setUpClass(cls): + super(AdjutantAPITestCase, cls).setUpClass() + cls._apply_settings_changes() + + @classmethod + def tearDownClass(cls): + cls._remove_settings_changes() + super(AdjutantAPITestCase, cls).tearDownClass()