Ticket #20237: 20237-2.diff

File 20237-2.diff, 3.3 KB (added by Claude Paroz, 11 years ago)

Also handle pure binary assertContains text param

  • django/test/testcases.py

    diff --git a/django/test/testcases.py b/django/test/testcases.py
    index 358cb3f..a9fcc2b 100644
    a b class TransactionTestCase(SimpleTestCase):  
    622622        if not isinstance(text, bytes) or html:
    623623            text = force_text(text, encoding=response._charset)
    624624            content = content.decode(response._charset)
     625            text_repr = "'%s'" % text
     626        else:
     627            text_repr = repr(text)
    625628        if html:
    626629            content = assert_and_parse_html(self, content, None,
    627630                "Response's content is not valid HTML:")
    class TransactionTestCase(SimpleTestCase):  
    630633        real_count = content.count(text)
    631634        if count is not None:
    632635            self.assertEqual(real_count, count,
    633                 msg_prefix + "Found %d instances of '%s' in response"
    634                 " (expected %d)" % (real_count, text, count))
     636                msg_prefix + "Found %d instances of %s in response"
     637                " (expected %d)" % (real_count, text_repr, count))
    635638        else:
    636639            self.assertTrue(real_count != 0,
    637                 msg_prefix + "Couldn't find '%s' in response" % text)
     640                msg_prefix + "Couldn't find %s in response" % text_repr)
    638641
    639642    def assertNotContains(self, response, text, status_code=200,
    640643                          msg_prefix='', html=False):
    class TransactionTestCase(SimpleTestCase):  
    661664        if not isinstance(text, bytes) or html:
    662665            text = force_text(text, encoding=response._charset)
    663666            content = content.decode(response._charset)
     667            text_repr = "'%s'" % text
     668        else:
     669            text_repr = repr(text)
    664670        if html:
    665671            content = assert_and_parse_html(self, content, None,
    666672                'Response\'s content is not valid HTML:')
    667673            text = assert_and_parse_html(self, text, None,
    668674                'Second argument is not valid HTML:')
    669675        self.assertEqual(content.count(text), 0,
    670             msg_prefix + "Response should not contain '%s'" % text)
     676            msg_prefix + "Response should not contain %s" % text_repr)
    671677
    672678    def assertFormError(self, response, form, field, errors, msg_prefix=''):
    673679        """
  • tests/test_client_regress/tests.py

    diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py
    index fe0a410..2582b21 100644
    a b class AssertContainsTests(TestCase):  
    133133
    134134    def test_binary_contains(self):
    135135        r = self.client.get('/test_client_regress/check_binary/')
    136         self.assertContains(r, b'PDF document')
     136        self.assertContains(r, b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e')
    137137        with self.assertRaises(AssertionError):
    138             self.assertContains(r, b'PDF document', count=2)
    139         self.assertNotContains(r, b'ODF document')
     138            self.assertContains(r, b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e', count=2)
     139
     140    def test_binary_not_contains(self):
     141        r = self.client.get('/test_client_regress/check_binary/')
     142        self.assertNotContains(r, b'%ODF-1.4\r\n%\x93\x8c\x8b\x9e')
     143        with self.assertRaises(AssertionError):
     144            self.assertNotContains(r, b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e')
    140145
    141146    def test_nontext_contains(self):
    142147        r = self.client.get('/test_client_regress/no_template_view/')
Back to Top