From 60fc527ef0f2453ff8ce4b7a925a97dd5cce81ae Mon Sep 17 00:00:00 2001 From: Joost Cassee Date: Wed, 29 Jun 2011 08:10:02 +0200 Subject: [PATCH] Add @with/out_apps TestCase decorators Add decorators for adding and removing apps from INSTALLED_APPS in test cases. --- analytical/tests/test_tag_chartbeat.py | 13 +++++-------- analytical/tests/utils.py | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/analytical/tests/test_tag_chartbeat.py b/analytical/tests/test_tag_chartbeat.py index 337ca3f..023479d 100644 --- a/analytical/tests/test_tag_chartbeat.py +++ b/analytical/tests/test_tag_chartbeat.py @@ -11,16 +11,18 @@ from django.template import Context from analytical.templatetags.chartbeat import ChartbeatTopNode, \ ChartbeatBottomNode -from analytical.tests.utils import TagTestCase, override_settings +from analytical.tests.utils import TagTestCase, with_apps, without_apps from analytical.utils import AnalyticalException -@override_settings(INSTALLED_APPS=[ - a for a in settings.INSTALLED_APPS if a != 'django.contrib.sites']) + +@without_apps('django.contrib.sites') class ChartbeatTagTestCaseNoSites(TagTestCase): def test_rendering_setup_no_site(self): r = ChartbeatBottomNode().render(Context()) self.assertTrue('var _sf_async_config={"uid": "12345"};' in r, r) + +@with_apps('django.contrib.sites') class ChartbeatTagTestCaseWithSites(TagTestCase): def test_rendering_setup_site(self): site = Site.objects.create(domain="test.com", name="test") @@ -31,11 +33,6 @@ class ChartbeatTagTestCaseWithSites(TagTestCase): self.assertTrue(re.search( 'var _sf_async_config={.*"domain": "test.com".*};', r), r) -# Ensure django.contrib.sites is in INSTALLED_APPS -if "django.contrib.sites" not in settings.INSTALLED_APPS: - installed_apps = list(settings.INSTALLED_APPS) - installed_apps.append("django.contrib.sites") - ChartbeatTagTestCaseWithSites = override_settings(INSTALLED_APPS=installed_apps)(ChartbeatTagTestCaseWithSites) class ChartbeatTagTestCase(TagTestCase): """ diff --git a/analytical/tests/utils.py b/analytical/tests/utils.py index ff2b09c..38752cc 100644 --- a/analytical/tests/utils.py +++ b/analytical/tests/utils.py @@ -13,6 +13,8 @@ from django.utils.functional import wraps class override_settings(object): """ + Temporarily override Django settings. + Acts as either a decorator, or a context manager. If it's a decorator it takes a function and returns a wrapped function. If it's a contextmanager it's used with the ``with`` statement. In either event entering/exiting @@ -46,6 +48,7 @@ class override_settings(object): def disable(self): settings._wrapped = self.wrapped + def run_tests(labels=()): """ Use the Django test runner to run the tests. @@ -53,6 +56,25 @@ def run_tests(labels=()): django_run_tests(labels, verbosity=1, interactive=True) +def with_apps(*apps): + """ + Class decorator that makes sure the passed apps are present in + INSTALLED_APPS. + """ + apps_set = set(settings.INSTALLED_APPS) + apps_set.update(apps) + return override_settings(INSTALLED_APPS=list(apps_set)) + + +def without_apps(*apps): + """ + Class decorator that makes sure the passed apps are not present in + INSTALLED_APPS. + """ + apps_list = [a for a in settings.INSTALLED_APPS if a not in apps] + return override_settings(INSTALLED_APPS=apps_list) + + class TestCase(DjangoTestCase): """ Base test case for the django-analytical tests.