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: else:
return super(ContextList, self).__getitem__(key) 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): def __contains__(self, key):
try: try:
self[key] self[key]

View File

@ -961,6 +961,15 @@ class ContextTests(TestDataMixin, TestCase):
# in every Context without needing to be added. # in every Context without needing to be added.
self.assertEqual({'None', 'True', 'False', 'hello', 'goodbye', 'python', 'dolly'}, k.keys()) 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): def test_15368(self):
# Need to insert a context processor that assumes certain things about # Need to insert a context processor that assumes certain things about
# the request instance. This triggers a bug caused by some ways of # the request instance. This triggers a bug caused by some ways of