Public repository: timo / mitch

Reader

A migration stitcher (therefore mitch) for PostgreSQL, SQLite, MariaDB, and others

mitch / tests / test_project.py · 4.3 KB · at 76a2c3d332a1 · raw

Outline: 13 definitions
1
import subprocess
2
from pathlib import Path
3
4
import pytest
5
from typer import Exit
6
7
from mitch.errors import MissingMigrationError, NoProjectFound
8
from mitch.project import Project
9
from mitch.vcs import GitManager
10
11
12
def _run_git(cwd, *args):
13
    result = subprocess.run(
14
        ["git", *args],
15
        cwd=cwd,
16
        capture_output=True,
17
        text=True,
18
        check=False,
19
    )
20
    assert result.returncode == 0, result.stderr or result.stdout
21
    return result.stdout.strip()
22
23
24
def _init_git_repo(path):
25
    _run_git(path, "init")
26
27
28
def _write_project_config(path: Path, project_id: str = "tests-project") -> None:
29
    (path / "mitch.toml").write_text(f'[project]\nid = "{project_id}"\n', encoding="utf-8")
30
31
32
def test_load_project():
33
    project = Project.from_path(Path("tests/migrations"))
34
35
    migration = project.by_id("001-init")
36
    assert migration is not None
37
38
    assert migration.id == "001-init"
39
    assert migration.up_script.name == "001-init.up.sql"
40
    assert migration.down_script is None
41
    assert migration.config_file is None
42
    assert migration.transactional is True
43
    assert migration.idempotent is False
44
    assert migration.dependencies == set()
45
46
47
def test_load_project_with_dependencies():
48
    project = Project.from_path(Path("tests/migrations"))
49
    migrations = list(project.by_ids_with_dependencies(["002-add-column"]))
50
    assert migrations[0].id == "001-init"
51
    assert migrations[1].id == "002-add-column"
52
53
54
def test_load_project_with_dependants():
55
    project = Project.from_path(Path("tests/migrations"))
56
    migrations = list(project.by_ids_with_dependants(["001-init"]))
57
    assert migrations[0].id == "002-add-column"
58
    assert migrations[1].id == "001-init"
59
60
61
def test_sort_by_created_at_then_id(tmp_path):
62
    _write_project_config(tmp_path, "sort-test")
63
    (tmp_path / "001-init.up.sql").write_text("SELECT 1;", encoding="utf-8")
64
    (tmp_path / "002-second.up.sql").write_text("SELECT 1;", encoding="utf-8")
65
66
    (tmp_path / "003-old.up.sql").write_text("SELECT 1;", encoding="utf-8")
67
    (tmp_path / "003-old.config.toml").write_text("created_at = 2020-01-01\n", encoding="utf-8")
68
69
    (tmp_path / "004-new.up.sql").write_text("SELECT 1;", encoding="utf-8")
70
    (tmp_path / "004-new.config.toml").write_text("created_at = 2020-01-02\n", encoding="utf-8")
71
72
    project = Project.from_path(tmp_path)
73
    migrations = list(project.by_ids_with_dependencies(["001-init", "002-second", "003-old", "004-new"]))
74
75
    assert [m.id for m in migrations] == ["003-old", "004-new", "001-init", "002-second"]
76
77
78
def test_by_paths_raises_if_any_path_is_missing():
79
    project = Project.from_path(Path("tests/migrations"))
80
81
    with pytest.raises(MissingMigrationError):
82
        list(
83
            project.by_paths(
84
                [
85
                    "tests/migrations/001-init.up.sql",
86
                    "tests/migrations/does-not-exist.up.sql",
87
                ]
88
            )
89
        )
90
91
92
def test_get_migration_manager_finds_git_in_parent_directory(tmp_path):
93
    project_root = tmp_path / "project"
94
    project_root.mkdir()
95
    _write_project_config(project_root, "git-parent")
96
    _init_git_repo(project_root)
97
98
    migrations_path = project_root / "migrations"
99
    migrations_path.mkdir()
100
    (migrations_path / "001-init.up.sql").write_text("SELECT 1;", encoding="utf-8")
101
102
    project = Project.from_path(migrations_path)
103
104
    manager = project.get_migration_manager()
105
106
    assert isinstance(manager, GitManager)
107
108
109
def test_get_migration_manager_raises_when_no_repository_metadata(tmp_path):
110
    _write_project_config(tmp_path, "no-vcs")
111
    project = Project.from_path(tmp_path)
112
113
    with pytest.raises(Exit):
114
        project.get_migration_manager()
115
116
117
def test_from_path_finds_project_root_in_parent_directory(tmp_path):
118
    _write_project_config(tmp_path, "parent-root")
119
    nested = tmp_path / "nested" / "deeper"
120
    nested.mkdir(parents=True)
121
122
    project = Project.from_path(nested)
123
124
    assert project.path == tmp_path.resolve()
125
    assert project.id == "parent-root"
126
127
128
def test_from_path_raises_when_config_is_missing(tmp_path):
129
    with pytest.raises(NoProjectFound):
130
        Project.from_path(tmp_path)
131
132
133
def test_project_config_requires_project_id(tmp_path):
134
    (tmp_path / "mitch.toml").write_text("[project]\n", encoding="utf-8")
135
136
    with pytest.raises(ValueError):
137
        Project.from_path(tmp_path)