Files
stack/tests/notebooks/test_event_study_nb.py
kert e6e5d2d920
All checks were successful
CI / lint (push) Successful in 49s
CI / notebooks-smoke (push) Successful in 2m8s
Deploy / changes (push) Successful in 19s
Infra CI / notebooks (push) Successful in 1m4s
Infra CI / zotero (push) Successful in 17s
Infra CI / docs (push) Successful in 20s
CI / test (push) Successful in 5m9s
Infra CI / api (push) Successful in 20s
Infra CI / bcda (push) Successful in 18s
Infra CI / llm (push) Successful in 18s
Infra CI / mc (push) Successful in 23s
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Deploy / api (push) Has been skipped
Deploy / bcda (push) Has been skipped
Deploy / llm (push) Has been skipped
Deploy / mc (push) Has been skipped
Deploy / notebooks (push) Successful in 5m3s
Deploy / report (push) Successful in 15s
feat(notebooks): market series panel in the event-study notebook (#797)
2026-11-17 12:44:18 -05:00

198 lines
5.6 KiB
Python

"""notebooks/event_study.py — structure, headless degradation, live fixture panels (#696)."""
from __future__ import annotations
import ast
import importlib.util
import re
from pathlib import Path
import pytest
NB = Path(__file__).resolve().parents[2] / "notebooks" / "event_study.py"
_ROOT = Path(__file__).resolve().parents[2]
_HAS_DATA = (_ROOT / "data" / "replica" / "aco.ro.duckdb").exists() and (
_ROOT / "data" / "bib.sqlite"
).exists()
_live = pytest.mark.skipif(not _HAS_DATA, reason="needs the live replica and bib")
def _load():
spec = importlib.util.spec_from_file_location("event_study_nb", NB)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
def test_notebook_is_a_marimo_app():
assert _load().app.__class__.__name__ == "App"
def test_cells_are_anonymous_and_banners_present():
src = NB.read_text()
names = [n.name for n in ast.parse(src).body if isinstance(n, ast.FunctionDef)]
assert names and set(names) == {"_"}
assert re.findall(r"# ── (\S+)\. ", src) == ["0", "1", "2", "3", "4", "5", "6"]
def test_no_causal_language():
src = NB.read_text().lower()
assert (
"caused" not in src and "causal estimate" in src
) # the disclaimer, nothing else
def test_headless_run_degrades_without_data(monkeypatch):
monkeypatch.setenv("STACK_DUCKDB_REPLICA", "1")
mod = _load()
import conf.connect as cc
monkeypatch.setattr(
cc,
"duckdb",
lambda *a, **k: (_ for _ in ()).throw(FileNotFoundError("no replica")),
)
monkeypatch.setattr(
cc, "bib", lambda *a, **k: (_ for _ in ()).throw(FileNotFoundError("no bib"))
)
outputs, _ = mod.app.run()
rendered = "\n".join(o._repr_html_() for o in outputs if hasattr(o, "_repr_html_"))
assert "Not built yet" in rendered and "Traceback" not in rendered
def _rendered(outputs) -> str:
return "\n".join(o._repr_html_() for o in outputs if hasattr(o, "_repr_html_"))
def test_market_panel_headless_not_built(monkeypatch):
monkeypatch.setenv("STACK_DUCKDB_REPLICA", "1")
mod = _load()
import conf.connect as cc
monkeypatch.setattr(
cc,
"duckdb",
lambda *a, **k: (_ for _ in ()).throw(FileNotFoundError("no replica")),
)
monkeypatch.setattr(
cc, "bib", lambda *a, **k: (_ for _ in ()).throw(FileNotFoundError("no bib"))
)
outputs, _ = mod.app.run()
rendered = _rendered(outputs)
assert "stack market prices --write" in rendered
assert "Traceback" not in rendered
def test_market_panel_renders_from_memory_tables(monkeypatch):
from datetime import date
import duckdb
from market.tables import (
CarRow,
MonthlyRow,
ensure_tables,
write_cars,
write_series,
)
from pfs.codetables import ensure_tables as ensure_pfs
con = duckdb.connect(":memory:")
ensure_tables(con)
ensure_pfs(con)
con.execute(
"INSERT INTO pfs.code_exposure VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
[
"99490",
"CCM",
date(2015, 1, 1),
2015,
"becomes-payable",
"",
"",
0,
0.0,
0.0,
"exp-key",
1,
0,
"fr",
False,
"",
],
)
write_series(
con,
"tiingo:XLV:tri",
[
MonthlyRow("tiingo:XLV:tri", "XLV", "tiingo", y, 12, v, "xlv-bib")
for y, v in ((2013, 100.0), (2014, 110.0), (2015, 120.0), (2016, 130.0))
],
)
write_cars(
con,
"family",
"CCM",
"market",
[
CarRow(
"rule-final:2015",
"family",
"CCM",
"portfolio:family:CCM",
"",
"market",
-1,
1,
0.02,
None,
None,
None,
10,
3,
0,
'["association, not a causal estimate"]',
date(2015, 12, 31),
)
],
)
import conf.connect as cc
monkeypatch.setattr(cc, "duckdb", lambda *a, **k: con)
mod = _load()
outputs, _ = mod.app.run()
con.close()
rendered = _rendered(outputs)
assert "Traceback" not in rendered
assert "XLV" in rendered
assert "caveat" in rendered
assert "xlv-bib" in rendered
@_live
def test_market_panel_live_ccm_2015_apcm_2025():
mod = _load()
outputs, _ = mod.app.run()
rendered = _rendered(outputs)
assert "Traceback" not in rendered
assert "Market series (#797)" in rendered
assert "XLV" in rendered
assert "no control set" in rendered
assert "Provenance:" in rendered
assert "caveat" in rendered
assert "stack market prices --write" not in rendered
assert "99490 becomes-payable 2015" in rendered and "DE2VH9PD" in rendered
assert "G0556 becomes-payable 2025" in rendered and "JJ6AM5HJ" in rendered
@_live
def test_ccm_2015_and_apcm_2025_panels_render_with_anchors():
mod = _load()
outputs, _ = mod.app.run()
rendered = "\n".join(o._repr_html_() for o in outputs if hasattr(o, "_repr_html_"))
assert "Traceback" not in rendered
# section 4 lists every fixture with its anchor: CCM 2015 (DE2VH9PD) and APCM 2025 (JJ6AM5HJ)
assert "99490 becomes-payable 2015" in rendered and "DE2VH9PD" in rendered
assert "G0556 becomes-payable 2025" in rendered and "JJ6AM5HJ" in rendered
assert "no post-period on file yet" in rendered