Skip to content

Commit 1e06212

Browse files
test: add MigrationsTestCase to verify forward and backward migrations (#477)
Agent-Logs-Url: https://github.com/fabiocaccamo/django-admin-interface/sessions/2fa3d623-bdd8-4737-93ba-94c0965cbc3b Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: fabiocaccamo <1035294+fabiocaccamo@users.noreply.github.com>
1 parent 45e53ef commit 1e06212

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

tests/test_migrations.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from django.core.management import call_command
2+
from django.db.migrations.exceptions import IrreversibleError
3+
from django.test import TransactionTestCase
4+
5+
6+
class MigrationsTestCase(TransactionTestCase):
7+
"""
8+
Generic test case that verifies all migrations for a given app can be
9+
applied forward and reversed (to zero) without errors.
10+
11+
Reusable in other projects: just subclass and set ``app_label``.
12+
"""
13+
14+
app_label = "admin_interface"
15+
16+
def tearDown(self):
17+
# Restore migrations to the latest state even if the test fails
18+
# so that subsequent tests find the DB in the expected state.
19+
call_command("migrate", self.app_label, verbosity=0)
20+
super().tearDown()
21+
22+
def test_migrate_forward_and_backward(self):
23+
try:
24+
call_command("migrate", self.app_label, "zero", verbosity=0)
25+
except IrreversibleError as e:
26+
self.fail(
27+
f"Migrations for '{self.app_label}' are not fully reversible: {e}"
28+
)
29+
call_command("migrate", self.app_label, verbosity=0)

0 commit comments

Comments
 (0)