ShopTRAINING/test/test_complete_fixes.py
2025-07-02 11:05:23 +08:00

98 lines
4.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
测试所有模型管理修复
"""
import urllib.request
import json
def test_complete_fixes():
try:
print("=== 综合测试模型管理修复 ===")
# 1. 测试基本模型列表
print("\n1. 测试基本模型列表:")
url = 'http://localhost:5000/api/models'
with urllib.request.urlopen(url) as response:
data = response.read().decode('utf-8')
result = json.loads(data)
print(f"状态码: {response.status}")
models = result.get('data', [])
pagination = result.get('pagination', {})
print(f"模型数量: {len(models)}")
print(f"总数: {pagination.get('total', 0)}")
# 检查每个模型的关键字段
for i, model in enumerate(models):
print(f"\n模型 {i+1}:")
model_id = model.get('model_id', '')
filename = model.get('filename', '')
metrics = model.get('metrics', {})
print(f" model_id: '{model_id}' {'' if model_id else ''}")
print(f" filename: '{filename}' {'' if filename else ''}")
print(f" product_id: '{model.get('product_id', '')}'")
print(f" model_type: '{model.get('model_type', '')}'")
print(f" metrics: {len(metrics)} 个指标 {'' if metrics else ''}")
if metrics:
print(f" - R2: {metrics.get('R2', 'N/A')}")
print(f" - RMSE: {metrics.get('RMSE', 'N/A')}")
print(f" - MAE: {metrics.get('MAE', 'N/A')}")
# 模型能否删除基于model_id
if model_id:
print(f" 删除URL: /api/models/{model_id}")
else:
print(f" 删除URL: 无法构造 ✗")
# 2. 测试分页功能
print("\n\n2. 测试分页功能:")
url = 'http://localhost:5000/api/models?page=1&page_size=2'
with urllib.request.urlopen(url) as response:
data = response.read().decode('utf-8')
result = json.loads(data)
models = result.get('data', [])
pagination = result.get('pagination', {})
print(f"请求第1页每页2条")
print(f"返回数量: {len(models)}")
print(f"分页信息:")
print(f" 当前页: {pagination.get('page', 0)}")
print(f" 每页大小: {pagination.get('page_size', 0)}")
print(f" 总数: {pagination.get('total', 0)}")
print(f" 总页数: {pagination.get('total_pages', 0)}")
print(f" 有下一页: {pagination.get('has_next', False)}")
# 3. 测试单个模型删除URL不实际删除
if models and models[0].get('model_id'):
model_id = models[0]['model_id']
print(f"\n3. 测试删除端点(不实际删除):")
print(f"模型ID: {model_id}")
print(f"删除URL: DELETE /api/models/{model_id}")
print("前端应该能正确构造此URL")
# 4. 总结
print(f"\n\n=== 修复状态总结 ===")
total_models = pagination.get('total', 0)
working_models = sum(1 for m in models if m.get('model_id') and m.get('filename'))
models_with_metrics = sum(1 for m in models if m.get('metrics'))
print(f"✓ 分页功能: 已实现")
print(f"✓ 总模型数: {total_models}")
print(f"✓ 可用模型: {working_models}/{len(models)} (有model_id)")
print(f"✓ 有指标模型: {models_with_metrics}/{len(models)}")
print(f"✓ 删除功能: {'可用' if working_models > 0 else '不可用'}")
if working_models == len(models) and models_with_metrics > 0:
print("\n🎉 所有功能修复成功!")
else:
print(f"\n⚠️ 仍需要重启API服务器以应用所有修复")
except Exception as e:
print(f"测试失败: {e}")
if __name__ == "__main__":
test_complete_fixes()