Fixed a bug when setting a config var before getting one

This commit is contained in:
Oskar Hahn 2013-08-11 10:05:47 +02:00
parent 20cf960c82
commit 25bac4a704
3 changed files with 18 additions and 3 deletions

View File

@ -34,7 +34,12 @@ class ConfigHandler(object):
updated_rows = ConfigStore.objects.filter(key=key).update(value=value) updated_rows = ConfigStore.objects.filter(key=key).update(value=value)
if not updated_rows: if not updated_rows:
ConfigStore.objects.create(key=key, value=value) 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): def setup_cache(self):
""" """

View File

@ -25,5 +25,6 @@ class TestCase(_TestCase):
def _pre_setup(self, *args, **kwargs): def _pre_setup(self, *args, **kwargs):
return_value = super(TestCase, self)._pre_setup(*args, **kwargs) return_value = super(TestCase, self)._pre_setup(*args, **kwargs)
post_database_setup.send(sender=self) post_database_setup.send(sender=self)
config.setup_cache() # Resetting the config object by deleting the cache
del config._cache
return return_value return return_value

View File

@ -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') config_signal.disconnect(set_simple_config_page_multiple_vars, dispatch_uid='set_simple_config_page_multiple_vars_for_testing')
def test_database_queries(self): 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') self.assertNumQueries(0, self.get_config_var, key='string_var')
def test_setup_config_var(self): def test_setup_config_var(self):
@ -57,13 +61,18 @@ class HandleConfigTest(TestCase):
self.assertEqual(config['string_var'], 'other_special_unique_string dauTex9eAiy7jeen') self.assertEqual(config['string_var'], 'other_special_unique_string dauTex9eAiy7jeen')
def test_missing_cache_(self): def test_missing_cache_(self):
del config._cache
self.assertEqual(config['string_var'], 'default_string_rien4ooCZieng6ah') self.assertEqual(config['string_var'], 'default_string_rien4ooCZieng6ah')
def test_config_contains(self): def test_config_contains(self):
self.assertTrue('string_var' in config) self.assertTrue('string_var' in config)
self.assertFalse('unknown_config_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): class ConfigFormTest(TestCase):