Skip to content

Commit 6591ef3

Browse files
committed
added more tests for forwards and backwards commands
1 parent 4f86cfe commit 6591ef3

File tree

3 files changed

+142
-32
lines changed

3 files changed

+142
-32
lines changed

piccolo/apps/migrations/commands/backwards.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
from __future__ import annotations
2+
import sys
3+
14
from piccolo.apps.migrations.auto import MigrationManager
5+
from piccolo.apps.migrations.commands.base import BaseMigrationManager
26
from piccolo.apps.migrations.tables import Migration
37
from piccolo.utils.sync import run_sync
4-
from .base import BaseMigrationManager
58

69

710
class BackwardsMigrationManager(BaseMigrationManager):
@@ -14,6 +17,8 @@ def __init__(
1417
super().__init__()
1518

1619
def run(self):
20+
self.create_migration_table()
21+
1722
app_modules = self.get_app_modules()
1823

1924
migration_modules = []
@@ -30,8 +35,7 @@ def run(self):
3035
app_name=self.app_name
3136
)
3237
if len(ran_migration_ids) == 0:
33-
print("No migrations to reverse!")
34-
return
38+
sys.exit("No migrations to reverse!")
3539

3640
#######################################################################
3741

@@ -43,7 +47,7 @@ def run(self):
4347
earliest_migration_id = self.migration_id
4448

4549
if earliest_migration_id not in ran_migration_ids:
46-
print(
50+
sys.exit(
4751
"Unrecognized migration name - must be one of "
4852
f"{ran_migration_ids}"
4953
)
@@ -61,13 +65,13 @@ def run(self):
6165
#######################################################################
6266

6367
_continue = (
64-
input(
68+
"y"
69+
if self.auto_agree
70+
else input(
6571
"About to undo the following migrations:\n"
6672
f"{reversed_migration_ids}\n"
6773
"Enter y to continue.\n"
6874
)
69-
if not self.auto_agree
70-
else "y"
7175
)
7276
if _continue == "y":
7377
print("Undoing migrations")
@@ -83,8 +87,8 @@ def run(self):
8387
Migration.delete().where(
8488
Migration.name == migration_id
8589
).run_sync()
86-
else:
87-
print("Not proceeding.")
90+
else: # pragma: no cover
91+
sys.exit("Not proceeding.")
8892

8993

9094
def backwards(
@@ -109,20 +113,22 @@ def backwards(
109113
sorted_app_names.reverse()
110114

111115
_continue = (
112-
input(
116+
"y"
117+
if auto_agree
118+
else input(
113119
"You're about to undo the migrations for the following apps:\n"
114120
f"{sorted_app_names}\n"
115121
"Are you sure you want to continue?\n"
116122
"Enter y to continue.\n"
117123
)
118-
if not auto_agree
119-
else "y"
120124
)
121125
if _continue == "y":
122126
for _app_name in sorted_app_names:
123127
print(f"Undoing {_app_name}")
124128
manager = BackwardsMigrationManager(
125-
app_name=_app_name, migration_id="all"
129+
app_name=_app_name,
130+
migration_id="all",
131+
auto_agree=auto_agree,
126132
)
127133
manager.run()
128134
else:

piccolo/apps/migrations/commands/forwards.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
import sys
33
import typing as t
44

5-
from .base import BaseMigrationManager
6-
from piccolo.conf.apps import AppConfig
7-
from piccolo.apps.migrations.tables import Migration
85
from piccolo.apps.migrations.auto import MigrationManager
9-
from piccolo.conf.apps import MigrationModule
6+
from piccolo.apps.migrations.commands.base import BaseMigrationManager
7+
from piccolo.apps.migrations.tables import Migration
8+
from piccolo.conf.apps import AppConfig, MigrationModule
109
from piccolo.utils.sync import run_sync
1110

1211

@@ -40,8 +39,7 @@ def run_migrations(self, app_config: AppConfig) -> None:
4039
print(f"Haven't run = {havent_run}")
4140

4241
if len(havent_run) == 0:
43-
print("No migrations left to run!")
44-
return
42+
sys.exit("No migrations left to run!")
4543

4644
if self.migration_id == "all":
4745
subset = havent_run

tests/apps/migrations/commands/test_forwards_backwards.py

Lines changed: 119 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from __future__ import annotations
2-
from unittest import TestCase
2+
33
import typing as t
4+
from unittest import TestCase
45

6+
from piccolo.apps.migrations.tables import Migration
57
from piccolo.apps.migrations.commands.backwards import backwards
68
from piccolo.apps.migrations.commands.forwards import forwards
7-
89
from tests.example_app.tables import (
9-
Manager,
1010
Band,
11-
Venue,
1211
Concert,
13-
Ticket,
12+
Manager,
1413
Poster,
14+
Ticket,
15+
Venue,
1516
)
1617

1718
if t.TYPE_CHECKING:
@@ -35,23 +36,128 @@ class TestForwardsBackwards(TestCase):
3536
it.
3637
"""
3738

38-
# TODO - use pytest parameterise ...
3939
def test_forwards_backwards_all_migrations(self):
4040
"""
4141
Test running all of the migrations forwards, then backwards.
4242
"""
43+
for app_name in ("example_app", "all"):
44+
forwards(app_name=app_name, migration_id="all")
45+
46+
# Check the tables exist
47+
for table_class in TABLE_CLASSES:
48+
self.assertTrue(table_class.table_exists().run_sync())
49+
50+
backwards(app_name=app_name, migration_id="all", auto_agree=True)
51+
52+
# Check the tables don't exist
53+
for table_class in TABLE_CLASSES:
54+
self.assertTrue(not table_class.table_exists().run_sync())
55+
56+
def test_forwards_backwards_single_migration(self):
57+
"""
58+
Test running a single migrations forwards, then backwards.
59+
"""
60+
for migration_id in ["1", "2020-12-17T18:44:30"]:
61+
forwards(app_name="example_app", migration_id=migration_id)
62+
63+
table_classes = [Band, Manager]
64+
65+
# Check the tables exist
66+
for table_class in table_classes:
67+
self.assertTrue(table_class.table_exists().run_sync())
68+
69+
backwards(
70+
app_name="example_app",
71+
migration_id=migration_id,
72+
auto_agree=True,
73+
)
74+
75+
# Check the tables don't exist
76+
for table_class in table_classes:
77+
self.assertTrue(not table_class.table_exists().run_sync())
78+
79+
def test_forwards_unknown_migration(self):
80+
"""
81+
Test running an unknown migrations forwards.
82+
"""
83+
with self.assertRaises(SystemExit) as manager:
84+
forwards(app_name="example_app", migration_id="migration-12345")
85+
86+
self.assertEqual(
87+
manager.exception.__str__(), "migration-12345 is unrecognised"
88+
)
89+
90+
def test_backwards_unknown_migration(self):
91+
"""
92+
Test running an unknown migrations backwards.
93+
"""
4394
forwards(app_name="example_app", migration_id="all")
4495

45-
# Check the tables exist
46-
for table_class in TABLE_CLASSES:
47-
self.assertTrue(table_class.table_exists().run_sync())
96+
with self.assertRaises(SystemExit) as manager:
97+
backwards(
98+
app_name="example_app",
99+
migration_id="migration-12345",
100+
auto_agree=True,
101+
)
102+
103+
self.assertTrue(
104+
manager.exception.__str__().startswith(
105+
"Unrecognized migration name - must be one of "
106+
)
107+
)
108+
109+
def test_backwards_no_migrations(self):
110+
"""
111+
Test running migrations backwards if none have been run previously.
112+
"""
113+
with self.assertRaises(SystemExit) as manager:
114+
backwards(
115+
app_name="example_app",
116+
migration_id="2020-12-17T18:44:30",
117+
auto_agree=True,
118+
)
119+
120+
self.assertEqual(
121+
manager.exception.__str__(), "No migrations to reverse!"
122+
)
48123

49-
backwards(app_name="example_app", migration_id="all", auto_agree=True)
124+
def test_forwards_no_migrations(self):
125+
"""
126+
Test running the migrations if they've already run.
127+
"""
128+
forwards(app_name="example_app", migration_id="all")
129+
130+
with self.assertRaises(SystemExit) as manager:
131+
forwards(app_name="example_app", migration_id="all")
132+
133+
self.assertEqual(
134+
manager.exception.__str__(), "No migrations left to run!"
135+
)
136+
137+
def test_forwards_fake(self):
138+
"""
139+
Test running the migrations if they've already run.
140+
"""
141+
forwards(app_name="example_app", migration_id="all", fake=True)
50142

51-
# Check the tables don't exist
52143
for table_class in TABLE_CLASSES:
53144
self.assertTrue(not table_class.table_exists().run_sync())
54145

146+
ran_migration_names = (
147+
Migration.select(Migration.name).output(as_list=True).run_sync()
148+
)
149+
150+
self.assertEqual(
151+
ran_migration_names,
152+
[
153+
"2020-12-17T18:44:30",
154+
"2020-12-17T18:44:39",
155+
"2020-12-17T18:44:44",
156+
],
157+
)
158+
55159
def tearDown(self):
56-
for table_class in TABLE_CLASSES:
57-
table_class.alter().drop_table(if_exists=True).run_sync()
160+
for table_class in TABLE_CLASSES + [Migration]:
161+
table_class.alter().drop_table(
162+
cascade=True, if_exists=True
163+
).run_sync()

0 commit comments

Comments
 (0)