238 lines
9.3 KiB
Python
238 lines
9.3 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
测试新功能脚本
|
|
"""
|
|
|
|
import requests
|
|
import json
|
|
import time
|
|
|
|
def test_knowledge_pagination():
|
|
"""测试知识库分页功能"""
|
|
print("📚 测试知识库分页功能")
|
|
print("-" * 40)
|
|
|
|
base_url = "http://localhost:5000"
|
|
|
|
try:
|
|
# 测试分页获取
|
|
response = requests.get(f"{base_url}/api/knowledge?page=1&per_page=5")
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
if 'knowledge' in data:
|
|
print(f"✅ 分页数据获取成功")
|
|
print(f" - 当前页: {data.get('page', 1)}")
|
|
print(f" - 每页数量: {data.get('per_page', 10)}")
|
|
print(f" - 总页数: {data.get('total_pages', 0)}")
|
|
print(f" - 总条目数: {data.get('total', 0)}")
|
|
print(f" - 当前页条目数: {len(data.get('knowledge', []))}")
|
|
else:
|
|
print("❌ 分页数据格式错误")
|
|
else:
|
|
print(f"❌ HTTP错误: {response.status_code}")
|
|
except Exception as e:
|
|
print(f"❌ 请求错误: {e}")
|
|
|
|
def test_knowledge_verification():
|
|
"""测试知识库验证功能"""
|
|
print("\n🔍 测试知识库验证功能")
|
|
print("-" * 40)
|
|
|
|
base_url = "http://localhost:5000"
|
|
|
|
try:
|
|
# 1. 添加测试知识库
|
|
test_knowledge = {
|
|
"question": "测试验证功能 - 如何测试新功能?",
|
|
"answer": "这是一个测试答案,用于验证知识库验证功能。",
|
|
"category": "技术问题",
|
|
"confidence_score": 0.8
|
|
}
|
|
|
|
response = requests.post(f"{base_url}/api/knowledge", json=test_knowledge)
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
if data.get("success"):
|
|
print("✅ 测试知识库添加成功")
|
|
|
|
# 2. 获取知识库列表查看验证状态
|
|
response = requests.get(f"{base_url}/api/knowledge?page=1&per_page=10")
|
|
if response.status_code == 200:
|
|
knowledge_data = response.json()
|
|
if 'knowledge' in knowledge_data:
|
|
# 查找刚添加的知识库
|
|
test_entry = None
|
|
for entry in knowledge_data['knowledge']:
|
|
if "测试验证功能" in entry['question']:
|
|
test_entry = entry
|
|
break
|
|
|
|
if test_entry:
|
|
print(f"✅ 找到测试知识库条目 (ID: {test_entry['id']})")
|
|
print(f" - 验证状态: {'已验证' if test_entry.get('is_verified') else '未验证'}")
|
|
|
|
# 3. 测试验证功能
|
|
verify_response = requests.post(
|
|
f"{base_url}/api/knowledge/verify/{test_entry['id']}",
|
|
json={"verified_by": "test_user"}
|
|
)
|
|
|
|
if verify_response.status_code == 200:
|
|
verify_data = verify_response.json()
|
|
if verify_data.get("success"):
|
|
print("✅ 知识库验证成功")
|
|
|
|
# 4. 再次检查验证状态
|
|
response = requests.get(f"{base_url}/api/knowledge?page=1&per_page=10")
|
|
if response.status_code == 200:
|
|
knowledge_data = response.json()
|
|
if 'knowledge' in knowledge_data:
|
|
for entry in knowledge_data['knowledge']:
|
|
if entry['id'] == test_entry['id']:
|
|
print(f"✅ 验证状态更新成功: {'已验证' if entry.get('is_verified') else '未验证'}")
|
|
break
|
|
else:
|
|
print("❌ 知识库验证失败")
|
|
else:
|
|
print(f"❌ 验证请求失败: {verify_response.status_code}")
|
|
else:
|
|
print("❌ 未找到测试知识库条目")
|
|
else:
|
|
print("❌ 知识库数据格式错误")
|
|
else:
|
|
print(f"❌ 获取知识库失败: {response.status_code}")
|
|
else:
|
|
print("❌ 测试知识库添加失败")
|
|
else:
|
|
print(f"❌ HTTP错误: {response.status_code}")
|
|
|
|
except Exception as e:
|
|
print(f"❌ 测试过程中出现错误: {e}")
|
|
|
|
def test_vehicle_data():
|
|
"""测试车辆数据功能"""
|
|
print("\n🚗 测试车辆数据功能")
|
|
print("-" * 40)
|
|
|
|
base_url = "http://localhost:5000"
|
|
|
|
try:
|
|
# 1. 初始化示例车辆数据
|
|
response = requests.post(f"{base_url}/api/vehicle/init-sample-data")
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
if data.get("success"):
|
|
print("✅ 示例车辆数据初始化成功")
|
|
else:
|
|
print("❌ 示例车辆数据初始化失败")
|
|
else:
|
|
print(f"❌ 初始化请求失败: {response.status_code}")
|
|
|
|
# 2. 获取车辆数据
|
|
response = requests.get(f"{base_url}/api/vehicle/data?vehicle_id=V001&limit=5")
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
print(f"✅ 车辆数据获取成功,共 {len(data)} 条记录")
|
|
for item in data[:2]: # 显示前2条
|
|
print(f" - {item['data_type']}: {item['timestamp']}")
|
|
else:
|
|
print(f"❌ 获取车辆数据失败: {response.status_code}")
|
|
|
|
# 3. 获取车辆最新数据
|
|
response = requests.get(f"{base_url}/api/vehicle/data/V001/latest")
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
print(f"✅ 车辆最新数据获取成功,数据类型: {list(data.keys())}")
|
|
else:
|
|
print(f"❌ 获取车辆最新数据失败: {response.status_code}")
|
|
|
|
# 4. 获取车辆摘要
|
|
response = requests.get(f"{base_url}/api/vehicle/data/V001/summary")
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
print(f"✅ 车辆摘要获取成功")
|
|
print(f" - 车辆ID: {data.get('vehicle_id')}")
|
|
print(f" - 状态: {data.get('status')}")
|
|
print(f" - 电池电量: {data.get('battery_level')}%")
|
|
print(f" - 故障数量: {data.get('fault_count')}")
|
|
else:
|
|
print(f"❌ 获取车辆摘要失败: {response.status_code}")
|
|
|
|
except Exception as e:
|
|
print(f"❌ 测试过程中出现错误: {e}")
|
|
|
|
def test_file_upload():
|
|
"""测试文件上传功能"""
|
|
print("\n📁 测试文件上传功能")
|
|
print("-" * 40)
|
|
|
|
base_url = "http://localhost:5000"
|
|
|
|
try:
|
|
# 创建测试文件内容
|
|
test_content = """
|
|
车辆远程启动使用指南
|
|
|
|
1. 远程启动条件
|
|
- 车辆处于P档
|
|
- 手刹拉起
|
|
- 车门已锁
|
|
- 电池电量充足
|
|
|
|
2. 操作步骤
|
|
- 打开APP
|
|
- 点击远程启动按钮
|
|
- 确认启动条件
|
|
- 等待启动完成
|
|
|
|
3. 注意事项
|
|
- 启动后10分钟内需要踩刹车
|
|
- 如遇问题请及时联系客服
|
|
"""
|
|
|
|
# 模拟文件上传
|
|
files = {'file': ('vehicle_guide.txt', test_content, 'text/plain')}
|
|
data = {
|
|
'process_method': 'auto',
|
|
'category': '远程控制',
|
|
'confidence_score': 0.8
|
|
}
|
|
|
|
response = requests.post(f"{base_url}/api/knowledge/upload", files=files, data=data)
|
|
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
if result.get("success"):
|
|
print(f"✅ 文件上传成功,生成了 {result.get('knowledge_count', 0)} 条知识")
|
|
else:
|
|
print(f"❌ 文件上传失败: {result.get('error', '未知错误')}")
|
|
else:
|
|
print(f"❌ HTTP错误: {response.status_code}")
|
|
|
|
except Exception as e:
|
|
print(f"❌ 测试过程中出现错误: {e}")
|
|
|
|
def main():
|
|
"""主函数"""
|
|
print("🧪 TSP智能助手 - 新功能测试")
|
|
print("="*50)
|
|
|
|
# 测试各项功能
|
|
test_knowledge_pagination()
|
|
test_knowledge_verification()
|
|
test_vehicle_data()
|
|
test_file_upload()
|
|
|
|
print("\n" + "="*50)
|
|
print("🎉 新功能测试完成!")
|
|
print("\n新功能总结:")
|
|
print("✅ 知识库分页显示 - 解决只能显示两个的问题")
|
|
print("✅ 知识库验证功能 - 未经核实的知识库不可输出")
|
|
print("✅ 车辆实时数据 - 支持车辆数据查询和管理")
|
|
print("✅ 文件上传功能 - 支持文件自动生成知识库")
|
|
print("✅ 分页显示 - 新增知识库可以正常显示")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|