#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 测试优化历史功能 - 使用腾讯云prompt表 """ import requests import json import time # 测试配置 BASE_URL = "http://localhost:5002" API_BASE = f"{BASE_URL}/api/optimization-history" def test_add_history(): """测试添加历史记录""" print("🧪 测试添加历史记录...") data = { "original_text": "写一个登录页面", "optimized_text": "请创建一个现代风格的登录页面,包含用户名和密码输入框、登录按钮,使用响应式设计,支持移动端和桌面端。页面应该具有简洁美观的UI设计,包含适当的验证和错误提示功能。", "wx_user_id": 1 } try: response = requests.post(API_BASE, json=data, timeout=10) result = response.json() if result.get('success'): print(f"✅ 添加成功: {result.get('message')}") return result.get('data', {}).get('id') else: print(f"❌ 添加失败: {result.get('message')}") return None except Exception as e: print(f"❌ 请求失败: {str(e)}") return None def test_get_history(): """测试获取历史记录""" print("🧪 测试获取历史记录...") try: response = requests.get(API_BASE, timeout=10) result = response.json() if result.get('success'): history = result.get('data', {}).get('history', []) pagination = result.get('data', {}).get('pagination', {}) print(f"✅ 获取成功: 共 {pagination.get('total', 0)} 条记录") print(f" 当前页: {pagination.get('page', 1)}/{pagination.get('pages', 1)}") if history: print(f" 最新记录: {history[0].get('original_text', '')[:50]}...") return True else: print(f"❌ 获取失败: {result.get('message')}") return False except Exception as e: print(f"❌ 请求失败: {str(e)}") return False def test_search_history(): """测试搜索历史记录""" print("🧪 测试搜索历史记录...") try: params = {"search": "登录"} response = requests.get(API_BASE, params=params, timeout=10) result = response.json() if result.get('success'): history = result.get('data', {}).get('history', []) print(f"✅ 搜索成功: 找到 {len(history)} 条相关记录") return True else: print(f"❌ 搜索失败: {result.get('message')}") return False except Exception as e: print(f"❌ 请求失败: {str(e)}") return False def test_get_stats(): """测试获取统计信息""" print("🧪 测试获取统计信息...") try: response = requests.get(f"{API_BASE}/stats", timeout=10) result = response.json() if result.get('success'): stats = result.get('data', {}) print(f"✅ 统计信息:") print(f" 总记录数: {stats.get('total_count', 0)}") print(f" 今日记录: {stats.get('today_count', 0)}") print(f" 本周记录: {stats.get('week_count', 0)}") print(f" 本月记录: {stats.get('month_count', 0)}") return True else: print(f"❌ 获取统计失败: {result.get('message')}") return False except Exception as e: print(f"❌ 请求失败: {str(e)}") return False def test_export_history(): """测试导出历史记录""" print("🧪 测试导出历史记录...") try: response = requests.get(f"{API_BASE}/export", timeout=10) result = response.json() if result.get('success'): data = result.get('data', []) count = result.get('count', 0) print(f"✅ 导出成功: 共 {count} 条记录") return True else: print(f"❌ 导出失败: {result.get('message')}") return False except Exception as e: print(f"❌ 请求失败: {str(e)}") return False def test_delete_history(history_id): """测试删除历史记录""" if not history_id: print("⚠️ 跳过删除测试(没有可删除的记录)") return True print(f"🧪 测试删除历史记录 (ID: {history_id})...") try: response = requests.delete(f"{API_BASE}/{history_id}", timeout=10) result = response.json() if result.get('success'): print(f"✅ 删除成功: {result.get('message')}") return True else: print(f"❌ 删除失败: {result.get('message')}") return False except Exception as e: print(f"❌ 请求失败: {str(e)}") return False def main(): """主测试函数""" print("=" * 60) print("🚀 优化历史功能测试 - 腾讯云prompt表") print("=" * 60) # 检查服务是否运行 try: response = requests.get(BASE_URL, timeout=5) print(f"✅ 服务运行正常: {BASE_URL}") except Exception as e: print(f"❌ 服务连接失败: {str(e)}") print("请确保服务正在运行: python run_dev_fixed.py") return print("\n" + "=" * 60) print("开始测试...") print("=" * 60) # 测试步骤 tests = [ ("添加历史记录", test_add_history), ("获取历史记录", test_get_history), ("搜索历史记录", test_search_history), ("获取统计信息", test_get_stats), ("导出历史记录", test_export_history), ] results = [] history_id = None for test_name, test_func in tests: print(f"\n📋 {test_name}") print("-" * 40) if test_name == "添加历史记录": history_id = test_func() results.append((test_name, history_id is not None)) else: success = test_func() results.append((test_name, success)) time.sleep(1) # 避免请求过快 # 测试删除(如果有记录) if history_id: print(f"\n📋 删除历史记录") print("-" * 40) success = test_delete_history(history_id) results.append(("删除历史记录", success)) # 输出测试结果 print("\n" + "=" * 60) print("📊 测试结果汇总") print("=" * 60) passed = 0 total = len(results) for test_name, success in results: status = "✅ 通过" if success else "❌ 失败" print(f"{status} {test_name}") if success: passed += 1 print(f"\n🎯 测试完成: {passed}/{total} 通过") if passed == total: print("🎉 所有测试通过!优化历史功能运行正常。") else: print("⚠️ 部分测试失败,请检查日志和配置。") if __name__ == "__main__": main()