ShopTRAINING/test/force_reload_test.py

83 lines
2.9 KiB
Python
Raw Permalink Normal View History

2025-07-02 11:05:23 +08:00
#!/usr/bin/env python3
"""
强制重新加载API模块并测试
"""
import sys
import os
import importlib
# 添加server目录到路径
server_path = os.path.join(os.path.dirname(__file__), 'server')
sys.path.insert(0, server_path)
def force_reload_test():
print("=== 强制重新加载测试 ===")
try:
# 清理模块缓存
modules_to_clear = []
for module_name in list(sys.modules.keys()):
if 'model_manager' in module_name or 'utils' in module_name:
modules_to_clear.append(module_name)
for module_name in modules_to_clear:
if module_name in sys.modules:
del sys.modules[module_name]
print(f"清理模块缓存: {module_name}")
# 重新导入
from utils.model_manager import ModelManager
# 测试ModelManager
manager = ModelManager()
models = manager.list_models()
print(f"\n重新加载后ModelManager找到 {len(models)} 个模型")
# 手动执行API端点逻辑
print("\n=== 手动模拟API端点逻辑 ===")
formatted_models = []
for model in models:
# 生成model_id的逻辑与API相同
model_id = model.get('filename', '').replace('.pth', '')
if not model_id:
# 备用方案
product_id = model.get('product_id', 'unknown')
model_type = model.get('model_type', 'unknown')
version = model.get('version', 'v1')
model_id = f"{model_type}_product_{product_id}_{version}"
formatted_model = {
'model_id': model_id,
'filename': model.get('filename', ''), # 关键字段
'product_id': model.get('product_id', ''),
'model_type': model.get('model_type', ''),
'version': model.get('version', 'v1'),
'metrics': model.get('metrics', {}),
}
formatted_models.append(formatted_model)
print(f"格式化后模型数量: {len(formatted_models)}")
for i, model in enumerate(formatted_models):
print(f"\n模型 {i+1}:")
print(f" filename: '{model.get('filename', 'MISSING')}'")
print(f" model_id: '{model.get('model_id', 'MISSING')}'")
print(f" product_id: '{model.get('product_id', 'MISSING')}'")
if model.get('filename') and model.get('model_id'):
print(" ✓ 该模型应该能正常显示详情和删除按钮")
else:
print(" ✗ 该模型有问题")
return formatted_models
except Exception as e:
print(f"测试失败: {e}")
import traceback
traceback.print_exc()
return []
if __name__ == "__main__":
force_reload_test()