web/lib/django/test/utils.py
changeset 38 77b6da96e6f1
equal deleted inserted replaced
37:8d941af65caf 38:77b6da96e6f1
       
     1 import sys, time, os
       
     2 from django.conf import settings
       
     3 from django.core import mail
       
     4 from django.core.mail.backends import locmem
       
     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(key)
       
    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 
       
    32 def setup_test_environment():
       
    33     """Perform any global pre-test setup. This involves:
       
    34 
       
    35         - Installing the instrumented test renderer
       
    36         - Set the email backend to the locmem email backend.
       
    37         - Setting the active locale to match the LANGUAGE_CODE setting.
       
    38     """
       
    39     Template.original_render = Template._render
       
    40     Template._render = instrumented_test_render
       
    41 
       
    42     mail.original_SMTPConnection = mail.SMTPConnection
       
    43     mail.SMTPConnection = locmem.EmailBackend
       
    44 
       
    45     mail.original_email_backend = settings.EMAIL_BACKEND
       
    46     settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
       
    47 
       
    48     mail.outbox = []
       
    49 
       
    50     deactivate()
       
    51 
       
    52 def teardown_test_environment():
       
    53     """Perform any global post-test teardown. This involves:
       
    54 
       
    55         - Restoring the original test renderer
       
    56         - Restoring the email sending functions
       
    57 
       
    58     """
       
    59     Template._render = Template.original_render
       
    60     del Template.original_render
       
    61 
       
    62     mail.SMTPConnection = mail.original_SMTPConnection
       
    63     del mail.original_SMTPConnection
       
    64 
       
    65     settings.EMAIL_BACKEND = mail.original_email_backend
       
    66     del mail.original_email_backend
       
    67 
       
    68     del mail.outbox
       
    69 
       
    70 def get_runner(settings):
       
    71     test_path = settings.TEST_RUNNER.split('.')
       
    72     # Allow for Python 2.5 relative paths
       
    73     if len(test_path) > 1:
       
    74         test_module_name = '.'.join(test_path[:-1])
       
    75     else:
       
    76         test_module_name = '.'
       
    77     test_module = __import__(test_module_name, {}, {}, test_path[-1])
       
    78     test_runner = getattr(test_module, test_path[-1])
       
    79     return test_runner