test: Fix test_unit_routers.py#7530
Conversation
|
@SahilJat is attempting to deploy a commit to the Flagsmith Team on Vercel. A member of the Team first needs to authorize it. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7530 +/- ##
==========================================
- Coverage 98.47% 98.31% -0.16%
==========================================
Files 1400 1400
Lines 53036 52915 -121
==========================================
- Hits 52226 52023 -203
- Misses 810 892 +82 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
khvn26
left a comment
There was a problem hiding this comment.
The approach looks sensible to me — can you add spec=Model to the mocks you've added?
0b3b81f to
53a0d7b
Compare
|
Hii @khvn26 i made the changes and updated the test. Thanks |
5e39f1c to
abf4be6
Compare
|
Thanks for the hint @khvn26 , did mock the _meta explicitly in this change. Please have a look and let me know if its gtg |
|
@SahilJat sorry, tiny ask — could you add |
282f5d3 to
b90a1fd
Compare
for more information, see https://pre-commit.ci
|
@khvn26 no problem, added the spec=Options this time. |
test_unit_routers.py
test_unit_routers.pytest_unit_routers.py
|
Thanks for the contribution! |
Thanks for submitting a PR! Please check the boxes below:
docs/if required so people know about the feature.Changes
Closes #7368
Fixes flaky CI failures caused by inline
models.Modelsubclasses intest_unit_app_routers.pypolluting Django's apps registry.models.Modelsubclasses (AnalyticsModel,MyModel,MyModel1,MyModel2) from the parametrised test functions.mock.MagicMock()objects that expose_meta.app_label— the only attribute the router inspects.from django.db import modelsimport, replaced withfrom unittest import mock.allow_migratetest 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 realapp_analyticsapp, causingmakemigrations --checkto find unmigrated models and Migrator teardown to fail withrelation "app_analytics_analyticsmodel" already existsacross multiple xdist workers.How did you test this code?
makemigrationstest no longer detects phantom models.DuplicateTableerrors.