123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import requests
- import json
- def test_goalid_direct():
- """
- 直接测试goalId问题
- """
- try:
- print("=== 直接测试goalId问题 ===")
-
- # 使用一个新的goalId进行测试
- test_data = {
- "userId": 1,
- "goalId": 9, # 使用新的goalId
- "subject": "数学",
- "knowledgePoint": "基础减法运算",
- "difficulty": "简单",
- "type": "选择题",
- "totalQuantity": 1
- }
-
- print(f"步骤1: 调用Coze API生成题目,goalId={test_data['goalId']}")
- print(f"请求数据: {json.dumps(test_data, indent=2, ensure_ascii=False)}")
-
- response = requests.post(
- "http://localhost:8080/api/coze/generate-questions",
- json=test_data,
- timeout=30
- )
-
- print(f"\n响应状态码: {response.status_code}")
-
- if response.status_code == 200:
- api_result = response.json()
- print(f"API响应: {json.dumps(api_result, indent=2, ensure_ascii=False)}")
-
- if api_result.get('success'):
- questions = api_result.get('questions', [])
- print(f"\n生成了 {len(questions)} 道题目")
-
- # 检查API响应中的题目是否包含goalId
- for i, question in enumerate(questions, 1):
- print(f"\n题目 {i} (来自API响应):")
- print(f" - detailId: {question.get('detailId')}")
- print(f" - goalId: {question.get('goalId')}")
- print(f" - content: {question.get('content', '')[:50]}...")
-
- if 'goalId' in question:
- if question.get('goalId') == test_data['goalId']:
- print(f" ✅ API响应中goalId一致: {question.get('goalId')}")
- else:
- print(f" ❌ API响应中goalId不一致: 预期={test_data['goalId']}, 实际={question.get('goalId')}")
- else:
- print(f" ❌ API响应中缺少goalId字段!")
-
- # 查询数据库验证
- print(f"\n步骤2: 查询数据库中goalId={test_data['goalId']}的题目")
- db_response = requests.get(
- f"http://localhost:8080/api/ai/questions/{test_data['goalId']}",
- timeout=10
- )
-
- if db_response.status_code == 200:
- db_result = db_response.json()
- print(f"数据库查询响应: {json.dumps(db_result, indent=2, ensure_ascii=False)}")
-
- if 'data' in db_result and db_result['data']:
- db_questions = db_result['data']
- print(f"\n数据库中找到 {len(db_questions)} 道题目")
-
- for i, question in enumerate(db_questions, 1):
- print(f"\n题目 {i} (来自数据库):")
- print(f" - detailId: {question.get('detailId')}")
- print(f" - goalId: {question.get('goalId')}")
- print(f" - content: {question.get('content', '')[:50]}...")
-
- if 'goalId' in question:
- if question.get('goalId') == test_data['goalId']:
- print(f" ✅ 数据库中goalId一致: {question.get('goalId')}")
- else:
- print(f" ❌ 数据库中goalId不一致: 预期={test_data['goalId']}, 实际={question.get('goalId')}")
- else:
- print(f" ❌ 数据库查询结果中缺少goalId字段!")
- else:
- print("❌ 数据库中没有找到题目")
- else:
- print(f"❌ 数据库查询失败: {db_response.status_code}")
- print(db_response.text)
- else:
- print(f"❌ API调用失败: {api_result.get('message', 'Unknown error')}")
- else:
- print(f"❌ API调用失败: {response.status_code}")
- print(response.text)
-
- except requests.exceptions.Timeout:
- print("❌ 请求超时")
- except Exception as e:
- print(f"❌ 请求异常: {e}")
- if __name__ == "__main__":
- test_goalid_direct()
|