181 lines
5.0 KiB
Python
181 lines
5.0 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
"""
|
|||
|
|
简单测试脚本:测试日报生成和下载
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import requests
|
|||
|
|
import json
|
|||
|
|
|
|||
|
|
BASE_URL = "http://localhost:5000"
|
|||
|
|
|
|||
|
|
def test_generate():
|
|||
|
|
"""测试生成日报"""
|
|||
|
|
print("="*50)
|
|||
|
|
print("测试1:生成日报")
|
|||
|
|
print("="*50)
|
|||
|
|
|
|||
|
|
# 先获取配置列表
|
|||
|
|
print("\n1. 获取配置列表...")
|
|||
|
|
response = requests.get(f"{BASE_URL}/api/config")
|
|||
|
|
print(f" 状态码: {response.status_code}")
|
|||
|
|
|
|||
|
|
if response.status_code != 200:
|
|||
|
|
print(f" ❌ 获取配置失败: {response.text}")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
configs = response.json().get('data', [])
|
|||
|
|
print(f" ✓ 找到 {len(configs)} 个配置")
|
|||
|
|
|
|||
|
|
if not configs:
|
|||
|
|
print(" ❌ 没有配置,请先创建配置")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 使用第一个配置
|
|||
|
|
config = configs[0]
|
|||
|
|
config_id = config['id']
|
|||
|
|
config_name = config['config_name']
|
|||
|
|
print(f" 使用配置: {config_name} (ID: {config_id})")
|
|||
|
|
|
|||
|
|
# 生成日报
|
|||
|
|
print("\n2. 生成日报...")
|
|||
|
|
payload = {
|
|||
|
|
'config_id': config_id,
|
|||
|
|
'start_time': '2025-01-07 08:00:00',
|
|||
|
|
'end_time': '2025-01-08 08:00:00'
|
|||
|
|
}
|
|||
|
|
print(f" 请求数据: {json.dumps(payload, ensure_ascii=False)}")
|
|||
|
|
|
|||
|
|
response = requests.post(f"{BASE_URL}/api/report/generate", json=payload)
|
|||
|
|
print(f" 状态码: {response.status_code}")
|
|||
|
|
print(f" 响应: {json.dumps(response.json(), ensure_ascii=False, indent=2)}")
|
|||
|
|
|
|||
|
|
if response.status_code != 200:
|
|||
|
|
print(f" ❌ 生成失败")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
result = response.json()
|
|||
|
|
if not result.get('success'):
|
|||
|
|
print(f" ❌ 生成失败: {result.get('message')}")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
print(f" ✓ 生成成功")
|
|||
|
|
print(f" 订单数: {result.get('total_orders')}")
|
|||
|
|
print(f" 总金额: {result.get('total_amount')}")
|
|||
|
|
print(f" 文件路径: {result.get('file_path')}")
|
|||
|
|
|
|||
|
|
return result
|
|||
|
|
|
|||
|
|
def test_history():
|
|||
|
|
"""测试历史记录"""
|
|||
|
|
print("\n" + "="*50)
|
|||
|
|
print("测试2:查看历史记录")
|
|||
|
|
print("="*50)
|
|||
|
|
|
|||
|
|
response = requests.get(f"{BASE_URL}/api/report/history")
|
|||
|
|
print(f"状态码: {response.status_code}")
|
|||
|
|
|
|||
|
|
if response.status_code != 200:
|
|||
|
|
print(f"❌ 获取历史记录失败: {response.text}")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
result = response.json()
|
|||
|
|
if not result.get('success'):
|
|||
|
|
print(f"❌ 获取失败: {result.get('message')}")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
history = result.get('data', [])
|
|||
|
|
print(f"✓ 找到 {len(history)} 条记录")
|
|||
|
|
|
|||
|
|
if not history:
|
|||
|
|
print("❌ 没有历史记录")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
print("\n最近的记录:")
|
|||
|
|
for item in history[:3]:
|
|||
|
|
print(f" ID: {item['id']}")
|
|||
|
|
print(f" 状态: {'成功' if item.get('status') == 1 else '失败'}")
|
|||
|
|
print(f" file_path: {item.get('file_path', '空')}")
|
|||
|
|
print(f" 日期: {item.get('report_date')}")
|
|||
|
|
print(f" 名称: {item.get('split_name')}")
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
# 检查是否有可下载的文件
|
|||
|
|
has_file = any(item.get('file_path') for item in history)
|
|||
|
|
if has_file:
|
|||
|
|
print("✓ 有可下载的文件")
|
|||
|
|
else:
|
|||
|
|
print("❌ 没有可下载的文件(所有记录的file_path都为空)")
|
|||
|
|
|
|||
|
|
def test_download():
|
|||
|
|
"""测试下载"""
|
|||
|
|
print("\n" + "="*50)
|
|||
|
|
print("测试3:测试下载")
|
|||
|
|
print("="*50)
|
|||
|
|
|
|||
|
|
# 获取历史记录
|
|||
|
|
response = requests.get(f"{BASE_URL}/api/report/history")
|
|||
|
|
if response.status_code != 200:
|
|||
|
|
print("❌ 获取历史记录失败")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
history = response.json().get('data', [])
|
|||
|
|
if not history:
|
|||
|
|
print("❌ 没有历史记录")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 找到有文件的记录
|
|||
|
|
file_record = None
|
|||
|
|
for item in history:
|
|||
|
|
if item.get('file_path'):
|
|||
|
|
file_record = item
|
|||
|
|
break
|
|||
|
|
|
|||
|
|
if not file_record:
|
|||
|
|
print("❌ 没有找到有文件的记录")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
file_path = file_record['file_path']
|
|||
|
|
print(f"找到文件: {file_path}")
|
|||
|
|
|
|||
|
|
# 测试下载
|
|||
|
|
print(f"\n测试下载: {file_path}")
|
|||
|
|
response = requests.get(f"{BASE_URL}/api/download", params={'path': file_path})
|
|||
|
|
print(f"状态码: {response.status_code}")
|
|||
|
|
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
print(f"✓ 下载成功")
|
|||
|
|
print(f"文件大小: {len(response.content)} 字节")
|
|||
|
|
else:
|
|||
|
|
print(f"❌ 下载失败: {response.text}")
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
print("\n" + "="*50)
|
|||
|
|
print("日报系统测试工具")
|
|||
|
|
print("="*50)
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
# 测试生成
|
|||
|
|
result = test_generate()
|
|||
|
|
|
|||
|
|
# 测试历史
|
|||
|
|
test_history()
|
|||
|
|
|
|||
|
|
# 测试下载
|
|||
|
|
test_download()
|
|||
|
|
|
|||
|
|
print("\n" + "="*50)
|
|||
|
|
print("测试完成")
|
|||
|
|
print("="*50)
|
|||
|
|
|
|||
|
|
except requests.exceptions.ConnectionError:
|
|||
|
|
print("\n❌ 无法连接到服务器")
|
|||
|
|
print("请确保服务正在运行: python app.py")
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"\n❌ 测试出错: {e}")
|
|||
|
|
|
|||
|
|
print("\n按回车键退出...")
|
|||
|
|
input()
|
|||
|
|
|
|||
|
|
if __name__ == '__main__':
|
|||
|
|
main()
|