Add TestCase.assertQueries(), for advanced query checks.
Review Request #12345 — Created June 8, 2022 and submitted
This introduces
TestCase.assertQueries()
, a new unit test function
that can assert not just the number of queries performed by also the
complexity of those queries.This takes in a list of queries built by Django and executed, checking
them for the lists of fields selected, tables joined, filter
conditions, annotations, ordering, and more.This can offer a lot of advantages when writing tests, helping ensure
that queries are as performant as possible, or at least don't contain
unexpected complexity.
Made use of this in a bunch of unit tests in Review Board. Tested all
the major types of operations that can be performed when querying.
Summary | ID |
---|---|
5d12cc2d9e4d1b9fc1d61c79b4e494d4756dea7e |
Description | From | Last Updated |
---|---|---|
too many blank lines (2) Column: 9 Error code: E303 |
reviewbot | |
expected 1 blank line before a nested definition, found 0 Column: 9 Error code: E306 |
reviewbot | |
redefinition of unused '_execute_sql' from line 429 Column: 9 Error code: F811 |
reviewbot | |
redefinition of unused '_execute_sql' from line 441 Column: 9 Error code: F811 |
reviewbot |
- Change Summary:
-
- Removed an unwanted blank line.
- Changed the function names for the spies to be unique.
- Commits:
-
Summary ID 5680f25b55c0668827cb1a47e8ffce236028ff24 a36eb981eb45b503461b1eeffd823852692b41ba - Diff:
-
Revision 2 (+670 -2)
Checks run (2 succeeded)
- Change Summary:
-
Fixed a typo in a comment.
- Commits:
-
Summary ID a36eb981eb45b503461b1eeffd823852692b41ba 05f77e0d4b91f5f5dd6fd74427f1b7f4a26263a6 - Diff:
-
Revision 3 (+668 -2)
Checks run (2 succeeded)
- Change Summary:
-
Changed the
Query.chain
spy toQuery.clone
, which is more comprehensive. - Commits:
-
Summary ID 05f77e0d4b91f5f5dd6fd74427f1b7f4a26263a6 93f4695119235b393c85f71ab60bfd076958f9b0 - Diff:
-
Revision 4 (+668 -2)
Checks run (2 succeeded)
- Change Summary:
-
- Forced Django's fast-deletion to be disabled during query collection, ensuring stable query counts/information during deletes.
- Added generated SQL to any failed query information, to aid with debugging of failed tests.
- Improved error reporting when the number of expected and executed queries don't match. Now we still compare all executed queries.
- Fixed a default value when comparing tables when there's a non-JOINed query.
- Commits:
-
Summary ID 93f4695119235b393c85f71ab60bfd076958f9b0 30dba59525421a643aaee15af796d6476e4462b5 - Diff:
-
Revision 5 (+736 -2)
Checks run (2 succeeded)
- Change Summary:
-
- Fixed JOIN count and table tracking to only consider tables with a JOIN refcount > 0.
- Fixed a double-recording for
UPDATE
operations. - Worked around a bug in Django with SQL generation for SQLite
INSERT OR IGNORE
calls.
- Commits:
-
Summary ID 30dba59525421a643aaee15af796d6476e4462b5 5d12cc2d9e4d1b9fc1d61c79b4e494d4756dea7e - Diff:
-
Revision 6 (+770 -2)