Skip to content

Add query count tests and remove N+1 queries in page rendering and search - #501

Open
ShubhJain09 wants to merge 3 commits into
wagtail:mainfrom
ShubhJain09:test-481-query-counts
Open

ShubhJain09 wants to merge 3 commits into
wagtail:mainfrom
ShubhJain09:test-481-query-counts

Conversation

@ShubhJain09

@ShubhJain09 ShubhJain09 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Refs #481

Description

This covers the first item on #481 (query count assertions in tests), plus two fixes for places that were making a query per item.

Tests (apps/core/tests/test_query_counts.py)

  • Exact query counts for a content page (16) and the homepage (12).
  • Checks that child pages, menu pages and homepage sections don't add a query each. I made sure each one fails if a per-item query is added to its template.
  • They use an in-memory cache, since production uses Redis and cache reads there aren't database queries.

Table of contents
create_table_of_contents rendered the whole page body a second time just to find the headings, so every link in the body was looked up twice. It now reads the headings and their ids from the stored rich text, and makes no queries. A content page with 6 linked text blocks goes from 52 queries to 34.

Two existing tests expected each block template to render twice (test_page_response and test_text_block_annotated), which was this double render, so I updated their counts.

Search suggestions
search_json looked up each result's parent section with a separate query, and built each URL without the request. It now gets all the sections in one query and passes the request through. With 20 results it goes from 26 queries to 7 (or 46 to 8 counting cache reads). There were no tests for the search views, so I added some for the sections, URLs and empty queries.

Left for later
Some per-link queries come from Wagtail itself: the Markdown converter looks up each linked page and its parent separately, and Locale.get_active() isn't cached. Happy to raise those on the Wagtail repo.

Testing
All 151 tests pass locally on PostgreSQL 17 and SQLite, along with the backend lint and Django checks. CI is currently failing on main at npm run lint because of 3 YAML files in content/evals, so that step will fail here too until main is fixed.

AI usage

None

Copilot AI balanced review requested due to automatic review settings October 6, 2026 06:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The optimizations preserve existing behavior and include focused regression coverage for correctness and query scaling.

Review effort: Balanced
Findings: None

What changed in this PR

Adds query-count regression coverage and removes N+1 queries from page rendering and search suggestions.

Changes:

  • Builds table-of-contents data directly from stored rich text.
  • Batch-loads search-result sections and generates URLs with request context.
  • Adds query-count and search endpoint tests.
File Description
apps/​search/​views.py Optimizes search serialization queries.
apps/​search/​tests/​test_views.py Tests search output and query scaling.
apps/​search/​tests/​__init__.py Initializes the search test package.
apps/​core/​models/​content.py Avoids rendering page bodies twice.
apps/​core/​tests/​test_content_page.py Tests TOC extraction and query behavior.
apps/​core/​tests/​test_query_counts.py Adds page-rendering query regressions.
apps/​core/​tests/​test_page_response.py Updates expected template render counts.
apps/​core/​tests/​test_text_block_annotated.py Updates annotated-block render expectations.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@ShubhJain09 ShubhJain09 changed the title Test 481 query counts Add query count tests and remove N+1 queries in page rendering and search Oct 6, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants