2007-09-04 08:50:06 +08:00
|
|
|
import re
|
|
|
|
import unittest
|
2007-11-11 11:54:21 +08:00
|
|
|
from urlparse import urlsplit, urlunsplit
|
2007-09-04 08:50:06 +08:00
|
|
|
|
2007-08-31 19:37:28 +08:00
|
|
|
from django.http import QueryDict
|
2006-08-27 20:24:59 +08:00
|
|
|
from django.db import transaction
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.core import mail
|
|
|
|
from django.core.management import call_command
|
2007-05-31 21:18:12 +08:00
|
|
|
from django.test import _doctest as doctest
|
2007-05-05 11:03:33 +08:00
|
|
|
from django.test.client import Client
|
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s)
|
|
|
|
|
2007-09-04 07:14:51 +08:00
|
|
|
def to_list(value):
|
|
|
|
"""
|
|
|
|
Puts value into a list if it's not already one.
|
|
|
|
Returns an empty list if value is None.
|
|
|
|
"""
|
|
|
|
if value is None:
|
|
|
|
value = []
|
|
|
|
elif not isinstance(value, list):
|
|
|
|
value = [value]
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
class OutputChecker(doctest.OutputChecker):
|
|
|
|
def check_output(self, want, got, optionflags):
|
|
|
|
ok = doctest.OutputChecker.check_output(self, want, got, optionflags)
|
|
|
|
|
|
|
|
# Doctest does an exact string comparison of output, which means long
|
|
|
|
# integers aren't equal to normal integers ("22L" vs. "22"). The
|
|
|
|
# following code normalizes long integers so that they equal normal
|
|
|
|
# integers.
|
|
|
|
if not ok:
|
|
|
|
return normalize_long_ints(want) == normalize_long_ints(got)
|
|
|
|
return ok
|
2007-09-04 08:50:06 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
class DocTestRunner(doctest.DocTestRunner):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
doctest.DocTestRunner.__init__(self, *args, **kwargs)
|
|
|
|
self.optionflags = doctest.ELLIPSIS
|
2007-09-04 08:50:06 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
def report_unexpected_exception(self, out, test, example, exc_info):
|
2007-09-04 08:50:06 +08:00
|
|
|
doctest.DocTestRunner.report_unexpected_exception(self, out, test,
|
|
|
|
example, exc_info)
|
2006-08-27 20:24:59 +08:00
|
|
|
# Rollback, in case of database errors. Otherwise they'd have
|
|
|
|
# side effects on other tests.
|
|
|
|
transaction.rollback_unless_managed()
|
|
|
|
|
2007-09-04 08:50:06 +08:00
|
|
|
class TestCase(unittest.TestCase):
|
2007-05-08 19:19:34 +08:00
|
|
|
def _pre_setup(self):
|
2007-09-04 08:50:06 +08:00
|
|
|
"""Performs any pre-test setup. This includes:
|
|
|
|
|
|
|
|
* If the Test Case class has a 'fixtures' member, clearing the
|
|
|
|
database and installing the named fixtures at the start of each
|
|
|
|
test.
|
2007-05-08 19:19:34 +08:00
|
|
|
* Clearing the mail test outbox.
|
2007-03-01 21:11:08 +08:00
|
|
|
"""
|
2007-08-16 14:06:55 +08:00
|
|
|
call_command('flush', verbosity=0, interactive=False)
|
2007-03-01 21:11:08 +08:00
|
|
|
if hasattr(self, 'fixtures'):
|
2007-08-16 14:06:55 +08:00
|
|
|
# We have to use this slightly awkward syntax due to the fact
|
|
|
|
# that we're using *args and **kwargs together.
|
|
|
|
call_command('loaddata', *self.fixtures, **{'verbosity': 0})
|
2007-05-08 19:19:34 +08:00
|
|
|
mail.outbox = []
|
2007-05-13 00:53:27 +08:00
|
|
|
|
|
|
|
def __call__(self, result=None):
|
|
|
|
"""
|
|
|
|
Wrapper around default __call__ method to perform common Django test
|
|
|
|
set up. This means that user-defined Test Cases aren't required to
|
|
|
|
include a call to super().setUp().
|
2007-03-01 21:11:08 +08:00
|
|
|
"""
|
2007-05-05 11:03:33 +08:00
|
|
|
self.client = Client()
|
2007-05-08 19:19:34 +08:00
|
|
|
self._pre_setup()
|
2007-05-13 00:53:27 +08:00
|
|
|
super(TestCase, self).__call__(result)
|
2007-05-05 11:03:33 +08:00
|
|
|
|
2007-09-04 08:50:06 +08:00
|
|
|
def assertRedirects(self, response, expected_url, status_code=302,
|
2007-11-11 11:54:21 +08:00
|
|
|
target_status_code=200, host=None):
|
2007-09-04 08:50:06 +08:00
|
|
|
"""Asserts that a response redirected to a specific URL, and that the
|
2007-05-05 11:03:33 +08:00
|
|
|
redirect URL can be loaded.
|
2007-09-04 08:50:06 +08:00
|
|
|
|
|
|
|
Note that assertRedirects won't work for external links since it uses
|
2007-08-31 19:37:28 +08:00
|
|
|
TestClient to do a request.
|
2007-05-05 11:03:33 +08:00
|
|
|
"""
|
2007-09-04 08:50:06 +08:00
|
|
|
self.assertEqual(response.status_code, status_code,
|
|
|
|
("Response didn't redirect as expected: Response code was %d"
|
|
|
|
" (expected %d)" % (response.status_code, status_code)))
|
2007-09-14 13:28:00 +08:00
|
|
|
url = response['Location']
|
|
|
|
scheme, netloc, path, query, fragment = urlsplit(url)
|
2007-11-11 11:54:21 +08:00
|
|
|
e_scheme, e_netloc, e_path, e_query, e_fragment = urlsplit(expected_url)
|
|
|
|
if not (e_scheme or e_netloc):
|
|
|
|
expected_url = urlunsplit(('http', host or 'testserver', e_path,
|
|
|
|
e_query, e_fragment))
|
2007-09-04 08:50:06 +08:00
|
|
|
self.assertEqual(url, expected_url,
|
2007-08-31 19:37:28 +08:00
|
|
|
"Response redirected to '%s', expected '%s'" % (url, expected_url))
|
2007-09-04 08:50:06 +08:00
|
|
|
|
2007-09-03 19:21:40 +08:00
|
|
|
# Get the redirection page, using the same client that was used
|
|
|
|
# to obtain the original response.
|
|
|
|
redirect_response = response.client.get(path, QueryDict(query))
|
2007-09-04 08:50:06 +08:00
|
|
|
self.assertEqual(redirect_response.status_code, target_status_code,
|
|
|
|
("Couldn't retrieve redirection page '%s': response code was %d"
|
|
|
|
" (expected %d)") %
|
|
|
|
(path, redirect_response.status_code, target_status_code))
|
|
|
|
|
2007-07-20 22:32:20 +08:00
|
|
|
def assertContains(self, response, text, count=None, status_code=200):
|
2007-09-04 08:50:06 +08:00
|
|
|
"""
|
|
|
|
Asserts that a response indicates that a page was retreived
|
|
|
|
successfully, (i.e., the HTTP status code was as expected), and that
|
|
|
|
``text`` occurs ``count`` times in the content of the response.
|
|
|
|
If ``count`` is None, the count doesn't matter - the assertion is true
|
|
|
|
if the text occurs at least once in the response.
|
2007-05-05 11:03:33 +08:00
|
|
|
"""
|
2007-05-10 19:27:59 +08:00
|
|
|
self.assertEqual(response.status_code, status_code,
|
2007-09-04 08:50:06 +08:00
|
|
|
"Couldn't retrieve page: Response code was %d (expected %d)'" %
|
2007-05-10 19:27:59 +08:00
|
|
|
(response.status_code, status_code))
|
2007-05-05 11:03:33 +08:00
|
|
|
real_count = response.content.count(text)
|
2007-07-21 12:36:28 +08:00
|
|
|
if count is not None:
|
2007-07-20 22:32:20 +08:00
|
|
|
self.assertEqual(real_count, count,
|
2007-09-04 08:50:06 +08:00
|
|
|
"Found %d instances of '%s' in response (expected %d)" %
|
|
|
|
(real_count, text, count))
|
2007-07-20 22:32:20 +08:00
|
|
|
else:
|
2007-09-04 08:50:06 +08:00
|
|
|
self.failUnless(real_count != 0,
|
|
|
|
"Couldn't find '%s' in response" % text)
|
|
|
|
|
2007-05-07 20:34:18 +08:00
|
|
|
def assertFormError(self, response, form, field, errors):
|
2007-09-04 08:50:06 +08:00
|
|
|
"""
|
|
|
|
Asserts that a form used to render the response has a specific field
|
|
|
|
error.
|
|
|
|
"""
|
2007-09-04 07:14:51 +08:00
|
|
|
# Put context(s) into a list to simplify processing.
|
2007-09-04 08:50:06 +08:00
|
|
|
contexts = to_list(response.context)
|
2007-09-04 07:14:51 +08:00
|
|
|
if not contexts:
|
|
|
|
self.fail('Response did not use any contexts to render the'
|
|
|
|
' response')
|
2007-05-07 20:34:18 +08:00
|
|
|
|
2007-09-04 07:14:51 +08:00
|
|
|
# Put error(s) into a list to simplify processing.
|
|
|
|
errors = to_list(errors)
|
2007-09-04 08:50:06 +08:00
|
|
|
|
2007-05-07 20:34:18 +08:00
|
|
|
# Search all contexts for the error.
|
|
|
|
found_form = False
|
|
|
|
for i,context in enumerate(contexts):
|
2007-09-04 08:50:06 +08:00
|
|
|
if form not in context:
|
|
|
|
continue
|
|
|
|
found_form = True
|
|
|
|
for err in errors:
|
|
|
|
if field:
|
|
|
|
if field in context[form].errors:
|
|
|
|
field_errors = context[form].errors[field]
|
|
|
|
self.failUnless(err in field_errors,
|
|
|
|
"The field '%s' on form '%s' in"
|
|
|
|
" context %d does not contain the"
|
|
|
|
" error '%s' (actual errors: %s)" %
|
|
|
|
(field, form, i, err,
|
2007-10-28 13:40:26 +08:00
|
|
|
repr(field_errors)))
|
2007-09-04 08:50:06 +08:00
|
|
|
elif field in context[form].fields:
|
|
|
|
self.fail("The field '%s' on form '%s' in context %d"
|
|
|
|
" contains no errors" % (field, form, i))
|
2007-05-10 21:48:18 +08:00
|
|
|
else:
|
2007-09-04 08:50:06 +08:00
|
|
|
self.fail("The form '%s' in context %d does not"
|
|
|
|
" contain the field '%s'" %
|
|
|
|
(form, i, field))
|
|
|
|
else:
|
|
|
|
non_field_errors = context[form].non_field_errors()
|
|
|
|
self.failUnless(err in non_field_errors,
|
|
|
|
"The form '%s' in context %d does not contain the"
|
|
|
|
" non-field error '%s' (actual errors: %s)" %
|
|
|
|
(form, i, err, non_field_errors))
|
2007-05-07 20:34:18 +08:00
|
|
|
if not found_form:
|
2007-09-04 08:50:06 +08:00
|
|
|
self.fail("The form '%s' was not used to render the response" %
|
|
|
|
form)
|
|
|
|
|
2007-05-07 20:34:18 +08:00
|
|
|
def assertTemplateUsed(self, response, template_name):
|
2007-09-04 08:50:06 +08:00
|
|
|
"""
|
|
|
|
Asserts that the template with the provided name was used in rendering
|
|
|
|
the response.
|
|
|
|
"""
|
2007-09-04 07:14:51 +08:00
|
|
|
template_names = [t.name for t in to_list(response.template)]
|
|
|
|
if not template_names:
|
2007-05-07 20:34:18 +08:00
|
|
|
self.fail('No templates used to render the response')
|
2007-09-04 07:14:51 +08:00
|
|
|
self.failUnless(template_name in template_names,
|
|
|
|
(u"Template '%s' was not a template used to render the response."
|
2007-09-04 08:50:06 +08:00
|
|
|
u" Actual template(s) used: %s") % (template_name,
|
2007-09-04 07:14:51 +08:00
|
|
|
u', '.join(template_names)))
|
2007-05-07 20:34:18 +08:00
|
|
|
|
|
|
|
def assertTemplateNotUsed(self, response, template_name):
|
2007-09-04 08:50:06 +08:00
|
|
|
"""
|
|
|
|
Asserts that the template with the provided name was NOT used in
|
|
|
|
rendering the response.
|
|
|
|
"""
|
2007-09-04 07:14:51 +08:00
|
|
|
template_names = [t.name for t in to_list(response.template)]
|
|
|
|
self.failIf(template_name in template_names,
|
|
|
|
(u"Template '%s' was used unexpectedly in rendering the"
|
2007-09-04 08:50:06 +08:00
|
|
|
u" response") % template_name)
|