Skip to content

fix: test_unit_routers.py for ci/cd#7530

Open
SahilJat wants to merge 2 commits into
Flagsmith:mainfrom
SahilJat:fix/app-tests-pollution-failing-tests
Open

fix: test_unit_routers.py for ci/cd#7530
SahilJat wants to merge 2 commits into
Flagsmith:mainfrom
SahilJat:fix/app-tests-pollution-failing-tests

Conversation

@SahilJat
Copy link
Copy Markdown
Contributor

Thanks for submitting a PR! Please check the boxes below:

  • [ x ] I have read the Contributing Guide.
  • [ x ] I have added information to docs/ if required so people know about the feature.
  • [ x ] I have filled in the "Changes" section below.
  • [ x ] I have filled in the "How did you test this code" section below.

Changes

Closes #7368

Fixes flaky CI failures caused by inline models.Model subclasses in test_unit_app_routers.py polluting Django's apps registry.

  • Removed all inline models.Model subclasses (AnalyticsModel, MyModel, MyModel1, MyModel2) from the parametrised test functions.
  • Replaced them with mock.MagicMock() objects that expose _meta.app_label — the only attribute the router inspects.
  • Removed the from django.db import models import, replaced with from unittest import mock.
  • The allow_migrate test was already clean and required no changes.

Root Cause:
The root cause was that defining a Django model class registers it permanently in the apps registry for the lifetime of the worker process. When tests ran with app_label="app_analytics", the four model classes were permanently registered against the real app_analytics app, causing makemigrations --check to find unmigrated models and Migrator teardown to fail with relation "app_analytics_analyticsmodel" already exists across multiple xdist workers.

How did you test this code?

  • Ran the updated test file locally and confirmed all 4 tests pass.
  • Confirmed the makemigrations test no longer detects phantom models.
  • Confirmed migration tests no longer fail with DuplicateTable errors.

@SahilJat SahilJat requested a review from a team as a code owner May 18, 2026 05:14
@SahilJat SahilJat requested review from khvn26 and removed request for a team May 18, 2026 05:14
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 18, 2026

@SahilJat is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the api Issue related to the REST API label May 18, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.31%. Comparing base (82c5dfb) to head (0b3b81f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7530      +/-   ##
==========================================
- Coverage   98.47%   98.31%   -0.16%     
==========================================
  Files        1400     1400              
  Lines       53036    52909     -127     
==========================================
- Hits        52226    52017     -209     
- Misses        810      892      +82     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

api Issue related to the REST API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API Tests: Apps registry pollution from test_unit_app_routers.py breaks migration tests

1 participant