Skip to content

Order topics deterministically in serialization queryset - #3765

Open
mbertrand wants to merge 3 commits into
mainfrom
fix-flaky-topic-ordering
Open

Order topics deterministically in serialization queryset#3765
mbertrand wants to merge 3 commits into
mainfrom
fix-flaky-topic-ordering

Conversation

@mbertrand

@mbertrand mbertrand commented Aug 12, 2026

Copy link
Copy Markdown
Member

What are the relevant tickets?

Closes https://github.com/mitodl/hq/issues/12833

Description (What does it do?)

LearningResourceTopic had no Meta.ordering, so topics came back in whatever order Postgres chose. test_serialize_bulk_learning_resources builds its expected values from one query and the serializer builds its results from another, so the two disagreed intermittently. Adding ordering = ["name"] to the model makes topic order deterministic in the test and in API responses — same thing LearningResourceDepartment already does.

How can this be tested?

docker compose run --rm web uv run pytest \
  learning_resources_search/serializers_test.py::test_serialize_bulk_learning_resources \
  --count=50 -q --no-cov

Should pass every time

LearningResourceTopic has no Meta.ordering, so the topics prefetch and the
test's own expected values came back in whatever order Postgres chose, making
test_serialize_bulk_learning_resources flake. Ordering by name also makes
topic order stable in API responses.

Fixes mitodl/hq#12833

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings August 12, 2026 19:12
@github-actions

github-actions Bot commented Aug 12, 2026

Copy link
Copy Markdown

OpenAPI Changes

No changes detected

View full changelog

Unexpected changes? Ensure your branch is up-to-date with main (consider rebasing).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Makes serialized topic ordering deterministic, preventing intermittent test and API ordering differences.

Changes:

  • Orders serialization topics alphabetically by name.

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

Comment thread learning_resources/models.py Outdated
mbertrand and others added 2 commits August 12, 2026 15:30
The bulk-serialization test builds both its expected and actual topic lists
through this queryset, so it can't independently pin the ordering.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…yset

Ordering only for_serialization() left the plain related manager unordered,
and serializers use both: ContentFileSerializer.get_topics() reads
learning_resource.topics directly while the API queryset prefetches through
for_serialization(). The two disagreed, breaking test_learning_resource_serializer
and test_get_contentfiles_detail_endpoint. Meta.ordering covers every path,
matching what LearningResourceDepartment already does.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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