From 25bac4a70487223cea67d0ab60e7c01ae4d8522f Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Sun, 11 Aug 2013 10:05:47 +0200 Subject: [PATCH] Fixed a bug when setting a config var before getting one --- openslides/config/api.py | 7 ++++++- openslides/utils/test.py | 3 ++- tests/config/test_config.py | 11 ++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/openslides/config/api.py b/openslides/config/api.py index fdef1aa67..165a5e56a 100644 --- a/openslides/config/api.py +++ b/openslides/config/api.py @@ -34,7 +34,12 @@ class ConfigHandler(object): updated_rows = ConfigStore.objects.filter(key=key).update(value=value) if not updated_rows: ConfigStore.objects.create(key=key, value=value) - self._cache[key] = value + try: + self._cache[key] = value + except AttributeError: + # This happens, when a config-var is set, before __getitem__ was + # called. In this case nothing should happen. + pass def setup_cache(self): """ diff --git a/openslides/utils/test.py b/openslides/utils/test.py index 263aea32e..c19111101 100644 --- a/openslides/utils/test.py +++ b/openslides/utils/test.py @@ -25,5 +25,6 @@ class TestCase(_TestCase): def _pre_setup(self, *args, **kwargs): return_value = super(TestCase, self)._pre_setup(*args, **kwargs) post_database_setup.send(sender=self) - config.setup_cache() + # Resetting the config object by deleting the cache + del config._cache return return_value diff --git a/tests/config/test_config.py b/tests/config/test_config.py index 13dfceaca..d58e22921 100644 --- a/tests/config/test_config.py +++ b/tests/config/test_config.py @@ -44,6 +44,10 @@ class HandleConfigTest(TestCase): config_signal.disconnect(set_simple_config_page_multiple_vars, dispatch_uid='set_simple_config_page_multiple_vars_for_testing') def test_database_queries(self): + """ + Test that no database queries are send, after the cache was created. + """ + config.setup_cache() self.assertNumQueries(0, self.get_config_var, key='string_var') def test_setup_config_var(self): @@ -57,13 +61,18 @@ class HandleConfigTest(TestCase): self.assertEqual(config['string_var'], 'other_special_unique_string dauTex9eAiy7jeen') def test_missing_cache_(self): - del config._cache self.assertEqual(config['string_var'], 'default_string_rien4ooCZieng6ah') def test_config_contains(self): self.assertTrue('string_var' in config) self.assertFalse('unknown_config_var' in config) + def test_set_value_before_getting_it(self): + """ + Try to call __setitem__ before __getitem. + """ + config['my_config_var'] = 'value' + class ConfigFormTest(TestCase):