Fix backtest benchmark read after optional table probe
Deploy / lint (push) Successful in 17s
Deploy / test (push) Successful in 2m34s
Deploy / deploy (push) Successful in 53s

This commit is contained in:
dennisthiessen committed 2026-09-25 13:17:51 +02:00
1 parent 9846977d00
commit 9c48eb20c5
2 files changed
+32 -3

No files matched your search

+26
View File
@@ -9,9 +9,11 @@ from types import SimpleNamespace
import pytest
from app.models.benchmark_price import BenchmarkPrice
from app.models.ohlcv import OHLCVRecord
from app.models.ticker import Ticker
from app.services import backtest_service as bt
from app.services.benchmark_service import load_benchmark_closes
from app.services.outcome_service import (
OUTCOME_EXPIRED,
OUTCOME_STOP_HIT,
@@ -1600,6 +1602,30 @@ def _pin_benchmark(monkeypatch, closes: dict | None = None) -> None:
monkeypatch.setattr(bt, "_load_benchmark_closes_for_backtest", _stub)
async def test_missing_research_table_does_not_abort_benchmark_read(session, monkeypatch):
"""The optional research table is absent online; its failed probe must be
isolated so PostgreSQL can still read the SPY prices in the same session.
"""
close_date = date(2025, 1, 2)
session.add(BenchmarkPrice(symbol="SPY", date=close_date, close=470.0))
await session.commit()
real_execute = session.execute
probed_in_savepoint = False
async def track_probe(statement, *args, **kwargs):
nonlocal probed_in_savepoint
if str(statement) == "SELECT symbol FROM research_rank_only":
probed_in_savepoint = session.in_nested_transaction()
return await real_execute(statement, *args, **kwargs)
monkeypatch.setattr(session, "execute", track_probe)
assert await bt._load_research_rank_only_symbols(session) == set()
assert probed_in_savepoint
assert await load_benchmark_closes(session) == {close_date: 470.0}
async def _seed_oscillating_ticker(session, symbol: str, n: int = 160) -> None:
t = Ticker(symbol=symbol)
session.add(t)