Tortoise/tests/test_real_sources.py
2026-06-23 10:37:31 +08:00

68 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""真实数据源集成测试。"""
import os
from pathlib import Path
import pytest
from sidecar.config import SidecarConfig
from sidecar.service import UnifiedDataService
from sidecar.sources.mootdx_source import MootdxSource
from sidecar.sources.tencent import TencentFinanceSource
from sidecar.storage import DuckDBStore
pytestmark = pytest.mark.skipif(
os.getenv("RUN_REAL_MARKET_TESTS") != "1",
reason="真实网络测试需设置 RUN_REAL_MARKET_TESTS=1",
)
def test_3_1_real_tencent_snapshot() -> None:
"""
功能说明:验证腾讯财经真实快照接口可用。
参数说明:无。
返回值说明:无返回值。
注意事项:用例编号 3-1需要可访问 qt.gtimg.cn。
"""
snapshot = TencentFinanceSource(timeout_seconds=10).fetch_snapshot("600000")
assert snapshot.symbol == "sh600000"
assert snapshot.name
assert snapshot.price is not None
assert snapshot.source == "tencent"
def test_3_2_real_mootdx_kline() -> None:
"""
功能说明:验证 mootdx 真实 K 线接口可用。
参数说明:无。
返回值说明:无返回值。
注意事项:用例编号 3-2需要可连接通达信行情服务器。
"""
bars = MootdxSource().fetch_kline("600000", "day", 5)
assert len(bars) > 0
assert bars[-1].symbol == "sh600000"
assert bars[-1].close is not None
assert bars[-1].source == "mootdx"
def test_3_3_real_unified_service_cache(tmp_path: Path) -> None:
"""
功能说明:验证统一服务可使用真实数据源写入 DuckDB 缓存。
参数说明tmp_path 为 pytest 临时目录。
返回值说明:无返回值。
注意事项:用例编号 3-3同时覆盖腾讯快照、mootdx K 线和 DuckDB 往返。
"""
config = SidecarConfig(db_path=str(tmp_path / "real.duckdb"), snapshot_ttl_seconds=60)
store = DuckDBStore(config.db_path)
mootdx = MootdxSource()
service = UnifiedDataService(store, config, mootdx, [TencentFinanceSource(10), mootdx])
bars = service.get_kline("600000", limit=5, refresh=True)
snapshot = service.get_snapshot("600000", refresh=True)
assert len(bars) > 0
assert store.load_kline("sh600000", "day", 5)
assert snapshot.price is not None
assert store.load_snapshot("sh600000") is not None