forked from piccolo-orm/piccolo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_forwards_backwards.py
More file actions
184 lines (155 loc) · 5.5 KB
/
test_forwards_backwards.py
File metadata and controls
184 lines (155 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from __future__ import annotations
import sys
import typing as t
from unittest import TestCase
from unittest.mock import patch, call, MagicMock
from piccolo.apps.migrations.tables import Migration
from piccolo.apps.migrations.commands.backwards import backwards
from piccolo.apps.migrations.commands.forwards import forwards
from piccolo.utils.sync import run_sync
from tests.base import postgres_only
from tests.example_app.tables import (
Band,
Concert,
Manager,
Poster,
Ticket,
Venue,
)
if t.TYPE_CHECKING: # pragma: no cover
from piccolo.table import Table
TABLE_CLASSES: t.List[t.Type[Table]] = [
Manager,
Band,
Venue,
Concert,
Ticket,
Poster,
]
@postgres_only
class TestForwardsBackwards(TestCase):
"""
Test the forwards and backwards migration commands.
"""
def test_forwards_backwards_all_migrations(self):
"""
Test running all of the migrations forwards, then backwards.
"""
for app_name in ("example_app", "all"):
run_sync(forwards(app_name=app_name, migration_id="all"))
# Check the tables exist
for table_class in TABLE_CLASSES:
self.assertTrue(table_class.table_exists().run_sync())
run_sync(
backwards(
app_name=app_name, migration_id="all", auto_agree=True
)
)
# Check the tables don't exist
for table_class in TABLE_CLASSES:
self.assertTrue(not table_class.table_exists().run_sync())
def test_forwards_backwards_single_migration(self):
"""
Test running a single migrations forwards, then backwards.
"""
for migration_id in ["1", "2020-12-17T18:44:30"]:
run_sync(
forwards(app_name="example_app", migration_id=migration_id)
)
table_classes = [Band, Manager]
# Check the tables exist
for table_class in table_classes:
self.assertTrue(table_class.table_exists().run_sync())
run_sync(
backwards(
app_name="example_app",
migration_id=migration_id,
auto_agree=True,
)
)
# Check the tables don't exist
for table_class in table_classes:
self.assertTrue(not table_class.table_exists().run_sync())
@patch("piccolo.apps.migrations.commands.forwards.print")
def test_forwards_unknown_migration(self, print_: MagicMock):
"""
Test running an unknown migrations forwards.
"""
with self.assertRaises(SystemExit):
run_sync(
forwards(
app_name="example_app", migration_id="migration-12345"
)
)
self.assertTrue(
call("migration-12345 is unrecognised", file=sys.stderr)
in print_.mock_calls
)
@patch("piccolo.apps.migrations.commands.backwards.print")
def test_backwards_unknown_migration(self, print_: MagicMock):
"""
Test running an unknown migrations backwards.
"""
run_sync(forwards(app_name="example_app", migration_id="all"))
with self.assertRaises(SystemExit):
run_sync(
backwards(
app_name="example_app",
migration_id="migration-12345",
auto_agree=True,
)
)
self.assertTrue(
tuple(print_.mock_calls[0])[1][0].startswith(
"Unrecognized migration name - must be one of "
)
)
@patch("piccolo.apps.migrations.commands.backwards.print")
def test_backwards_no_migrations(self, print_: MagicMock):
"""
Test running migrations backwards if none have been run previously.
"""
run_sync(
backwards(
app_name="example_app",
migration_id="2020-12-17T18:44:30",
auto_agree=True,
)
)
self.assertTrue(call("No migrations to reverse!") in print_.mock_calls)
@patch("piccolo.apps.migrations.commands.forwards.print")
def test_forwards_no_migrations(self, print_: MagicMock):
"""
Test running the migrations if they've already run.
"""
run_sync(forwards(app_name="example_app", migration_id="all"))
run_sync(forwards(app_name="example_app", migration_id="all"))
self.assertTrue(
print_.mock_calls[-1] == call("No migrations left to run!")
)
def test_forwards_fake(self):
"""
Test running the migrations if they've already run.
"""
run_sync(
forwards(app_name="example_app", migration_id="all", fake=True)
)
for table_class in TABLE_CLASSES:
self.assertTrue(not table_class.table_exists().run_sync())
ran_migration_names = (
Migration.select(Migration.name).output(as_list=True).run_sync()
)
self.assertEqual(
ran_migration_names,
# TODO - rather than hardcoding, might fetch these dynamically.
[
"2020-12-17T18:44:30",
"2020-12-17T18:44:39",
"2020-12-17T18:44:44",
],
)
def tearDown(self):
for table_class in TABLE_CLASSES + [Migration]:
table_class.alter().drop_table(
cascade=True, if_exists=True
).run_sync()