Opened 15 years ago

Closed 14 years ago

Last modified 14 years ago

#13092 closed (fixed)

ContextList objects do not support "in" operator

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

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 15 years ago.
Patch to allow tests like 'key in contextlist'

Download all attachments as: .zip

Change History (8)

by Ian Clelland, 15 years ago

Attachment: test_key_in_context_1.diff added

Patch to allow tests like 'key in contextlist'

comment:1 by Russell Keith-Magee, 15 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Tomek Paczkowski, 15 years ago

I second on this. Quite annoying.

comment:3 by bryanchow, 15 years ago

Thanks, clelland for the patch. Works great.

comment:4 by Gabriel Hurley, 15 years ago

Agreed. Good patch with tests.

comment:5 by Chris Beaven, 15 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Russell Keith-Magee, 14 years ago

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 by Luke Plant, 14 years ago

(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