[Django] #21357: Test client session does not behave as stated in the django documentation.

21 views
Skip to first unread message

Django

unread,
Oct 30, 2013, 6:51:55 PM10/30/13
to django-...@googlegroups.com
#21357: Test client session does not behave as stated in the django documentation.
-----------------------------------+--------------------
Reporter: aaronmerriam@… | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 1.5
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------
In the documentation for testing, it states that you can run the following
code to manually insert values into the test client session.


{{{
def test_something(self):
session = self.client.session
session['somekey'] = 'test'
session.save()
}}}

https://docs.djangoproject.com/en/1.5/topics/testing/overview/#django.test.client.Client.session

In actuality, `self.client.session` is of type `dict` and a calling
`save()` raises an `AttributeError`.

--
Ticket URL: <https://code.djangoproject.com/ticket/21357>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 30, 2013, 6:59:43 PM10/30/13
to django-...@googlegroups.com
#21357: Test client session does not behave as stated in the django documentation.
-----------------------------------+------------------------------------

Reporter: aaronmerriam@… | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Changes (by bmispelon):

* cc: bmispelon@… (added)
* needs_better_patch: => 0
* needs_tests: => 0
* version: 1.5 => master
* needs_docs: => 0
* stage: Unreviewed => Accepted


Comment:

I can reproduce this issue.

It looks like the nature of `client.session` can depend on the settings
[1] which might be what's causing the discrepancy here.

I'm not sure if that's indicative of a bug in the test client (maybe it
should return something other than a plain `dict` if `contrib.sessions` is
not installed) or if it's a documentation issue (in which case it should
be mentionned that the given code only works with `contrib.sessions`
installed).

In any case, I'm marking this as `accepted`.

Thanks for the report.

[1]
https://github.com/django/django/blob/ed516a5fc8061615ec94bee37425d4fee8ca872b/django/test/client.py#L373-L382

--
Ticket URL: <https://code.djangoproject.com/ticket/21357#comment:1>

Django

unread,
Oct 30, 2013, 7:24:44 PM10/30/13
to django-...@googlegroups.com
#21357: Test client session does not behave as stated in the django documentation.
-----------------------------------+------------------------------------

Reporter: aaronmerriam@… | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------

Comment (by Harm Geerts <hgeerts@…>):

There also seems to be a regression in that code from the commit
https://github.com/django/django/commit/36b164d838c3de168defe9f1ebc02ea1abc790be

{{{
>>> from django.test.client import Client
>>> client = Client()
>>> client.session
{}
>>> client.session['var'] = 'val'
>>> client.session
{}
>>>
}}}
A test case which tested the above used to exist but was removed in
https://github.com/django/django/commit/a935e834433cfc691882c2bbf1249a39abecd35b

--
Ticket URL: <https://code.djangoproject.com/ticket/21357#comment:2>

Django

unread,
Oct 30, 2013, 9:17:10 PM10/30/13
to django-...@googlegroups.com
#21357: Test client session does not behave as stated in the django documentation.
-----------------------------------+------------------------------------

Reporter: aaronmerriam@… | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------

Comment (by prestontimmons):

I added a pull request with a fix for this:

https://github.com/django/django/pull/1832

For reference, this issue was reported in #15740. That was marked as a
duplicate of #10899, but I no longer think that is correct. While the
feature in #10899 would fix this case, the behavior it tries to add is
more clever than it needs to be.

--
Ticket URL: <https://code.djangoproject.com/ticket/21357#comment:3>

Django

unread,
May 22, 2014, 6:42:53 PM5/22/14
to django-...@googlegroups.com
#21357: Test client session does not behave as stated in the django documentation.
-----------------------------------+------------------------------------

Reporter: aaronmerriam@… | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Changes (by timo):

* needs_better_patch: 0 => 1
* has_patch: 0 => 1


Comment:

Test on PR does not currently pass (possibly due to upstream changes since
the PR was submitted).

--
Ticket URL: <https://code.djangoproject.com/ticket/21357#comment:4>

Django

unread,
May 22, 2014, 8:30:43 PM5/22/14
to django-...@googlegroups.com
#21357: Test client session does not behave as stated in the django documentation.
-----------------------------------+------------------------------------
Reporter: aaronmerriam@… | Owner: nobody
Type: Bug | Status: closed

Component: Testing framework | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"be88b062afaa58559bb12623e8ed8843f07b97a1"]:
{{{
#!CommitTicketReference repository=""
revision="be88b062afaa58559bb12623e8ed8843f07b97a1"
Fixed #21357 -- Fixed test client session initialization.

The test client will now create a session when it is first accessed
if no session already exists.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/21357#comment:5>

Django

unread,
May 5, 2015, 4:31:00 AM5/5/15
to django-...@googlegroups.com
#21357: Test client session does not behave as stated in the django documentation.
-----------------------------------+------------------------------------
Reporter: aaronmerriam@… | Owner: nobody

Type: Bug | Status: closed
Component: Testing framework | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------

Comment (by sbaechler):

The fix has only been added to the 1.8 branch. In Django 1.7 the behavior
is still not the same as stated in the documentation.
https://docs.djangoproject.com/en/1.7/topics/testing/tools/#persistent-
state

--
Ticket URL: <https://code.djangoproject.com/ticket/21357#comment:6>

Django

unread,
May 5, 2015, 12:36:47 PM5/5/15
to django-...@googlegroups.com
#21357: Test client session does not behave as stated in the django documentation.
-----------------------------------+------------------------------------
Reporter: aaronmerriam@… | Owner: nobody

Type: Bug | Status: closed
Component: Testing framework | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"d42cbde0c602dad97bb452280baa4a4c7a056bf4" d42cbde0]:
{{{
#!CommitTicketReference repository=""
revision="d42cbde0c602dad97bb452280baa4a4c7a056bf4"
[1.7.x] Refs #21357 -- Added a working session example to the docs.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/21357#comment:7>

Reply all
Reply to author
Forward
0 new messages