2007-12-17 16:50:50 +08:00
|
|
|
import sys, time, os
|
2006-08-27 20:24:59 +08:00
|
|
|
from django.conf import settings
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.core import mail
|
2009-11-03 20:53:26 +08:00
|
|
|
from django.core.mail.backends import locmem
|
2006-09-02 17:26:24 +08:00
|
|
|
from django.test import signals
|
|
|
|
from django.template import Template
|
2007-10-22 01:26:32 +08:00
|
|
|
from django.utils.translation import deactivate
|
2006-08-27 20:24:59 +08:00
|
|
|
|
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
|
|
|
|
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
|
|
|
"""
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.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
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
mail.original_SMTPConnection = mail.SMTPConnection
|
2009-11-03 20:53:26 +08:00
|
|
|
mail.SMTPConnection = locmem.EmailBackend
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
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
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
mail.SMTPConnection = mail.original_SMTPConnection
|
|
|
|
del mail.original_SMTPConnection
|
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
|
|
|
|
|
|
|
def get_runner(settings):
|
|
|
|
test_path = settings.TEST_RUNNER.split('.')
|
|
|
|
# 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
|