diff options
| author | Carl Meyer <carl@oddbird.net> | 2010-10-10 02:16:33 +0000 |
|---|---|---|
| committer | Carl Meyer <carl@oddbird.net> | 2010-10-10 02:16:33 +0000 |
| commit | 501546df6f4cbb56ea94da073e8206804fef3040 (patch) | |
| tree | 767b30579c796b9fd9c94d1f8d30a4efb15d6d65 /django/test | |
| parent | d084439c415e2037a1d13b217b044fd1bf3b39cb (diff) | |
Fixed #12226 -- Deprecated test client Response.template attribute in favor of templates attribute, which is always a list. Thanks Russell for patch review.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14106 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/test')
| -rw-r--r-- | django/test/client.py | 32 | ||||
| -rw-r--r-- | django/test/testcases.py | 4 |
2 files changed, 23 insertions, 13 deletions
diff --git a/django/test/client.py b/django/test/client.py index 08e3ff6b71..05880884f0 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -4,6 +4,7 @@ import sys import os import re import mimetypes +import warnings try: from cStringIO import StringIO except ImportError: @@ -93,7 +94,7 @@ def store_rendered_templates(store, signal, sender, template, context, **kwargs) """ Stores templates and contexts that are rendered. """ - store.setdefault('template', []).append(template) + store.setdefault('templates', []).append(template) store.setdefault('context', ContextList()).append(context) def encode_multipart(boundary, data): @@ -260,16 +261,25 @@ class Client(object): response.request = request # Add any rendered template detail to the response. - # If there was only one template rendered (the most likely case), - # flatten the list to a single element. - for detail in ('template', 'context'): - if data.get(detail): - if len(data[detail]) == 1: - setattr(response, detail, data[detail][0]); - else: - setattr(response, detail, data[detail]) - else: - setattr(response, detail, None) + response.templates = data.get("templates", []) + response.context = data.get("context") + + # Flatten a single context. Not really necessary anymore thanks to + # the __getattr__ flattening in ContextList, but has some edge-case + # backwards-compatibility implications. + if response.context and len(response.context) == 1: + response.context = response.context[0] + + # Provide a backwards-compatible (but pending deprecation) response.template + def _get_template(self): + warnings.warn("response.template is deprecated; use response.templates instead (which is always a list)", + PendingDeprecationWarning) + if not self.templates: + return None + elif len(self.templates) == 1: + return self.templates[0] + return self.templates + response.__class__.template = property(_get_template) # Update persistent cookie data. if response.cookies: diff --git a/django/test/testcases.py b/django/test/testcases.py index 90319a72e9..40b3c82500 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -443,7 +443,7 @@ class TransactionTestCase(unittest.TestCase): if msg_prefix: msg_prefix += ": " - template_names = [t.name for t in to_list(response.template)] + template_names = [t.name for t in response.templates] if not template_names: self.fail(msg_prefix + "No templates used to render the response") self.failUnless(template_name in template_names, @@ -459,7 +459,7 @@ class TransactionTestCase(unittest.TestCase): if msg_prefix: msg_prefix += ": " - template_names = [t.name for t in to_list(response.template)] + template_names = [t.name for t in response.templates] self.failIf(template_name in template_names, msg_prefix + "Template '%s' was used unexpectedly in rendering" " the response" % template_name) |
