Ticket #12226: 12226_r13760.2.diff

File 12226_r13760.2.diff, 7.3 KB (added by Carl Meyer, 14 years ago)
  • django/test/client.py

    diff --git a/django/test/client.py b/django/test/client.py
    index 08e3ff6..0588088 100644
    a b import sys  
    44import os
    55import re
    66import mimetypes
     7import warnings
    78try:
    89    from cStringIO import StringIO
    910except ImportError:
    def store_rendered_templates(store, signal, sender, template, context, **kwargs)  
    9394    """
    9495    Stores templates and contexts that are rendered.
    9596    """
    96     store.setdefault('template', []).append(template)
     97    store.setdefault('templates', []).append(template)
    9798    store.setdefault('context', ContextList()).append(context)
    9899
    99100def encode_multipart(boundary, data):
    class Client(object):  
    260261            response.request = request
    261262
    262263            # Add any rendered template detail to the response.
    263             # If there was only one template rendered (the most likely case),
    264             # flatten the list to a single element.
    265             for detail in ('template', 'context'):
    266                 if data.get(detail):
    267                     if len(data[detail]) == 1:
    268                         setattr(response, detail, data[detail][0]);
    269                     else:
    270                         setattr(response, detail, data[detail])
    271                 else:
    272                     setattr(response, detail, None)
     264            response.templates = data.get("templates", [])
     265            response.context = data.get("context")
     266
     267            # Flatten a single context. Not really necessary anymore thanks to
     268            # the __getattr__ flattening in ContextList, but has some edge-case
     269            # backwards-compatibility implications.
     270            if response.context and len(response.context) == 1:
     271                response.context = response.context[0]
     272
     273            # Provide a backwards-compatible (but pending deprecation) response.template
     274            def _get_template(self):
     275                warnings.warn("response.template is deprecated; use response.templates instead (which is always a list)",
     276                              PendingDeprecationWarning)
     277                if not self.templates:
     278                    return None
     279                elif len(self.templates) == 1:
     280                    return self.templates[0]
     281                return self.templates
     282            response.__class__.template = property(_get_template)
    273283
    274284            # Update persistent cookie data.
    275285            if response.cookies:
  • django/test/testcases.py

    diff --git a/django/test/testcases.py b/django/test/testcases.py
    index 10bd6c6..1a34fd9 100644
    a b class TransactionTestCase(unittest.TestCase):  
    445445        if msg_prefix:
    446446            msg_prefix += ": "
    447447
    448         template_names = [t.name for t in to_list(response.template)]
     448        template_names = [t.name for t in response.templates]
    449449        if not template_names:
    450450            self.fail(msg_prefix + "No templates used to render the response")
    451451        self.failUnless(template_name in template_names,
    class TransactionTestCase(unittest.TestCase):  
    461461        if msg_prefix:
    462462            msg_prefix += ": "
    463463
    464         template_names = [t.name for t in to_list(response.template)]
     464        template_names = [t.name for t in response.templates]
    465465        self.failIf(template_name in template_names,
    466466            msg_prefix + "Template '%s' was used unexpectedly in rendering"
    467467            " the response" % template_name)
  • tests/modeltests/test_client/models.py

    diff --git a/tests/modeltests/test_client/models.py b/tests/modeltests/test_client/models.py
    index 3052008..5cd61bf 100644
    a b class ClientTest(TestCase):  
    3737        # Check some response details
    3838        self.assertContains(response, 'This is a test')
    3939        self.assertEqual(response.context['var'], u'\xf2')
    40         self.assertEqual(response.template.name, 'GET Template')
     40        self.assertEqual(response.templates[0].name, 'GET Template')
    4141
    4242    def test_get_post_view(self):
    4343        "GET a view that normally expects POSTs"
    class ClientTest(TestCase):  
    4545
    4646        # Check some response details
    4747        self.assertEqual(response.status_code, 200)
    48         self.assertEqual(response.template.name, 'Empty GET Template')
     48        self.assertEqual(response.templates[0].name, 'Empty GET Template')
    4949        self.assertTemplateUsed(response, 'Empty GET Template')
    5050        self.assertTemplateNotUsed(response, 'Empty POST Template')
    5151
    class ClientTest(TestCase):  
    5555
    5656        # Check some response details
    5757        self.assertEqual(response.status_code, 200)
    58         self.assertEqual(response.template.name, 'Empty POST Template')
     58        self.assertEqual(response.templates[0].name, 'Empty POST Template')
    5959        self.assertTemplateNotUsed(response, 'Empty GET Template')
    6060        self.assertTemplateUsed(response, 'Empty POST Template')
    6161
    class ClientTest(TestCase):  
    6969        # Check some response details
    7070        self.assertEqual(response.status_code, 200)
    7171        self.assertEqual(response.context['data'], '37')
    72         self.assertEqual(response.template.name, 'POST Template')
     72        self.assertEqual(response.templates[0].name, 'POST Template')
    7373        self.failUnless('Data received' in response.content)
    7474
    7575    def test_response_headers(self):
    class ClientTest(TestCase):  
    8484        response = self.client.post("/test_client/raw_post_view/", test_doc,
    8585                                    content_type="text/xml")
    8686        self.assertEqual(response.status_code, 200)
    87         self.assertEqual(response.template.name, "Book template")
     87        self.assertEqual(response.templates[0].name, "Book template")
    8888        self.assertEqual(response.content, "Blink - Malcolm Gladwell")
    8989
    9090    def test_redirect(self):
  • 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 22b59e5..20ed3af 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
    1313from django.template import loader
    1414from django.test.client import encode_file
    1515
    class RequestHeadersTest(TestCase):  
    861861        self.assertEquals(response.content, "HTTP_X_ARG_CHECK: Testing 123")
    862862        self.assertRedirects(response, '/test_client_regress/check_headers/',
    863863            status_code=301, target_status_code=200)
     864
     865class ResponseTemplateDeprecationTests(TestCase):
     866    """
     867    Response.template still works backwards-compatibly, but with pending deprecation warning. Refs #12226.
     868
     869    """
     870    def test_response_template_data(self):
     871        response = self.client.get("/test_client_regress/request_data/", data={'foo':'whiz'})
     872        self.assertEqual(response.template.__class__, Template)
     873        self.assertEqual(response.template.name, 'base.html')
     874
     875    def test_response_no_template(self):
     876        response = self.client.get("/test_client_regress/request_methods/")
     877        self.assertEqual(response.template, None)
     878
Back to Top