2026-12-02 13:41:50 +01:00
|
|
|
"""Test the `velofy.console.decorators` module."""
|
|
|
|
|
|
|
|
|
|
import click
|
2026-12-02 13:51:49 +01:00
|
|
|
import pytest
|
|
|
|
|
from alembic import config as alembic_config
|
|
|
|
|
from alembic import script as alembic_script
|
2026-12-02 13:41:50 +01:00
|
|
|
|
2026-12-02 13:51:49 +01:00
|
|
|
import velofy
|
2026-12-02 13:41:50 +01:00
|
|
|
from velofy.console import decorators
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
REQUIRED = "abcdef123456"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@click.command()
|
|
|
|
|
@decorators.db_revision(REQUIRED)
|
|
|
|
|
def stage() -> None:
|
|
|
|
|
"""A stage locked to the `REQUIRED` revision."""
|
|
|
|
|
click.echo("stage executed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_stage_runs_at_the_required_revision(cli, monkeypatch):
|
|
|
|
|
"""The stage runs if the database is at the required revision."""
|
|
|
|
|
monkeypatch.setattr(decorators, "current_db_revision", lambda: REQUIRED)
|
|
|
|
|
|
|
|
|
|
result = cli.invoke(stage)
|
|
|
|
|
|
|
|
|
|
assert result.exit_code == 0
|
|
|
|
|
assert "stage executed" in result.output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_stage_refuses_at_another_revision(cli, monkeypatch):
|
|
|
|
|
"""The stage refuses to run once the database has moved on."""
|
|
|
|
|
current = "fedcba654321"
|
|
|
|
|
monkeypatch.setattr(decorators, "current_db_revision", lambda: current)
|
|
|
|
|
|
|
|
|
|
result = cli.invoke(stage)
|
|
|
|
|
|
|
|
|
|
assert result.exit_code == 1
|
|
|
|
|
assert "stage executed" not in result.output
|
|
|
|
|
assert current in result.output
|
|
|
|
|
assert REQUIRED in result.output
|
2026-12-02 13:51:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.db
|
|
|
|
|
@pytest.mark.usefixtures("db_connection")
|
|
|
|
|
def test_current_db_revision(monkeypatch, request):
|
|
|
|
|
"""`current_db_revision()` reads the revision of the test database."""
|
|
|
|
|
# The migrations name the test database's version table
|
|
|
|
|
# after the test schema, as configured in migrations/env.py
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
velofy.config,
|
|
|
|
|
"ALEMBIC_TABLE",
|
|
|
|
|
f"{velofy.config.ALEMBIC_TABLE}_{velofy.config.CLEAN_SCHEMA}",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
result = decorators.current_db_revision()
|
|
|
|
|
|
|
|
|
|
if request.node.callspec.params["db_connection"] == "sequentially":
|
|
|
|
|
cfg = alembic_config.Config("alembic.ini")
|
|
|
|
|
head = alembic_script.ScriptDirectory.from_config(cfg).get_current_head()
|
|
|
|
|
assert result == head
|
|
|
|
|
|
|
|
|
|
else: # "all_at_once" creates no version table
|
|
|
|
|
assert result is None
|