Removed some duplication in the Django `TestCase` methods by introducing a `to_list` function for putting a value into a list if it's not already one.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6041 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Gary Wilson Jr 2007-09-03 23:14:51 +00:00
parent 853f9c7db9
commit bce7de9647
2 changed files with 31 additions and 30 deletions

View File

@ -10,6 +10,18 @@ from django.test.client import Client
normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s) normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s)
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
class OutputChecker(doctest.OutputChecker): class OutputChecker(doctest.OutputChecker):
def check_output(self, want, got, optionflags): def check_output(self, want, got, optionflags):
ok = doctest.OutputChecker.check_output(self, want, got, optionflags) ok = doctest.OutputChecker.check_output(self, want, got, optionflags)
@ -106,18 +118,14 @@ class TestCase(unittest.TestCase):
def assertFormError(self, response, form, field, errors): def assertFormError(self, response, form, field, errors):
"Assert that a form used to render the response has a specific field error" "Assert that a form used to render the response has a specific field error"
if not response.context: # Put context(s) into a list to simplify processing.
self.fail('Response did not use any contexts to render the response') contexts = to_list(response.context)
if not contexts:
self.fail('Response did not use any contexts to render the'
' response')
# If there is a single context, put it into a list to simplify processing # Put error(s) into a list to simplify processing.
if not isinstance(response.context, list): errors = to_list(errors)
contexts = [response.context]
else:
contexts = response.context
# If a single error string is provided, make it a list to simplify processing
if not isinstance(errors, list):
errors = [errors]
# Search all contexts for the error. # Search all contexts for the error.
found_form = False found_form = False
@ -144,24 +152,17 @@ class TestCase(unittest.TestCase):
def assertTemplateUsed(self, response, template_name): def assertTemplateUsed(self, response, template_name):
"Assert that the template with the provided name was used in rendering the response" "Assert that the template with the provided name was used in rendering the response"
if isinstance(response.template, list): template_names = [t.name for t in to_list(response.template)]
template_names = [t.name for t in response.template] if not template_names:
self.failUnless(template_name in template_names,
u"Template '%s' was not one of the templates used to render the response. Templates used: %s" %
(template_name, u', '.join(template_names)))
elif response.template:
self.assertEqual(template_name, response.template.name,
u"Template '%s' was not used to render the response. Actual template was '%s'" %
(template_name, response.template.name))
else:
self.fail('No templates used to render the response') self.fail('No templates used to render the response')
self.failUnless(template_name in template_names,
(u"Template '%s' was not a template used to render the response."
" Actual template(s) used: %s") % (template_name,
u', '.join(template_names)))
def assertTemplateNotUsed(self, response, template_name): def assertTemplateNotUsed(self, response, template_name):
"Assert that the template with the provided name was NOT used in rendering the response" "Assert that the template with the provided name was NOT used in rendering the response"
if isinstance(response.template, list): template_names = [t.name for t in to_list(response.template)]
self.failIf(template_name in [t.name for t in response.template], self.failIf(template_name in template_names,
u"Template '%s' was used unexpectedly in rendering the response" % template_name) (u"Template '%s' was used unexpectedly in rendering the"
elif response.template: " response") % template_name)
self.assertNotEqual(template_name, response.template.name,
u"Template '%s' was used unexpectedly in rendering the response" % template_name)

View File

@ -75,7 +75,7 @@ class AssertTemplateUsedTests(TestCase):
try: try:
self.assertTemplateUsed(response, 'Empty POST Template') self.assertTemplateUsed(response, 'Empty POST Template')
except AssertionError, e: except AssertionError, e:
self.assertEquals(str(e), "Template 'Empty POST Template' was not used to render the response. Actual template was 'Empty GET Template'") self.assertEquals(str(e), "Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template")
def test_multiple_context(self): def test_multiple_context(self):
"Template assertions work when there are multiple contexts" "Template assertions work when there are multiple contexts"
@ -101,7 +101,7 @@ class AssertTemplateUsedTests(TestCase):
try: try:
self.assertTemplateUsed(response, "Valid POST Template") self.assertTemplateUsed(response, "Valid POST Template")
except AssertionError, e: except AssertionError, e:
self.assertEquals(str(e), "Template 'Valid POST Template' was not one of the templates used to render the response. Templates used: form_view.html, base.html") self.assertEquals(str(e), "Template 'Valid POST Template' was not a template used to render the response. Actual template(s) used: form_view.html, base.html")
class AssertRedirectsTests(TestCase): class AssertRedirectsTests(TestCase):
def test_redirect_page(self): def test_redirect_page(self):