Ticket #10314: msg_prefix_patch

File msg_prefix_patch, 19.2 KB (added by christian.oudard@…, 15 years ago)

Prefixes error messages with custom message instead of replacing it

Line 
1diff --git a/django/test/testcases.py b/django/test/testcases.py
2index 8c73c63..b1a37d9 100644
3--- a/django/test/testcases.py
4+++ b/django/test/testcases.py
5@@ -273,34 +273,41 @@ class TransactionTestCase(unittest.TestCase):
6 clear_url_caches()
7
8 def assertRedirects(self, response, expected_url, status_code=302,
9- target_status_code=200, host=None):
10+ target_status_code=200, host=None, msg_prefix=""):
11 """Asserts that a response redirected to a specific URL, and that the
12 redirect URL can be loaded.
13
14 Note that assertRedirects won't work for external links since it uses
15 TestClient to do a request.
16 """
17+ if msg_prefix:
18+ msg_prefix += ": "
19+
20 if hasattr(response, 'redirect_chain'):
21 # The request was a followed redirect
22 self.failUnless(len(response.redirect_chain) > 0,
23- ("Response didn't redirect as expected: Response code was %d"
24- " (expected %d)" % (response.status_code, status_code)))
25+ msg_prefix + "Response didn't redirect as expected: Response"
26+ " code was %d (expected %d)" %
27+ (response.status_code, status_code))
28
29 self.assertEqual(response.redirect_chain[0][1], status_code,
30- ("Initial response didn't redirect as expected: Response code was %d"
31- " (expected %d)" % (response.redirect_chain[0][1], status_code)))
32+ msg_prefix + "Initial response didn't redirect as expected:"
33+ " Response code was %d (expected %d)" %
34+ (response.redirect_chain[0][1], status_code))
35
36 url, status_code = response.redirect_chain[-1]
37
38 self.assertEqual(response.status_code, target_status_code,
39- ("Response didn't redirect as expected: Final Response code was %d"
40- " (expected %d)" % (response.status_code, target_status_code)))
41+ msg_prefix + "Response didn't redirect as expected: Final"
42+ " Response code was %d (expected %d)" %
43+ (response.status_code, target_status_code))
44
45 else:
46 # Not a followed redirect
47 self.assertEqual(response.status_code, status_code,
48- ("Response didn't redirect as expected: Response code was %d"
49- " (expected %d)" % (response.status_code, status_code)))
50+ msg_prefix + "Response didn't redirect as expected: Response"
51+ " code was %d (expected %d)" %
52+ (response.status_code, status_code))
53
54 url = response['Location']
55 scheme, netloc, path, query, fragment = urlsplit(url)
56@@ -310,9 +317,9 @@ class TransactionTestCase(unittest.TestCase):
57 # Get the redirection page, using the same client that was used
58 # to obtain the original response.
59 self.assertEqual(redirect_response.status_code, target_status_code,
60- ("Couldn't retrieve redirection page '%s': response code was %d"
61- " (expected %d)") %
62- (path, redirect_response.status_code, target_status_code))
63+ msg_prefix + "Couldn't retrieve redirection page '%s':"
64+ " response code was %d (expected %d)" %
65+ (path, redirect_response.status_code, target_status_code))
66
67 e_scheme, e_netloc, e_path, e_query, e_fragment = urlsplit(expected_url)
68 if not (e_scheme or e_netloc):
69@@ -320,10 +327,11 @@ class TransactionTestCase(unittest.TestCase):
70 e_query, e_fragment))
71
72 self.assertEqual(url, expected_url,
73- "Response redirected to '%s', expected '%s'" % (url, expected_url))
74-
75+ msg_prefix + "Response redirected to '%s', expected '%s'" %
76+ (url, expected_url))
77
78- def assertContains(self, response, text, count=None, status_code=200):
79+ def assertContains(self, response, text, count=None, status_code=200,
80+ msg_prefix=""):
81 """
82 Asserts that a response indicates that a page was retrieved
83 successfully, (i.e., the HTTP status code was as expected), and that
84@@ -331,42 +339,52 @@ class TransactionTestCase(unittest.TestCase):
85 If ``count`` is None, the count doesn't matter - the assertion is true
86 if the text occurs at least once in the response.
87 """
88+ if msg_prefix:
89+ msg_prefix += ": "
90+
91 self.assertEqual(response.status_code, status_code,
92- "Couldn't retrieve page: Response code was %d (expected %d)'" %
93- (response.status_code, status_code))
94+ msg_prefix + "Couldn't retrieve page: Response code was %d"
95+ " (expected %d)" % (response.status_code, status_code))
96 text = smart_str(text, response._charset)
97 real_count = response.content.count(text)
98 if count is not None:
99 self.assertEqual(real_count, count,
100- "Found %d instances of '%s' in response (expected %d)" %
101- (real_count, text, count))
102+ msg_prefix + "Found %d instances of '%s' in response"
103+ " (expected %d)" % (real_count, text, count))
104 else:
105 self.failUnless(real_count != 0,
106- "Couldn't find '%s' in response" % text)
107+ msg_prefix + "Couldn't find '%s' in response" % text)
108
109- def assertNotContains(self, response, text, status_code=200):
110+ def assertNotContains(self, response, text, status_code=200,
111+ msg_prefix=""):
112 """
113 Asserts that a response indicates that a page was retrieved
114 successfully, (i.e., the HTTP status code was as expected), and that
115 ``text`` doesn't occurs in the content of the response.
116 """
117+ if msg_prefix:
118+ msg_prefix += ": "
119+
120 self.assertEqual(response.status_code, status_code,
121- "Couldn't retrieve page: Response code was %d (expected %d)'" %
122- (response.status_code, status_code))
123+ msg_prefix + "Couldn't retrieve page: Response code was %d"
124+ " (expected %d)" % (response.status_code, status_code))
125 text = smart_str(text, response._charset)
126- self.assertEqual(response.content.count(text),
127- 0, "Response should not contain '%s'" % text)
128+ self.assertEqual(response.content.count(text), 0,
129+ msg_prefix + "Response should not contain '%s'" % text)
130
131- def assertFormError(self, response, form, field, errors):
132+ def assertFormError(self, response, form, field, errors, msg_prefix=""):
133 """
134 Asserts that a form used to render the response has a specific field
135 error.
136 """
137+ if msg_prefix:
138+ msg_prefix += ": "
139+
140 # Put context(s) into a list to simplify processing.
141 contexts = to_list(response.context)
142 if not contexts:
143- self.fail('Response did not use any contexts to render the'
144- ' response')
145+ self.fail(msg_prefix + "Response did not use any contexts to"
146+ "render the response")
147
148 # Put error(s) into a list to simplify processing.
149 errors = to_list(errors)
150@@ -382,50 +400,57 @@ class TransactionTestCase(unittest.TestCase):
151 if field in context[form].errors:
152 field_errors = context[form].errors[field]
153 self.failUnless(err in field_errors,
154- "The field '%s' on form '%s' in"
155- " context %d does not contain the"
156- " error '%s' (actual errors: %s)" %
157- (field, form, i, err,
158- repr(field_errors)))
159+ msg_prefix + "The field '%s' on form '%s' in"
160+ " context %d does not contain the error '%s'"
161+ " (actual errors: %s)" %
162+ (field, form, i, err, repr(field_errors)))
163 elif field in context[form].fields:
164- self.fail("The field '%s' on form '%s' in context %d"
165- " contains no errors" % (field, form, i))
166+ self.fail(msg_prefix + "The field '%s' on form '%s'"
167+ " in context %d contains no errors" %
168+ (field, form, i))
169 else:
170- self.fail("The form '%s' in context %d does not"
171- " contain the field '%s'" %
172+ self.fail(msg_prefix + "The form '%s' in context %d"
173+ " does not contain the field '%s'" %
174 (form, i, field))
175 else:
176 non_field_errors = context[form].non_field_errors()
177 self.failUnless(err in non_field_errors,
178- "The form '%s' in context %d does not contain the"
179- " non-field error '%s' (actual errors: %s)" %
180+ msg_prefix + "The form '%s' in context %d does not"
181+ " contain the non-field error '%s'"
182+ " (actual errors: %s)" %
183 (form, i, err, non_field_errors))
184 if not found_form:
185- self.fail("The form '%s' was not used to render the response" %
186- form)
187+ self.fail(msg_prefix + "The form '%s' was not used to render the"
188+ " response" % form)
189
190- def assertTemplateUsed(self, response, template_name):
191+ def assertTemplateUsed(self, response, template_name, msg_prefix=""):
192 """
193 Asserts that the template with the provided name was used in rendering
194 the response.
195 """
196+ if msg_prefix:
197+ msg_prefix += ": "
198+
199 template_names = [t.name for t in to_list(response.template)]
200 if not template_names:
201- self.fail('No templates used to render the response')
202+ self.fail(msg_prefix + "No templates used to render the response")
203 self.failUnless(template_name in template_names,
204- (u"Template '%s' was not a template used to render the response."
205- u" Actual template(s) used: %s") % (template_name,
206- u', '.join(template_names)))
207+ msg_prefix + "Template '%s' was not a template used to render"
208+ " the response. Actual template(s) used: %s" %
209+ (template_name, u', '.join(template_names)))
210
211- def assertTemplateNotUsed(self, response, template_name):
212+ def assertTemplateNotUsed(self, response, template_name, msg_prefix=""):
213 """
214 Asserts that the template with the provided name was NOT used in
215 rendering the response.
216 """
217+ if msg_prefix:
218+ msg_prefix += ": "
219+
220 template_names = [t.name for t in to_list(response.template)]
221 self.failIf(template_name in template_names,
222- (u"Template '%s' was used unexpectedly in rendering the"
223- u" response") % template_name)
224+ msg_prefix + "Template '%s' was used unexpectedly in rendering"
225+ " the response" % template_name)
226
227 class TestCase(TransactionTestCase):
228 """
229diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
230index be2cb8f..813087c 100644
231--- a/tests/regressiontests/test_client_regress/models.py
232+++ b/tests/regressiontests/test_client_regress/models.py
233@@ -31,39 +31,85 @@ class AssertContainsTests(TestCase):
234 self.assertContains(response, 'twice', 2)
235
236 try:
237+ self.assertContains(response, 'text', status_code=999)
238+ except AssertionError, e:
239+ self.assertEquals(str(e), "Couldn't retrieve page: Response code was 200 (expected 999)")
240+ try:
241+ self.assertContains(response, 'text', status_code=999, msg_prefix='abc')
242+ except AssertionError, e:
243+ self.assertEquals(str(e), "abc: Couldn't retrieve page: Response code was 200 (expected 999)")
244+
245+ try:
246+ self.assertNotContains(response, 'text', status_code=999)
247+ except AssertionError, e:
248+ self.assertEquals(str(e), "Couldn't retrieve page: Response code was 200 (expected 999)")
249+ try:
250+ self.assertNotContains(response, 'text', status_code=999, msg_prefix='abc')
251+ except AssertionError, e:
252+ self.assertEquals(str(e), "abc: Couldn't retrieve page: Response code was 200 (expected 999)")
253+
254+ try:
255 self.assertNotContains(response, 'once')
256 except AssertionError, e:
257 self.assertEquals(str(e), "Response should not contain 'once'")
258+ try:
259+ self.assertNotContains(response, 'once', msg_prefix='abc')
260+ except AssertionError, e:
261+ self.assertEquals(str(e), "abc: Response should not contain 'once'")
262
263 try:
264 self.assertContains(response, 'never', 1)
265 except AssertionError, e:
266 self.assertEquals(str(e), "Found 0 instances of 'never' in response (expected 1)")
267+ try:
268+ self.assertContains(response, 'never', 1, msg_prefix='abc')
269+ except AssertionError, e:
270+ self.assertEquals(str(e), "abc: Found 0 instances of 'never' in response (expected 1)")
271
272 try:
273 self.assertContains(response, 'once', 0)
274 except AssertionError, e:
275 self.assertEquals(str(e), "Found 1 instances of 'once' in response (expected 0)")
276+ try:
277+ self.assertContains(response, 'once', 0, msg_prefix='abc')
278+ except AssertionError, e:
279+ self.assertEquals(str(e), "abc: Found 1 instances of 'once' in response (expected 0)")
280
281 try:
282 self.assertContains(response, 'once', 2)
283 except AssertionError, e:
284 self.assertEquals(str(e), "Found 1 instances of 'once' in response (expected 2)")
285+ try:
286+ self.assertContains(response, 'once', 2, msg_prefix='abc')
287+ except AssertionError, e:
288+ self.assertEquals(str(e), "abc: Found 1 instances of 'once' in response (expected 2)")
289
290 try:
291 self.assertContains(response, 'twice', 1)
292 except AssertionError, e:
293 self.assertEquals(str(e), "Found 2 instances of 'twice' in response (expected 1)")
294+ try:
295+ self.assertContains(response, 'twice', 1, msg_prefix='abc')
296+ except AssertionError, e:
297+ self.assertEquals(str(e), "abc: Found 2 instances of 'twice' in response (expected 1)")
298
299 try:
300 self.assertContains(response, 'thrice')
301 except AssertionError, e:
302 self.assertEquals(str(e), "Couldn't find 'thrice' in response")
303+ try:
304+ self.assertContains(response, 'thrice', msg_prefix='abc')
305+ except AssertionError, e:
306+ self.assertEquals(str(e), "abc: Couldn't find 'thrice' in response")
307
308 try:
309 self.assertContains(response, 'thrice', 3)
310 except AssertionError, e:
311 self.assertEquals(str(e), "Found 0 instances of 'thrice' in response (expected 3)")
312+ try:
313+ self.assertContains(response, 'thrice', 3, msg_prefix='abc')
314+ except AssertionError, e:
315+ self.assertEquals(str(e), "abc: Found 0 instances of 'thrice' in response (expected 3)")
316
317 def test_unicode_contains(self):
318 "Unicode characters can be found in template context"
319@@ -79,6 +125,7 @@ class AssertContainsTests(TestCase):
320 self.assertNotContains(r, u'はたけ')
321 self.assertNotContains(r, '\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8'))
322
323+
324 class AssertTemplateUsedTests(TestCase):
325 fixtures = ['testdata.json']
326
327@@ -290,6 +337,10 @@ class AssertFormErrorTests(TestCase):
328 self.assertFormError(response, 'wrong_form', 'some_field', 'Some error.')
329 except AssertionError, e:
330 self.assertEqual(str(e), "The form 'wrong_form' was not used to render the response")
331+ try:
332+ self.assertFormError(response, 'wrong_form', 'some_field', 'Some error.', msg_prefix='abc')
333+ except AssertionError, e:
334+ self.assertEqual(str(e), "abc: The form 'wrong_form' was not used to render the response")
335
336 def test_unknown_field(self):
337 "An assertion is raised if the field name is unknown"
338@@ -308,6 +359,10 @@ class AssertFormErrorTests(TestCase):
339 self.assertFormError(response, 'form', 'some_field', 'Some error.')
340 except AssertionError, e:
341 self.assertEqual(str(e), "The form 'form' in context 0 does not contain the field 'some_field'")
342+ try:
343+ self.assertFormError(response, 'form', 'some_field', 'Some error.', msg_prefix='abc')
344+ except AssertionError, e:
345+ self.assertEqual(str(e), "abc: The form 'form' in context 0 does not contain the field 'some_field'")
346
347 def test_noerror_field(self):
348 "An assertion is raised if the field doesn't have any errors"
349@@ -326,6 +381,10 @@ class AssertFormErrorTests(TestCase):
350 self.assertFormError(response, 'form', 'value', 'Some error.')
351 except AssertionError, e:
352 self.assertEqual(str(e), "The field 'value' on form 'form' in context 0 contains no errors")
353+ try:
354+ self.assertFormError(response, 'form', 'value', 'Some error.', msg_prefix='abc')
355+ except AssertionError, e:
356+ self.assertEqual(str(e), "abc: The field 'value' on form 'form' in context 0 contains no errors")
357
358 def test_unknown_error(self):
359 "An assertion is raised if the field doesn't contain the provided error"
360@@ -344,6 +403,10 @@ class AssertFormErrorTests(TestCase):
361 self.assertFormError(response, 'form', 'email', 'Some error.')
362 except AssertionError, e:
363 self.assertEqual(str(e), "The field 'email' on form 'form' in context 0 does not contain the error 'Some error.' (actual errors: [u'Enter a valid e-mail address.'])")
364+ try:
365+ self.assertFormError(response, 'form', 'email', 'Some error.', msg_prefix='abc')
366+ except AssertionError, e:
367+ self.assertEqual(str(e), "abc: The field 'email' on form 'form' in context 0 does not contain the error 'Some error.' (actual errors: [u'Enter a valid e-mail address.'])")
368
369 def test_unknown_nonfield_error(self):
370 """
371@@ -365,6 +428,10 @@ class AssertFormErrorTests(TestCase):
372 self.assertFormError(response, 'form', None, 'Some error.')
373 except AssertionError, e:
374 self.assertEqual(str(e), "The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )")
375+ try:
376+ self.assertFormError(response, 'form', None, 'Some error.', msg_prefix='abc')
377+ except AssertionError, e:
378+ self.assertEqual(str(e), "abc: The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )")
379
380 class LoginTests(TestCase):
381 fixtures = ['testdata']
Back to Top