test_null_goalid.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import requests
  2. import json
  3. def test_null_goalid():
  4. """
  5. 测试当goalId为null时会发生什么
  6. """
  7. print("=== 测试goalId为null的情况 ===")
  8. # 测试1: goalId为null
  9. test_data_null = {
  10. "userId": 1,
  11. "goalId": None, # 设置为null
  12. "subject": "数学",
  13. "knowledgePoint": "测试null goalId",
  14. "difficulty": "简单",
  15. "type": "选择题",
  16. "totalQuantity": 1
  17. }
  18. print(f"测试数据1 (goalId=null): {json.dumps(test_data_null, indent=2, ensure_ascii=False)}")
  19. try:
  20. response = requests.post(
  21. "http://localhost:8080/api/coze/generate-questions",
  22. json=test_data_null,
  23. timeout=30
  24. )
  25. print(f"响应状态码: {response.status_code}")
  26. if response.status_code == 200:
  27. result = response.json()
  28. print(f"API响应: {json.dumps(result, ensure_ascii=False, indent=2)}")
  29. if result.get('success') and result.get('questions'):
  30. for question in result['questions']:
  31. print(f"生成的题目goalId: {question.get('goalId')}")
  32. else:
  33. print(f"请求失败: {response.text}")
  34. except Exception as e:
  35. print(f"请求异常: {e}")
  36. print("\n" + "="*50 + "\n")
  37. # 测试2: 不传goalId字段
  38. test_data_missing = {
  39. "userId": 1,
  40. # 完全不传goalId字段
  41. "subject": "数学",
  42. "knowledgePoint": "测试缺失goalId",
  43. "difficulty": "简单",
  44. "type": "选择题",
  45. "totalQuantity": 1
  46. }
  47. print(f"测试数据2 (不传goalId): {json.dumps(test_data_missing, indent=2, ensure_ascii=False)}")
  48. try:
  49. response = requests.post(
  50. "http://localhost:8080/api/coze/generate-questions",
  51. json=test_data_missing,
  52. timeout=30
  53. )
  54. print(f"响应状态码: {response.status_code}")
  55. if response.status_code == 200:
  56. result = response.json()
  57. print(f"API响应: {json.dumps(result, ensure_ascii=False, indent=2)}")
  58. if result.get('success') and result.get('questions'):
  59. for question in result['questions']:
  60. print(f"生成的题目goalId: {question.get('goalId')}")
  61. else:
  62. print(f"请求失败: {response.text}")
  63. except Exception as e:
  64. print(f"请求异常: {e}")
  65. if __name__ == "__main__":
  66. test_null_goalid()