fix(state): repository iv_rv_history time-stable + input validation

Risponde alla code review di 395191e:
- iv_rv_history accetta as_of (default now UTC) invece di
  affidarsi al clock SQLite, rendendo i test time-stable.
- Valida max_days > 0 e raise se as_of/reference sono naive.
- Aggiunge 3 test sulle nuove guard.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
root
2026-05-08 23:00:06 +00:00
parent 395191ea13
commit 8221aba10f
2 changed files with 72 additions and 7 deletions
+16 -3
View File
@@ -414,20 +414,31 @@ class Repository:
*,
asset: str,
max_days: int,
as_of: datetime | None = None,
) -> list[Decimal]:
"""Lista IV-RV ordinata ASC sull'intervallo `[now-max_days, now]`.
"""Lista IV-RV ordinata ASC sull'intervallo `[as_of - max_days, as_of]`.
Esclude righe con ``fetch_ok=0`` o ``iv_minus_rv IS NULL``.
Usata dal validator quando il gate adattivo è abilitato.
Args:
as_of: Reference time for the rolling window. Defaults to
``datetime.now(UTC)``. Tests can pin a fixed value.
"""
if max_days <= 0:
raise ValueError(f"max_days must be positive, got {max_days}")
ref = as_of if as_of is not None else datetime.now(UTC)
if ref.tzinfo is None:
raise ValueError("as_of must be timezone-aware")
cutoff = ref - timedelta(days=max_days)
rows = conn.execute(
"SELECT iv_minus_rv FROM market_snapshots "
"WHERE asset = ? "
" AND fetch_ok = 1 "
" AND iv_minus_rv IS NOT NULL "
" AND timestamp >= datetime('now', ?) "
" AND timestamp >= ? "
"ORDER BY timestamp ASC",
(asset, f"-{int(max_days)} days"),
(asset, _enc_dt(cutoff)),
).fetchall()
return [Decimal(str(r["iv_minus_rv"])) for r in rows]
@@ -445,6 +456,8 @@ class Repository:
``dvol IS NOT NULL``) entro la tolerance. Usato dal Vol-of-Vol
guard per stimare DVOL N ore fa.
"""
if reference.tzinfo is None:
raise ValueError("reference must be timezone-aware")
ref_lo = (reference - timedelta(minutes=tolerance_minutes)).astimezone(UTC)
ref_hi = (reference + timedelta(minutes=tolerance_minutes)).astimezone(UTC)
row = conn.execute(
+56 -4
View File
@@ -50,7 +50,12 @@ def db_with_history(tmp_path) -> sqlite3.Connection:
def test_iv_rv_history_returns_ordered_asc(db_with_history) -> None:
repo = Repository()
history = repo.iv_rv_history(db_with_history, asset="ETH", max_days=60)
history = repo.iv_rv_history(
db_with_history,
asset="ETH",
max_days=60,
as_of=datetime(2026, 5, 2, 0, 0, tzinfo=UTC),
)
assert len(history) == 96
assert history == sorted(history)
assert history[0] == Decimal("2.00")
@@ -58,7 +63,12 @@ def test_iv_rv_history_returns_ordered_asc(db_with_history) -> None:
def test_iv_rv_history_filters_other_asset(db_with_history) -> None:
repo = Repository()
history = repo.iv_rv_history(db_with_history, asset="BTC", max_days=60)
history = repo.iv_rv_history(
db_with_history,
asset="BTC",
max_days=60,
as_of=datetime(2026, 5, 2, 0, 0, tzinfo=UTC),
)
assert history == []
@@ -86,7 +96,12 @@ def test_iv_rv_history_skips_null_values(db_with_history) -> None:
),
)
db_with_history.commit()
history = repo.iv_rv_history(db_with_history, asset="ETH", max_days=60)
history = repo.iv_rv_history(
db_with_history,
asset="ETH",
max_days=60,
as_of=datetime(2026, 5, 3, 0, 0, tzinfo=UTC),
)
assert len(history) == 96
@@ -114,7 +129,12 @@ def test_iv_rv_history_skips_fetch_failed(db_with_history) -> None:
),
)
db_with_history.commit()
history = repo.iv_rv_history(db_with_history, asset="ETH", max_days=60)
history = repo.iv_rv_history(
db_with_history,
asset="ETH",
max_days=60,
as_of=datetime(2026, 5, 4, 0, 0, tzinfo=UTC),
)
assert Decimal("99") not in history
@@ -136,3 +156,35 @@ def test_dvol_lookback_returns_none_when_gap(db_with_history) -> None:
db_with_history, asset="ETH", reference=target, tolerance_minutes=15
)
assert out is None
def test_iv_rv_history_rejects_non_positive_max_days(db_with_history) -> None:
repo = Repository()
with pytest.raises(ValueError, match="max_days must be positive"):
repo.iv_rv_history(
db_with_history,
asset="ETH",
max_days=0,
as_of=datetime(2026, 5, 2, 0, 0, tzinfo=UTC),
)
def test_iv_rv_history_rejects_naive_as_of(db_with_history) -> None:
repo = Repository()
with pytest.raises(ValueError, match="timezone-aware"):
repo.iv_rv_history(
db_with_history,
asset="ETH",
max_days=60,
as_of=datetime(2026, 5, 2, 0, 0), # naive
)
def test_dvol_lookback_rejects_naive_reference(db_with_history) -> None:
repo = Repository()
with pytest.raises(ValueError, match="timezone-aware"):
repo.dvol_lookback(
db_with_history,
asset="ETH",
reference=datetime(2026, 5, 1, 12, 0), # naive
)