diff --git a/django/test/utils.py b/django/test/utils.py index d178c9b29c6..be586c75a6d 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -60,6 +60,16 @@ class ContextList(list): return False return True + def keys(self): + """ + Flattened keys of subcontexts. + """ + keys = set() + for subcontext in self: + for dict in subcontext: + keys |= set(dict.keys()) + return keys + def instrumented_test_render(self, context): """ diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py index 654f65235a9..dc656c127dd 100644 --- a/tests/test_client_regress/tests.py +++ b/tests/test_client_regress/tests.py @@ -6,9 +6,8 @@ from __future__ import unicode_literals import os -from django.conf import settings from django.core.urlresolvers import reverse -from django.template import (TemplateDoesNotExist, TemplateSyntaxError, +from django.template import (TemplateSyntaxError, Context, Template, loader) import django.template.context from django.test import Client, TestCase @@ -897,6 +896,21 @@ class ContextTests(TestCase): except KeyError as e: self.assertEqual(e.args[0], 'does-not-exist') + def test_contextlist_keys(self): + c1 = Context() + c1.update({'hello': 'world', 'goodbye': 'john'}) + c1.update({'hello': 'dolly', 'dolly': 'parton'}) + c2 = Context() + c2.update({'goodbye': 'world', 'python': 'rocks'}) + c2.update({'goodbye': 'dolly'}) + + l = ContextList([c1, c2]) + # None, True and False are builtins of BaseContext, and present + # in every Context without needing to be added. + self.assertEqual(set(['None', 'True', 'False', 'hello', 'goodbye', + 'python', 'dolly']), + l.keys()) + def test_15368(self): # Need to insert a context processor that assumes certain things about # the request instance. This triggers a bug caused by some ways of