[Django] #27516: Optimize test client response.json()

18 views
Skip to first unread message

Django

unread,
Nov 20, 2016, 1:14:04 PM11/20/16
to django-...@googlegroups.com
#27516: Optimize test client response.json()
------------------------------------------------+------------------------
Reporter: Adam Chainz | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Testing framework | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
It's quite common for a test to look like:

{{{
resp = self.client.get('/api/things/')
assert 'users' in resp.json()
assert 'groups' in resp.json()
}}}

Each call to `json()` currently re-parses the unchanged body. It could
cache this :)

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

Django

unread,
Nov 20, 2016, 1:15:46 PM11/20/16
to django-...@googlegroups.com
#27516: Optimize test client response.json()
-------------------------------------+-------------------------------------
Reporter: Adam Chainz | Owner: Adam
Type: | Chainz
Cleanup/optimization | Status: assigned

Component: Testing framework | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Adam Chainz):

* cc: me@… (added)
* owner: nobody => Adam Chainz
* has_patch: 0 => 1
* status: new => assigned


Comment:

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

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

Django

unread,
Nov 21, 2016, 7:06:26 AM11/21/16
to django-...@googlegroups.com
#27516: Optimize test client response.json()
-------------------------------------+-------------------------------------
Reporter: Adam Chainz | Owner: Adam
Type: | Chainz
Cleanup/optimization | Status: assigned
Component: Testing framework | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Ready for checkin


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

Django

unread,
Nov 21, 2016, 8:14:24 AM11/21/16
to django-...@googlegroups.com
#27516: Optimize test client response.json()
-------------------------------------+-------------------------------------
Reporter: Adam Chainz | Owner: Adam
Type: | Chainz
Cleanup/optimization | Status: closed

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

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

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


Comment:

In [changeset:"19e2114634c322d5a58f3dd282a3a3cfd0f5aa24" 19e21146]:
{{{
#!CommitTicketReference repository=""
revision="19e2114634c322d5a58f3dd282a3a3cfd0f5aa24"
Fixed #27516 -- Made test client's response.json() cache the parsed JSON.
}}}

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

Django

unread,
Feb 16, 2024, 6:59:04 AM2/16/24
to django-...@googlegroups.com
#27516: Optimize test client response.json()
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
Type: | Johnson
Cleanup/optimization | Status: closed
Component: Testing framework | Version: 1.10
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Mounir):

Sorry for revisiting this old issue.

I recently ran into trouble debugging a test. The test involved retrieving
JSON results and popping keys from them via a variable:


{{{
response_data = response.json()
item1 = response_data.pop('item1')
}}}


While debugging, I was puzzled as to why the URL returned incomplete data.
I had assumed that calling response.json() would return the full JSON
data, but due to the caching mechanism in place, this wasn't the case.

Comparing this behaviour to the requests library, which has no caching,
leads to different user expectations. Some users may find this distinction
difficult to grasp.
--
Ticket URL: <https://code.djangoproject.com/ticket/27516#comment:4>
Reply all
Reply to author
Forward
0 new messages