Skip to content

Commit 2e17679

Browse files
asmodehnAlexandre Vincent
andauthored
[chores:fix] Add TestMigrationGraphIntegrity to render all project_states
Co-authored-by: Alexandre Vincent <alexandre.vincent@stellar.tc>
1 parent 445b432 commit 2e17679

4 files changed

Lines changed: 41 additions & 2 deletions

File tree

openwisp_controller/connection/migrations/0001_initial.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ class Migration(migrations.Migration):
2323
swapper.dependency(
2424
*swapper.split(settings.AUTH_USER_MODEL), version="0004_default_groups"
2525
),
26-
swapper.dependency("config", "Device"),
26+
swapper.dependency(
27+
*swapper.split(settings.CONFIG_DEVICE_MODEL),
28+
version="0004_add_device_model"
29+
),
2730
]
2831

2932
operations = [

openwisp_controller/geo/migrations/0001_initial.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ class Migration(migrations.Migration):
2121
swapper.dependency(
2222
*swapper.split(settings.AUTH_USER_MODEL), version="0004_default_groups"
2323
),
24-
swapper.dependency("config", "Device"),
24+
swapper.dependency(
25+
*swapper.split(settings.CONFIG_DEVICE_MODEL),
26+
version="0004_add_device_model"
27+
),
2528
]
2629

2730
operations = [
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from __future__ import annotations
2+
3+
from django.db.migrations.loader import MigrationLoader
4+
from django.test import SimpleTestCase, tag
5+
6+
7+
@tag("slow")
8+
class TestMigrationGraphIntegrity(SimpleTestCase):
9+
def test_all_migration_states_render_apps(self) -> None:
10+
"""
11+
Fail fast if any migration target produces an invalid historical StateApps.
12+
13+
This catches missing dependencies / broken swappable references that only
14+
show up when Django renders historical models.
15+
16+
Note : issues in third-party dependency migrations
17+
will also be detected.
18+
"""
19+
loader = MigrationLoader(None, ignore_no_migrations=True)
20+
21+
failures: list[str] = []
22+
for key in sorted(loader.graph.nodes.keys()):
23+
try:
24+
state = loader.project_state([key])
25+
_ = state.apps # triggers StateApps rendering
26+
except (LookupError, ValueError, AttributeError, TypeError) as e:
27+
failures.append(f"{key[0]}.{key[1]} -> {type(e).__name__}: {e}")
28+
29+
if failures:
30+
self.fail(
31+
"Some migration states cannot be rendered:\n" + "\n".join(failures)
32+
)

runtests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def run_tests(extra_args, settings_module, test_app):
4646
else:
4747
test_app = "openwisp2"
4848
app_dir = "tests/openwisp2/"
49+
args += ["--exclude-tag", "slow"]
4950
# Run Django tests
5051
django_tests = run_tests(args, "openwisp2.settings", test_app)
5152
# Run pytest tests

0 commit comments

Comments
 (0)