Skip to content

Commit cc16b1f

Browse files
committed
started adding integration tests for migrations
1 parent a20ba73 commit cc16b1f

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Integration tests
2+
3+
The integration tests are for testing the migrations end to end - from auto generating a migration file, to running it.
4+
5+
Migrations are complex - columns can be added, deleted, renamed, and modified (likewise with tables). Migrations can also be run backwards. To properly test all of the possible options, we need a lot of test cases.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import os
2+
import shutil
3+
import tempfile
4+
import time
5+
import typing as t
6+
from unittest import TestCase
7+
8+
from piccolo.conf.apps import AppConfig
9+
from piccolo.columns.column_types import Varchar
10+
from piccolo.apps.migrations.commands.new import (
11+
_create_new_migration,
12+
_create_migrations_folder,
13+
)
14+
from piccolo.apps.migrations.commands.forwards import ForwardsMigrationManager
15+
from piccolo.table import Table, create_table_class
16+
from piccolo.utils.sync import run_sync
17+
18+
19+
class TestMigrations(TestCase):
20+
def tearDown(self):
21+
create_table_class("MyTable").alter().drop_table(if_exists=True)
22+
23+
def run_migrations(self, app_config: AppConfig):
24+
manager = ForwardsMigrationManager(app_name=app_config.app_name)
25+
run_sync(manager.create_migration_table())
26+
run_sync(manager.run_migrations(app_config=app_config))
27+
28+
def _test_migrations(self, table_1: t.Type[Table], table_2: t.Type[Table]):
29+
temp_directory_path = tempfile.gettempdir()
30+
migrations_folder_path = os.path.join(
31+
temp_directory_path, "piccolo_migrations"
32+
)
33+
34+
if os.path.exists(migrations_folder_path):
35+
shutil.rmtree(migrations_folder_path)
36+
37+
_create_migrations_folder(migrations_folder_path)
38+
39+
app_config = AppConfig(
40+
app_name="test_app",
41+
migrations_folder_path=migrations_folder_path,
42+
table_classes=[table_1],
43+
)
44+
45+
meta = run_sync(
46+
_create_new_migration(app_config=app_config, auto=True)
47+
)
48+
self.assertTrue(os.path.exists(meta.migration_path))
49+
self.run_migrations(app_config=app_config)
50+
51+
#######################################################################
52+
53+
# It's kind of absurd sleeping for 1 microsecond, but it guarantees
54+
# the migration IDs will be unique, and just in case computers and / or
55+
# Python get insanely fast in the future :)
56+
time.sleep(1e-6)
57+
58+
#######################################################################
59+
60+
app_config.table_classes = [table_2]
61+
meta = run_sync(
62+
_create_new_migration(app_config=app_config, auto=True)
63+
)
64+
self.assertTrue(os.path.exists(meta.migration_path))
65+
self.run_migrations(app_config=app_config)
66+
67+
# TODO - check the migrations ran correctly
68+
69+
def test_add_column(self):
70+
table_1 = create_table_class("MyTable")
71+
table_2 = create_table_class(
72+
"MyTable", class_members={"name": Varchar()}
73+
)
74+
75+
self._test_migrations(table_1=table_1, table_2=table_2)

piccolo/apps/migrations/commands/forwards.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212

1313

1414
class ForwardsMigrationManager(BaseMigrationManager):
15-
def __init__(self, app_name: str, migration_id: str, fake: bool = False):
15+
def __init__(
16+
self, app_name: str, migration_id: str = "all", fake: bool = False
17+
):
1618
self.app_name = app_name
1719
self.migration_id = migration_id
1820
self.fake = fake
1921
super().__init__()
2022

2123
async def run_migrations(self, app_config: AppConfig) -> MigrationResult:
2224
already_ran = await Migration.get_migrations_which_ran(
23-
app_name=self.app_name
25+
app_name=app_config.app_name
2426
)
2527

2628
migration_modules: t.Dict[
@@ -67,7 +69,7 @@ async def run_migrations(self, app_config: AppConfig) -> MigrationResult:
6769
print(f"-> Ran {_id}")
6870

6971
await Migration.insert().add(
70-
Migration(name=_id, app_name=self.app_name)
72+
Migration(name=_id, app_name=app_config.app_name)
7173
).run()
7274

7375
return MigrationResult(success=True, message="Ran successfully")

0 commit comments

Comments
 (0)