Merge pull request #851 from ostcar/fix_config

Fixed a bug when setting a config var before getting one
This commit is contained in:
Oskar Hahn 2013-08-20 07:16:08 -07:00
commit 142003ab7f
3 changed files with 27 additions and 4 deletions

View File

@ -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):
"""

View File

@ -25,5 +25,14 @@ 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()
return return_value
def _post_teardown(self, *args, **kwargs):
return_value = super(TestCase, self)._post_teardown(*args, **kwargs)
# Resets the config object by deleting the cache
try:
del config._cache
except AttributeError:
# The cache has only to be deleted if it exists.
pass
return return_value

View File

@ -24,7 +24,7 @@ from openslides.config.exceptions import ConfigError, ConfigNotFound
class HandleConfigTest(TestCase):
def get_config_var(self, key):
return config[key]
return config[key]
def test_get_config_default_value(self):
self.assertEqual(config['string_var'], 'default_string_rien4ooCZieng6ah')
@ -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):