refactor(BT-001C): share historical fixture provider

This commit is contained in:
Bu5hm4nn
2026-03-27 21:41:50 +01:00
parent 477514f838
commit 554a41a060
8 changed files with 236 additions and 110 deletions

View File

@@ -0,0 +1,74 @@
from __future__ import annotations
from datetime import date
import pytest
from app.services.backtesting.fixture_source import (
SEEDED_GLD_2024_FIXTURE_HISTORY,
SharedHistoricalFixtureSource,
WindowPolicy,
)
from app.services.backtesting.ui_service import BacktestPageService
from app.services.event_comparison_ui import EventComparisonPageService
def test_shared_fixture_source_exact_window_requires_seeded_bounds() -> None:
source = SharedHistoricalFixtureSource(
feature_label="BT-001A",
supported_symbol="GLD",
history=SEEDED_GLD_2024_FIXTURE_HISTORY,
window_policy=WindowPolicy.EXACT,
)
rows = source.load_daily_closes("GLD", date(2024, 1, 2), date(2024, 1, 8))
assert [row.date.isoformat() for row in rows] == [
"2024-01-02",
"2024-01-03",
"2024-01-04",
"2024-01-05",
"2024-01-08",
]
with pytest.raises(ValueError, match="seeded 2024-01-02 through 2024-01-08 window"):
source.load_daily_closes("GLD", date(2024, 1, 3), date(2024, 1, 8))
def test_shared_fixture_source_bounded_window_allows_subranges_but_fails_closed_outside_seeded_bounds() -> None:
source = SharedHistoricalFixtureSource(
feature_label="BT-003A",
supported_symbol="GLD",
history=SEEDED_GLD_2024_FIXTURE_HISTORY,
window_policy=WindowPolicy.BOUNDED,
)
rows = source.load_daily_closes("GLD", date(2024, 1, 3), date(2024, 1, 5))
assert [row.date.isoformat() for row in rows] == ["2024-01-03", "2024-01-04", "2024-01-05"]
with pytest.raises(ValueError, match="seeded 2024-01-02 through 2024-01-08 window"):
source.load_daily_closes("GLD", date(2024, 1, 1), date(2024, 1, 8))
with pytest.raises(ValueError, match="start_date must be on or before end_date"):
source.load_daily_closes("GLD", date(2024, 1, 5), date(2024, 1, 3))
def test_backtest_page_service_uses_shared_exact_fixture_source() -> None:
service = BacktestPageService()
source = service.backtest_service.provider.source
assert isinstance(source, SharedHistoricalFixtureSource)
assert source.feature_label == "BT-001A"
assert source.window_policy is WindowPolicy.EXACT
def test_event_comparison_page_service_uses_shared_bounded_fixture_source() -> None:
service = EventComparisonPageService()
source = service.comparison_service.provider.source
assert isinstance(source, SharedHistoricalFixtureSource)
assert source.feature_label == "BT-003A"
assert source.window_policy is WindowPolicy.BOUNDED