80 lines
2.6 KiB
Python
80 lines
2.6 KiB
Python
from __future__ import annotations
|
|
|
|
import types
|
|
|
|
import ariadne.migrate as migrate_module
|
|
|
|
|
|
class DummyDatabase:
|
|
def __init__(self, name: str):
|
|
self.name = name
|
|
self.calls: list[tuple[int, bool, bool]] = []
|
|
self.closed = False
|
|
|
|
def migrate(self, lock_id: int, *, include_ariadne_tables: bool, include_access_requests: bool) -> None:
|
|
self.calls.append((lock_id, include_ariadne_tables, include_access_requests))
|
|
|
|
def close(self) -> None:
|
|
self.closed = True
|
|
|
|
|
|
def test_build_db_uses_settings(monkeypatch) -> None:
|
|
captured = {}
|
|
monkeypatch.setattr(
|
|
migrate_module,
|
|
"settings",
|
|
types.SimpleNamespace(
|
|
ariadne_db_pool_min=1,
|
|
ariadne_db_pool_max=3,
|
|
ariadne_db_connect_timeout_sec=5,
|
|
ariadne_db_lock_timeout_sec=7,
|
|
ariadne_db_statement_timeout_sec=11,
|
|
ariadne_db_idle_in_tx_timeout_sec=13,
|
|
),
|
|
)
|
|
|
|
def fake_database(dsn, config):
|
|
captured["dsn"] = dsn
|
|
captured["config"] = config
|
|
return DummyDatabase("ariadne")
|
|
|
|
monkeypatch.setattr(migrate_module, "Database", fake_database)
|
|
migrate_module._build_db("postgresql://db", "ariadne_migrate")
|
|
|
|
assert captured["dsn"] == "postgresql://db"
|
|
assert captured["config"].application_name == "ariadne_migrate"
|
|
assert captured["config"].lock_timeout_sec == 7
|
|
|
|
|
|
def test_main_returns_when_migrations_disabled(monkeypatch) -> None:
|
|
monkeypatch.setattr(
|
|
migrate_module,
|
|
"settings",
|
|
types.SimpleNamespace(ariadne_run_migrations=False),
|
|
)
|
|
monkeypatch.setattr(migrate_module, "_build_db", lambda *_args, **_kwargs: (_ for _ in ()).throw(RuntimeError("should not build")))
|
|
migrate_module.main()
|
|
|
|
|
|
def test_main_runs_ariadne_and_portal_migrations(monkeypatch) -> None:
|
|
ariadne_db = DummyDatabase("ariadne")
|
|
portal_db = DummyDatabase("portal")
|
|
databases = [ariadne_db, portal_db]
|
|
monkeypatch.setattr(
|
|
migrate_module,
|
|
"settings",
|
|
types.SimpleNamespace(
|
|
ariadne_run_migrations=True,
|
|
ariadne_database_url="postgresql://ariadne",
|
|
portal_database_url="postgresql://portal",
|
|
),
|
|
)
|
|
monkeypatch.setattr(migrate_module, "_build_db", lambda *_args, **_kwargs: databases.pop(0))
|
|
|
|
migrate_module.main()
|
|
|
|
assert ariadne_db.calls == [(migrate_module.ARIADNE_MIGRATION_LOCK_ID, True, False)]
|
|
assert portal_db.calls == [(migrate_module.PORTAL_MIGRATION_LOCK_ID, False, True)]
|
|
assert ariadne_db.closed is True
|
|
assert portal_db.closed is True
|