mirror of
https://github.com/jazzband/django-analytical.git
synced 2026-03-16 22:20:25 +00:00
Add @with/out_apps TestCase decorators
Add decorators for adding and removing apps from INSTALLED_APPS in test cases.
This commit is contained in:
parent
e848c599c3
commit
60fc527ef0
2 changed files with 27 additions and 8 deletions
|
|
@ -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):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Reference in a new issue