146 lines
5.0 KiB
Python
146 lines
5.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
测试配置管理器和pydantic模型的兼容性
|
|
"""
|
|
|
|
import os
|
|
import json
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
from core.config.manager import ConfigManager
|
|
from core.config.models import (
|
|
BaseConfig, AIModelConfig, SystemConfig, GenerateTopicConfig, ResourceConfig,
|
|
GenerateContentConfig, PosterConfig, ContentConfig, ReferItem, ReferConfig
|
|
)
|
|
|
|
def test_config_creation():
|
|
"""测试创建配置实例"""
|
|
print("测试创建配置实例...")
|
|
|
|
# 创建各种配置实例
|
|
ai_config = AIModelConfig(model="test-model", api_key="test-key")
|
|
print(f"AI配置: {ai_config.model}, {ai_config.api_key}")
|
|
|
|
# 测试嵌套配置
|
|
resource_config = ResourceConfig(
|
|
resource_dirs=["resource"],
|
|
refer=ReferConfig(
|
|
refer_list=[
|
|
ReferItem(path="path1", sampling_rate=0.5, step="topic"),
|
|
ReferItem(path="path2", sampling_rate=0.8, step="content")
|
|
]
|
|
)
|
|
)
|
|
|
|
# 验证嵌套配置
|
|
print(f"资源目录: {resource_config.resource_dirs}")
|
|
print(f"Refer项数量: {len(resource_config.refer.refer_list)}")
|
|
print(f"第一个Refer项: {resource_config.refer.refer_list[0].path}, {resource_config.refer.refer_list[0].sampling_rate}")
|
|
|
|
# 测试序列化
|
|
config_dict = resource_config.to_dict()
|
|
print(f"序列化后的字典: {json.dumps(config_dict, indent=2)}")
|
|
|
|
return True
|
|
|
|
def test_config_manager():
|
|
"""测试配置管理器"""
|
|
print("\n测试配置管理器...")
|
|
|
|
# 创建临时目录
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
# 创建测试配置文件
|
|
ai_config_path = Path(temp_dir) / "ai_model.json"
|
|
ai_config = {
|
|
"model": "test-model",
|
|
"api_key": "test-key",
|
|
"temperature": 0.8
|
|
}
|
|
|
|
with open(ai_config_path, "w") as f:
|
|
json.dump(ai_config, f)
|
|
|
|
# 创建嵌套配置文件
|
|
resource_config_path = Path(temp_dir) / "resource.json"
|
|
resource_config = {
|
|
"resource_dirs": ["test_resource"],
|
|
"refer": {
|
|
"refer_list": [
|
|
{"path": "test_path", "sampling_rate": 0.7, "step": "judge"}
|
|
]
|
|
}
|
|
}
|
|
|
|
with open(resource_config_path, "w") as f:
|
|
json.dump(resource_config, f)
|
|
|
|
# 初始化配置管理器
|
|
config_manager = ConfigManager()
|
|
config_manager.load_from_directory(temp_dir)
|
|
|
|
# 获取并验证AI配置
|
|
ai_model_config = config_manager.get_config("ai_model", AIModelConfig)
|
|
print(f"加载的AI配置: {ai_model_config.model}, {ai_model_config.api_key}, {ai_model_config.temperature}")
|
|
assert ai_model_config.model == "test-model"
|
|
assert ai_model_config.api_key == "test-key"
|
|
assert ai_model_config.temperature == 0.8
|
|
|
|
# 获取并验证资源配置
|
|
resource_config = config_manager.get_config("resource", ResourceConfig)
|
|
print(f"加载的资源配置: {resource_config.resource_dirs}")
|
|
print(f"加载的Refer项: {resource_config.refer.refer_list[0].path}, {resource_config.refer.refer_list[0].sampling_rate}")
|
|
assert resource_config.resource_dirs == ["test_resource"]
|
|
assert len(resource_config.refer.refer_list) == 1
|
|
assert resource_config.refer.refer_list[0].path == "test_path"
|
|
assert resource_config.refer.refer_list[0].sampling_rate == 0.7
|
|
|
|
# 测试更新配置
|
|
ai_model_config.update({"model": "updated-model"})
|
|
print(f"更新后的AI配置: {ai_model_config.model}")
|
|
assert ai_model_config.model == "updated-model"
|
|
|
|
# 测试保存配置
|
|
config_manager.save_config("ai_model")
|
|
|
|
# 重新加载并验证
|
|
with open(ai_config_path, "r") as f:
|
|
saved_config = json.load(f)
|
|
print(f"保存的配置: {saved_config['model']}")
|
|
assert saved_config["model"] == "updated-model"
|
|
|
|
return True
|
|
|
|
def test_config_type_conversion():
|
|
"""测试配置类型转换"""
|
|
print("\n测试配置类型转换...")
|
|
|
|
# 创建配置管理器
|
|
config_manager = ConfigManager()
|
|
|
|
# 注册一个SystemConfig
|
|
config_manager.register_config("test_config", SystemConfig)
|
|
|
|
# 尝试以不同类型获取
|
|
system_config = config_manager.get_config("test_config", SystemConfig)
|
|
print(f"获取为SystemConfig: {type(system_config).__name__}")
|
|
|
|
# 尝试转换类型
|
|
try:
|
|
content_config = config_manager.get_config("test_config", ContentConfig)
|
|
print(f"成功转换为ContentConfig: {type(content_config).__name__}")
|
|
except TypeError as e:
|
|
print(f"类型转换失败,符合预期: {e}")
|
|
|
|
return True
|
|
|
|
if __name__ == "__main__":
|
|
print("开始测试pydantic配置模型和ConfigManager...")
|
|
|
|
test_config_creation()
|
|
test_config_manager()
|
|
test_config_type_conversion()
|
|
|
|
print("\n所有测试完成!") |