- CLI:init/serve/update/query/export/status(typer + rich),单写者锁三元判活 - 存储:DuckDB schema migrations(14 表 + 8 联邦视图,语句元组逐条执行) - ETL:A股证券/日历/日K,整区替换幂等;日历全年补全(calendar 语义唯一) - 网关:POST /v1/query(线程池 + interrupt 超时 504)、/v1/export、meta/stats/state、healthz/ready - 安全:SQL 闸 sqlglot fail-closed(文件/网络表函数默认拒)、非 loopback 空 token 拒启、导出路径双判 - 验收:254 测试全绿、1265 语句 100% 覆盖、[CI] 真起服验收链、smoke_m1.py 交付自检 - 文档:设计文档 v0.7(圆桌 45 条采纳 + 3 条实施校准),README 重写 - 发布:release.yml Trusted Publishing(OIDC 免密钥),action refs 已核验
105 lines
3.4 KiB
Python
105 lines
3.4 KiB
Python
"""migrations:版本管理、幂等、TooNew 拒启、视图列集。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from pyquantdata.store import (
|
|
KNOWN_MAX_VERSION,
|
|
MIGRATIONS,
|
|
SchemaTooNewError,
|
|
applied_versions,
|
|
current_version,
|
|
migrate,
|
|
)
|
|
|
|
BAR_COLUMNS = {
|
|
"market", "category", "symbol", "date", "open", "high", "low",
|
|
"close", "volume", "amount", "turnover", "adj_factor",
|
|
}
|
|
|
|
|
|
def test_fresh_migrate(catalog):
|
|
assert current_version(catalog) == KNOWN_MAX_VERSION
|
|
assert applied_versions(catalog) == [1]
|
|
|
|
|
|
def test_migrate_idempotent(catalog):
|
|
assert migrate(catalog) == KNOWN_MAX_VERSION
|
|
assert migrate(catalog) == KNOWN_MAX_VERSION
|
|
assert applied_versions(catalog) == [1]
|
|
|
|
|
|
def test_core_tables_exist(catalog):
|
|
rows = catalog.execute(
|
|
"SELECT table_name FROM information_schema.tables WHERE table_schema='main' "
|
|
"AND table_type='BASE TABLE' ORDER BY table_name"
|
|
).fetchall()
|
|
names = {r[0] for r in rows}
|
|
assert {
|
|
"exchanges", "securities", "calendar", "corp_actions", "sw_industry",
|
|
"stock_status", "futures_meta", "bars_1d", "announcements", "reports",
|
|
"news", "etl_checkpoint", "export_jobs", "quality_reports",
|
|
"schema_migrations",
|
|
} <= names
|
|
|
|
|
|
def test_day_views_exist_with_exact_columns(catalog):
|
|
from pyquantdata.store.views import DAY_VIEWS
|
|
|
|
for view, market, category in DAY_VIEWS:
|
|
cols = [
|
|
r[0]
|
|
for r in catalog.execute(
|
|
"SELECT column_name FROM information_schema.columns "
|
|
"WHERE table_schema='main' AND table_name=? ORDER BY ordinal_position",
|
|
[view],
|
|
).fetchall()
|
|
]
|
|
assert set(cols) == BAR_COLUMNS, view
|
|
# 列集恒等:三市场同品类视图列集一致(§3.3)
|
|
stock_views = [v for v, _, c in DAY_VIEWS if c == "stock"]
|
|
assert len(stock_views) >= 3 # cn/us/hk
|
|
|
|
|
|
def test_day_view_filters_market(catalog):
|
|
catalog.execute(
|
|
"INSERT INTO bars_1d VALUES ('cn','stock','sh600000',DATE '2026-09-01',"
|
|
"10,11,9,10.5,1000,10500,1.2,1.0)"
|
|
)
|
|
assert catalog.execute("SELECT count(*) FROM cn_stock_1d").fetchone()[0] == 1
|
|
assert catalog.execute("SELECT count(*) FROM us_stock_1d").fetchone()[0] == 0
|
|
|
|
|
|
def test_schema_too_new_rejected(catalog):
|
|
catalog.execute("INSERT INTO schema_migrations (version) VALUES (99)")
|
|
with pytest.raises(SchemaTooNewError):
|
|
migrate(catalog)
|
|
|
|
|
|
def test_migration_failure_rolls_back(catalog, monkeypatch):
|
|
"""迁移包事务:中途失败回滚,版本停在最后一个成功迁移(§11)。"""
|
|
import duckdb
|
|
|
|
from pyquantdata.store import migrations as m
|
|
|
|
broken = list(m.MIGRATIONS) + [
|
|
(2, "0002_bad", "CREATE TABLE oops (x INT); CREATE TABLE oops (x INT)")
|
|
]
|
|
monkeypatch.setattr(m, "MIGRATIONS", tuple(broken))
|
|
monkeypatch.setattr(m, "KNOWN_MAX_VERSION", 2)
|
|
conn2 = duckdb.connect(":memory:")
|
|
with pytest.raises(Exception):
|
|
m.migrate(conn2)
|
|
# migration 1 已提交、migration 2 回滚:无 oops 表
|
|
assert m.current_version(conn2) == 1
|
|
has_oops = conn2.execute(
|
|
"SELECT count(*) FROM information_schema.tables WHERE table_name='oops'"
|
|
).fetchone()[0]
|
|
assert has_oops == 0
|
|
conn2.close()
|
|
|
|
|
|
def test_known_max_version_matches_list():
|
|
assert KNOWN_MAX_VERSION == MIGRATIONS[-1][0]
|