File size: 5,557 Bytes
ba0785e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | """CLI contract: exit codes, argument wiring, and publication gating."""
from __future__ import annotations
import json
from datetime import date
import pytest
from recipe.cli import build_parser, main
from recipe.discover import ELECTRONIC_COVERAGE_START
def test_validate_passes_on_an_empty_tree_and_writes_a_report(tmp_path, capsys) -> None:
report_path = tmp_path / "quality.json"
code = main(["validate", "--data-dir", str(tmp_path / "data"), "--report", str(report_path)])
assert code == 0
payload = json.loads(capsys.readouterr().out)
assert payload["errors"] == []
assert json.loads(report_path.read_text())["metrics"]["filings"] == 0
def test_validate_returns_nonzero_when_the_dataset_is_invalid(tmp_path, capsys) -> None:
import polars as pl
from recipe.schema import FILING_SCHEMA, TRANSACTION_SCHEMA, frame_from_records
from recipe.storage import AppendOnlyParquetStore
from .conftest import filing_row, transaction_row
data_dir = tmp_path / "data"
store = AppendOnlyParquetStore(data_dir)
store.append(
"filings",
frame_from_records([filing_row(form_type="5")], FILING_SCHEMA),
key="accession_number",
)
store.append(
"transactions",
frame_from_records([transaction_row(shares=-5.0)], TRANSACTION_SCHEMA),
key="transaction_id",
)
code = main(["validate", "--data-dir", str(data_dir)])
assert code == 1
payload = json.loads(capsys.readouterr().out)
assert any("unsupported form types" in error for error in payload["errors"])
assert any("negative share" in error for error in payload["errors"])
assert isinstance(pl.DataFrame(), pl.DataFrame)
def test_diagnostics_reports_distributions_and_anomalies(tmp_path, capsys) -> None:
from recipe.schema import FILING_SCHEMA, TRANSACTION_SCHEMA, frame_from_records
from recipe.storage import AppendOnlyParquetStore
from .conftest import filing_row, transaction_row
data_dir = tmp_path / "data"
store = AppendOnlyParquetStore(data_dir)
store.append(
"filings", frame_from_records([filing_row()], FILING_SCHEMA), key="accession_number"
)
store.append(
"transactions",
frame_from_records([transaction_row()], TRANSACTION_SCHEMA),
key="transaction_id",
)
code = main(["diagnostics", "--data-dir", str(data_dir), "--output", str(tmp_path / "d.json")])
assert code == 0
payload = json.loads(capsys.readouterr().out)
assert payload["counts"]["transactions"] == 1
assert payload["transaction_code_distribution"] == {"P": 1}
assert payload["filings_per_year"] == {"2026": 1}
assert payload["anomalies"]["duplicate_transaction_ids"] == 0
assert (tmp_path / "d.json").is_file()
def test_build_derived_materializes_pit_and_features(tmp_path, capsys) -> None:
from recipe.schema import FILING_SCHEMA, TRANSACTION_SCHEMA, frame_from_records
from recipe.storage import AppendOnlyParquetStore
from .conftest import filing_row, transaction_row
data_dir = tmp_path / "data"
store = AppendOnlyParquetStore(data_dir)
store.append(
"filings", frame_from_records([filing_row()], FILING_SCHEMA), key="accession_number"
)
store.append(
"transactions",
frame_from_records([transaction_row()], TRANSACTION_SCHEMA),
key="transaction_id",
)
code = main(["build-derived", "--data-dir", str(data_dir)])
assert code == 0
payload = json.loads(capsys.readouterr().out)
assert payload["pit_events"] == 1
assert payload["feature_rows"] >= 1
assert payload["quality_ok"] is True
assert list(data_dir.glob("pit/**/*.parquet"))
assert list(data_dir.glob("features/**/*.parquet"))
def test_backfill_and_update_defaults_match_the_documented_contract() -> None:
parser = build_parser()
backfill = parser.parse_args(["backfill"])
assert backfill.start == ELECTRONIC_COVERAGE_START == date(2003, 6, 30)
assert backfill.end is None
assert backfill.publish is False
update = parser.parse_args(["update"])
assert update.lookback_days == 7
assert update.publish is False
assert update.no_bootstrap is False
def test_schedule_requires_an_id_for_lifecycle_actions() -> None:
with pytest.raises(SystemExit):
main(["schedule", "delete"])
def test_schedule_picks_the_flavor_matching_the_job(monkeypatch) -> None:
from recipe import cli, hf_jobs
captured: dict[str, object] = {}
def fake_create(spec, *, schedule, secrets, namespace, token):
captured["spec"] = spec
captured["schedule"] = schedule
captured["secrets"] = secrets
return "scheduled-job-id"
monkeypatch.setattr(hf_jobs, "create_schedule", fake_create)
monkeypatch.setenv("HF_TOKEN", "hf_fake_token")
monkeypatch.setenv("SEC_USER_AGENT", "ZipLime insider-trading/1.0 algo@limex.com")
assert cli.main(["schedule", "create", "--job", "backfill"]) == 0
spec = captured["spec"]
assert spec.flavor == hf_jobs.BACKFILL_FLAVOR
assert spec.script == "jobs/run.py"
assert "--mode" in spec.script_args and "backfill" in spec.script_args
assert captured["secrets"]["HF_TOKEN"] == "hf_fake_token"
def test_schedule_rejects_a_placeholder_sec_contact(monkeypatch, capsys) -> None:
monkeypatch.setenv("HF_TOKEN", "hf_fake_token")
monkeypatch.setenv("SEC_USER_AGENT", "Research contact@example.com")
assert main(["schedule", "create"]) == 1
assert "placeholder" in capsys.readouterr().err
|