66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
from types import SimpleNamespace
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from govoplan_core.commands.wait_for_database import main
|
|
from govoplan_core.db.migrations import configured_migration_heads
|
|
|
|
|
|
class WaitForDatabaseTests(unittest.TestCase):
|
|
def test_configured_heads_resolve_cross_branch_dependencies(self) -> None:
|
|
scripts = MagicMock()
|
|
scripts.get_revisions.return_value = (
|
|
SimpleNamespace(revision="head-b"),
|
|
SimpleNamespace(revision="head-a"),
|
|
)
|
|
|
|
with (
|
|
patch("govoplan_core.db.migrations.alembic_config"),
|
|
patch(
|
|
"govoplan_core.db.migrations.ScriptDirectory.from_config",
|
|
return_value=scripts,
|
|
),
|
|
):
|
|
heads = configured_migration_heads("sqlite://")
|
|
|
|
self.assertEqual(("head-a", "head-b"), heads)
|
|
scripts.get_revisions.assert_called_once_with("heads")
|
|
scripts.get_heads.assert_not_called()
|
|
|
|
def test_waits_until_exact_configured_heads_are_visible(self) -> None:
|
|
with (
|
|
patch(
|
|
"govoplan_core.commands.wait_for_database.configured_migration_heads",
|
|
return_value=("head-a", "head-b"),
|
|
),
|
|
patch(
|
|
"govoplan_core.commands.wait_for_database.database_migration_heads",
|
|
side_effect=[("head-a",), ("head-a", "head-b")],
|
|
),
|
|
patch("govoplan_core.commands.wait_for_database.time.sleep"),
|
|
):
|
|
result = main(["--timeout-seconds", "1", "--poll-seconds", "0.01"])
|
|
|
|
self.assertEqual(0, result)
|
|
|
|
def test_returns_temporary_failure_when_heads_do_not_match(self) -> None:
|
|
with (
|
|
patch(
|
|
"govoplan_core.commands.wait_for_database.configured_migration_heads",
|
|
return_value=("head-b",),
|
|
),
|
|
patch(
|
|
"govoplan_core.commands.wait_for_database.database_migration_heads",
|
|
return_value=("head-a",),
|
|
),
|
|
):
|
|
result = main(["--timeout-seconds", "0"])
|
|
|
|
self.assertEqual(75, result)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|