Files
pyquantdata/tests/test_config.py
T
openbot d37c697bbe M1 最小闭环:CLI + FastAPI 网关 + SQL 闸 + 100% 覆盖率验收链
- 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 已核验
2026-09-24 09:06:47 +08:00

94 lines
2.6 KiB
Python

"""config.py:默认生成、roundtrip、extra=forbid、${ENV} 展开。"""
from __future__ import annotations
from datetime import datetime, timezone
import pytest
from pydantic import ValidationError
from pyquantdata.config import (
AppConfig,
default_config,
expand_env,
load_config,
write_config,
)
def test_default_config_three_markets():
cfg = default_config()
assert set(cfg.markets) == {"cn", "us", "hk"}
assert cfg.markets["cn"].history_from == "2004-01-01"
assert cfg.markets["us"].history_from == "2020-01-01"
assert cfg.markets["hk"].history_from_bars_1d is None # 跟随 history_from
assert cfg.http.port == 8765
assert cfg.schedule.timezone == "Asia/Shanghai"
assert cfg.sql.max_concurrent == 4
assert cfg.export.formats == ["csv", "parquet", "qlib", "backtrader"]
def test_default_config_subset_markets():
cfg = default_config(["cn"])
assert set(cfg.markets) == {"cn"}
def test_roundtrip(tmp_path):
cfg = default_config()
path = tmp_path / "config.json"
write_config(cfg, path)
loaded = load_config(path)
assert loaded == cfg
text = path.read_text(encoding="utf-8")
assert '"version": 1' in text
def test_extra_field_forbidden(tmp_path):
cfg = default_config()
import json
raw = json.loads(cfg.model_dump_json(by_alias=True))
raw["hhtp"] = {"port": 1} # 拼错字段必须报错而不是静默失效
bad = tmp_path / "bad.json"
bad.write_text(json.dumps(raw), encoding="utf-8")
with pytest.raises(ValidationError):
load_config(bad)
def test_expand_env_token(monkeypatch):
monkeypatch.setenv("QD_TOKEN", "s3cret")
assert expand_env("${QD_TOKEN}") == "s3cret"
def test_expand_env_missing_var():
assert expand_env("${QD_NOT_SET_XYZ}") == ""
def test_expand_env_plain_string():
assert expand_env("no-var-here") == "no-var-here"
def test_load_applies_env_expansion(tmp_path, monkeypatch):
monkeypatch.setenv("QD_TOKEN", "abcd1234")
cfg = default_config()
cfg.http.token = "${QD_TOKEN}"
path = tmp_path / "config.json"
write_config(cfg, path)
loaded = load_config(path)
assert loaded.http.token == "abcd1234"
def test_now_override_is_used():
now = datetime(2026, 1, 2, 3, 4, 5, tzinfo=timezone.utc)
cfg = default_config(now=now)
assert cfg.dbpath_created.startswith("2026-01-02T03:04:05")
def test_app_config_forbids_unknown_section():
import json
raw = json.loads(default_config().model_dump_json(by_alias=True))
raw["mystery"] = 1
with pytest.raises(ValidationError):
AppConfig.model_validate(raw)