|
0
|
1 |
import sys, time, os |
|
|
2 |
from django.conf import settings |
|
|
3 |
from django.db import connection |
|
|
4 |
from django.core import mail |
|
|
5 |
from django.test import signals |
|
|
6 |
from django.template import Template |
|
|
7 |
from django.utils.translation import deactivate |
|
|
8 |
|
|
|
9 |
class ContextList(list): |
|
|
10 |
"""A wrapper that provides direct key access to context items contained |
|
|
11 |
in a list of context objects. |
|
|
12 |
""" |
|
|
13 |
def __getitem__(self, key): |
|
|
14 |
if isinstance(key, basestring): |
|
|
15 |
for subcontext in self: |
|
|
16 |
if key in subcontext: |
|
|
17 |
return subcontext[key] |
|
|
18 |
raise KeyError |
|
|
19 |
else: |
|
|
20 |
return super(ContextList, self).__getitem__(key) |
|
|
21 |
|
|
|
22 |
|
|
|
23 |
def instrumented_test_render(self, context): |
|
|
24 |
""" |
|
|
25 |
An instrumented Template render method, providing a signal |
|
|
26 |
that can be intercepted by the test system Client |
|
|
27 |
""" |
|
|
28 |
signals.template_rendered.send(sender=self, template=self, context=context) |
|
|
29 |
return self.nodelist.render(context) |
|
|
30 |
|
|
|
31 |
class TestSMTPConnection(object): |
|
|
32 |
"""A substitute SMTP connection for use during test sessions. |
|
|
33 |
The test connection stores email messages in a dummy outbox, |
|
|
34 |
rather than sending them out on the wire. |
|
|
35 |
|
|
|
36 |
""" |
|
|
37 |
def __init__(*args, **kwargs): |
|
|
38 |
pass |
|
|
39 |
def open(self): |
|
|
40 |
"Mock the SMTPConnection open() interface" |
|
|
41 |
pass |
|
|
42 |
def close(self): |
|
|
43 |
"Mock the SMTPConnection close() interface" |
|
|
44 |
pass |
|
|
45 |
def send_messages(self, messages): |
|
|
46 |
"Redirect messages to the dummy outbox" |
|
|
47 |
mail.outbox.extend(messages) |
|
|
48 |
return len(messages) |
|
|
49 |
|
|
|
50 |
def setup_test_environment(): |
|
|
51 |
"""Perform any global pre-test setup. This involves: |
|
|
52 |
|
|
|
53 |
- Installing the instrumented test renderer |
|
|
54 |
- Diverting the email sending functions to a test buffer |
|
|
55 |
- Setting the active locale to match the LANGUAGE_CODE setting. |
|
|
56 |
""" |
|
|
57 |
Template.original_render = Template.render |
|
|
58 |
Template.render = instrumented_test_render |
|
|
59 |
|
|
|
60 |
mail.original_SMTPConnection = mail.SMTPConnection |
|
|
61 |
mail.SMTPConnection = TestSMTPConnection |
|
|
62 |
|
|
|
63 |
mail.outbox = [] |
|
|
64 |
|
|
|
65 |
deactivate() |
|
|
66 |
|
|
|
67 |
def teardown_test_environment(): |
|
|
68 |
"""Perform any global post-test teardown. This involves: |
|
|
69 |
|
|
|
70 |
- Restoring the original test renderer |
|
|
71 |
- Restoring the email sending functions |
|
|
72 |
|
|
|
73 |
""" |
|
|
74 |
Template.render = Template.original_render |
|
|
75 |
del Template.original_render |
|
|
76 |
|
|
|
77 |
mail.SMTPConnection = mail.original_SMTPConnection |
|
|
78 |
del mail.original_SMTPConnection |
|
|
79 |
|
|
|
80 |
del mail.outbox |
|
|
81 |
|
|
|
82 |
|
|
|
83 |
def get_runner(settings): |
|
|
84 |
test_path = settings.TEST_RUNNER.split('.') |
|
|
85 |
# Allow for Python 2.5 relative paths |
|
|
86 |
if len(test_path) > 1: |
|
|
87 |
test_module_name = '.'.join(test_path[:-1]) |
|
|
88 |
else: |
|
|
89 |
test_module_name = '.' |
|
|
90 |
test_module = __import__(test_module_name, {}, {}, test_path[-1]) |
|
|
91 |
test_runner = getattr(test_module, test_path[-1]) |
|
|
92 |
return test_runner |