test_submit_answer.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 测试答题提交功能
  5. """
  6. import requests
  7. import json
  8. def create_test_goal_and_questions():
  9. """创建测试目标和题目"""
  10. base_url = "http://localhost:8080/api"
  11. # 1. 创建测试目标
  12. goal_data = {
  13. "userId": 1,
  14. "subject": "测试学科",
  15. "goalType": "练习",
  16. "difficulty": "简单",
  17. "goalContent": "测试答题提交功能",
  18. "totalQuantity": 2
  19. }
  20. print("=== 创建测试目标 ===")
  21. response = requests.post(f"{base_url}/goals", json=goal_data)
  22. print(f"状态码: {response.status_code}")
  23. print(f"响应: {response.text}")
  24. if response.status_code != 200:
  25. print("创建目标失败")
  26. return None, []
  27. goal_response = response.json()
  28. goal_id = goal_response['data']['goalId']
  29. print(f"创建的目标ID: {goal_id}")
  30. # 2. 为目标生成题目
  31. question_data = {
  32. "goalId": goal_id,
  33. "userId": 1,
  34. "subject": "测试学科",
  35. "difficulty": "简单",
  36. "type": "选择题",
  37. "totalQuantity": 2,
  38. "knowledgePoint": "测试知识点"
  39. }
  40. print("=== 生成题目 ===")
  41. generate_response = requests.post(f"{base_url}/coze/generate-questions", json=question_data)
  42. print(f"状态码: {generate_response.status_code}")
  43. print(f"响应: {generate_response.text}")
  44. if generate_response.status_code != 200:
  45. print("生成题目失败")
  46. return goal_id, []
  47. questions_data = generate_response.json()
  48. if 'questions' in questions_data:
  49. questions = questions_data['questions']
  50. else:
  51. questions = questions_data['data']
  52. print(f"生成的题目数量: {len(questions)}")
  53. return goal_id, questions
  54. def test_submit_answer():
  55. base_url = "http://localhost:8080"
  56. # 创建测试数据
  57. goal_id, questions = create_test_goal_and_questions()
  58. if not goal_id or not questions:
  59. print("无法创建测试数据,跳过答题测试")
  60. return
  61. # 使用第一道题目进行测试
  62. question = questions[0]
  63. content_id = question['detailId']
  64. correct_answer = question.get('answer', '测试答案')
  65. print(f"\n=== 使用题目ID: {content_id} 进行测试 ===")
  66. # 测试数据
  67. test_cases = [
  68. {
  69. "name": "正常提交正确答案",
  70. "data": {
  71. "userId": 1,
  72. "goalId": goal_id,
  73. "contentId": content_id,
  74. "userAnswer": correct_answer,
  75. "answerDuration": 30
  76. }
  77. },
  78. {
  79. "name": "正常提交错误答案",
  80. "data": {
  81. "userId": 1,
  82. "goalId": goal_id,
  83. "contentId": content_id,
  84. "userAnswer": "错误答案",
  85. "answerDuration": 25
  86. }
  87. },
  88. {
  89. "name": "缺少用户答案",
  90. "data": {
  91. "userId": 1,
  92. "goalId": goal_id,
  93. "contentId": content_id,
  94. "answerDuration": 30
  95. }
  96. },
  97. {
  98. "name": "无效的题目ID",
  99. "data": {
  100. "userId": 1,
  101. "goalId": goal_id,
  102. "contentId": 999999, # 不存在的题目ID
  103. "userAnswer": "测试答案",
  104. "answerDuration": 30
  105. }
  106. },
  107. {
  108. "name": "缺少必要参数",
  109. "data": {
  110. "goalId": goal_id,
  111. "contentId": content_id,
  112. "userAnswer": "测试答案"
  113. }
  114. }
  115. ]
  116. for test_case in test_cases:
  117. print(f"\n=== {test_case['name']} ===")
  118. try:
  119. response = requests.post(
  120. f"{base_url}/answers/submit",
  121. json=test_case['data'],
  122. headers={'Content-Type': 'application/json'},
  123. timeout=10
  124. )
  125. print(f"状态码: {response.status_code}")
  126. if response.headers.get('content-type', '').startswith('application/json'):
  127. try:
  128. response_data = response.json()
  129. print(f"响应消息: {response_data.get('message', 'N/A')}")
  130. print(f"响应代码: {response_data.get('code', 'N/A')}")
  131. if response_data.get('data'):
  132. data = response_data['data']
  133. print(f"答题记录ID: {data.get('answerId', 'N/A')}")
  134. print(f"是否正确: {data.get('isCorrect', 'N/A')}")
  135. print(f"用户答案: {data.get('userAnswer', 'N/A')}")
  136. except:
  137. print(f"响应内容: {response.text}")
  138. else:
  139. print(f"响应内容: {response.text}")
  140. except requests.exceptions.RequestException as e:
  141. print(f"请求失败: {e}")
  142. except Exception as e:
  143. print(f"发生错误: {e}")
  144. if __name__ == "__main__":
  145. test_submit_answer()