feat(backend): Phase 1 数据层 — Domain / Provider / Failover 审计 + 持久化 + 同步 CLI

- domain:市场数据实体(Stock / 交易日历 / 日线 / 复权 / 财务含 announce_date)+ Repository 与 MarketDataProvider Protocol
- 数据源:TushareProvider(归一化、重试、鉴权错误归类)、SinaProvider(备用,明确前复权口径与能力边界)、FailoverProvider + SyncLog 审计(禁止静默切换)
- 持久化:SQLAlchemy 2.x Models + Repository 实现(按业务键幂等 upsert、as_of_date 防未来函数过滤)+ Alembic 迁移
- CLI:uv run python -m app.cli.sync {basic|calendar|daily|financial|verify},支持 --resume 断点续传
- 真实 Tushare 验证:stock 5556 / 交易日历 366 / daily+factor 242 / 财务 55;sync_log 审计完整
- 测试:38 passed(domain / provider / failover / repository / 未来函数 / 迁移),ruff clean
This commit is contained in:
Simon
2026-09-06 16:59:28 +08:00
parent 7a89d97c0b
commit 2da234220a
23 changed files with 2666 additions and 1 deletions
+62
View File
@@ -0,0 +1,62 @@
"""Alembic 迁移测试:全新数据库 upgrade head 后应包含全部 Phase 1 表。"""
from __future__ import annotations
import sqlite3
from pathlib import Path
from alembic import command
from alembic.config import Config
BACKEND_ROOT = Path(__file__).resolve().parents[1]
def _alembic_config(db_path: Path) -> Config:
cfg = Config(str(BACKEND_ROOT / "alembic.ini"))
cfg.set_main_option("script_location", "app/infrastructure/persistence/migrations")
cfg.set_main_option("sqlalchemy.url", f"sqlite:///{db_path}")
return cfg
def test_upgrade_head_creates_phase1_tables(tmp_path) -> None:
db_path = tmp_path / "fresh.db"
command.upgrade(_alembic_config(db_path), "head")
con = sqlite3.connect(db_path)
try:
tables = {
row[0]
for row in con.execute("select name from sqlite_master where type='table'").fetchall()
}
finally:
con.close()
expected = {
"stock",
"stock_daily",
"adjust_factor",
"trading_calendar",
"financial_indicator",
"sync_log",
"alembic_version",
}
assert expected <= tables
# 关键防未来函数列存在
con = sqlite3.connect(db_path)
try:
fin_cols = {
row[1] for row in con.execute("pragma table_info(financial_indicator)").fetchall()
}
daily_cols = {row[1] for row in con.execute("pragma table_info(stock_daily)").fetchall()}
finally:
con.close()
assert {"report_date", "announce_date"} <= fin_cols
assert {"symbol", "trade_date", "close"} <= daily_cols
def test_upgrade_head_idempotent(tmp_path) -> None:
db_path = tmp_path / "again.db"
cfg = _alembic_config(db_path)
command.upgrade(cfg, "head")
command.upgrade(cfg, "head") # 二次执行不报错