- config.yaml 新增 agent.llm:base_url / model 明文配置(默认百炼兼容端点 qwen-plus),api_key 只从 .env 的 LLM_API_KEY 读取;LLM_BASE_URL / LLM_MODEL 仍可经 .env 覆盖 - .env.example 注释同步;core.config 读取逻辑 env > yaml > 默认 - docs/USAGE.md:安装/配置/数据同步/启动/研究 API 与 Job/Experiment/Agent/测试门禁/已知限制/FAQ - README 增加使用说明入口 - 新增 config LLM 测试 2 项(yaml 默认 + env 覆盖)→ pytest 81 passed / ruff clean
71 lines
2.6 KiB
Python
71 lines
2.6 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"
|
|
|
|
|
|
def test_llm_from_config_yaml() -> None:
|
|
"""URL 与模型名来自 config.yaml 明文,Key 仅来自环境(当前未设置)。"""
|
|
settings = get_settings()
|
|
assert settings.llm_model == "qwen-plus"
|
|
assert settings.llm_base_url == "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
|
assert settings.llm_api_key == "" # .env 未提供 → 空,Agent 层给出引导提示
|
|
|
|
|
|
def test_llm_env_overrides_yaml(monkeypatch) -> None:
|
|
from app.core.config import get_settings
|
|
|
|
monkeypatch.setenv("LLM_MODEL", "env-model")
|
|
monkeypatch.setenv("LLM_BASE_URL", "https://example.com/v1")
|
|
monkeypatch.setenv("LLM_API_KEY", "sk-test")
|
|
get_settings.cache_clear() # Settings 有 lru_cache,刷新以读取新环境变量
|
|
s = get_settings()
|
|
assert s.llm_model == "env-model"
|
|
assert s.llm_base_url == "https://example.com/v1"
|
|
assert s.llm_api_key == "sk-test"
|
|
get_settings.cache_clear()
|