#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 测试Coze API调用和日志记录 用于验证goal_id一致性检查功能 """ import requests import json import time def test_coze_api_with_logging(): """ 测试Coze API调用并检查日志输出 """ base_url = "http://localhost:8080" # 测试数据 test_request = { "userId": 1, "goalId": 3, # 使用新的goalId进行测试 "subject": "数学", "type": "选择题", "difficulty": "中等", "totalQuantity": 2, "knowledgePoint": "导数的应用" } print(f"开始测试Coze API调用...") print(f"测试请求参数: {json.dumps(test_request, ensure_ascii=False, indent=2)}") try: # 1. 调用Coze生成题目API print("\n=== 步骤1: 调用Coze生成题目API ===") response = requests.post( f"{base_url}/api/coze/generate-questions", json=test_request, headers={"Content-Type": "application/json"}, timeout=30 ) print(f"响应状态码: {response.status_code}") if response.status_code == 200: result = response.json() print(f"响应结果: {json.dumps(result, ensure_ascii=False, indent=2)}") # 2. 检查生成的题目 print("\n=== 步骤2: 检查生成的题目 ===") if result.get('success') and result.get('questions'): print(f"成功生成 {len(result['questions'])} 道题目") for i, question in enumerate(result['questions'], 1): print(f"题目{i}: {question.get('question', '无题目内容')[:50]}...") else: print("未生成任何题目") if 'debugUrl' in result: print(f"调试链接: {result['debugUrl']}") # 3. 等待一下,然后检查数据库中的题目 print("\n=== 步骤3: 检查数据库中的题目 ===") time.sleep(2) # 等待数据库保存完成 questions_response = requests.get( f"{base_url}/api/ai/questions/{test_request['goalId']}", timeout=10 ) if questions_response.status_code == 200: questions_data = questions_response.json() print(f"数据库查询结果: {json.dumps(questions_data, ensure_ascii=False, indent=2)}") if questions_data.get('data'): print(f"数据库中找到 {len(questions_data['data'])} 道题目") # 验证goal_id一致性 for question in questions_data['data']: if question.get('goalId') != test_request['goalId']: print(f"❌ 发现goal_id不一致!预期: {test_request['goalId']}, 实际: {question.get('goalId')}") else: print(f"✅ goal_id一致: {question.get('goalId')}") else: print("数据库中未找到题目") else: print(f"查询数据库失败,状态码: {questions_response.status_code}") else: print(f"API调用失败: {response.text}") except requests.exceptions.RequestException as e: print(f"请求异常: {e}") except Exception as e: print(f"其他异常: {e}") if __name__ == "__main__": test_coze_api_with_logging()