velofy/tests/console/test_decorators.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
1.9 KiB
Python
Raw Permalink Normal View History

"""Test the `velofy.console.decorators` module."""
import click
import pytest
from alembic import config as alembic_config
from alembic import script as alembic_script
import velofy
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
@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