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
|
2008-08-11 20:11:25 +08:00
|
|
|
from django.db import connection
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.core import mail
|
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]
|
|
|
|
raise KeyError
|
|
|
|
else:
|
|
|
|
return super(ContextList, self).__getitem__(key)
|
|
|
|
|
|
|
|
|
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
|
|
|
class TestSMTPConnection(object):
|
|
|
|
"""A substitute SMTP connection for use during test sessions.
|
|
|
|
The test connection stores email messages in a dummy outbox,
|
|
|
|
rather than sending them out on the wire.
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
"""
|
|
|
|
def __init__(*args, **kwargs):
|
|
|
|
pass
|
|
|
|
def open(self):
|
|
|
|
"Mock the SMTPConnection open() interface"
|
|
|
|
pass
|
|
|
|
def close(self):
|
|
|
|
"Mock the SMTPConnection close() interface"
|
|
|
|
pass
|
|
|
|
def send_messages(self, messages):
|
|
|
|
"Redirect messages to the dummy outbox"
|
|
|
|
mail.outbox.extend(messages)
|
2007-08-16 18:54:28 +08:00
|
|
|
return len(messages)
|
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
|
2007-05-08 19:19:34 +08:00
|
|
|
- Diverting the email sending functions to a test buffer
|
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
|
|
|
"""
|
|
|
|
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
|
|
|
|
mail.SMTPConnection = TestSMTPConnection
|
|
|
|
|
|
|
|
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
|
|
|
"""
|
|
|
|
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
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
del mail.outbox
|
2007-08-16 14:06:55 +08:00
|
|
|
|
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
|