Merge pull request #4 from edavis/tests

Fix tests
This commit is contained in:
Joost Cassee 2011-07-04 05:02:24 -07:00
commit 52e54c8db8
3 changed files with 57 additions and 30 deletions

View file

@ -100,9 +100,14 @@ def contribute_to_analytical(add_node):
def _get_domain(context):
domain = context.get(DOMAIN_CONTEXT_KEY)
if domain is None and getattr(settings, 'CHARTBEAT_AUTO_DOMAIN', True):
try:
domain = Site.objects.get_current().domain
except (ImproperlyConfigured, Site.DoesNotExist): #pylint: disable=E1101
pass
return domain
if domain is not None:
return domain
else:
if 'django.contrib.sites' not in settings.INSTALLED_APPS:
return
elif getattr(settings, 'CHARTBEAT_AUTO_DOMAIN', True):
try:
return Site.objects.get_current().domain
except (ImproperlyConfigured, Site.DoesNotExist): #pylint: disable=E1101
return

View file

@ -8,34 +8,47 @@ from django.conf import settings
from django.contrib.sites.models import Site
from django.http import HttpRequest
from django.template import Context
from django.test import TestCase
from analytical.templatetags.chartbeat import ChartbeatTopNode, \
ChartbeatBottomNode
from analytical.tests.utils import TagTestCase, override_settings
from analytical.utils import AnalyticalException
@override_settings(INSTALLED_APPS=[
a for a in settings.INSTALLED_APPS if a != 'django.contrib.sites'])
class ChartbeatTagTestCaseNoSites(TagTestCase):
@override_settings(INSTALLED_APPS=[a for a in settings.INSTALLED_APPS if a != 'django.contrib.sites'],
CHARTBEAT_USER_ID="12345")
class ChartbeatTagTestCaseNoSites(TestCase):
def test_rendering_setup_no_site(self):
r = ChartbeatBottomNode().render(Context())
self.assertTrue('var _sf_async_config={"uid": "12345"};' in r, r)
class ChartbeatTagTestCaseWithSites(TagTestCase):
@override_settings(INSTALLED_APPS=settings.INSTALLED_APPS + ["django.contrib.sites"],
CHARTBEAT_USER_ID="12345")
class ChartbeatTagTestCaseWithSites(TestCase):
def setUp(self):
from django.core.management import call_command
from django.db.models import loading
loading.cache.loaded = False
call_command("syncdb", verbosity=0)
def test_rendering_setup_site(self):
site = Site.objects.create(domain="test.com", name="test")
with override_settings(SITE_ID=site.id, CHARTBEAT_USER_ID="12345"):
with override_settings(SITE_ID=site.id):
r = ChartbeatBottomNode().render(Context())
self.assertTrue(re.search(
'var _sf_async_config={.*"uid": "12345".*};', r), r)
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)
@override_settings(CHARTBEAT_AUTO_DOMAIN=False)
def test_auto_domain_false(self):
"""
Even if 'django.contrib.sites' is in INSTALLED_APPS, if
CHARTBEAT_AUTO_DOMAIN is False, ensure there is no 'domain'
in _sf_async_config.
"""
r = ChartbeatBottomNode().render(Context())
self.assertTrue('var _sf_async_config={"uid": "12345"};' in r, r)
class ChartbeatTagTestCase(TagTestCase):
"""

View file

@ -7,18 +7,16 @@ from django.conf import settings, UserSettingsHolder
from django.core.management import call_command
from django.db.models import loading
from django.template import Template, Context, RequestContext
from django.test.simple import run_tests as django_run_tests
from django.test.testcases import TestCase as DjangoTestCase
from django.utils.functional import wraps
# Backported from Django trunk (r16377)
class override_settings(object):
"""
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
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
are called before and after, respectively, the function/block is executed.
Via: http://djangosnippets.org/snippets/2437/
"""
def __init__(self, **kwargs):
self.options = kwargs
@ -30,11 +28,21 @@ class override_settings(object):
def __exit__(self, exc_type, exc_value, traceback):
self.disable()
def __call__(self, func):
@wraps(func)
def inner(*args, **kwargs):
with self:
return func(*args, **kwargs)
def __call__(self, test_func):
from django.test import TestCase
if isinstance(test_func, type) and issubclass(test_func, TestCase):
class inner(test_func):
def _pre_setup(innerself):
self.enable()
super(inner, innerself)._pre_setup()
def _post_teardown(innerself):
super(inner, innerself)._post_teardown()
self.disable()
else:
@wraps(test_func)
def inner(*args, **kwargs):
with self:
return test_func(*args, **kwargs)
return inner
def enable(self):
@ -46,12 +54,13 @@ class override_settings(object):
def disable(self):
settings._wrapped = self.wrapped
def run_tests(labels=()):
def run_tests():
"""
Use the Django test runner to run the tests.
"""
django_run_tests(labels, verbosity=1, interactive=True)
from django.test.simple import DjangoTestSuiteRunner
runner = DjangoTestSuiteRunner(verbosity=2)
runner.run_tests(None)
class TestCase(DjangoTestCase):
"""