Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #10183 -- Corrected the handling of unicode in assertCo…

…ntains and assertNotContains. Thanks to trbs for the patch.

Merge of r10414 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10415 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3931829c6e2251f992b24701b893d20720f0384e 1 parent bb55853
Russell Keith-Magee authored April 07, 2009
7  django/test/testcases.py
@@ -12,6 +12,7 @@
12 12
 from django.test import _doctest as doctest
13 13
 from django.test.client import Client
14 14
 from django.utils import simplejson
  15
+from django.utils.encoding import smart_str
15 16
 
16 17
 normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s)
17 18
 
@@ -264,6 +265,7 @@ def assertContains(self, response, text, count=None, status_code=200):
264 265
         self.assertEqual(response.status_code, status_code,
265 266
             "Couldn't retrieve page: Response code was %d (expected %d)'" %
266 267
                 (response.status_code, status_code))
  268
+        text = smart_str(text, response._charset)
267 269
         real_count = response.content.count(text)
268 270
         if count is not None:
269 271
             self.assertEqual(real_count, count,
@@ -282,8 +284,9 @@ def assertNotContains(self, response, text, status_code=200):
282 284
         self.assertEqual(response.status_code, status_code,
283 285
             "Couldn't retrieve page: Response code was %d (expected %d)'" %
284 286
                 (response.status_code, status_code))
285  
-        self.assertEqual(response.content.count(text), 0,
286  
-                         "Response should not contain '%s'" % text)
  287
+        text = smart_str(text, response._charset)
  288
+        self.assertEqual(response.content.count(text),
  289
+             0, "Response should not contain '%s'" % text)
287 290
 
288 291
     def assertFormError(self, response, form, field, errors):
289 292
         """
22  tests/regressiontests/test_client_regress/models.py
... ...
@@ -1,3 +1,4 @@
  1
+# -*- coding: utf-8 -*-
1 2
 """
2 3
 Regression tests for the Test Client, especially the customized assertions.
3 4
 """
@@ -10,6 +11,13 @@
10 11
 from django.template import TemplateDoesNotExist, TemplateSyntaxError
11 12
 
12 13
 class AssertContainsTests(TestCase):
  14
+    def setUp(self):
  15
+        self.old_templates = settings.TEMPLATE_DIRS
  16
+        settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), 'templates'),)
  17
+
  18
+    def tearDown(self):
  19
+        settings.TEMPLATE_DIRS = self.old_templates
  20
+
13 21
     def test_contains(self):
14 22
         "Responses can be inspected for content, including counting repeated substrings"
15 23
         response = self.client.get('/test_client_regress/no_template_view/')
@@ -56,6 +64,20 @@ def test_contains(self):
56 64
         except AssertionError, e:
57 65
             self.assertEquals(str(e), "Found 0 instances of 'thrice' in response (expected 3)")
58 66
 
  67
+    def test_unicode_contains(self):
  68
+        "Unicode characters can be found in template context"
  69
+        #Regression test for #10183
  70
+        r = self.client.get('/test_client_regress/check_unicode/')
  71
+        self.assertContains(r, u'さかき')
  72
+        self.assertContains(r, '\xe5\xb3\xa0'.decode('utf-8'))
  73
+
  74
+    def test_unicode_not_contains(self):
  75
+        "Unicode characters can be searched for, and not found in template context"
  76
+        #Regression test for #10183
  77
+        r = self.client.get('/test_client_regress/check_unicode/')
  78
+        self.assertNotContains(r, u'はたけ')
  79
+        self.assertNotContains(r, '\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8'))
  80
+
59 81
 class AssertTemplateUsedTests(TestCase):
60 82
     fixtures = ['testdata.json']
61 83
 
5  tests/regressiontests/test_client_regress/templates/unicode.html
... ...
@@ -0,0 +1,5 @@
  1
+* 峠 (とうげ tōge "mountain pass")
  2
+* 榊 (さかき sakaki "tree, genus Cleyera")
  3
+* 辻 (つじ tsuji "crossroads, street")
  4
+* 働 (どう dō, はたら hatara(ku) "work")
  5
+* 腺 (せん sen, "gland")
1  tests/regressiontests/test_client_regress/urls.py
@@ -9,4 +9,5 @@
9 9
     (r'^login_protected_redirect_view/$', views.login_protected_redirect_view),
10 10
     (r'^set_session/$', views.set_session_view),
11 11
     (r'^check_session/$', views.check_session_view),
  12
+    (r'^check_unicode/$', views.return_unicode),
12 13
 )
4  tests/regressiontests/test_client_regress/views.py
... ...
@@ -1,6 +1,7 @@
1 1
 from django.contrib.auth.decorators import login_required
2 2
 from django.http import HttpResponse, HttpResponseRedirect
3 3
 from django.core.exceptions import SuspiciousOperation
  4
+from django.shortcuts import render_to_response
4 5
 
5 6
 def no_template_view(request):
6 7
     "A simple view that expects a GET request, and returns a rendered template"
@@ -43,3 +44,6 @@ def set_session_view(request):
43 44
 def check_session_view(request):
44 45
     "A view that reads a session variable"
45 46
     return HttpResponse(request.session.get('session_var', 'NO'))
  47
+
  48
+def return_unicode(request):
  49
+    return render_to_response('unicode.html')

0 notes on commit 3931829

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