2011-05-18 20:08:53 +08:00
|
|
|
from __future__ import with_statement
|
|
|
|
|
2010-11-11 23:06:20 +08:00
|
|
|
import warnings
|
2011-05-18 20:08:53 +08:00
|
|
|
from django.conf import settings, UserSettingsHolder
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.core import mail
|
2011-05-18 20:08:53 +08:00
|
|
|
from django.test.signals import template_rendered, setting_changed
|
2011-04-17 12:52:17 +08:00
|
|
|
from django.template import Template, loader, TemplateDoesNotExist
|
|
|
|
from django.template.loaders import cached
|
2007-10-22 01:26:32 +08:00
|
|
|
from django.utils.translation import deactivate
|
2011-05-18 20:08:53 +08:00
|
|
|
from django.utils.functional import wraps
|
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
|
2011-05-18 20:08:53 +08:00
|
|
|
__all__ = (
|
|
|
|
'Approximate', 'ContextList', 'get_runner', 'override_settings',
|
|
|
|
'setup_test_environment', 'teardown_test_environment',
|
|
|
|
)
|
2010-08-20 22:28:42 +08:00
|
|
|
|
2011-04-17 12:52:17 +08:00
|
|
|
RESTORE_LOADERS_ATTR = '_original_template_source_loaders'
|
|
|
|
|
2010-10-19 00:08:25 +08:00
|
|
|
|
2010-08-20 22:28:42 +08:00
|
|
|
class Approximate(object):
|
|
|
|
def __init__(self, val, places=7):
|
|
|
|
self.val = val
|
|
|
|
self.places = places
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return repr(self.val)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if self.val == other:
|
|
|
|
return True
|
|
|
|
return round(abs(self.val-other), self.places) == 0
|
|
|
|
|
|
|
|
|
2009-03-18 18:46:55 +08:00
|
|
|
class ContextList(list):
|
|
|
|
"""A wrapper that provides direct key access to context items contained
|
|
|
|
in a list of context objects.
|
|
|
|
"""
|
|
|
|
def __getitem__(self, key):
|
|
|
|
if isinstance(key, basestring):
|
|
|
|
for subcontext in self:
|
|
|
|
if key in subcontext:
|
|
|
|
return subcontext[key]
|
2010-01-22 23:31:14 +08:00
|
|
|
raise KeyError(key)
|
2009-03-18 18:46:55 +08:00
|
|
|
else:
|
|
|
|
return super(ContextList, self).__getitem__(key)
|
|
|
|
|
2010-08-07 00:34:34 +08:00
|
|
|
def __contains__(self, key):
|
|
|
|
try:
|
|
|
|
value = self[key]
|
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
return True
|
2009-03-18 18:46:55 +08:00
|
|
|
|
2010-11-11 23:06:20 +08:00
|
|
|
|
2006-09-02 17:26:24 +08:00
|
|
|
def instrumented_test_render(self, context):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
"""
|
|
|
|
An instrumented Template render method, providing a signal
|
2007-06-22 15:15:04 +08:00
|
|
|
that can be intercepted by the test system Client
|
2006-09-02 17:26:24 +08:00
|
|
|
"""
|
2011-05-18 20:08:53 +08:00
|
|
|
template_rendered.send(sender=self, template=self, context=context)
|
2006-09-02 17:26:24 +08:00
|
|
|
return self.nodelist.render(context)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
|
2006-09-02 17:26:24 +08:00
|
|
|
def setup_test_environment():
|
|
|
|
"""Perform any global pre-test setup. This involves:
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2006-09-02 17:26:24 +08:00
|
|
|
- Installing the instrumented test renderer
|
2009-11-03 20:53:26 +08:00
|
|
|
- Set the email backend to the locmem email backend.
|
2007-10-22 01:26:32 +08:00
|
|
|
- Setting the active locale to match the LANGUAGE_CODE setting.
|
2006-09-02 17:26:24 +08:00
|
|
|
"""
|
2009-12-14 20:08:23 +08:00
|
|
|
Template.original_render = Template._render
|
|
|
|
Template._render = instrumented_test_render
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2009-11-03 20:53:26 +08:00
|
|
|
mail.original_email_backend = settings.EMAIL_BACKEND
|
2010-01-04 20:05:04 +08:00
|
|
|
settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
|
2007-05-08 19:19:34 +08:00
|
|
|
|
|
|
|
mail.outbox = []
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-10-22 01:26:32 +08:00
|
|
|
deactivate()
|
|
|
|
|
2010-11-11 23:06:20 +08:00
|
|
|
|
2006-09-02 17:26:24 +08:00
|
|
|
def teardown_test_environment():
|
|
|
|
"""Perform any global post-test teardown. This involves:
|
|
|
|
|
|
|
|
- Restoring the original test renderer
|
2007-05-08 19:19:34 +08:00
|
|
|
- Restoring the email sending functions
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2006-09-02 17:26:24 +08:00
|
|
|
"""
|
2009-12-14 20:08:23 +08:00
|
|
|
Template._render = Template.original_render
|
2007-06-22 15:15:04 +08:00
|
|
|
del Template.original_render
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2009-11-03 20:53:26 +08:00
|
|
|
settings.EMAIL_BACKEND = mail.original_email_backend
|
|
|
|
del mail.original_email_backend
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2009-11-03 20:53:26 +08:00
|
|
|
del mail.outbox
|
2009-02-28 12:46:38 +08:00
|
|
|
|
2010-11-11 23:06:20 +08:00
|
|
|
|
|
|
|
def get_warnings_state():
|
|
|
|
"""
|
|
|
|
Returns an object containing the state of the warnings module
|
|
|
|
"""
|
|
|
|
# There is no public interface for doing this, but this implementation of
|
|
|
|
# get_warnings_state and restore_warnings_state appears to work on Python
|
|
|
|
# 2.4 to 2.7.
|
|
|
|
return warnings.filters[:]
|
|
|
|
|
|
|
|
|
|
|
|
def restore_warnings_state(state):
|
|
|
|
"""
|
|
|
|
Restores the state of the warnings module when passed an object that was
|
|
|
|
returned by get_warnings_state()
|
|
|
|
"""
|
|
|
|
warnings.filters = state[:]
|
|
|
|
|
|
|
|
|
2011-06-10 16:26:05 +08:00
|
|
|
def get_runner(settings, test_runner_class=None):
|
|
|
|
if not test_runner_class:
|
|
|
|
test_runner_class = settings.TEST_RUNNER
|
|
|
|
|
|
|
|
test_path = test_runner_class.split('.')
|
2009-02-28 12:46:38 +08:00
|
|
|
# Allow for Python 2.5 relative paths
|
|
|
|
if len(test_path) > 1:
|
|
|
|
test_module_name = '.'.join(test_path[:-1])
|
|
|
|
else:
|
|
|
|
test_module_name = '.'
|
|
|
|
test_module = __import__(test_module_name, {}, {}, test_path[-1])
|
|
|
|
test_runner = getattr(test_module, test_path[-1])
|
|
|
|
return test_runner
|
2011-04-17 12:52:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
def setup_test_template_loader(templates_dict, use_cached_loader=False):
|
|
|
|
"""
|
|
|
|
Changes Django to only find templates from within a dictionary (where each
|
|
|
|
key is the template name and each value is the corresponding template
|
|
|
|
content to return).
|
|
|
|
|
|
|
|
Use meth:`restore_template_loaders` to restore the original loaders.
|
|
|
|
"""
|
|
|
|
if hasattr(loader, RESTORE_LOADERS_ATTR):
|
|
|
|
raise Exception("loader.%s already exists" % RESTORE_LOADERS_ATTR)
|
|
|
|
|
|
|
|
def test_template_loader(template_name, template_dirs=None):
|
|
|
|
"A custom template loader that loads templates from a dictionary."
|
|
|
|
try:
|
|
|
|
return (templates_dict[template_name], "test:%s" % template_name)
|
|
|
|
except KeyError:
|
|
|
|
raise TemplateDoesNotExist(template_name)
|
|
|
|
|
|
|
|
if use_cached_loader:
|
|
|
|
template_loader = cached.Loader(('test_template_loader',))
|
|
|
|
template_loader._cached_loaders = (test_template_loader,)
|
|
|
|
else:
|
|
|
|
template_loader = test_template_loader
|
|
|
|
|
|
|
|
setattr(loader, RESTORE_LOADERS_ATTR, loader.template_source_loaders)
|
|
|
|
loader.template_source_loaders = (template_loader,)
|
|
|
|
return template_loader
|
|
|
|
|
|
|
|
|
|
|
|
def restore_template_loaders():
|
|
|
|
"""
|
|
|
|
Restores the original template loaders after
|
|
|
|
:meth:`setup_test_template_loader` has been run.
|
|
|
|
"""
|
|
|
|
loader.template_source_loaders = getattr(loader, RESTORE_LOADERS_ATTR)
|
|
|
|
delattr(loader, RESTORE_LOADERS_ATTR)
|
2011-05-18 20:08:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
are called before and after, respectively, the function/block is executed.
|
|
|
|
"""
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.options = kwargs
|
|
|
|
self.wrapped = settings._wrapped
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.enable()
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
self.disable()
|
|
|
|
|
2011-06-12 04:40:02 +08:00
|
|
|
def __call__(self, test_func):
|
2011-08-11 06:26:34 +08:00
|
|
|
from django.test import TransactionTestCase
|
|
|
|
if isinstance(test_func, type) and issubclass(test_func, TransactionTestCase):
|
2011-10-08 16:16:17 +08:00
|
|
|
original_pre_setup = test_func._pre_setup
|
|
|
|
original_post_teardown = test_func._post_teardown
|
2011-08-23 08:32:35 +08:00
|
|
|
def _pre_setup(innerself):
|
|
|
|
self.enable()
|
2011-10-08 16:16:17 +08:00
|
|
|
original_pre_setup(innerself)
|
2011-08-23 08:32:35 +08:00
|
|
|
def _post_teardown(innerself):
|
2011-10-08 16:16:17 +08:00
|
|
|
original_post_teardown(innerself)
|
2011-08-23 08:32:35 +08:00
|
|
|
self.disable()
|
2011-10-08 16:16:17 +08:00
|
|
|
test_func._pre_setup = _pre_setup
|
|
|
|
test_func._post_teardown = _post_teardown
|
|
|
|
return test_func
|
2011-06-12 04:40:02 +08:00
|
|
|
else:
|
|
|
|
@wraps(test_func)
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
with self:
|
|
|
|
return test_func(*args, **kwargs)
|
2011-05-18 20:08:53 +08:00
|
|
|
return inner
|
|
|
|
|
|
|
|
def enable(self):
|
2012-03-14 07:02:31 +08:00
|
|
|
override = UserSettingsHolder(settings._wrapped)
|
2011-05-18 20:08:53 +08:00
|
|
|
for key, new_value in self.options.items():
|
|
|
|
setattr(override, key, new_value)
|
|
|
|
settings._wrapped = override
|
2012-03-14 07:02:31 +08:00
|
|
|
for key, new_value in self.options.items():
|
|
|
|
setting_changed.send(sender=settings._wrapped.__class__,
|
|
|
|
setting=key, value=new_value)
|
2011-05-18 20:08:53 +08:00
|
|
|
|
|
|
|
def disable(self):
|
|
|
|
settings._wrapped = self.wrapped
|
2012-03-14 07:02:31 +08:00
|
|
|
for key in self.options:
|
|
|
|
new_value = getattr(settings, key, None)
|
|
|
|
setting_changed.send(sender=settings._wrapped.__class__,
|
|
|
|
setting=key, value=new_value)
|
|
|
|
|