Skip to content

Commit aa1ae69

Browse files
committed
get_migration_managers now accepts app_config instead of app_name
This is to make testing easier
1 parent 7c0ba1b commit aa1ae69

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

piccolo/apps/migrations/commands/base.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import typing as t
77

88
from piccolo.conf.apps import (
9+
AppConfig,
910
MigrationModule,
1011
Finder,
1112
)
@@ -73,7 +74,7 @@ def get_migration_ids(
7374

7475
async def get_migration_managers(
7576
self,
76-
app_name: str,
77+
app_config: AppConfig,
7778
max_migration_id: t.Optional[str] = None,
7879
offset: int = 0,
7980
) -> t.List[MigrationManager]:
@@ -88,8 +89,6 @@ async def get_migration_managers(
8889
"""
8990
migration_managers: t.List[MigrationManager] = []
9091

91-
app_config = self.get_app_config(app_name=app_name)
92-
9392
migrations_folder = app_config.migrations_folder_path
9493

9594
migration_modules: t.Dict[
@@ -129,8 +128,11 @@ async def get_table_from_snaphot(
129128
This will generate a SchemaSnapshot up to the given migration ID, and
130129
will return a DiffableTable class from that snapshot.
131130
"""
131+
app_config = self.get_app_config(app_name=app_name)
132132
migration_managers = await self.get_migration_managers(
133-
app_name=app_name, max_migration_id=max_migration_id, offset=offset
133+
app_config=app_config,
134+
max_migration_id=max_migration_id,
135+
offset=offset,
134136
)
135137
schema_snapshot = SchemaSnapshot(managers=migration_managers)
136138

piccolo/apps/migrations/commands/new.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ async def get_alter_statements(
131131
Works out which alter statements are required.
132132
"""
133133
migration_managers = await self.get_migration_managers(
134-
app_name=app_config.app_name
134+
app_config=app_config
135135
)
136136

137137
schema_snapshot = SchemaSnapshot(migration_managers)

tests/apps/migrations/auto/test_migration_manager.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
from piccolo.conf.apps import AppConfig
23
from piccolo.columns.column_types import ForeignKey
34
from unittest.mock import patch, MagicMock
45

@@ -86,7 +87,8 @@ async def run():
8687
asyncio.run(manager.run_backwards())
8788

8889
@postgres_only
89-
def test_add_table(self):
90+
@patch.object(BaseMigrationManager, "get_app_config")
91+
def test_add_table(self, get_app_config: MagicMock):
9092
"""
9193
Test adding a table to a MigrationManager.
9294
"""
@@ -106,6 +108,10 @@ def test_add_table(self):
106108
self.assertEqual(response, [{"id": 1, "name": "Bob Jones"}])
107109

108110
# Reverse
111+
112+
get_app_config.return_value = AppConfig(
113+
app_name="music", migrations_folder_path=""
114+
)
109115
asyncio.run(manager.run_backwards())
110116
self.assertEqual(self.table_exists("musician"), False)
111117
self.run_sync("DROP TABLE IF EXISTS musician;")
@@ -255,7 +261,10 @@ def test_add_non_nullable_column(self):
255261

256262
@postgres_only
257263
@patch.object(BaseMigrationManager, "get_migration_managers")
258-
def test_drop_column(self, get_migration_managers: MagicMock):
264+
@patch.object(BaseMigrationManager, "get_app_config")
265+
def test_drop_column(
266+
self, get_app_config: MagicMock, get_migration_managers: MagicMock
267+
):
259268
"""
260269
Test dropping a column with MigrationManager.
261270
"""
@@ -284,6 +293,8 @@ def test_drop_column(self, get_migration_managers: MagicMock):
284293

285294
# Reverse
286295
set_mock_return_value(get_migration_managers, [manager_1])
296+
app_config = AppConfig(app_name="music", migrations_folder_path="")
297+
get_app_config.return_value = app_config
287298
asyncio.run(manager_2.run_backwards())
288299
response = self.run_sync("SELECT * FROM musician;")
289300
self.assertEqual(response, [{"id": 1, "name": ""}])
@@ -596,7 +607,10 @@ def test_alter_column_set_length(self):
596607

597608
@postgres_only
598609
@patch.object(BaseMigrationManager, "get_migration_managers")
599-
def test_drop_table(self, get_migration_managers: MagicMock):
610+
@patch.object(BaseMigrationManager, "get_app_config")
611+
def test_drop_table(
612+
self, get_app_config: MagicMock, get_migration_managers: MagicMock
613+
):
600614
self.run_sync("DROP TABLE IF EXISTS musician;")
601615

602616
name_column = Varchar()
@@ -612,14 +626,16 @@ def test_drop_table(self, get_migration_managers: MagicMock):
612626
manager_2.drop_table(class_name="Musician", tablename="musician")
613627
asyncio.run(manager_2.run())
614628

615-
set_mock_return_value(get_migration_managers, [manager_1])
616-
617629
self.assertTrue(not self.table_exists("musician"))
618630

631+
# Reverse
632+
set_mock_return_value(get_migration_managers, [manager_1])
633+
app_config = AppConfig(app_name="music", migrations_folder_path="")
634+
get_app_config.return_value = app_config
619635
asyncio.run(manager_2.run_backwards())
620636

621637
get_migration_managers.assert_called_with(
622-
app_name="music", max_migration_id="2", offset=-1
638+
app_config=app_config, max_migration_id="2", offset=-1
623639
)
624640
self.assertTrue(self.table_exists("musician"))
625641

tests/apps/migrations/commands/test_base.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from unittest import TestCase
3-
from unittest.mock import patch
3+
from unittest.mock import MagicMock, patch
44

55
from piccolo.conf.apps import AppConfig
66
from piccolo.apps.migrations.commands.base import (
@@ -21,17 +21,18 @@ def tearDown(self):
2121

2222

2323
class TestGetMigrationModules(TestCase):
24-
@patch.object(BaseMigrationManager, "get_app_config")
25-
def test_get_migration_modules(self, get_app_config):
26-
get_app_config.return_value = AppConfig(
24+
def test_get_migration_modules(self):
25+
app_config = AppConfig(
2726
app_name="music",
2827
migrations_folder_path=os.path.join(
2928
os.path.dirname(__file__), "test_migrations"
3029
),
3130
)
3231

3332
migration_managers = run_sync(
34-
BaseMigrationManager().get_migration_managers(app_name="music")
33+
BaseMigrationManager().get_migration_managers(
34+
app_config=app_config
35+
)
3536
)
3637

3738
self.assertTrue(len(migration_managers) == 1)
@@ -42,7 +43,7 @@ def test_get_migration_modules(self, get_app_config):
4243

4344
class TestGetTableFromSnapshot(TestCase):
4445
@patch.object(BaseMigrationManager, "get_app_config")
45-
def test_get_table_from_snaphot(self, get_app_config):
46+
def test_get_table_from_snaphot(self, get_app_config: MagicMock):
4647
"""
4748
Test the get_table_from_snaphot method.
4849
"""

0 commit comments

Comments
 (0)