Opened 7 years ago

Closed 7 years ago

Last modified 2 months ago

#27516 closed Cleanup/optimization (fixed)

Optimize test client response.json()

Reported by: Adam Johnson Owned by: Adam Johnson
Component: Testing framework Version: 1.10
Severity: Normal Keywords:
Cc: me@… 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

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 :)

Change History (4)

comment:1 by Adam Johnson, 7 years ago

Cc: me@… added
Has patch: set
Owner: changed from nobody to Adam Johnson
Status: newassigned

comment:2 by Tim Graham, 7 years ago

Triage Stage: UnreviewedReady for checkin

comment:3 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 19e21146:

Fixed #27516 -- Made test client's response.json() cache the parsed JSON.

comment:4 by Mounir, 2 months ago

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.

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