diff options
| author | Gary Wilson Jr <gary.wilson@gmail.com> | 2007-09-03 23:14:51 +0000 |
|---|---|---|
| committer | Gary Wilson Jr <gary.wilson@gmail.com> | 2007-09-03 23:14:51 +0000 |
| commit | bce7de96477086f98f8cde8aef41a15e6e17b8af (patch) | |
| tree | 5b4de18bbc5c3ddd997524103e50fe5698b2128c | |
| parent | 853f9c7db9f506de20228eb74d9d34abeba10dbf (diff) | |
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
| -rw-r--r-- | django/test/testcases.py | 57 | ||||
| -rw-r--r-- | tests/regressiontests/test_client_regress/models.py | 4 |
2 files changed, 31 insertions, 30 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py index 400d66cfab..fd08d45ee3 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -10,6 +10,18 @@ from django.test.client import Client 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): def 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): "Assert that a form used to render the response has a specific field error" - if not response.context: - 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 - if not isinstance(response.context, list): - contexts = [response.context] - else: - contexts = response.context + # Put context(s) into a list to simplify processing. + contexts = to_list(response.context) + if not contexts: + self.fail('Response did not use any contexts to render the' + ' response') - # If a single error string is provided, make it a list to simplify processing - if not isinstance(errors, list): - errors = [errors] + # Put error(s) into a list to simplify processing. + errors = to_list(errors) # Search all contexts for the error. found_form = False @@ -144,24 +152,17 @@ class TestCase(unittest.TestCase): def assertTemplateUsed(self, response, template_name): "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 response.template] - 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: + template_names = [t.name for t in to_list(response.template)] + if not template_names: 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): "Assert that the template with the provided name was NOT used in rendering the response" - if isinstance(response.template, list): - self.failIf(template_name in [t.name for t in response.template], - u"Template '%s' was used unexpectedly in rendering the response" % template_name) - elif response.template: - self.assertNotEqual(template_name, response.template.name, - u"Template '%s' was used unexpectedly in rendering the response" % template_name) - + 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" + " response") % template_name) diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py index aa18a48852..6ec093e8a5 100644 --- a/tests/regressiontests/test_client_regress/models.py +++ b/tests/regressiontests/test_client_regress/models.py @@ -75,7 +75,7 @@ class AssertTemplateUsedTests(TestCase): try: self.assertTemplateUsed(response, 'Empty POST Template') 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): "Template assertions work when there are multiple contexts" @@ -101,7 +101,7 @@ class AssertTemplateUsedTests(TestCase): try: self.assertTemplateUsed(response, "Valid POST Template") 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): def test_redirect_page(self): |
