76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
"""配置加载测试"""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
|
|
class TestConfig:
|
|
"""配置加载测试"""
|
|
|
|
def test_all_required_env_vars_are_documented(self):
|
|
"""所有必需的环境变量都应在.env.example中"""
|
|
# 读取.env.example
|
|
env_example_path = Path(__file__).parent.parent / ".env.example"
|
|
assert env_example_path.exists(), ".env.example文件不存在"
|
|
|
|
content = env_example_path.read_text()
|
|
|
|
# 检查以下变量是否存在
|
|
required_vars = [
|
|
"MOONSHOT_API_KEY", # Kimi
|
|
"BAIDU_QIANFAN_API_KEY", # 文心
|
|
"DOUBAO_API_KEY", # 豆包
|
|
"DEEPSEEK_API_KEY",
|
|
"OPENAI_API_KEY",
|
|
"REDIS_URL",
|
|
"DATABASE_URL",
|
|
"JWT_SECRET",
|
|
]
|
|
|
|
for var in required_vars:
|
|
assert var in content, f".env.example中缺少必需的环境变量: {var}"
|
|
|
|
def test_env_var_loading_without_errors(self):
|
|
"""环境变量加载应无错误"""
|
|
# 设置测试环境变量
|
|
os.environ["JWT_SECRET"] = "test-secret-key-that-is-long-enough-for-validation"
|
|
os.environ["ENABLE_LLM"] = "false"
|
|
|
|
# 重新加载配置模块
|
|
import importlib
|
|
import app.config as config_module
|
|
importlib.reload(config_module)
|
|
|
|
from app.config import settings
|
|
|
|
assert settings is not None
|
|
assert settings.JWT_SECRET == "test-secret-key-that-is-long-enough-for-validation"
|
|
|
|
def test_llm_providers_have_factory(self):
|
|
"""LLM提供商应有工厂函数"""
|
|
# 设置测试用 API keys
|
|
os.environ["OPENAI_API_KEY"] = "test-openai-key"
|
|
os.environ["DEEPSEEK_API_KEY"] = "test-deepseek-key"
|
|
|
|
from app.services.llm.factory import LLMFactory
|
|
|
|
# 验证已注册的提供商
|
|
providers = ["openai", "deepseek"]
|
|
registered = LLMFactory.list_providers()
|
|
for p in providers:
|
|
assert p in registered, f"Provider {p} 未注册"
|
|
|
|
# 验证能创建提供商实例
|
|
for p in providers:
|
|
provider = LLMFactory.create(provider=p)
|
|
assert provider is not None
|
|
assert provider.provider_name == p
|
|
|
|
def test_rate_limiter_config_exists(self):
|
|
"""限流器配置应存在"""
|
|
from app.services.llm.rate_limiter import TokenBucketRateLimiter
|
|
|
|
limiter = TokenBucketRateLimiter(max_rpm=30.0)
|
|
assert limiter is not None
|
|
assert limiter.max_rpm == 30.0
|