insider-trading / tests /test_cli.py
ZipLime's picture
Update SEC Form 4 point-in-time dataset
ba0785e verified
Raw History Blame Contribute Delete
5.56 kB
"""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