Skip to content

Commit

Permalink
[#1665] Add new_tests.helpers.change_config() decorator
Browse files Browse the repository at this point in the history
This allows you to temporarily change a CKAN's config value during a test.
It'll restore the values to what they were before, after you test is ran.

Conflicts:
	ckan/new_tests/helpers.py
  • Loading branch information
vitorbaptista authored and wardi committed Jun 13, 2014
1 parent fde7096 commit 746cfc5
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion ckan/new_tests/helpers.py
Expand Up @@ -17,8 +17,9 @@
This module is reserved for these very useful functions.
'''
import pylons.config as config
import webtest
from pylons import config
import nose.tools

import ckan.config.middleware
import ckan.model as model
Expand Down Expand Up @@ -137,3 +138,37 @@ def _get_test_app():
app = ckan.config.middleware.make_app(config['global_conf'], **config)
app = webtest.TestApp(app)
return app


def change_config(key, value):
'''Decorator to temporarily changes Pylons' config to a new value
This allows you to easily create tests that need specific config values to
be set, making sure it'll be reverted to what it was originally, after your
test is run.
Usage::
@helpers.change_config('ckan.site_title', 'My Test CKAN')
def test_ckan_site_title(self):
assert pylons.config['ckan.site_title'] == 'My Test CKAN'
:param key: the config key to be changed, e.g. ``'ckan.site_title'``
:type key: string
:param value: the new config key's value, e.g. ``'My Test CKAN'``
:type value: string
'''
def decorator(func):
def wrapper(*args, **kwargs):
_original_config = config.copy()
config[key] = value

return_value = func(*args, **kwargs)

config.clear()
config.update(_original_config)

return return_value
return nose.tools.make_decorator(func)(wrapper)
return decorator

0 comments on commit 746cfc5

Please sign in to comment.