Ticket #12089: 12089-r11678-patch_test_client_subcontext_v2.diff
File 12089-r11678-patch_test_client_subcontext_v2.diff, 6.2 KB (added by , 15 years ago) |
---|
-
django/test/client.py
diff --git a/django/test/client.py b/django/test/client.py index 63ad1c1..551dce1 100644
a b from urlparse import urlparse, urlunparse, urlsplit 3 3 import sys 4 4 import os 5 5 import re 6 import copy 6 7 try: 7 8 from cStringIO import StringIO 8 9 except ImportError: … … def store_rendered_templates(store, signal, sender, template, context, **kwargs) 89 90 Stores templates and contexts that are rendered. 90 91 """ 91 92 store.setdefault('template', []).append(template) 92 store.setdefault('context', ContextList()).append(context) 93 context_clone = copy.copy(context) 94 context_clone.dicts = [d.copy() for d in context.dicts] 95 store.setdefault('context', ContextList()).append(context_clone) 93 96 94 97 def encode_multipart(boundary, data): 95 98 """ -
tests/regressiontests/test_client_regress/models.py
diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py index 58693cc..ce12496 100644
a b from django.test import Client, TestCase 9 9 from django.test.utils import ContextList 10 10 from django.core.urlresolvers import reverse 11 11 from django.core.exceptions import SuspiciousOperation 12 from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context 12 from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context, Template 13 13 14 14 class AssertContainsTests(TestCase): 15 15 def setUp(self): … … class ContextTests(TestCase): 498 498 self.assertEqual(response.context['request-foo'], 'whiz') 499 499 self.assertEqual(response.context['data'], 'bacon') 500 500 501 def test_subcontext(self): 502 "Context variables can be retrieved from subcontexts" 503 response = self.client.get('/test_client_regress/subcontext/', data={'supervar': 1, 'value_for_subvar': 2}) 504 self.assertEqual(len(response.context), 2) 505 self.assertEqual(response.context['supervar'], u'1') 506 self.assertEqual(response.context['subvar'], u'2') 507 508 def test_subcontext_direct(self): 509 "Context variables retrieved from subcontexts can be accessed separately" 510 response = self.client.get('/test_client_regress/subcontext/', data={'supervar': 1, 'value_for_subvar': 2}) 511 512 # context for the supertemplate: subcontext not yet set 513 self.assertEqual(response.context[0]['supervar'], u'1') 514 self.assertFalse('subvar' in response.context[0]) 515 516 # context for the subtemplate: subcontext has been set 517 self.assertEqual(response.context[1]['supervar'], u'1') 518 self.assertEqual(response.context[1]['subvar'], u'2') 519 520 def test_template_in_context(self): 521 """Context variables with complex data can be retrieved 522 523 The test client must be able to collect contexts which contain 524 e.g. compiled templates as variable values. 525 """ 526 response = self.client.get('/test_client_regress/template_in_context/') 527 self.assertEqual([c.keys() for c in response.context], [['testvar']]) 528 self.assertTrue(isinstance(response.context['testvar'], Template)) 529 501 530 class SessionTests(TestCase): 502 531 fixtures = ['testdata.json'] 503 532 -
tests/regressiontests/test_client_regress/urls.py
diff --git a/tests/regressiontests/test_client_regress/urls.py b/tests/regressiontests/test_client_regress/urls.py index 99eb7b7..d5977d6 100644
a b urlpatterns = patterns('', 24 24 (r'^request_methods/$', views.request_methods_view), 25 25 (r'^check_unicode/$', views.return_unicode), 26 26 (r'^parse_unicode_json/$', views.return_json_file), 27 (r'^subcontext/$', views.view_with_subcontext), 28 (r'^template_in_context/$', views.view_with_template_in_context), 27 29 ) -
tests/regressiontests/test_client_regress/views.py
diff --git a/tests/regressiontests/test_client_regress/views.py b/tests/regressiontests/test_client_regress/views.py index 5ba7cc3..42cb581 100644
a b from django.contrib.auth.decorators import login_required 3 3 from django.http import HttpResponse, HttpResponseRedirect 4 4 from django.core.exceptions import SuspiciousOperation 5 5 from django.shortcuts import render_to_response 6 from django.template import Template, RequestContext 6 7 from django.utils import simplejson 7 8 from django.utils.encoding import smart_str 8 9 from django.core.serializers.json import DjangoJSONEncoder … … def return_json_file(request): 86 87 mimetype='application/json; charset=' + charset) 87 88 response['Content-Disposition'] = 'attachment; filename=testfile.json' 88 89 return response 90 91 def view_with_subcontext(request): 92 """A view that augments the context when including a sub-template 93 94 The purpose of this view is to check that the test client 95 correctly catches context variables from sub-contexts. This 96 happens when a sub-template is rendered with a context modified or 97 created by a template tag. 98 """ 99 return render_to_response("testclient/super.html", 100 {'supervar': request.GET['supervar'], 101 'value_for_subvar': request.GET['value_for_subvar']}) 102 103 def view_with_template_in_context(request): 104 """A view that renders a template with a Template object in the context 105 106 The purpose of this view is to check that complex data in the 107 context doesn't cause errors when the test client collects 108 contexts used for rendering templates. 109 """ 110 return render_to_response("base.html", 111 {'testvar': Template("{% cycle 'a' 'b' %}")}) -
new file tests/templates/testclient/sub.html
diff --git a/tests/templates/testclient/sub.html b/tests/templates/testclient/sub.html new file mode 100644 index 0000000..bb4cad6
- + 1 {{ subvar }} 2 -
new file tests/templates/testclient/super.html
diff --git a/tests/templates/testclient/super.html b/tests/templates/testclient/super.html new file mode 100644 index 0000000..998fb0c
- + 1 Value of supervar: {{ supervar }} 2 Value of subvar: {% with value_for_subvar as subvar %}{% include "testclient/sub.html" %}{% endwith %}