2010-08-20 22:28:42 +08:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import os
|
2010-11-11 23:06:20 +08:00
|
|
|
import warnings
|
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
|
|
|
|
2010-10-19 00:08:25 +08:00
|
|
|
__all__ = ('Approximate', 'ContextList', 'setup_test_environment',
|
2010-10-18 23:53:55 +08:00
|
|
|
'teardown_test_environment', 'get_runner')
|
2010-08-20 22:28:42 +08:00
|
|
|
|
2010-10-19 00:08:25 +08:00
|
|
|
|
2010-08-20 22:28:42 +08:00
|
|
|
class Approximate(object):
|
|
|
|
def __init__(self, val, places=7):
|
|
|
|
self.val = val
|
|
|
|
self.places = places
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return repr(self.val)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if self.val == other:
|
|
|
|
return True
|
|
|
|
return round(abs(self.val-other), self.places) == 0
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2010-11-11 23:06:20 +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()
|
|
|
|
|
2010-11-11 23:06:20 +08:00
|
|
|
|
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
|
|
|
|
2010-11-11 23:06:20 +08:00
|
|
|
|
|
|
|
def get_warnings_state():
|
|
|
|
"""
|
|
|
|
Returns an object containing the state of the warnings module
|
|
|
|
"""
|
|
|
|
# There is no public interface for doing this, but this implementation of
|
|
|
|
# get_warnings_state and restore_warnings_state appears to work on Python
|
|
|
|
# 2.4 to 2.7.
|
|
|
|
return warnings.filters[:]
|
|
|
|
|
|
|
|
|
|
|
|
def restore_warnings_state(state):
|
|
|
|
"""
|
|
|
|
Restores the state of the warnings module when passed an object that was
|
|
|
|
returned by get_warnings_state()
|
|
|
|
"""
|
|
|
|
warnings.filters = state[:]
|
|
|
|
|
|
|
|
|
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
|