Ticket #12089: 12089-r11678-patch_test_client_subcontext_v2.diff

File 12089-r11678-patch_test_client_subcontext_v2.diff, 6.2 KB (added by Antti Kaihola, 15 years ago)

improved patch works for complex data (e.g. compiled templates) in the context

  • 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  
    33import sys
    44import os
    55import re
     6import copy
    67try:
    78    from cStringIO import StringIO
    89except ImportError:
    def store_rendered_templates(store, signal, sender, template, context, **kwargs)  
    8990    Stores templates and contexts that are rendered.
    9091    """
    9192    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)
    9396
    9497def encode_multipart(boundary, data):
    9598    """
  • 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  
    99from django.test.utils import ContextList
    1010from django.core.urlresolvers import reverse
    1111from django.core.exceptions import SuspiciousOperation
    12 from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context
     12from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context, Template
    1313
    1414class AssertContainsTests(TestCase):
    1515    def setUp(self):
    class ContextTests(TestCase):  
    498498        self.assertEqual(response.context['request-foo'], 'whiz')
    499499        self.assertEqual(response.context['data'], 'bacon')
    500500
     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
    501530class SessionTests(TestCase):
    502531    fixtures = ['testdata.json']
    503532
  • 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('',  
    2424    (r'^request_methods/$', views.request_methods_view),
    2525    (r'^check_unicode/$', views.return_unicode),
    2626    (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),
    2729)
  • 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  
    33from django.http import HttpResponse, HttpResponseRedirect
    44from django.core.exceptions import SuspiciousOperation
    55from django.shortcuts import render_to_response
     6from django.template import Template, RequestContext
    67from django.utils import simplejson
    78from django.utils.encoding import smart_str
    89from django.core.serializers.json import DjangoJSONEncoder
    def return_json_file(request):  
    8687                            mimetype='application/json; charset=' + charset)
    8788    response['Content-Disposition'] = 'attachment; filename=testfile.json'
    8889    return response
     90
     91def 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
     103def 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
    - +  
     1Value of supervar: {{ supervar }}
     2Value of subvar: {% with value_for_subvar as subvar %}{% include "testclient/sub.html" %}{% endwith %}
Back to Top