Opened 7 years ago

Closed 6 years ago

Last modified 6 years ago

#13092 closed (fixed)

ContextList objects do not support "in" operator

Reported by: anonymous Owned by: nobody
Component: Testing framework Version: master
Severity: Keywords: ContextList
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

When running tests, if a single template is used to product a response to an HTTP request, then the test client's response context object is a regular Context (django.template.Context) object. When multiple templates are used, however, then the test client's response context object will be a django.test.utils.ContextList object instead.

The ContextList class tries to emulate a Context object for dictionary-style lookups, so that tests which reference response.contextkey? will still work. However, ContextList does not implement _contains_, so expressions of the form "'key' in request.context", which would be true when only a single template is used, become false as soon as a second template is introduced.

For example, with this template:

<h1>No Content here</h1>

And this view:

from django.shortcuts import render_to_response
def view(request):
    return render_to_response("test_template.html", { "foo": "bar" })

Then a test which says:

class SimpleTest(TestCase):
    def test_context(self):
        response = self.client.get("/test_url/")
        self.assertTrue("foo" in response.context)

will succeed, but if the template is changed to this:

{% extends "base.html" %}
<h1>No content here</h1>

then the same test will fail. I'm attaching a patch which fixes this, as well as a two line patch for the Context/ContextList tests.

Attachments (1)

test_key_in_context_1.diff (1.7 KB) - added by Ian Clelland 7 years ago.
Patch to allow tests like 'key in contextlist'

Download all attachments as: .zip

Change History (8)

Changed 7 years ago by Ian Clelland

Attachment: test_key_in_context_1.diff added

Patch to allow tests like 'key in contextlist'

comment:1 Changed 7 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

comment:2 Changed 7 years ago by Tomek Paczkowski

I second on this. Quite annoying.

comment:3 Changed 7 years ago by bryanchow

Thanks, clelland for the patch. Works great.

comment:4 Changed 7 years ago by Gabriel Hurley

Agreed. Good patch with tests.

comment:5 Changed 7 years ago by Chris Beaven

Triage Stage: AcceptedReady for checkin

comment:6 Changed 6 years ago by Russell Keith-Magee

Resolution: fixed
Status: newclosed

(In [13510]) Fixed #13092 -- Added support for the "in" operator when dealing with context lists. Thanks to clelland for the patch.

comment:7 Changed 6 years ago by Luke Plant

(In [13986]) [1.2.X] Fixed #13092 -- Added support for the "in" operator when dealing with context lists. Thanks to clelland for the patch.

Backport of [13510] from trunk. Backported in order to support some other
tests which need to be backported.

Note: See TracTickets for help on using tickets.
Back to Top