summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2009-04-07 12:06:05 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2009-04-07 12:06:05 +0000
commit366710e6368fbb7530ccfae92a2b6faa40ea4bc1 (patch)
tree007a89f3096ad5ea6cd7fee58f818c5f60d50940
parent37c21ef05d678d09a1143c3699c2165d0b8fa300 (diff)
Fixed #10183 -- Corrected the handling of unicode in assertContains and assertNotContains. Thanks to trbs for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10414 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/test/testcases.py7
-rw-r--r--tests/regressiontests/test_client_regress/models.py22
-rw-r--r--tests/regressiontests/test_client_regress/templates/unicode.html5
-rw-r--r--tests/regressiontests/test_client_regress/urls.py1
-rw-r--r--tests/regressiontests/test_client_regress/views.py3
5 files changed, 36 insertions, 2 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py
index 550baf9137..794bc5039d 100644
--- a/django/test/testcases.py
+++ b/django/test/testcases.py
@@ -12,6 +12,7 @@ from django.http import QueryDict
from django.test import _doctest as doctest
from django.test.client import Client
from django.utils import simplejson
+from django.utils.encoding import smart_str
normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s)
normalize_decimals = lambda s: re.sub(r"Decimal\('(\d+(\.\d*)?)'\)", lambda m: "Decimal(\"%s\")" % m.groups()[0], s)
@@ -330,6 +331,7 @@ class TransactionTestCase(unittest.TestCase):
self.assertEqual(response.status_code, status_code,
"Couldn't retrieve page: Response code was %d (expected %d)'" %
(response.status_code, status_code))
+ text = smart_str(text, response._charset)
real_count = response.content.count(text)
if count is not None:
self.assertEqual(real_count, count,
@@ -348,8 +350,9 @@ class TransactionTestCase(unittest.TestCase):
self.assertEqual(response.status_code, status_code,
"Couldn't retrieve page: Response code was %d (expected %d)'" %
(response.status_code, status_code))
- self.assertEqual(response.content.count(text), 0,
- "Response should not contain '%s'" % text)
+ text = smart_str(text, response._charset)
+ self.assertEqual(response.content.count(text),
+ 0, "Response should not contain '%s'" % text)
def assertFormError(self, response, form, field, errors):
"""
diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
index 90383dba84..fe55ffff92 100644
--- a/tests/regressiontests/test_client_regress/models.py
+++ b/tests/regressiontests/test_client_regress/models.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
"""
Regression tests for the Test Client, especially the customized assertions.
"""
@@ -11,6 +12,13 @@ from django.core.exceptions import SuspiciousOperation
from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context
class AssertContainsTests(TestCase):
+ def setUp(self):
+ self.old_templates = settings.TEMPLATE_DIRS
+ settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), 'templates'),)
+
+ def tearDown(self):
+ settings.TEMPLATE_DIRS = self.old_templates
+
def test_contains(self):
"Responses can be inspected for content, including counting repeated substrings"
response = self.client.get('/test_client_regress/no_template_view/')
@@ -57,6 +65,20 @@ class AssertContainsTests(TestCase):
except AssertionError, e:
self.assertEquals(str(e), "Found 0 instances of 'thrice' in response (expected 3)")
+ def test_unicode_contains(self):
+ "Unicode characters can be found in template context"
+ #Regression test for #10183
+ r = self.client.get('/test_client_regress/check_unicode/')
+ self.assertContains(r, u'さかき')
+ self.assertContains(r, '\xe5\xb3\xa0'.decode('utf-8'))
+
+ def test_unicode_not_contains(self):
+ "Unicode characters can be searched for, and not found in template context"
+ #Regression test for #10183
+ r = self.client.get('/test_client_regress/check_unicode/')
+ self.assertNotContains(r, u'はたけ')
+ self.assertNotContains(r, '\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8'))
+
class AssertTemplateUsedTests(TestCase):
fixtures = ['testdata.json']
diff --git a/tests/regressiontests/test_client_regress/templates/unicode.html b/tests/regressiontests/test_client_regress/templates/unicode.html
new file mode 100644
index 0000000000..bdb6c91a8f
--- /dev/null
+++ b/tests/regressiontests/test_client_regress/templates/unicode.html
@@ -0,0 +1,5 @@
+* 峠 (とうげ tōge "mountain pass")
+* 榊 (さかき sakaki "tree, genus Cleyera")
+* 辻 (つじ tsuji "crossroads, street")
+* 働 (どう dō, はたら hatara(ku) "work")
+* 腺 (せん sen, "gland")
diff --git a/tests/regressiontests/test_client_regress/urls.py b/tests/regressiontests/test_client_regress/urls.py
index 0f9082d4cb..c5872bf899 100644
--- a/tests/regressiontests/test_client_regress/urls.py
+++ b/tests/regressiontests/test_client_regress/urls.py
@@ -22,4 +22,5 @@ urlpatterns = patterns('',
(r'^set_session/$', views.set_session_view),
(r'^check_session/$', views.check_session_view),
(r'^request_methods/$', views.request_methods_view),
+ (r'^check_unicode/$', views.return_unicode),
)
diff --git a/tests/regressiontests/test_client_regress/views.py b/tests/regressiontests/test_client_regress/views.py
index bd0b8aff50..75b8d887e8 100644
--- a/tests/regressiontests/test_client_regress/views.py
+++ b/tests/regressiontests/test_client_regress/views.py
@@ -60,3 +60,6 @@ def check_session_view(request):
def request_methods_view(request):
"A view that responds with the request method"
return HttpResponse('request method: %s' % request.method)
+
+def return_unicode(request):
+ return render_to_response('unicode.html')