Ticket #12089: 12089-r11655-patch_test-client-subcontext.diff

File 12089-r11655-patch_test-client-subcontext.diff, 4.3 KB (added by Antti Kaihola, 15 years ago)

patch with tests to fix test client to collect sub-contexts correctly

  • django/test/client.py

    diff --git a/django/test/client.py b/django/test/client.py
    index 348bfa7..5fc5877 100644
    a b from urlparse import urlparse, urlunparse, urlsplit  
    33import sys
    44import os
    55import re
     6from copy import deepcopy
    67try:
    78    from cStringIO import StringIO
    89except ImportError:
    def store_rendered_templates(store, signal, sender, template, context, **kwargs)  
    8485    Stores templates and contexts that are rendered.
    8586    """
    8687    store.setdefault('template', []).append(template)
    87     store.setdefault('context', ContextList()).append(context)
     88    store.setdefault('context', ContextList()).append(deepcopy(context))
    8889
    8990def encode_multipart(boundary, data):
    9091    """
  • 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 be2cb8f..973bf75 100644
    a b 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
    501520class SessionTests(TestCase):
    502521    fixtures = ['testdata.json']
    503522
  • 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..f1db23a 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),
    2728)
  • 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..0e0c3aa 100644
    a b def return_json_file(request):  
    8686                            mimetype='application/json; charset=' + charset)
    8787    response['Content-Disposition'] = 'attachment; filename=testfile.json'
    8888    return response
     89
     90def view_with_subcontext(request):
     91    """A view that augments the context when including a sub-template
     92
     93    The purpose of this view is to check that the test client
     94    correctly catches context variables from sub-contexts.  This
     95    happens when a sub-template is rendered with a context modified or
     96    created by a template tag.
     97    """
     98    return render_to_response("testclient/super.html",
     99                              {'supervar': request.GET['supervar'],
     100                               'value_for_subvar': request.GET['value_for_subvar']})
  • 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