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
+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
)