assertNumQueries is buggy with views and the test client if used more than once in a test
|Reported by:||lukeplant||Owned by:||ojii|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
With code like this:
with self.assertNumQueries(5): self.client.get("some/view/") with self.assertNumQueries(5): self.client.get("some/view/")
The second one always fails, with '0' being the number of queries it calculates. (I do not have any caching enabled).
If the second client.get goes to a view that actually uses less queries than the first, then assertNumQueries calculates a negative number of queries, which seems to be the true number minus the number of queries from the last assertNumQueries. I can't quite pin down the behaviour.
I'm guessing this has to do with the way test client/request life-cycle/connections interact. I do not see this with normal queries done directly in the function.
If someone else could confirm that would be helpful.
- Using assertNumQueries either with the 'with' statement or passing a function in.
- with SQLite (haven't tested with others)
- with or without the TransactionMiddleware
Change History (11)
comment:1 Changed 6 years ago by Alex
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:2 Changed 6 years ago by Alex
- Component changed from Uncategorized to Testing framework
- milestone set to 1.3
- Triage Stage changed from Unreviewed to Accepted