#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 , 8 years ago
Cc: | added |
---|---|
Has patch: | set |
Owner: | changed from | to
Status: | new → assigned |
comment:2 by , 8 years ago
Triage Stage: | Unreviewed → Ready for checkin |
---|
comment:4 by , 10 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.
https://github.com/django/django/pull/7584