diff --git a/tests/data/test_index_downloader.py b/tests/data/test_index_downloader.py index 0888f2a..e724ffa 100644 --- a/tests/data/test_index_downloader.py +++ b/tests/data/test_index_downloader.py @@ -131,67 +131,55 @@ def test_download_index_clears_proxy(tmp_path): assert "https_proxy" not in os.environ -def test_read_index_daily_reads_parquet(tmp_path): - """Test that read_index_daily reads index parquet files correctly.""" - # Import after implementation +def test_read_index_daily_reads_from_vnpy_db(tmp_path): + """read_index_daily 经 vnpy get_database.load_bar_data 读指数日线,返回 DataFrame。 + + 注:read_index_daily 数据源是 vnpy DbBarData 表(非 parquet);D2 根治拟切 dbbardata, + 届时 mock 数据源随之更新。当前测 vnpy 路径(与 read_db_daily 同 mock 模式)。 + """ + from types import SimpleNamespace + from datetime import datetime from sanguo_data.datareader import read_index_daily - # Create test parquet file with same structure as stock data - year_dir = tmp_path / "2024" - year_dir.mkdir() - - df = pd.DataFrame({ - "date": ["2024-01-02", "2024-01-03", "2024-01-04"], - "open": [3495.0, 3505.0, 3515.0], - "high": [3505.0, 3515.0, 3525.0], - "low": [3490.0, 3500.0, 3510.0], - "close": [3500.0, 3510.0, 3520.0], - "volume": [100000, 120000, 110000], - }) - df.to_parquet(year_dir / "sh000300_daily.parquet") - cfg = DataConfig( - data_paths={"daily_dir": str(tmp_path)}, + data_paths={"vnpy_db": str(tmp_path / "q.db")}, data_sources={}, validation={}, performance={}, ) + # vnpy load_bar_data 返回类 BarData 对象(有 datetime/open_price/... 属性) + bars = [ + SimpleNamespace(datetime=datetime(2024, 1, 2), open_price=3495.0, + high_price=3505.0, low_price=3490.0, close_price=3500.0, volume=100000), + SimpleNamespace(datetime=datetime(2024, 1, 3), open_price=3505.0, + high_price=3515.0, low_price=3500.0, close_price=3510.0, volume=120000), + SimpleNamespace(datetime=datetime(2024, 1, 4), open_price=3515.0, + high_price=3525.0, low_price=3510.0, close_price=3520.0, volume=110000), + ] + mock_db = MagicMock() + mock_db.load_bar_data.return_value = bars + # read_index_daily 内 lazy import,patch 源模块属性(同 test_datareader) + with patch("vnpy.trader.database.get_database", return_value=mock_db): + result = read_index_daily("sh000300", date(2024, 1, 1), date(2024, 12, 31), cfg) - # Read index daily data - result = read_index_daily("sh000300", date(2024, 1, 1), date(2024, 12, 31), cfg) - - # Verify result assert len(result) == 3 assert "close" in result.columns assert result["close"].iloc[0] == 3500.0 -def test_read_index_daily_handles_date_range(tmp_path): - """Test that read_index_daily filters by date range correctly.""" - # Import after implementation +def test_read_index_daily_passes_date_range_to_vnpy(tmp_path): + """read_index_daily 把 start/end 传给 vnpy load_bar_data(日期过滤在 vnpy 层)。""" + from datetime import datetime from sanguo_data.datareader import read_index_daily - # Create test parquet file - year_dir = tmp_path / "2024" - year_dir.mkdir() - - df = pd.DataFrame({ - "date": ["2024-01-02", "2024-06-15", "2024-12-31"], - "open": [3495.0, 3600.0, 3700.0], - "high": [3505.0, 3610.0, 3710.0], - "low": [3490.0, 3590.0, 3690.0], - "close": [3500.0, 3605.0, 3705.0], - "volume": [100000, 120000, 110000], - }) - df.to_parquet(year_dir / "sh000300_daily.parquet") - cfg = DataConfig( - data_paths={"daily_dir": str(tmp_path)}, + data_paths={"vnpy_db": str(tmp_path / "q.db")}, data_sources={}, validation={}, performance={}, ) + mock_db = MagicMock() + mock_db.load_bar_data.return_value = [] + with patch("vnpy.trader.database.get_database", return_value=mock_db): + read_index_daily("sh000300", date(2024, 1, 1), date(2024, 6, 30), cfg) - # Read with narrowed date range - result = read_index_daily("sh000300", date(2024, 1, 1), date(2024, 6, 30), cfg) - - # Should only get first 2 rows - assert len(result) == 2 - assert result["close"].iloc[0] == 3500.0 - assert result["close"].iloc[1] == 3605.0 + # start=date→combine 00:00:00; end=date→combine datetime.max.time()(23:59:59.999999) + call = mock_db.load_bar_data.call_args + assert call.kwargs["start"] == datetime(2024, 1, 1, 0, 0, 0) + assert call.kwargs["end"] == datetime(2024, 6, 30, 23, 59, 59, 999999)