Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #20918 -- Tidied up implementation of custom assertions

1. Moved common logic between assertContains and assertNotContains
   into a separate function _assert_contains()

2. Moved common logic between assertTemplateUsed and
   assertTemplateNotUsed into a separate function
   _assert_templateused()

Thanks Marc Tamlyn for the report and review.
  • Loading branch information...
commit a2374bcf476b49441793f1ad213126b07543da54 1 parent d70908e
ersran9 authored August 22, 2013 timgraham committed September 05, 2013

Showing 1 changed file with 42 additions and 57 deletions. Show diff stats Hide diff stats

  1. 99  django/test/testcases.py
99  django/test/testcases.py
@@ -288,16 +288,7 @@ def assertRedirects(self, response, expected_url, status_code=302,
288 288
             msg_prefix + "Response redirected to '%s', expected '%s'" %
289 289
                 (url, expected_url))
290 290
 
291  
-    def assertContains(self, response, text, count=None, status_code=200,
292  
-                       msg_prefix='', html=False):
293  
-        """
294  
-        Asserts that a response indicates that some content was retrieved
295  
-        successfully, (i.e., the HTTP status code was as expected), and that
296  
-        ``text`` occurs ``count`` times in the content of the response.
297  
-        If ``count`` is None, the count doesn't matter - the assertion is true
298  
-        if the text occurs at least once in the response.
299  
-        """
300  
-
  291
+    def _assert_contains(self, response, text, status_code, msg_prefix, html):
301 292
         # If the response supports deferred rendering and hasn't been rendered
302 293
         # yet, then ensure that it does get rendered before proceeding further.
303 294
         if (hasattr(response, 'render') and callable(response.render)
@@ -327,6 +318,20 @@ def assertContains(self, response, text, count=None, status_code=200,
327 318
             text = assert_and_parse_html(self, text, None,
328 319
                 "Second argument is not valid HTML:")
329 320
         real_count = content.count(text)
  321
+        return (text_repr, real_count, msg_prefix)
  322
+
  323
+    def assertContains(self, response, text, count=None, status_code=200,
  324
+                       msg_prefix='', html=False):
  325
+        """
  326
+        Asserts that a response indicates that some content was retrieved
  327
+        successfully, (i.e., the HTTP status code was as expected), and that
  328
+        ``text`` occurs ``count`` times in the content of the response.
  329
+        If ``count`` is None, the count doesn't matter - the assertion is true
  330
+        if the text occurs at least once in the response.
  331
+        """
  332
+        text_repr, real_count, msg_prefix = self._assert_contains(
  333
+            response, text, status_code, msg_prefix, html)
  334
+
330 335
         if count is not None:
331 336
             self.assertEqual(real_count, count,
332 337
                 msg_prefix + "Found %d instances of %s in response"
@@ -342,34 +347,11 @@ def assertNotContains(self, response, text, status_code=200,
342 347
         successfully, (i.e., the HTTP status code was as expected), and that
343 348
         ``text`` doesn't occurs in the content of the response.
344 349
         """
  350
+        text_repr, real_count, msg_prefix = self._assert_contains(
  351
+            response, text, status_code, msg_prefix, html)
345 352
 
346  
-        # If the response supports deferred rendering and hasn't been rendered
347  
-        # yet, then ensure that it does get rendered before proceeding further.
348  
-        if (hasattr(response, 'render') and callable(response.render)
349  
-            and not response.is_rendered):
350  
-            response.render()
351  
-
352  
-        if msg_prefix:
353  
-            msg_prefix += ": "
354  
-
355  
-        self.assertEqual(response.status_code, status_code,
356  
-            msg_prefix + "Couldn't retrieve content: Response code was %d"
357  
-            " (expected %d)" % (response.status_code, status_code))
358  
-
359  
-        content = response.content
360  
-        if not isinstance(text, bytes) or html:
361  
-            text = force_text(text, encoding=response._charset)
362  
-            content = content.decode(response._charset)
363  
-            text_repr = "'%s'" % text
364  
-        else:
365  
-            text_repr = repr(text)
366  
-        if html:
367  
-            content = assert_and_parse_html(self, content, None,
368  
-                'Response\'s content is not valid HTML:')
369  
-            text = assert_and_parse_html(self, text, None,
370  
-                'Second argument is not valid HTML:')
371  
-        self.assertEqual(content.count(text), 0,
372  
-            msg_prefix + "Response should not contain %s" % text_repr)
  353
+        self.assertEqual(real_count, 0,
  354
+                msg_prefix + "Response should not contain %s" % text_repr)
373 355
 
374 356
     def assertFormError(self, response, form, field, errors, msg_prefix=''):
375 357
         """
@@ -499,26 +481,36 @@ def assertFormsetError(self, response, formset, form_index, field, errors,
499 481
             self.fail(msg_prefix + "The formset '%s' was not used to render "
500 482
                       "the response" % formset)
501 483
 
502  
-    def assertTemplateUsed(self, response=None, template_name=None, msg_prefix=''):
503  
-        """
504  
-        Asserts that the template with the provided name was used in rendering
505  
-        the response. Also usable as context manager.
506  
-        """
  484
+    def _assert_template_used(self, response, template_name, msg_prefix):
  485
+
507 486
         if response is None and template_name is None:
508 487
             raise TypeError('response and/or template_name argument must be provided')
509 488
 
510 489
         if msg_prefix:
511 490
             msg_prefix += ": "
512 491
 
513  
-        # Use assertTemplateUsed as context manager.
514 492
         if not hasattr(response, 'templates') or (response is None and template_name):
515 493
             if response:
516 494
                 template_name = response
517 495
                 response = None
518  
-            context = _AssertTemplateUsedContext(self, template_name)
519  
-            return context
  496
+            # use this template with context manager
  497
+            return template_name, None, msg_prefix
520 498
 
521 499
         template_names = [t.name for t in response.templates]
  500
+        return None, template_names, msg_prefix
  501
+
  502
+    def assertTemplateUsed(self, response=None, template_name=None, msg_prefix=''):
  503
+        """
  504
+        Asserts that the template with the provided name was used in rendering
  505
+        the response. Also usable as context manager.
  506
+        """
  507
+        context_mgr_template, template_names, msg_prefix = self._assert_template_used(
  508
+            response, template_name, msg_prefix)
  509
+
  510
+        if context_mgr_template:
  511
+            # Use assertTemplateUsed as context manager.
  512
+            return _AssertTemplateUsedContext(self, context_mgr_template)
  513
+
522 514
         if not template_names:
523 515
             self.fail(msg_prefix + "No templates used to render the response")
524 516
         self.assertTrue(template_name in template_names,
@@ -531,21 +523,14 @@ def assertTemplateNotUsed(self, response=None, template_name=None, msg_prefix=''
531 523
         Asserts that the template with the provided name was NOT used in
532 524
         rendering the response. Also usable as context manager.
533 525
         """
534  
-        if response is None and template_name is None:
535  
-            raise TypeError('response and/or template_name argument must be provided')
536 526
 
537  
-        if msg_prefix:
538  
-            msg_prefix += ": "
  527
+        context_mgr_template, template_names, msg_prefix = self._assert_template_used(
  528
+            response, template_name, msg_prefix)
539 529
 
540  
-        # Use assertTemplateUsed as context manager.
541  
-        if not hasattr(response, 'templates') or (response is None and template_name):
542  
-            if response:
543  
-                template_name = response
544  
-                response = None
545  
-            context = _AssertTemplateNotUsedContext(self, template_name)
546  
-            return context
  530
+        if context_mgr_template:
  531
+            # Use assertTemplateNotUsed as context manager.
  532
+            return _AssertTemplateNotUsedContext(self, context_mgr_template)
547 533
 
548  
-        template_names = [t.name for t in response.templates]
549 534
         self.assertFalse(template_name in template_names,
550 535
             msg_prefix + "Template '%s' was used unexpectedly in rendering"
551 536
             " the response" % template_name)

0 notes on commit a2374bc

Please sign in to comment.
Something went wrong with that request. Please try again.