Files
aitsc/test_favorite_api.py
2025-08-29 00:34:40 +08:00

93 lines
3.3 KiB
Python
Raw 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.
# -*- coding: utf-8 -*-
"""
测试收藏API功能
"""
import requests
import json
def test_favorite_api():
"""测试收藏API"""
base_url = "http://127.0.0.1:5000"
print("=" * 60)
print("收藏API功能测试")
print("=" * 60)
# 测试数据
test_data = {
"template_id": 1,
"original_text": "写一篇关于陕西的文章",
"generated_prompt": "请撰写一篇关于陕西省的综合性文章内容需涵盖以下方面1. 地理与自然环境...",
"category": "通用工具"
}
try:
# 1. 测试快速添加收藏
print("📝 测试快速添加收藏...")
response = requests.post(
f"{base_url}/api/favorites/quick-add",
headers={'Content-Type': 'application/json'},
data=json.dumps(test_data)
)
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.text}")
if response.status_code == 200:
result = response.json()
if result.get('success'):
print("✅ 收藏添加成功!")
print(f" 收藏ID: {result.get('id')}")
else:
print(f"❌ 收藏添加失败: {result.get('message')}")
else:
print(f"❌ HTTP请求失败: {response.status_code}")
# 2. 测试获取收藏列表
print("\n📋 测试获取收藏列表...")
response = requests.get(f"{base_url}/api/favorites")
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.text}")
if response.status_code == 200:
result = response.json()
if result.get('success'):
print("✅ 获取收藏列表成功!")
print(f" 总收藏数: {result.get('total', 0)}")
print(f" 当前页数据: {len(result.get('data', []))}")
else:
print(f"❌ 获取收藏列表失败: {result.get('message')}")
else:
print(f"❌ HTTP请求失败: {response.status_code}")
# 3. 测试获取收藏统计
print("\n📊 测试获取收藏统计...")
response = requests.get(f"{base_url}/api/favorites/stats")
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.text}")
if response.status_code == 200:
result = response.json()
if result.get('success'):
print("✅ 获取收藏统计成功!")
stats = result.get('data', {})
print(f" 总收藏数: {stats.get('total', 0)}")
print(f" 分类统计: {stats.get('category_stats', {})}")
else:
print(f"❌ 获取收藏统计失败: {result.get('message')}")
else:
print(f"❌ HTTP请求失败: {response.status_code}")
except requests.exceptions.ConnectionError as e:
print(f"❌ 无法连接到服务器: {str(e)}")
print("请确保Flask应用正在运行在 http://127.0.0.1:5000")
except Exception as e:
print(f"❌ 测试过程中出现错误: {str(e)}")
import traceback
traceback.print_exc()
if __name__ == '__main__':
test_favorite_api()