Opened 5 years ago

Closed 5 years ago

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

Download all attachments as: .zip

Change History (8)

Changed 5 years ago by clelland

Patch to allow tests like 'key in contextlist'

comment:1 Changed 5 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 5 years ago by oinopion

I second on this. Quite annoying.

comment:3 Changed 5 years ago by bryanchow

Thanks, clelland for the patch. Works great.

comment:4 Changed 5 years ago by gabrielhurley

Agreed. Good patch with tests.

comment:5 Changed 5 years ago by SmileyChris

  • Triage Stage changed from Accepted to Ready for checkin

comment:6 Changed 5 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

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

comment:7 Changed 5 years ago by lukeplant

(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