def test_config_lazy_load_ok(self): """ It sets is_loaded *after* the reload. """ self.assertFalse(config._is_loaded) config._lazy_load() self.assertTrue(config._is_loaded)
def test_config_middleware_process_request(self): """ config middleware, reload cache """ ConfigModel.objects.create(key="char", value="foo") registry.register(BarForm) config._lazy_load() config._set("char", None) cache = config._cache # Should not reload since _updated_at does not exists (form was not saved) middleware = DjConfigMiddleware() middleware.process_request(request=None) self.assertIsNone(cache.get("char")) # Changing _updated_at should make it reload ConfigModel.objects.create(key="_updated_at", value="111") middleware.process_request(request=None) self.assertEqual(cache.get("char"), "foo") self.assertEqual(cache.get("_updated_at"), "111") # It does not update again, since _updated_at has not changed ConfigModel.objects.filter(key="char").update(value="bar") middleware.process_request(request=None) self.assertNotEqual(cache.get("char"), "bar") self.assertEqual(cache.get("_updated_at"), "111") # Changing _updated_at should make it reload ConfigModel.objects.filter(key="_updated_at").update(value="222") middleware.process_request(request=None) self.assertEqual(cache.get("char"), "bar") self.assertEqual(cache.get("_updated_at"), "222")
def test_config_load_from_database(self): """ Load configuration into the cache """ data = [ConfigModel(key='boolean', value=False), ConfigModel(key='boolean_false', value=True), ConfigModel(key='float_number', value=2.1), ConfigModel(key='char', value="foo2"), ConfigModel(key='email', value="*****@*****.**"), ConfigModel(key='integer', value=321), ConfigModel(key='url', value="foo2.com/")] ConfigModel.objects.bulk_create(data) registry.register(FooForm) config._lazy_load() keys = ['boolean', 'boolean_false', 'char', 'email', 'float_number', 'integer', 'url'] values = self.cache.get_many([prefixer(k) for k in keys]) self.assertDictEqual(values, {prefixer('boolean'): False, prefixer('boolean_false'): True, prefixer('float_number'): 2.1, prefixer('char'): "foo2", prefixer('email'): "*****@*****.**", prefixer('integer'): 321, prefixer('url'): "http://foo2.com/"}) # use initial if the field is not found in the db ConfigModel.objects.get(key='char').delete() config._reset() config._lazy_load() self.assertEqual(self.cache.get(prefixer('char')), "foo")
def test_config_middleware_process_request(self): """ config middleware, reload cache """ ConfigModel.objects.create(key="char", value="foo") registry.register(BarForm) config._lazy_load() config._set('char', None) cache = config._cache # Should not reload since _updated_at does not exists (form was not saved) middleware = DjConfigMiddleware() middleware.process_request(request=None) self.assertIsNone(cache.get('char')) # Changing _updated_at should make it reload ConfigModel.objects.create(key="_updated_at", value="111") middleware.process_request(request=None) self.assertEqual(cache.get('char'), "foo") self.assertEqual(cache.get("_updated_at"), "111") # It does not update again, since _updated_at has not changed ConfigModel.objects.filter(key="char").update(value="bar") middleware.process_request(request=None) self.assertNotEqual(cache.get('char'), "bar") self.assertEqual(cache.get("_updated_at"), "111") # Changing _updated_at should make it reload ConfigModel.objects.filter(key="_updated_at").update(value="222") middleware.process_request(request=None) self.assertEqual(cache.get('char'), "bar") self.assertEqual(cache.get("_updated_at"), "222")
def test_config_load_from_database_invalid(self): """ Load initial if the db value is invalid """ ConfigModel.objects.create(key='integer', value="string") registry.register(FooForm) config._lazy_load() self.assertEqual(self.cache.get(prefixer('integer')), 123)
def test_config_load_unicode(self): """ Load configuration into the cache """ ConfigModel.objects.create(key='char', value=u"áéíóú") registry.register(FooForm) config._lazy_load() self.assertEqual(self.cache.get(prefixer('char')), u"áéíóú")
def test_config_load_updated_at(self): """ Load updated_at """ registry.register(FooForm) config._lazy_load() value = self.cache.get(prefixer("_updated_at")) self.assertIsNone(value) ConfigModel.objects.create(key="_updated_at", value="string") config._reset() config._lazy_load() value = self.cache.get(prefixer("_updated_at")) self.assertEqual(value, "string")
def test_config_load(self): """ Load initial configuration into the cache """ registry.register(FooForm) config._lazy_load() keys = ['boolean', 'boolean_false', 'char', 'email', 'float_number', 'integer', 'url'] values = self.cache.get_many([prefixer(k) for k in keys]) self.assertDictEqual(values, {prefixer('boolean'): True, prefixer('boolean_false'): False, prefixer('char'): "foo", prefixer('email'): "*****@*****.**", prefixer('float_number'): 1.23, prefixer('integer'): 123, prefixer('url'): "foo.com/"})
def test_config_lazy_load_once(self): """ Reload is not called if already loaded """ class ConfigMock(Config): def _reload(self): raise ValueError config = ConfigMock() config._is_loaded = True self.assertIsNone(config._lazy_load())
def test_config_load_from_database(self): """ Load configuration into the cache """ data = [ ConfigModel(key='boolean', value=False), ConfigModel(key='boolean_false', value=True), ConfigModel(key='float_number', value=2.1), ConfigModel(key='char', value="foo2"), ConfigModel(key='email', value="*****@*****.**"), ConfigModel(key='integer', value=321), ConfigModel(key='url', value="foo2.com/") ] ConfigModel.objects.bulk_create(data) registry.register(FooForm) keys = [ 'boolean', 'boolean_false', 'char', 'email', 'float_number', 'integer', 'url' ] values = {k: getattr(config, k) for k in keys} self.assertDictEqual( values, { 'boolean': False, 'boolean_false': True, 'float_number': 2.1, 'char': "foo2", 'email': "*****@*****.**", 'integer': 321, 'url': "http://foo2.com/" }) # use initial if the field is not found in the db ConfigModel.objects.get(key='char').delete() config._reset() config._lazy_load() self.assertEqual(config.char, "foo")
def test_config_load_from_database(self): """ Load configuration into the cache """ data = [ ConfigModel(key="boolean", value=False), ConfigModel(key="boolean_false", value=True), ConfigModel(key="float_number", value=2.1), ConfigModel(key="char", value="foo2"), ConfigModel(key="email", value="*****@*****.**"), ConfigModel(key="integer", value=321), ConfigModel(key="url", value="foo2.com/"), ] ConfigModel.objects.bulk_create(data) registry.register(FooForm) keys = ["boolean", "boolean_false", "char", "email", "float_number", "integer", "url"] values = {k: getattr(config, k) for k in keys} self.assertDictEqual( values, { "boolean": False, "boolean_false": True, "float_number": 2.1, "char": "foo2", "email": "*****@*****.**", "integer": 321, "url": "http://foo2.com/", }, ) # use initial if the field is not found in the db ConfigModel.objects.get(key="char").delete() config._reset() config._lazy_load() self.assertEqual(config.char, "foo")