Skip to content

Commit f89d2c4

Browse files
authored
Merge pull request #10 from andersinno/test-migration-fix
tests: Fix migration test workdir issue
2 parents f330489 + 3b17ce4 commit f89d2c4

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

tests/db/test_migrate.py

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
"""
22
Test Alembic migrations
33
"""
4-
import subprocess
4+
import os
5+
from contextlib import contextmanager
6+
from os import fspath
7+
from pathlib import Path
8+
from typing import Iterator
59

6-
from passari_workflow.db.models import Base
7-
from alembic.config import Config
810
from alembic import command
11+
from alembic.config import Config
912

10-
from pathlib import Path
13+
from passari_workflow.db.models import Base
14+
15+
ROOT_PATH = Path(__file__).resolve().parent.parent.parent
1116

1217

1318
def test_migrate(session, engine, database):
1419
"""
1520
Test migrations by running all migrations and then downgrading
1621
"""
17-
config = Config(
18-
str(Path(__file__).resolve().parent.parent.parent / "alembic.ini")
19-
)
22+
config = Config(fspath(ROOT_PATH / "alembic.ini"))
2023

2124
try:
2225
Base.metadata.drop_all(engine)
2326

24-
# Upgrade the database
25-
command.upgrade(config, "head")
27+
with run_in_path(ROOT_PATH):
28+
# Upgrade the database
29+
command.upgrade(config, "head")
2630

27-
# Downgrade the database
28-
command.downgrade(config, "base")
31+
# Downgrade the database
32+
command.downgrade(config, "base")
2933
finally:
3034
# Ensure that the tables are the same after the test, even if
3135
# something fails
3236
Base.metadata.drop_all(engine)
3337
Base.metadata.create_all(engine)
38+
39+
40+
@contextmanager
41+
def run_in_path(path: Path) -> Iterator[None]:
42+
orig_cwd = os.getcwd()
43+
try:
44+
os.chdir(path)
45+
yield
46+
finally:
47+
os.chdir(orig_cwd)

0 commit comments

Comments
 (0)