|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from infrahub.core.branch.models import Branch |
| 8 | +from infrahub.core.constants import GLOBAL_BRANCH_NAME |
| 9 | +from infrahub.core.initialization import create_branch |
| 10 | +from infrahub.core.migrations.runner import MigrationRunner |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from infrahub.database import InfrahubDatabase |
| 14 | + |
| 15 | + |
| 16 | +def test_instantiation(default_branch: Branch) -> None: |
| 17 | + assert MigrationRunner(branch=Branch(name="foo")) |
| 18 | + |
| 19 | + with pytest.raises(ValueError): |
| 20 | + MigrationRunner(branch=default_branch) |
| 21 | + |
| 22 | + with pytest.raises(ValueError): |
| 23 | + MigrationRunner(branch=Branch(name=GLOBAL_BRANCH_NAME)) |
| 24 | + |
| 25 | + |
| 26 | +async def test_has_migrations(default_branch: Branch, db: InfrahubDatabase) -> None: |
| 27 | + branch = await create_branch(db=db, branch_name="foo") |
| 28 | + |
| 29 | + runner = MigrationRunner(branch=branch) |
| 30 | + assert not runner.has_migrations() |
| 31 | + |
| 32 | + branch.graph_version = None |
| 33 | + runner = MigrationRunner(branch=branch) |
| 34 | + await branch.save(db=db) |
| 35 | + assert runner.has_migrations() |
| 36 | + |
| 37 | + branch.graph_version = 40 |
| 38 | + runner = MigrationRunner(branch=branch) |
| 39 | + await branch.save(db=db) |
| 40 | + assert runner.has_migrations() |
| 41 | + |
| 42 | + |
| 43 | +async def test_applicable_migrations(default_branch: Branch, db: InfrahubDatabase) -> None: |
| 44 | + branch = await create_branch(db=db, branch_name="foo") |
| 45 | + |
| 46 | + runner = MigrationRunner(branch=branch) |
| 47 | + assert not runner.applicable_migrations |
| 48 | + |
| 49 | + branch.graph_version = None |
| 50 | + runner = MigrationRunner(branch=branch) |
| 51 | + await branch.save(db=db) |
| 52 | + assert runner.applicable_migrations |
| 53 | + assert [m.name for m in runner.applicable_migrations] == ["043_backfill_hfid_display_label_in_db"] |
| 54 | + |
| 55 | + branch.graph_version = 40 |
| 56 | + runner = MigrationRunner(branch=branch) |
| 57 | + await branch.save(db=db) |
| 58 | + assert runner.applicable_migrations |
| 59 | + assert [m.name for m in runner.applicable_migrations] == ["043_backfill_hfid_display_label_in_db"] |
0 commit comments