Ticket #10482: 10482.consistent_response_context.diff

File 10482.consistent_response_context.diff, 5.8 KB (added by julien, 6 years ago)

Patch contains tests and doc.

  • django/django/test/client.py

     
    2020from django.utils.http import urlencode
    2121from django.utils.itercompat import is_iterable
    2222from django.db import transaction, close_connection
     23from django.test.utils import ContextList
    2324
    2425BOUNDARY = 'BoUnDaRyStRiNg'
    2526MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY
     
    8182    Stores templates and contexts that are rendered.
    8283    """
    8384    store.setdefault('template',[]).append(template)
    84     store.setdefault('context',[]).append(context)
     85    store.setdefault('context',ContextList()).append(context)
    8586
    8687def encode_multipart(boundary, data):
    8788    """
  • django/django/test/utils.py

     
    66from django.template import Template
    77from django.utils.translation import deactivate
    88
     9class ContextList(list):
     10   
     11    def __getitem__(self, key):
     12        if type(key) == str:
     13            for subcontext in self:
     14                if key in subcontext:
     15                    return subcontext[key]
     16            raise KeyError
     17        else:
     18            return super(ContextList, self).__getitem__(key)
     19           
     20
    921def instrumented_test_render(self, context):
    1022    """
    1123    An instrumented Template render method, providing a signal
  • django/docs/topics/testing.txt

     
    710710        produced the response content.
    711711
    712712        If the rendered page used multiple templates, then ``context`` will be a
    713         list of ``Context`` objects, in the order in which they were rendered.
     713        list of ``Context`` objects, in the order in which they were rendered. Regardless
     714        of the number of templates used, context variables can be directly accessed from this
     715        attribute without looking in each ``Context`` object.
    714716
    715717    .. attribute:: request
    716718
  • django/tests/regressiontests/test_client_regress/context_templates/extended.html

     
     1{% extends "base.html" %}
     2{% block title %}Extended template{% endblock %}
     3{% block content %}
     4This is just a template extending the base.
     5{% endblock %}
     6 No newline at end of file
  • django/tests/regressiontests/test_client_regress/models.py

     
    455455        url = reverse('arg_view', args=['somename'])
    456456        self.assertEquals(url, '/test_client_regress/arg_view/somename/')
    457457
     458class ContextTests(TestCase):
     459    fixtures = ['testdata']
     460
     461    def setUp(self):
     462        self.old_templates = settings.TEMPLATE_DIRS
     463        settings.TEMPLATE_DIRS += (os.path.join(os.path.dirname(__file__), 'context_templates'),)
     464
     465    def tearDown(self):
     466        settings.TEMPLATE_DIRS = self.old_templates
     467       
     468    def test_context(self):
     469        # Without template inheritance
     470        response = self.client.get("/test_client_regress/request_data/", data={'foo':'whiz'})
     471        self.assertEqual(response.context['get-foo'], 'whiz')
     472        self.assertEqual(response.context['request-foo'], 'whiz')
     473       
     474        # With template inheritance
     475        response = self.client.get("/test_client_regress/request_data_extended/", data={'foo':'whiz'})
     476        self.assertEqual(response.context['get-foo'], 'whiz')
     477        self.assertEqual(response.context['request-foo'], 'whiz')
     478
    458479class SessionTests(TestCase):
    459480    fixtures = ['testdata.json']
    460481
  • django/tests/regressiontests/test_client_regress/urls.py

     
    77    (r'^staff_only/$', views.staff_only_view),
    88    (r'^get_view/$', views.get_view),
    99    (r'^request_data/$', views.request_data),
     10    (r'^request_data_extended/$', views.request_data, {'template':'extended.html'}),
    1011    url(r'^arg_view/(?P<name>.+)/$', views.view_with_argument, name='arg_view'),
    1112    (r'^login_protected_redirect_view/$', views.login_protected_redirect_view),
    1213    (r'^redirects/$', redirect_to, {'url': '/test_client_regress/redirects/further/'}),
  • django/tests/regressiontests/test_client_regress/views.py

     
    1919    return HttpResponse("Hello world")
    2020get_view = login_required(get_view)
    2121
    22 def request_data(request):
     22def request_data(request, template='base.html'):
    2323    "A simple view that returns the request data in the context"
    24     return render_to_response('base.html', {
     24    return render_to_response(template, {
    2525        'get-foo':request.GET.get('foo',None),
    2626        'get-bar':request.GET.get('bar',None),
    2727        'post-foo':request.POST.get('foo',None),
Back to Top