1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #27698 -- Added django.test.utils.ContextList.get()

This commit is contained in:
Tim Graham 2017-01-06 19:23:42 -05:00 committed by GitHub
parent 12cefee5d8
commit 8516f7c49b
2 changed files with 15 additions and 0 deletions

View File

@ -74,6 +74,12 @@ class ContextList(list):
else:
return super(ContextList, self).__getitem__(key)
def get(self, key, default=None):
try:
return self.__getitem__(key)
except KeyError:
return default
def __contains__(self, key):
try:
self[key]

View File

@ -961,6 +961,15 @@ class ContextTests(TestDataMixin, TestCase):
# in every Context without needing to be added.
self.assertEqual({'None', 'True', 'False', 'hello', 'goodbye', 'python', 'dolly'}, k.keys())
def test_contextlist_get(self):
c1 = Context({'hello': 'world', 'goodbye': 'john'})
c2 = Context({'goodbye': 'world', 'python': 'rocks'})
k = ContextList([c1, c2])
self.assertEqual(k.get('hello'), 'world')
self.assertEqual(k.get('goodbye'), 'john')
self.assertEqual(k.get('python'), 'rocks')
self.assertEqual(k.get('nonexistent', 'default'), 'default')
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