test_goalid_direct.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import requests
  2. import json
  3. def test_goalid_direct():
  4. """
  5. 直接测试goalId问题
  6. """
  7. try:
  8. print("=== 直接测试goalId问题 ===")
  9. # 使用一个新的goalId进行测试
  10. test_data = {
  11. "userId": 1,
  12. "goalId": 9, # 使用新的goalId
  13. "subject": "数学",
  14. "knowledgePoint": "基础减法运算",
  15. "difficulty": "简单",
  16. "type": "选择题",
  17. "totalQuantity": 1
  18. }
  19. print(f"步骤1: 调用Coze API生成题目,goalId={test_data['goalId']}")
  20. print(f"请求数据: {json.dumps(test_data, indent=2, ensure_ascii=False)}")
  21. response = requests.post(
  22. "http://localhost:8080/api/coze/generate-questions",
  23. json=test_data,
  24. timeout=30
  25. )
  26. print(f"\n响应状态码: {response.status_code}")
  27. if response.status_code == 200:
  28. api_result = response.json()
  29. print(f"API响应: {json.dumps(api_result, indent=2, ensure_ascii=False)}")
  30. if api_result.get('success'):
  31. questions = api_result.get('questions', [])
  32. print(f"\n生成了 {len(questions)} 道题目")
  33. # 检查API响应中的题目是否包含goalId
  34. for i, question in enumerate(questions, 1):
  35. print(f"\n题目 {i} (来自API响应):")
  36. print(f" - detailId: {question.get('detailId')}")
  37. print(f" - goalId: {question.get('goalId')}")
  38. print(f" - content: {question.get('content', '')[:50]}...")
  39. if 'goalId' in question:
  40. if question.get('goalId') == test_data['goalId']:
  41. print(f" ✅ API响应中goalId一致: {question.get('goalId')}")
  42. else:
  43. print(f" ❌ API响应中goalId不一致: 预期={test_data['goalId']}, 实际={question.get('goalId')}")
  44. else:
  45. print(f" ❌ API响应中缺少goalId字段!")
  46. # 查询数据库验证
  47. print(f"\n步骤2: 查询数据库中goalId={test_data['goalId']}的题目")
  48. db_response = requests.get(
  49. f"http://localhost:8080/api/ai/questions/{test_data['goalId']}",
  50. timeout=10
  51. )
  52. if db_response.status_code == 200:
  53. db_result = db_response.json()
  54. print(f"数据库查询响应: {json.dumps(db_result, indent=2, ensure_ascii=False)}")
  55. if 'data' in db_result and db_result['data']:
  56. db_questions = db_result['data']
  57. print(f"\n数据库中找到 {len(db_questions)} 道题目")
  58. for i, question in enumerate(db_questions, 1):
  59. print(f"\n题目 {i} (来自数据库):")
  60. print(f" - detailId: {question.get('detailId')}")
  61. print(f" - goalId: {question.get('goalId')}")
  62. print(f" - content: {question.get('content', '')[:50]}...")
  63. if 'goalId' in question:
  64. if question.get('goalId') == test_data['goalId']:
  65. print(f" ✅ 数据库中goalId一致: {question.get('goalId')}")
  66. else:
  67. print(f" ❌ 数据库中goalId不一致: 预期={test_data['goalId']}, 实际={question.get('goalId')}")
  68. else:
  69. print(f" ❌ 数据库查询结果中缺少goalId字段!")
  70. else:
  71. print("❌ 数据库中没有找到题目")
  72. else:
  73. print(f"❌ 数据库查询失败: {db_response.status_code}")
  74. print(db_response.text)
  75. else:
  76. print(f"❌ API调用失败: {api_result.get('message', 'Unknown error')}")
  77. else:
  78. print(f"❌ API调用失败: {response.status_code}")
  79. print(response.text)
  80. except requests.exceptions.Timeout:
  81. print("❌ 请求超时")
  82. except Exception as e:
  83. print(f"❌ 请求异常: {e}")
  84. if __name__ == "__main__":
  85. test_goalid_direct()