diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2009-03-18 10:46:55 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2009-03-18 10:46:55 +0000 |
| commit | ee2f04d79e5bca55637b9bb3301618738a4e342a (patch) | |
| tree | 91e94bc284c1cb44ce131b8acc972e3585b2b110 /django/test | |
| parent | 61a2708c4108939795c70cf124d5696275d6c255 (diff) | |
Fixed #10482 -- Unified access to response.context when inspecting responses from the test client. Thanks to James Bennett for the design, and Julien Phalip for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10084 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/test')
| -rw-r--r-- | django/test/client.py | 5 | ||||
| -rw-r--r-- | django/test/utils.py | 14 |
2 files changed, 17 insertions, 2 deletions
diff --git a/django/test/client.py b/django/test/client.py index 20283b7b3b..6c5f4ff4a9 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -20,6 +20,7 @@ from django.utils.encoding import smart_str from django.utils.http import urlencode from django.utils.itercompat import is_iterable from django.db import transaction, close_connection +from django.test.utils import ContextList BOUNDARY = 'BoUnDaRyStRiNg' MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY @@ -80,8 +81,8 @@ def store_rendered_templates(store, signal, sender, template, context, **kwargs) """ Stores templates and contexts that are rendered. """ - store.setdefault('template',[]).append(template) - store.setdefault('context',[]).append(context) + store.setdefault('template', []).append(template) + store.setdefault('context', ContextList()).append(context) def encode_multipart(boundary, data): """ diff --git a/django/test/utils.py b/django/test/utils.py index 29babec3ff..d34dd33d15 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -6,6 +6,20 @@ from django.test import signals from django.template import Template from django.utils.translation import deactivate +class ContextList(list): + """A wrapper that provides direct key access to context items contained + in a list of context objects. + """ + def __getitem__(self, key): + if isinstance(key, basestring): + for subcontext in self: + if key in subcontext: + return subcontext[key] + raise KeyError + else: + return super(ContextList, self).__getitem__(key) + + def instrumented_test_render(self, context): """ An instrumented Template render method, providing a signal |
