test_complete_flow.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import requests
  2. import json
  3. def test_complete_flow():
  4. """
  5. 测试完整的目标创建和题目生成流程
  6. 模拟Android应用的完整流程
  7. """
  8. print("=== 测试完整的目标创建和题目生成流程 ===")
  9. # 步骤1: 创建目标
  10. print("步骤1: 创建学习目标")
  11. goal_data = {
  12. "userId": 1,
  13. "subject": "物理",
  14. "goalType": "选择题",
  15. "goalContent": "万有引力定律的应用",
  16. "difficulty": "中等",
  17. "totalQuantity": 3,
  18. "estimatedTime": 45
  19. }
  20. print(f"创建目标请求: {json.dumps(goal_data, indent=2, ensure_ascii=False)}")
  21. try:
  22. # 调用创建目标API
  23. create_response = requests.post(
  24. "http://localhost:8080/api/goals",
  25. json=goal_data,
  26. timeout=10
  27. )
  28. if create_response.status_code != 200:
  29. print(f"创建目标失败: {create_response.status_code} - {create_response.text}")
  30. return
  31. create_result = create_response.json()
  32. print(f"创建目标响应: {json.dumps(create_result, ensure_ascii=False, indent=2)}")
  33. # 步骤2: 解析goalId(模拟修复后的Android逻辑)
  34. print("\n步骤2: 解析goalId")
  35. goal_id = None
  36. if 'data' in create_result and isinstance(create_result['data'], dict):
  37. goal_id = create_result['data'].get('goalId')
  38. print(f"✅ 成功解析goalId: {goal_id}")
  39. else:
  40. print("❌ 无法解析goalId")
  41. return
  42. # 步骤3: 调用Coze工作流API生成题目
  43. print(f"\n步骤3: 使用goalId({goal_id})调用Coze API生成题目")
  44. coze_data = {
  45. "userId": 1,
  46. "goalId": goal_id,
  47. "subject": goal_data["subject"],
  48. "knowledgePoint": goal_data["goalContent"],
  49. "difficulty": goal_data["difficulty"],
  50. "type": goal_data["goalType"],
  51. "totalQuantity": goal_data["totalQuantity"]
  52. }
  53. print(f"Coze API请求: {json.dumps(coze_data, indent=2, ensure_ascii=False)}")
  54. coze_response = requests.post(
  55. "http://localhost:8080/api/coze/generate-questions",
  56. json=coze_data,
  57. timeout=60
  58. )
  59. if coze_response.status_code != 200:
  60. print(f"Coze API调用失败: {coze_response.status_code} - {coze_response.text}")
  61. return
  62. coze_result = coze_response.json()
  63. print(f"\nCoze API响应: {json.dumps(coze_result, ensure_ascii=False, indent=2)}")
  64. # 步骤4: 验证生成的题目
  65. print("\n步骤4: 验证生成的题目")
  66. if coze_result.get('success'):
  67. questions = coze_result.get('questions', [])
  68. print(f"✅ 成功生成 {len(questions)} 道题目")
  69. # 验证每道题目的goalId
  70. all_correct = True
  71. for i, question in enumerate(questions, 1):
  72. question_goal_id = question.get('goalId')
  73. if question_goal_id == goal_id:
  74. print(f"✅ 题目{i}: goalId正确 ({question_goal_id})")
  75. print(f" 题目内容: {question.get('content', '无内容')[:50]}...")
  76. else:
  77. print(f"❌ 题目{i}: goalId错误!期望: {goal_id}, 实际: {question_goal_id}")
  78. all_correct = False
  79. if all_correct:
  80. print("\n🎉 所有题目的goalId都正确!")
  81. else:
  82. print("\n❌ 存在goalId不正确的题目")
  83. else:
  84. print(f"❌ 题目生成失败: {coze_result.get('message', '未知错误')}")
  85. # 步骤5: 验证数据库中的题目
  86. print(f"\n步骤5: 验证数据库中goalId={goal_id}的题目")
  87. db_response = requests.get(
  88. f"http://localhost:8080/api/ai/questions/{goal_id}",
  89. timeout=10
  90. )
  91. if db_response.status_code == 200:
  92. db_result = db_response.json()
  93. if db_result.get('data'):
  94. db_questions = db_result['data']
  95. print(f"✅ 数据库中找到 {len(db_questions)} 道题目")
  96. # 验证数据库中题目的goalId
  97. for i, question in enumerate(db_questions, 1):
  98. question_goal_id = question.get('goalId')
  99. if question_goal_id == goal_id:
  100. print(f"✅ 数据库题目{i}: goalId正确 ({question_goal_id})")
  101. else:
  102. print(f"❌ 数据库题目{i}: goalId错误!期望: {goal_id}, 实际: {question_goal_id}")
  103. else:
  104. print("❌ 数据库中未找到题目")
  105. else:
  106. print(f"❌ 查询数据库失败: {db_response.status_code}")
  107. print("\n=== 测试完成 ===")
  108. except Exception as e:
  109. print(f"测试过程中发生异常: {e}")
  110. import traceback
  111. traceback.print_exc()
  112. if __name__ == "__main__":
  113. test_complete_flow()