feat(selection): 补充策略执行结果查询链路
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""PostgreSQL reader contract tests using a fake connection."""
|
||||
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
from typing import cast
|
||||
|
||||
import psycopg
|
||||
@@ -8,6 +9,7 @@ import pytest
|
||||
|
||||
from zhixing_server.modules.selection.infrastructure.postgres_reader import (
|
||||
PostgresMarketDataReader,
|
||||
SelectionMarketDataNotReady,
|
||||
)
|
||||
|
||||
|
||||
@@ -82,3 +84,77 @@ def test_reader_parameterizes_target_and_maps_left_join(monkeypatch: pytest.Monk
|
||||
assert connection.parameters == ("000001.SZ", date(2024, 1, 3))
|
||||
assert "source_adj = 'qfq'" in cast(str, connection.query)
|
||||
assert "trade_date <= %s" in cast(str, connection.query)
|
||||
|
||||
|
||||
class SourceConnection:
|
||||
def __init__(self, source_row: tuple[object, ...] | None) -> None:
|
||||
self.source_row = source_row
|
||||
self.queries: list[tuple[str, tuple[object, ...]]] = []
|
||||
|
||||
def __enter__(self) -> "SourceConnection":
|
||||
return self
|
||||
|
||||
def __exit__(self, *args: object) -> None:
|
||||
return None
|
||||
|
||||
def execute(self, query: str, parameters: tuple[object, ...]) -> "SourceResult":
|
||||
self.queries.append((query, parameters))
|
||||
if "FROM market_sync_batch" in query:
|
||||
return SourceResult(row=self.source_row)
|
||||
return SourceResult(rows=[("000001.SZ", "平安银行")])
|
||||
|
||||
|
||||
class SourceResult:
|
||||
def __init__(
|
||||
self,
|
||||
row: tuple[object, ...] | None = None,
|
||||
rows: list[tuple[object, ...]] | None = None,
|
||||
) -> None:
|
||||
self.row = row
|
||||
self.rows = rows or []
|
||||
|
||||
def fetchone(self) -> tuple[object, ...] | None:
|
||||
return self.row
|
||||
|
||||
def fetchall(self) -> list[tuple[object, ...]]:
|
||||
return self.rows
|
||||
|
||||
|
||||
def test_reader_loads_only_eligible_stocks_from_finished_market_batch(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
connection = SourceConnection(("market-run-1", 2, 2, Decimal("1")))
|
||||
|
||||
def connect(database_url: str) -> SourceConnection:
|
||||
assert database_url == "postgresql://test"
|
||||
return connection
|
||||
|
||||
monkeypatch.setattr(psycopg, "connect", connect)
|
||||
|
||||
source = PostgresMarketDataReader("postgresql://test").load_execution_source(
|
||||
"zhixing_b1",
|
||||
date(2026, 8, 8),
|
||||
)
|
||||
|
||||
assert source.market_sync_batch_id == "market-run-1"
|
||||
assert source.target_count == 2
|
||||
assert source.coverage == Decimal("1")
|
||||
assert source.stocks[0].ts_code == "000001.SZ"
|
||||
assert "strategy_eligible = true" in connection.queries[0][0]
|
||||
assert "source_adj = 'qfq'" in connection.queries[1][0]
|
||||
|
||||
|
||||
def test_reader_rejects_date_without_eligible_market_batch(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
connection = SourceConnection(None)
|
||||
|
||||
def connect(database_url: str) -> SourceConnection:
|
||||
assert database_url == "postgresql://test"
|
||||
return connection
|
||||
|
||||
monkeypatch.setattr(psycopg, "connect", connect)
|
||||
|
||||
with pytest.raises(SelectionMarketDataNotReady):
|
||||
PostgresMarketDataReader("postgresql://test").load_execution_source(
|
||||
"zhixing_b1",
|
||||
date(2026, 8, 8),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user