11from __future__ import annotations
2- from unittest import TestCase
2+
33import typing as t
4+ from unittest import TestCase
45
6+ from piccolo .apps .migrations .tables import Migration
57from piccolo .apps .migrations .commands .backwards import backwards
68from piccolo .apps .migrations .commands .forwards import forwards
7-
89from 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
1718if 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