149 lines
4.6 KiB
Python
149 lines
4.6 KiB
Python
"""
|
||
API接口测试脚本
|
||
用于测试解析接口和文档生成接口
|
||
"""
|
||
import requests
|
||
import json
|
||
|
||
BASE_URL = "http://localhost:7500"
|
||
|
||
def test_extract_api():
|
||
"""测试解析接口"""
|
||
print("="*60)
|
||
print("测试解析接口 (/ai/extract)")
|
||
print("="*60)
|
||
|
||
url = f"{BASE_URL}/ai/extract"
|
||
|
||
# 测试数据
|
||
test_data = {
|
||
"inputData": [
|
||
{
|
||
"fieldCode": "clue_info",
|
||
"fieldValue": "被举报用户名称是张三,年龄30岁,某公司总经理,男性,1980年5月出生,中共党员"
|
||
}
|
||
],
|
||
"outputData": [
|
||
{"fieldCode": "target_name"},
|
||
{"fieldCode": "target_gender"},
|
||
{"fieldCode": "target_organization_and_position"}
|
||
]
|
||
}
|
||
|
||
print(f"\n请求URL: {url}")
|
||
print(f"请求数据:\n{json.dumps(test_data, ensure_ascii=False, indent=2)}")
|
||
|
||
try:
|
||
response = requests.post(url, json=test_data, timeout=30)
|
||
print(f"\n响应状态码: {response.status_code}")
|
||
|
||
result = response.json()
|
||
print(f"响应数据:\n{json.dumps(result, ensure_ascii=False, indent=2)}")
|
||
|
||
if result.get('isSuccess'):
|
||
print("\n✓ 解析接口测试成功!")
|
||
if result.get('data', {}).get('outData'):
|
||
print("\n提取的字段:")
|
||
for item in result['data']['outData']:
|
||
print(f" - {item.get('fieldCode')}: {item.get('fieldValue', '(空)')}")
|
||
else:
|
||
print(f"\n✗ 解析接口测试失败: {result.get('errorMsg', '未知错误')}")
|
||
|
||
except requests.exceptions.ConnectionError:
|
||
print("\n✗ 连接失败:请确保服务已启动")
|
||
except requests.exceptions.Timeout:
|
||
print("\n✗ 请求超时")
|
||
except Exception as e:
|
||
print(f"\n✗ 测试失败: {str(e)}")
|
||
|
||
print()
|
||
|
||
|
||
def test_generate_document_api():
|
||
"""测试文档生成接口"""
|
||
print("="*60)
|
||
print("测试文档生成接口 (/ai/generate-document)")
|
||
print("="*60)
|
||
|
||
url = f"{BASE_URL}/ai/generate-document"
|
||
|
||
# 测试数据
|
||
test_data = {
|
||
"inputData": [
|
||
{
|
||
"fieldCode": "target_name",
|
||
"fieldValue": "张三"
|
||
},
|
||
{
|
||
"fieldCode": "target_gender",
|
||
"fieldValue": "男"
|
||
},
|
||
{
|
||
"fieldCode": "target_organization_and_position",
|
||
"fieldValue": "某公司总经理"
|
||
}
|
||
],
|
||
"fpolicFieldParamFileList": [
|
||
{
|
||
"fileId": 1,
|
||
"fileName": "测试文档.doc",
|
||
"templateCode": "PRELIMINARY_VERIFICATION_APPROVAL"
|
||
}
|
||
]
|
||
}
|
||
|
||
print(f"\n请求URL: {url}")
|
||
print(f"请求数据:\n{json.dumps(test_data, ensure_ascii=False, indent=2)}")
|
||
|
||
try:
|
||
response = requests.post(url, json=test_data, timeout=60)
|
||
print(f"\n响应状态码: {response.status_code}")
|
||
|
||
result = response.json()
|
||
print(f"响应数据:\n{json.dumps(result, ensure_ascii=False, indent=2)}")
|
||
|
||
if result.get('isSuccess'):
|
||
print("\n✓ 文档生成接口测试成功!")
|
||
if result.get('data', {}).get('fpolicFieldParamFileList'):
|
||
print("\n生成的文件:")
|
||
for file_info in result['data']['fpolicFieldParamFileList']:
|
||
print(f" - {file_info.get('fileName')}: {file_info.get('filePath', '(无路径)')}")
|
||
else:
|
||
print(f"\n✗ 文档生成接口测试失败: {result.get('errorMsg', '未知错误')}")
|
||
print(f"错误码: {result.get('code', 'N/A')}")
|
||
|
||
except requests.exceptions.ConnectionError:
|
||
print("\n✗ 连接失败:请确保服务已启动")
|
||
except requests.exceptions.Timeout:
|
||
print("\n✗ 请求超时(文档生成可能需要较长时间)")
|
||
except Exception as e:
|
||
print(f"\n✗ 测试失败: {str(e)}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
|
||
print()
|
||
|
||
|
||
def main():
|
||
"""主函数"""
|
||
print("\n" + "="*60)
|
||
print("API接口测试")
|
||
print("="*60)
|
||
print(f"\n服务地址: {BASE_URL}")
|
||
print("请确保服务已启动(运行 python app.py)\n")
|
||
|
||
# 测试解析接口
|
||
test_extract_api()
|
||
|
||
# 测试文档生成接口(注释掉,因为需要MinIO配置和模板文件)
|
||
# test_generate_document_api()
|
||
|
||
print("="*60)
|
||
print("测试完成")
|
||
print("="*60)
|
||
|
||
|
||
if __name__ == '__main__':
|
||
main()
|
||
|