1 import sys, time, os |
1 import sys, time, os |
2 from django.conf import settings |
2 from django.conf import settings |
3 from django.db import connection |
|
4 from django.core import mail |
3 from django.core import mail |
|
4 from django.core.mail.backends import locmem |
5 from django.test import signals |
5 from django.test import signals |
6 from django.template import Template |
6 from django.template import Template |
7 from django.utils.translation import deactivate |
7 from django.utils.translation import deactivate |
8 |
8 |
9 class ContextList(list): |
9 class ContextList(list): |
13 def __getitem__(self, key): |
13 def __getitem__(self, key): |
14 if isinstance(key, basestring): |
14 if isinstance(key, basestring): |
15 for subcontext in self: |
15 for subcontext in self: |
16 if key in subcontext: |
16 if key in subcontext: |
17 return subcontext[key] |
17 return subcontext[key] |
18 raise KeyError |
18 raise KeyError(key) |
19 else: |
19 else: |
20 return super(ContextList, self).__getitem__(key) |
20 return super(ContextList, self).__getitem__(key) |
21 |
21 |
22 |
22 |
23 def instrumented_test_render(self, context): |
23 def instrumented_test_render(self, context): |
26 that can be intercepted by the test system Client |
26 that can be intercepted by the test system Client |
27 """ |
27 """ |
28 signals.template_rendered.send(sender=self, template=self, context=context) |
28 signals.template_rendered.send(sender=self, template=self, context=context) |
29 return self.nodelist.render(context) |
29 return self.nodelist.render(context) |
30 |
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 |
31 |
50 def setup_test_environment(): |
32 def setup_test_environment(): |
51 """Perform any global pre-test setup. This involves: |
33 """Perform any global pre-test setup. This involves: |
52 |
34 |
53 - Installing the instrumented test renderer |
35 - Installing the instrumented test renderer |
54 - Diverting the email sending functions to a test buffer |
36 - Set the email backend to the locmem email backend. |
55 - Setting the active locale to match the LANGUAGE_CODE setting. |
37 - Setting the active locale to match the LANGUAGE_CODE setting. |
56 """ |
38 """ |
57 Template.original_render = Template.render |
39 Template.original_render = Template._render |
58 Template.render = instrumented_test_render |
40 Template._render = instrumented_test_render |
59 |
41 |
60 mail.original_SMTPConnection = mail.SMTPConnection |
42 mail.original_SMTPConnection = mail.SMTPConnection |
61 mail.SMTPConnection = TestSMTPConnection |
43 mail.SMTPConnection = locmem.EmailBackend |
|
44 |
|
45 mail.original_email_backend = settings.EMAIL_BACKEND |
|
46 settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend' |
62 |
47 |
63 mail.outbox = [] |
48 mail.outbox = [] |
64 |
49 |
65 deactivate() |
50 deactivate() |
66 |
51 |
69 |
54 |
70 - Restoring the original test renderer |
55 - Restoring the original test renderer |
71 - Restoring the email sending functions |
56 - Restoring the email sending functions |
72 |
57 |
73 """ |
58 """ |
74 Template.render = Template.original_render |
59 Template._render = Template.original_render |
75 del Template.original_render |
60 del Template.original_render |
76 |
61 |
77 mail.SMTPConnection = mail.original_SMTPConnection |
62 mail.SMTPConnection = mail.original_SMTPConnection |
78 del mail.original_SMTPConnection |
63 del mail.original_SMTPConnection |
79 |
64 |
|
65 settings.EMAIL_BACKEND = mail.original_email_backend |
|
66 del mail.original_email_backend |
|
67 |
80 del mail.outbox |
68 del mail.outbox |
81 |
|
82 |
69 |
83 def get_runner(settings): |
70 def get_runner(settings): |
84 test_path = settings.TEST_RUNNER.split('.') |
71 test_path = settings.TEST_RUNNER.split('.') |
85 # Allow for Python 2.5 relative paths |
72 # Allow for Python 2.5 relative paths |
86 if len(test_path) > 1: |
73 if len(test_path) > 1: |