Test the revision lock of the CLI stages
The `@db_revision` decorator no longer skips its check under `TESTING`. The tests replace `current_db_revision()` with the revision they need.
This commit is contained in:
parent
eb059c1ea5
commit
2add322a25
3 changed files with 56 additions and 14 deletions
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
import collections.abc as cabc
|
import collections.abc as cabc
|
||||||
import functools
|
import functools
|
||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
import typing as t
|
import typing as t
|
||||||
|
|
||||||
|
|
@ -39,19 +38,17 @@ def db_revision(rev: str) -> cabc.Callable[..., cabc.Callable[..., t.Any]]:
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def ensure(*args: t.Any, **kwargs: t.Any) -> t.Any:
|
def ensure(*args: t.Any, **kwargs: t.Any) -> t.Any:
|
||||||
"""Do not execute the `func` if the revision does not match."""
|
"""Do not execute the `func` if the revision does not match."""
|
||||||
# Skip in tests as the database then either has no or the latest revision
|
current = current_db_revision()
|
||||||
if not os.getenv("TESTING"): # pragma: no cover
|
|
||||||
current = current_db_revision()
|
|
||||||
|
|
||||||
if current != rev:
|
if current != rev:
|
||||||
click.echo(
|
click.echo(
|
||||||
click.style(
|
click.style(
|
||||||
f"Database is at revision {current},"
|
f"Database is at revision {current},"
|
||||||
f" but this stage requires {rev}",
|
f" but this stage requires {rev}",
|
||||||
fg="red",
|
fg="red",
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
|
|
||||||
38
tests/console/test_decorators.py
Normal file
38
tests/console/test_decorators.py
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
"""Test the `velofy.console.decorators` module."""
|
||||||
|
|
||||||
|
import click
|
||||||
|
|
||||||
|
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
|
||||||
|
|
@ -47,6 +47,13 @@ def test_two_pixels_with_two_addresses(
|
||||||
# Hack the configuration regarding the grids to be created.
|
# Hack the configuration regarding the grids to be created.
|
||||||
monkeypatch.setattr(velofy.config, "GRID_SIDE_LENGTHS", (side_length,))
|
monkeypatch.setattr(velofy.config, "GRID_SIDE_LENGTHS", (side_length,))
|
||||||
|
|
||||||
|
# The `gridify` stage is locked to the revision it was carried out at.
|
||||||
|
monkeypatch.setattr(
|
||||||
|
gridify.decorators,
|
||||||
|
"current_db_revision",
|
||||||
|
lambda: "e86290e7305e",
|
||||||
|
)
|
||||||
|
|
||||||
result = cli.invoke(gridify.gridify)
|
result = cli.invoke(gridify.gridify)
|
||||||
|
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue