def test_get_existing_keys_from_default(self): config = app_config.Config({}, {'foo': { 'bar': 'baz', 'doo': 'ding', }}) self.assertEqual(config.get_config('foo', 'bar'), 'baz') self.assertEqual(config.get_config('foo', 'doo'), 'ding')
def test_set_jinja2(self): app = webapp2.WSGIApplication(debug=True) app.config = config.Config() self.assertEqual(len(app.registry), 0) jinja2.set_jinja2(jinja2.Jinja2(app), app=app) self.assertEqual(len(app.registry), 1) j = jinja2.get_jinja2(app=app) self.assertTrue(isinstance(j, jinja2.Jinja2))
def test_default_config(self): config = app_config.Config() from resources.template import default_config as template_config from resources.i18n import default_config as i18n_config self.assertEqual(config['resources.template']['templates_dir'], template_config['templates_dir']) self.assertEqual(config['resources.i18n']['locale'], i18n_config['locale']) self.assertEqual(config['resources.i18n']['timezone'], i18n_config['timezone'])
def test_override_config2(self): config = app_config.Config({ 'resources.i18n': { 'timezone': 'America/Sao_Paulo', }, }) self.assertEqual(config['resources.i18n']['locale'], 'en_US') self.assertEqual(config['resources.i18n']['timezone'], 'America/Sao_Paulo')
def test_setitem_no_dict_values(self): config = app_config.Config() def setitem(key, value): config[key] = value return config self.assertRaises(AssertionError, setitem, 'foo', 'bar') self.assertRaises(AssertionError, setitem, 'foo', None)
def test_get_template_attribute(self): app = webapp2.WSGIApplication(debug=True) app.config = config.Config( {'webapp2_extras.jinja2': { 'template_path': template_path, }}) j = jinja2.Jinja2(app) hello = j.get_template_attribute('hello.html', 'hello') self.assertEqual(hello('World'), 'Hello, World!')
def test_get_dict_existing_keys(self): config = app_config.Config({'foo': { 'bar': 'baz', 'doo': 'ding', }}) self.assertEqual(config.get_config('foo'), { 'bar': 'baz', 'doo': 'ding', })
def test_default_config_with_non_existing_key(self): config = app_config.Config() from resources.i18n import default_config as i18n_config # In the first time the module config will be loaded normally. self.assertEqual(config['resources.i18n']['locale'], i18n_config['locale']) # In the second time it won't be loaded, but won't find the value and then use the default. self.assertEqual(config['resources.i18n'].get('i_dont_exist', 'foo'), 'foo')
def test_setdefault(self): config = app_config.Config() self.assertRaises(KeyError, config.get_config, 'foo') config.setdefault('foo', { 'bar': 'baz', 'doo': 'ding', }) self.assertEqual(config.get_config('foo', 'bar'), 'baz') self.assertEqual(config.get_config('foo', 'doo'), 'ding')
def setUp(self): super(self.__class__, self).setUp() app = webapp2.WSGIApplication() request = webapp2.Request.blank('/') request.app = app app.set_globals(app=app, request=request) app.config = config.Config() self.app = app self.request = request
def test_get(self): config = app_config.Config({'foo': { 'bar': 'baz', 'doo': 'ding', }}) self.assertEqual(config.get('foo'), { 'bar': 'baz', 'doo': 'ding', }) self.assertEqual(config.get('bar'), {})
def test_update(self): config = app_config.Config({'foo': { 'bar': 'baz', 'doo': 'ding', }}) self.assertEqual(config.get_config('foo', 'bar'), 'baz') self.assertEqual(config.get_config('foo', 'doo'), 'ding') config.update('foo', {'bar': 'other'}) self.assertEqual(config.get_config('foo', 'bar'), 'other') self.assertEqual(config.get_config('foo', 'doo'), 'ding')
def test_override_config(self): config = app_config.Config({ 'resources.template': { 'templates_dir': 'apps/templates' }, 'resources.i18n': { 'locale': 'pt_BR', 'timezone': 'America/Sao_Paulo', }, }) self.assertEqual(config['resources.template']['templates_dir'], 'apps/templates') self.assertEqual(config['resources.i18n']['locale'], 'pt_BR') self.assertEqual(config['resources.i18n']['timezone'], 'America/Sao_Paulo')
def test_render_template_force_compiled(self): app = webapp2.WSGIApplication(debug=True) app.config = config.Config({ 'webapp2_extras.jinja2': { 'template_path': template_path, 'compiled_path': compiled_path, 'force_compiled': True, } }) req = webapp2.Request.blank('/') app.set_globals(app=app, request=req) j = jinja2.Jinja2(app) message = 'Hello, World!' res = j.render_template('template1.html', message=message) self.assertEqual(res, message)
def test_setdefault2(self): config = app_config.Config({'foo': { 'bar': 'baz', }}) self.assertEqual(config.get_config('foo'), { 'bar': 'baz', }) config.setdefault('foo', { 'bar': 'wooo', 'doo': 'ding', }) self.assertEqual(config.get_config('foo', 'bar'), 'baz') self.assertEqual(config.get_config('foo', 'doo'), 'ding')
def test_render_template_globals_filters(self): app = webapp2.WSGIApplication(debug=True) app.config = config.Config({ 'webapp2_extras.jinja2': { 'template_path': template_path, 'globals': dict(foo='fooglobal'), 'filters': dict(foo=lambda x: x + '-foofilter'), }, }) req = webapp2.Request.blank('/') app.set_globals(app=app, request=req) j = jinja2.Jinja2(app) message = 'fooglobal-foofilter' res = j.render_template('template3.html', message=message) self.assertEqual(res, message)
def test_render_template_with_i18n(self): app = webapp2.WSGIApplication(debug=True) app.config = config.Config({ 'webapp2_extras.jinja2': { 'template_path': template_path, 'compiled_path': compiled_path, 'environment_args': { 'autoescape': True, 'extensions': [ 'jinja2.ext.autoescape', 'jinja2.ext.with_', 'jinja2.ext.i18n', ], }, }, }) req = webapp2.Request.blank('/') app.set_globals(app=app, request=req) j = jinja2.Jinja2(app) message = 'Hello, i18n World!' res = j.render_template('template2.html', message=message) self.assertEqual(res, message)
def test_get_with_default(self): config = app_config.Config() self.assertRaises(KeyError, config.get_config, 'foo', 'bar', 'ooops') self.assertRaises(KeyError, config.get_config, 'foo', 'doo', 'wooo')
def test_get_dict_non_existing_keys(self): config = app_config.Config() self.assertRaises(KeyError, config.get_config, 'bar')
def test_missing_key(self): config = app_config.Config() self.assertRaises(KeyError, config['resources.i18n'].__getitem__, 'i_dont_exist')
def test_missing_module(self): config = app_config.Config() self.assertRaises(KeyError, config.__getitem__, 'i_dont_exist')
def test_missing_default_config(self): config = app_config.Config() self.assertRaises(KeyError, config.get_config, 'tipfy', 'foo')
def test_get_with_default_and_module_load(self): config = app_config.Config() self.assertEqual(config['resources.i18n']['locale'], 'en_US') self.assertEqual(config['resources.i18n'].get('locale', 'foo'), 'en_US')
def test_get_with_default(self): config = app_config.Config() self.assertEqual(config['resources.i18n'].get('bar', 'baz'), 'baz')
def test_get(self): config = app_config.Config({'foo': { 'bar': 'baz', }}) self.assertEqual(config['foo']['bar'], 'baz')
def test_required_config(self): config = app_config.Config() self.assertRaises(KeyError, config.get_config, 'resources.i18n', 'foo')
def test_missing_module2(self): config = app_config.Config() self.assertRaises(KeyError, config.get_config, 'i_dont_exist')
def test_get_with_default_and_none(self): config = app_config.Config({'foo': { 'bar': None, }}) self.assertEqual(config.get_config('foo', 'bar', 'ooops'), None)
def test_required_config(self): config = app_config.Config() self.assertRaises(KeyError, config['resources.i18n'].__getitem__, 'foo') self.assertRaises(KeyError, config['resources.i18n'].__getitem__, 'required')
def test_missing_key(self): config = app_config.Config() self.assertRaises(KeyError, config.get_config, 'resources.i18n', 'i_dont_exist')