import requests import json def test_multiple_goalids(): """测试多个不同goalId值的一致性""" test_cases = [ {'goalId': 10, 'subject': '英语'}, {'goalId': 11, 'subject': '物理'}, {'goalId': 12, 'subject': '化学'} ] for test_case in test_cases: print(f'\n=== 测试 goalId={test_case["goalId"]}, subject={test_case["subject"]} ===') test_data = { 'userId': 1, 'goalId': test_case['goalId'], 'subject': test_case['subject'], 'type': '选择题', 'difficulty': '简单', 'totalQuantity': 1, 'knowledgePoint': '基础知识' } try: # 调用API生成题目 response = requests.post( 'http://localhost:8080/api/coze/generate-questions', json=test_data, timeout=30 ) if response.status_code == 200: result = response.json() if result.get('success'): questions = result.get('questions', []) print(f'生成了 {len(questions)} 道题目') for i, question in enumerate(questions, 1): api_goal_id = question.get('goalId') print(f'题目 {i}: API返回goalId={api_goal_id}, 预期goalId={test_case["goalId"]}') if api_goal_id == test_case['goalId']: print(f'✅ API goalId一致') else: print(f'❌ API goalId不一致') # 查询数据库验证 db_response = requests.get( f'http://localhost:8080/api/ai/questions/{test_case["goalId"]}', timeout=10 ) if db_response.status_code == 200: db_result = db_response.json() db_questions = db_result.get('data', []) print(f'数据库查询到 {len(db_questions)} 道题目') for question in db_questions: db_goal_id = question.get('goalId') if db_goal_id == test_case['goalId']: print(f'✅ 数据库goalId一致: {db_goal_id}') else: print(f'❌ 数据库goalId不一致: 预期={test_case["goalId"]}, 实际={db_goal_id}') else: print(f'❌ 数据库查询失败: {db_response.status_code}') else: print(f'❌ API调用失败: {result.get("message", "Unknown error")}') else: print(f'❌ API调用失败: {response.status_code}') except Exception as e: print(f'❌ 测试异常: {e}') if __name__ == "__main__": test_multiple_goalids()