Changeset 6044
- Timestamp:
- 09/03/07 19:50:06 (1 year ago)
- Files:
-
- django/trunk/django/test/testcases.py (modified) (6 diffs)
- django/trunk/tests/modeltests/test_client/views.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/test/testcases.py
r6041 r6044 1 import re, unittest 1 import re 2 import unittest 2 3 from urlparse import urlsplit 4 3 5 from django.http import QueryDict 4 6 from django.db import transaction 5 7 from django.core import mail 6 8 from django.core.management import call_command 7 from django.db.models import get_apps8 9 from django.test import _doctest as doctest 9 10 from django.test.client import Client … … 34 35 return normalize_long_ints(want) == normalize_long_ints(got) 35 36 return ok 36 37 37 38 class DocTestRunner(doctest.DocTestRunner): 38 39 def __init__(self, *args, **kwargs): 39 40 doctest.DocTestRunner.__init__(self, *args, **kwargs) 40 41 self.optionflags = doctest.ELLIPSIS 41 42 42 43 def report_unexpected_exception(self, out, test, example, exc_info): 43 doctest.DocTestRunner.report_unexpected_exception(self, out,test,example,exc_info)44 44 doctest.DocTestRunner.report_unexpected_exception(self, out, test, 45 example, exc_info) 45 46 # Rollback, in case of database errors. Otherwise they'd have 46 47 # side effects on other tests. 47 from django.db import transaction48 48 transaction.rollback_unless_managed() 49 49 50 class TestCase(unittest.TestCase): 50 class TestCase(unittest.TestCase): 51 51 def _pre_setup(self): 52 """Perform any pre-test setup. This includes: 53 54 * If the Test Case class has a 'fixtures' member, clearing the 55 database and installing the named fixtures at the start of each test. 52 """Performs any pre-test setup. This includes: 53 54 * If the Test Case class has a 'fixtures' member, clearing the 55 database and installing the named fixtures at the start of each 56 test. 56 57 * Clearing the mail test outbox. 57 58 58 """ 59 59 call_command('flush', verbosity=0, interactive=False) … … 74 74 super(TestCase, self).__call__(result) 75 75 76 def assertRedirects(self, response, expected_url, status_code=302, target_status_code=200): 77 """Assert that a response redirected to a specific URL, and that the 76 def assertRedirects(self, response, expected_url, status_code=302, 77 target_status_code=200): 78 """Asserts that a response redirected to a specific URL, and that the 78 79 redirect URL can be loaded. 79 80 Note that assertRedirects won't work for external links since it uses 80 81 Note that assertRedirects won't work for external links since it uses 81 82 TestClient to do a request. 82 83 """ 83 self.assertEqual(response.status_code, status_code, 84 "Response didn't redirect as expected: Response code was %d (expected %d)" %85 (response.status_code, status_code))84 self.assertEqual(response.status_code, status_code, 85 ("Response didn't redirect as expected: Response code was %d" 86 " (expected %d)" % (response.status_code, status_code))) 86 87 scheme, netloc, path, query, fragment = urlsplit(response['Location']) 87 88 url = path … … 90 91 if fragment: 91 92 url += '#' + fragment 92 self.assertEqual(url, expected_url, 93 self.assertEqual(url, expected_url, 93 94 "Response redirected to '%s', expected '%s'" % (url, expected_url)) 94 95 95 96 # Get the redirection page, using the same client that was used 96 97 # to obtain the original response. 97 98 redirect_response = response.client.get(path, QueryDict(query)) 98 self.assertEqual(redirect_response.status_code, target_status_code, 99 "Couldn't retrieve redirection page '%s': response code was %d (expected %d)" % 100 (path, redirect_response.status_code, target_status_code)) 101 99 self.assertEqual(redirect_response.status_code, target_status_code, 100 ("Couldn't retrieve redirection page '%s': response code was %d" 101 " (expected %d)") % 102 (path, redirect_response.status_code, target_status_code)) 103 102 104 def assertContains(self, response, text, count=None, status_code=200): 103 """Assert that a response indicates that a page was retreived successfully, 104 (i.e., the HTTP status code was as expected), and that ``text`` occurs ``count`` 105 times in the content of the response. If ``count`` is None, the count doesn't 106 matter - the assertion is true if the text occurs at least once in the response. 107 105 """ 106 Asserts that a response indicates that a page was retreived 107 successfully, (i.e., the HTTP status code was as expected), and that 108 ``text`` occurs ``count`` times in the content of the response. 109 If ``count`` is None, the count doesn't matter - the assertion is true 110 if the text occurs at least once in the response. 108 111 """ 109 112 self.assertEqual(response.status_code, status_code, 110 "Couldn't retrieve page: Response code was %d (expected %d)'" % 113 "Couldn't retrieve page: Response code was %d (expected %d)'" % 111 114 (response.status_code, status_code)) 112 115 real_count = response.content.count(text) 113 116 if count is not None: 114 117 self.assertEqual(real_count, count, 115 "Found %d instances of '%s' in response (expected %d)" % (real_count, text, count)) 118 "Found %d instances of '%s' in response (expected %d)" % 119 (real_count, text, count)) 116 120 else: 117 self.failUnless(real_count != 0, "Couldn't find '%s' in response" % text) 118 121 self.failUnless(real_count != 0, 122 "Couldn't find '%s' in response" % text) 123 119 124 def assertFormError(self, response, form, field, errors): 120 "Assert that a form used to render the response has a specific field error" 125 """ 126 Asserts that a form used to render the response has a specific field 127 error. 128 """ 121 129 # Put context(s) into a list to simplify processing. 122 contexts = to_list(response.context) 130 contexts = to_list(response.context) 123 131 if not contexts: 124 132 self.fail('Response did not use any contexts to render the' … … 127 135 # Put error(s) into a list to simplify processing. 128 136 errors = to_list(errors) 129 137 130 138 # Search all contexts for the error. 131 139 found_form = False 132 140 for i,context in enumerate(contexts): 133 if form in context: 134 found_form = True 135 for err in errors: 136 if field: 137 if field in context[form].errors: 138 self.failUnless(err in context[form].errors[field], 139 "The field '%s' on form '%s' in context %d does not contain the error '%s' (actual errors: %s)" % 140 (field, form, i, err, list(context[form].errors[field]))) 141 elif field in context[form].fields: 142 self.fail("The field '%s' on form '%s' in context %d contains no errors" % 143 (field, form, i)) 144 else: 145 self.fail("The form '%s' in context %d does not contain the field '%s'" % (form, i, field)) 141 if form not in context: 142 continue 143 found_form = True 144 for err in errors: 145 if field: 146 if field in context[form].errors: 147 field_errors = context[form].errors[field] 148 self.failUnless(err in field_errors, 149 "The field '%s' on form '%s' in" 150 " context %d does not contain the" 151 " error '%s' (actual errors: %s)" % 152 (field, form, i, err, 153 list(field_errors))) 154 elif field in context[form].fields: 155 self.fail("The field '%s' on form '%s' in context %d" 156 " contains no errors" % (field, form, i)) 146 157 else: 147 self.failUnless(err in context[form].non_field_errors(), 148 "The form '%s' in context %d does not contain the non-field error '%s' (actual errors: %s)" % 149 (form, i, err, list(context[form].non_field_errors()))) 158 self.fail("The form '%s' in context %d does not" 159 " contain the field '%s'" % 160 (form, i, field)) 161 else: 162 non_field_errors = context[form].non_field_errors() 163 self.failUnless(err in non_field_errors, 164 "The form '%s' in context %d does not contain the" 165 " non-field error '%s' (actual errors: %s)" % 166 (form, i, err, non_field_errors)) 150 167 if not found_form: 151 self.fail("The form '%s' was not used to render the response" % form) 152 168 self.fail("The form '%s' was not used to render the response" % 169 form) 170 153 171 def assertTemplateUsed(self, response, template_name): 154 "Assert that the template with the provided name was used in rendering the response" 172 """ 173 Asserts that the template with the provided name was used in rendering 174 the response. 175 """ 155 176 template_names = [t.name for t in to_list(response.template)] 156 177 if not template_names: … … 158 179 self.failUnless(template_name in template_names, 159 180 (u"Template '%s' was not a template used to render the response." 160 " Actual template(s) used: %s") % (template_name,181 u" Actual template(s) used: %s") % (template_name, 161 182 u', '.join(template_names))) 162 183 163 184 def assertTemplateNotUsed(self, response, template_name): 164 "Assert that the template with the provided name was NOT used in rendering the response" 185 """ 186 Asserts that the template with the provided name was NOT used in 187 rendering the response. 188 """ 165 189 template_names = [t.name for t in to_list(response.template)] 166 190 self.failIf(template_name in template_names, 167 191 (u"Template '%s' was used unexpectedly in rendering the" 168 " response") % template_name)192 u" response") % template_name) django/trunk/tests/modeltests/test_client/views.py
r6031 r6044 1 1 from xml.dom.minidom import parseString 2 2 3 from django.core.mail import EmailMessage, SMTPConnection 3 4 from django.template import Context, Template … … 5 6 from django.contrib.auth.decorators import login_required 6 7 from django.newforms.forms import Form 7 from django.newforms import fields 8 from django.newforms import fields, ValidationError 8 9 from django.shortcuts import render_to_response 9 10
