You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.5 KiB
Python
70 lines
2.5 KiB
Python
from __future__ import annotations
|
|
|
|
import datetime as dt
|
|
|
|
from apps.tools import evaluate_book_overlay as mod
|
|
|
|
|
|
class _FakeStore:
|
|
def __init__(self) -> None:
|
|
self._dates = [dt.date(2025, 1, 2), dt.date(2025, 1, 3)]
|
|
|
|
def slice_by_date_range(self, start: dt.date, end: dt.date):
|
|
self._dates = [d for d in self._dates if start <= d <= end]
|
|
return self
|
|
|
|
def all_trading_days(self):
|
|
return list(self._dates)
|
|
|
|
def get_macro_for_date(self, date: dt.date):
|
|
return {"state": f"regime-{date.isoformat()}"}
|
|
|
|
|
|
def test_compute_regimes_uses_merged_store(monkeypatch) -> None:
|
|
fake_store = _FakeStore()
|
|
|
|
monkeypatch.setattr(mod, "load_manifest", lambda path: object())
|
|
monkeypatch.setattr(mod, "resolve_config", lambda manifest, config_root=".": object())
|
|
monkeypatch.setattr(mod, "_build_merged_snapshot_store", lambda manifest, config, snapshot_dir_override=None: fake_store)
|
|
monkeypatch.setattr(mod, "_macro_regime_state", lambda config, macro: macro["state"])
|
|
|
|
regimes = mod._compute_regimes(
|
|
snapshot_dir="data/parquet/example_snapshot",
|
|
split="train",
|
|
config_path="configs/experiments/example.json",
|
|
start_date=dt.date(2025, 1, 2),
|
|
end_date=dt.date(2025, 1, 3),
|
|
)
|
|
|
|
assert regimes == {
|
|
dt.date(2025, 1, 2): "regime-2025-01-02",
|
|
dt.date(2025, 1, 3): "regime-2025-01-03",
|
|
}
|
|
|
|
|
|
def test_compute_regimes_prefers_explicit_snapshot_dir(monkeypatch, tmp_path) -> None:
|
|
snapshot_dir = tmp_path / "snap"
|
|
snapshot_dir.mkdir()
|
|
(snapshot_dir / "train.parquet").write_text("stub")
|
|
|
|
fake_store = _FakeStore()
|
|
|
|
monkeypatch.setattr(mod, "load_manifest", lambda path: object())
|
|
monkeypatch.setattr(mod, "resolve_config", lambda manifest, config_root=".": object())
|
|
monkeypatch.setattr(mod, "load_merged_store_from_snapshot_dir", lambda snapshot_dir, oracle_url, db_dsn: fake_store)
|
|
monkeypatch.setattr(mod, "_build_merged_snapshot_store", lambda *args, **kwargs: (_ for _ in ()).throw(AssertionError("should not be called")))
|
|
monkeypatch.setattr(mod, "_macro_regime_state", lambda config, macro: macro["state"])
|
|
|
|
regimes = mod._compute_regimes(
|
|
snapshot_dir=snapshot_dir,
|
|
split="train",
|
|
config_path="configs/experiments/example.json",
|
|
start_date=dt.date(2025, 1, 2),
|
|
end_date=dt.date(2025, 1, 3),
|
|
)
|
|
|
|
assert regimes == {
|
|
dt.date(2025, 1, 2): "regime-2025-01-02",
|
|
dt.date(2025, 1, 3): "regime-2025-01-03",
|
|
}
|