- uv 工程(Python 3.12,pyproject + uv.lock,dev: pytest/ruff) - 分层包结构:api / application / domain / infrastructure / quant(qlib_adapter) / agent / core - app.core.config:加载根 config.yaml + .env,sqlite 相对路径解析到项目根 data/ - SQLAlchemy Base + Session 工厂(Infrastructure 层,业务走 Repository) - Alembic 迁移环境就位(env.py 复用应用配置,render_as_batch 兼容 SQLite) - GET /api/health + pytest 冒烟 6 项通过 / ruff clean / uvicorn 启动验证
49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
"""配置层测试:默认 SQLite 相对路径解析、config.yaml / .env 加载约定。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from app.core.config import PROJECT_ROOT, _load_env_file, get_settings
|
|
|
|
|
|
def test_project_root_points_to_repo_root() -> None:
|
|
# config.py 位于 <root>/backend/app/core/ → 项目根应含 config.yaml 与 backend/
|
|
assert (PROJECT_ROOT / "config.yaml").exists()
|
|
assert (PROJECT_ROOT / "backend").is_dir()
|
|
|
|
|
|
def test_default_database_url_resolves_to_project_data_dir() -> None:
|
|
settings = get_settings()
|
|
assert settings.database_url.startswith("sqlite:///")
|
|
# 相对路径应解析到 <项目根>/data/quant.db
|
|
assert settings.database_url.endswith("/data/quant.db")
|
|
db_path = Path(settings.database_url.removeprefix("sqlite:///"))
|
|
assert db_path.is_absolute()
|
|
assert db_path == PROJECT_ROOT / "data" / "quant.db"
|
|
|
|
|
|
def test_settings_loaded_from_config_yaml() -> None:
|
|
settings = get_settings()
|
|
assert settings.app_name == "qlib-platform"
|
|
assert settings.api_prefix == "/api"
|
|
assert settings.data_source_primary == "tushare"
|
|
assert settings.data_source_fallback == "sina"
|
|
|
|
|
|
def test_env_file_loading(tmp_path: Path, monkeypatch) -> None:
|
|
monkeypatch.delenv("TUSHARE_TOKEN", raising=False)
|
|
env_file = tmp_path / ".env"
|
|
env_file.write_text(
|
|
"# comment\nTUSHARE_TOKEN=demo-token\nDATABASE_URL=sqlite:////tmp/x.db\n",
|
|
encoding="utf-8",
|
|
)
|
|
_load_env_file(env_file)
|
|
assert os.environ.get("TUSHARE_TOKEN") == "demo-token"
|
|
|
|
# 不覆盖已存在的环境变量(shell 环境优先)
|
|
monkeypatch.setenv("TUSHARE_TOKEN", "kept")
|
|
_load_env_file(env_file)
|
|
assert os.environ["TUSHARE_TOKEN"] == "kept"
|