summaryrefslogtreecommitdiff
path: root/django/test/testcases.py
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2007-05-07 12:34:18 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2007-05-07 12:34:18 +0000
commitf073797f4ca8d3e2736d99f76674e43640399c0d (patch)
tree7d4b48a295dd516ddc2eef9db6f93b052a083ab4 /django/test/testcases.py
parent64adc413770c4762da5db697c4a332ac749cf4f3 (diff)
Added assertFormError, assertTemplateUsed and assertTemplateNotUsed for use during unit testing.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5156 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/test/testcases.py')
-rw-r--r--django/test/testcases.py60
1 files changed, 59 insertions, 1 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py
index efe392590f..80f55b20d3 100644
--- a/django/test/testcases.py
+++ b/django/test/testcases.py
@@ -77,4 +77,62 @@ class TestCase(unittest.TestCase):
real_count = response.content.count(text)
self.assertEqual(real_count, count,
"Could only find %d of %d instances of '%s' in response" % (real_count, count, text))
- \ No newline at end of file
+
+ 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
+
+ # 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.
+ found_form = False
+ for i,context in enumerate(contexts):
+ if form in context:
+ found_form = True
+ try:
+ for err in errors:
+ if field:
+ self.assertTrue(err in context[form].errors[field],
+ "The field '%s' on form '%s' in context %d does not contain the error '%s' (actual errors: %s)" %
+ (field, form, i, err, list(context[form].errors[field])))
+ else:
+ self.assertTrue(err in context[form].non_field_errors(),
+ "The form '%s' in context %d does not contain the non-field error '%s' (actual errors: %s)" %
+ (form, i, err, list(context[form].non_field_errors())))
+ except KeyError:
+ self.fail("The form '%s' in context %d does not contain the field '%s'" % (form, i, field))
+ if not found_form:
+ self.fail("The form '%s' was not used to render the response" % form)
+
+ 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.assertTrue(template_name in template_names,
+ "Template '%s' was not one of the templates used to render the response. Templates used: %s" %
+ (template_name, template_names))
+ elif response.template:
+ self.assertEqual(template_name, response.template.name,
+ "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')
+
+ 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.assertFalse(template_name in [t.name for t in response.template],
+ "Template '%s' was used unexpectedly in rendering the response" % template_name)
+ elif response.template:
+ self.assertNotEqual(template_name, response.template.name,
+ "Template '%s' was used unexpectedly in rendering the response" % template_name)
+ \ No newline at end of file