test_multiple_goalids.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import requests
  2. import json
  3. def test_multiple_goalids():
  4. """测试多个不同goalId值的一致性"""
  5. test_cases = [
  6. {'goalId': 10, 'subject': '英语'},
  7. {'goalId': 11, 'subject': '物理'},
  8. {'goalId': 12, 'subject': '化学'}
  9. ]
  10. for test_case in test_cases:
  11. print(f'\n=== 测试 goalId={test_case["goalId"]}, subject={test_case["subject"]} ===')
  12. test_data = {
  13. 'userId': 1,
  14. 'goalId': test_case['goalId'],
  15. 'subject': test_case['subject'],
  16. 'type': '选择题',
  17. 'difficulty': '简单',
  18. 'totalQuantity': 1,
  19. 'knowledgePoint': '基础知识'
  20. }
  21. try:
  22. # 调用API生成题目
  23. response = requests.post(
  24. 'http://localhost:8080/api/coze/generate-questions',
  25. json=test_data,
  26. timeout=30
  27. )
  28. if response.status_code == 200:
  29. result = response.json()
  30. if result.get('success'):
  31. questions = result.get('questions', [])
  32. print(f'生成了 {len(questions)} 道题目')
  33. for i, question in enumerate(questions, 1):
  34. api_goal_id = question.get('goalId')
  35. print(f'题目 {i}: API返回goalId={api_goal_id}, 预期goalId={test_case["goalId"]}')
  36. if api_goal_id == test_case['goalId']:
  37. print(f'✅ API goalId一致')
  38. else:
  39. print(f'❌ API goalId不一致')
  40. # 查询数据库验证
  41. db_response = requests.get(
  42. f'http://localhost:8080/api/ai/questions/{test_case["goalId"]}',
  43. timeout=10
  44. )
  45. if db_response.status_code == 200:
  46. db_result = db_response.json()
  47. db_questions = db_result.get('data', [])
  48. print(f'数据库查询到 {len(db_questions)} 道题目')
  49. for question in db_questions:
  50. db_goal_id = question.get('goalId')
  51. if db_goal_id == test_case['goalId']:
  52. print(f'✅ 数据库goalId一致: {db_goal_id}')
  53. else:
  54. print(f'❌ 数据库goalId不一致: 预期={test_case["goalId"]}, 实际={db_goal_id}')
  55. else:
  56. print(f'❌ 数据库查询失败: {db_response.status_code}')
  57. else:
  58. print(f'❌ API调用失败: {result.get("message", "Unknown error")}')
  59. else:
  60. print(f'❌ API调用失败: {response.status_code}')
  61. except Exception as e:
  62. print(f'❌ 测试异常: {e}')
  63. if __name__ == "__main__":
  64. test_multiple_goalids()