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

67 lines
2.3 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
"""
调试API是否使用最新代码
"""
import urllib.request
import json
import sys
import os
# 添加server目录到路径直接测试ModelManager
server_path = os.path.join(os.path.dirname(__file__), 'server')
sys.path.insert(0, server_path)
def test_modelmanager_vs_api():
print("=== 对比ModelManager与API返回的数据 ===")
# 1. 直接测试ModelManager
print("\n1. 直接测试ModelManager:")
try:
from utils.model_manager import ModelManager
manager = ModelManager()
models = manager.list_models()
print(f"ModelManager找到 {len(models)} 个模型")
if models:
model = models[0]
print(f"第一个模型filename: '{model.get('filename', 'MISSING')}'")
print(f"第一个模型product_id: '{model.get('product_id', 'MISSING')}'")
print(f"第一个模型model_type: '{model.get('model_type', 'MISSING')}'")
except Exception as e:
print(f"ModelManager测试失败: {e}")
# 2. 测试API端点
print("\n2. 测试API端点:")
try:
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"API返回 {len(models)} 个模型")
if models:
model = models[0]
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')}'")
except Exception as e:
print(f"API测试失败: {e}")
# 3. 检查API代码修改是否生效
print("\n3. 检查API代码修改:")
try:
with open('server/api.py', 'r', encoding='utf-8') as f:
content = f.read()
if "'filename': model.get('filename', '')" in content:
print("✓ API代码修改已存在")
else:
print("✗ API代码修改未找到")
except Exception as e:
print(f"代码检查失败: {e}")
if __name__ == "__main__":
test_modelmanager_vs_api()