Files
stack/tests/cli/test_drugs_cli.py

143 lines
4.0 KiB
Python

"""stack drugs — status."""
from __future__ import annotations
import duckdb
import pytest
from typer.testing import CliRunner
import cli.drugs as dg
from cli import app
from drugs import tables as T
from market import tables as MT
runner = CliRunner()
class _Batch:
def __init__(self, c):
self.c = c
def __enter__(self):
return self.c
def __exit__(self, *a):
return False
@pytest.fixture
def env(monkeypatch):
c = duckdb.connect(":memory:")
monkeypatch.setattr(dg, "_batch", lambda: _Batch(c))
monkeypatch.setattr(dg, "_read", lambda: c.cursor())
published: list[bool] = []
monkeypatch.setattr(dg, "_publish", lambda: published.append(True))
yield c, published
c.close()
def test_status_prints_counts_and_gaps(env):
c, _ = env
T.ensure_tables(c)
c.execute("CREATE TABLE drugs.probe (a INTEGER)")
c.execute("INSERT INTO drugs.probe VALUES (1), (2)")
MT.write_gap(c, "cms-asp:asp", "quarter 2026-Q4 missing")
MT.write_gap(c, "tiingo:prices", "no key")
res = runner.invoke(app, ["drugs", "status"])
assert res.exit_code == 0, res.output
assert "drugs.probe: 2" in res.output
assert "gap: cms-asp:asp: quarter 2026-Q4 missing" in res.output
assert "tiingo" not in res.output
def test_status_without_tables_or_gaps(env, monkeypatch):
c, _ = env
res = runner.invoke(app, ["drugs", "status"])
assert res.exit_code == 0 and "no drugs tables yet" in res.output
T.ensure_tables(c)
res = runner.invoke(app, ["drugs", "status"])
assert "no gaps recorded" in res.output
def test_status_missing_market_table_and_errors(env, monkeypatch):
c, _ = env
T.ensure_tables(c)
res = runner.invoke(app, ["drugs", "status"])
assert res.exit_code == 0
class _Broken:
def execute(self, *a, **k):
raise RuntimeError("x")
def close(self):
pass
monkeypatch.setattr(dg, "_read", lambda: _Broken())
assert isinstance(runner.invoke(app, ["drugs", "status"]).exception, RuntimeError)
def test_gap_helper_writes_only_gap(env):
c, published = env
dg._gap("cms-asp:asp", "x", write=False)
assert MT.read_gaps(c) == [] if _has_gap_table(c) else True
assert published == []
dg._gap("cms-asp:asp", "x", write=True)
assert [g.source for g in MT.read_gaps(c)] == ["cms-asp:asp"]
assert published == [True]
assert (
c.execute("SELECT count(*) FROM cms.ingest_log").fetchone() == (0,)
if _has_log(c)
else True
)
def _has_gap_table(c):
return bool(
c.execute(
"SELECT 1 FROM information_schema.tables WHERE table_name='source_gap'"
).fetchall()
)
def _has_log(c):
return bool(
c.execute(
"SELECT 1 FROM information_schema.tables WHERE table_name='ingest_log'"
).fetchall()
)
def test_default_indirections_resolve_lazily(monkeypatch, capsys):
import conf.connect as cc
calls: list[str] = []
monkeypatch.setattr(cc, "duckdb_batch", lambda n: calls.append(f"batch {n}") or n)
monkeypatch.setattr(
cc,
"duckdb",
lambda n, read_only=False: calls.append(f"read {n} {read_only}") or n,
)
monkeypatch.setattr(cc, "bib", lambda: calls.append("bib") or "store")
monkeypatch.setattr(cc, "publish_replica", lambda n: f"/r/{n}")
assert dg._batch() == "aco" and dg._read() == "aco" and dg._store() == "store"
dg._publish()
assert calls == ["batch aco", "read aco True", "bib"]
assert "replica → /r/aco" in capsys.readouterr().out
def test_status_gap_read_error_propagates(env, monkeypatch):
c, _ = env
T.ensure_tables(c)
class _Con:
def execute(self, sql, *a):
if "source_gap" in sql:
raise RuntimeError("boom")
return c.execute(sql, *a)
def close(self):
pass
monkeypatch.setattr(dg, "_read", lambda: _Con())
assert isinstance(runner.invoke(app, ["drugs", "status"]).exception, RuntimeError)