ShopTRAINING/test/test_model_details_endpoint.py

90 lines
3.8 KiB
Python
Raw Permalink Normal View History

2025-07-02 11:05:23 +08:00
#!/usr/bin/env python3
"""
测试模型详情端点
"""
import urllib.request
import json
def test_model_details_endpoint():
try:
print("=== 测试模型详情端点 ===")
# 首先获取模型列表,找到可用的模型
print("1. 获取模型列表...")
url = 'http://localhost:5000/api/models'
with urllib.request.urlopen(url) as response:
data = response.read().decode('utf-8')
result = json.loads(data)
models = result.get('data', [])
print(f"找到 {len(models)} 个模型")
if not models:
print("❌ 没有可用模型,无法测试详情端点")
return
# 测试每个模型的详情端点
for i, model in enumerate(models):
model_type = model.get('model_type', '')
product_id = model.get('product_id', '')
print(f"\n2.{i+1} 测试模型详情:")
print(f" 模型类型: {model_type}")
print(f" 产品ID: {product_id}")
if not model_type or not product_id:
print(" ❌ 缺少必要参数,跳过")
continue
# 构造详情端点URL
details_url = f'http://localhost:5000/api/models/{model_type}/{product_id}/details'
print(f" 请求URL: {details_url}")
try:
with urllib.request.urlopen(details_url) as details_response:
details_data = details_response.read().decode('utf-8')
details_result = json.loads(details_data)
print(f" ✓ 状态码: {details_response.status}")
if details_result.get('status') == 'success':
data = details_result.get('data', {})
model_info = data.get('model_info', {})
training_metrics = data.get('training_metrics', {})
print(f" ✓ 返回状态: success")
print(f" ✓ 模型信息: {len(model_info)} 个字段")
print(f" ✓ 训练指标: {len(training_metrics)}")
if training_metrics:
print(f" - 指标包含: {list(training_metrics.keys())}")
else:
print(f" ❌ 返回错误: {details_result.get('message', '未知错误')}")
except urllib.error.HTTPError as e:
print(f" ❌ HTTP错误: {e.code} - {e.reason}")
# 尝试读取错误响应
try:
error_data = e.read().decode('utf-8')
error_result = json.loads(error_data)
print(f" 错误详情: {error_result.get('message', '无详情')}")
except:
print(f" 无法解析错误响应")
except Exception as e:
print(f" ❌ 请求异常: {e}")
# 3. 测试CORS
print(f"\n3. CORS配置检查:")
print(f" 当前CORS设置: origins='*'")
print(f" 如果前端仍有CORS错误可能需要")
print(f" - 检查前端请求头")
print(f" - 确认API服务器地址")
print(f" - 检查浏览器开发者工具网络面板")
except Exception as e:
print(f"测试失败: {e}")
if __name__ == "__main__":
test_model_details_endpoint()