#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 测试goalId日志记录 用于验证添加的日志是否能正确显示goalId的值 """ import requests import json import time def test_goalid_logging(): """ 测试goalId日志记录功能 """ base_url = "http://localhost:8080" print("=== 测试goalId日志记录功能 ===") # 测试用例1: 使用正常的goalId print("\n--- 测试用例1: 使用正常goalId ---") test_data_1 = { "userId": 1, "goalId": 13, # 使用新的goalId "subject": "数学", "knowledgePoint": "测试goalId日志记录", "difficulty": "简单", "type": "选择题", "totalQuantity": 2 } print(f"请求数据: {json.dumps(test_data_1, ensure_ascii=False, indent=2)}") try: response_1 = requests.post( f"{base_url}/api/coze/generate-questions", json=test_data_1, timeout=30 ) if response_1.status_code == 200: result_1 = response_1.json() print(f"\n响应结果: {json.dumps(result_1, ensure_ascii=False, indent=2)}") if result_1.get('success'): questions = result_1.get('questions', []) print(f"\n✅ 成功生成 {len(questions)} 道题目") for i, question in enumerate(questions, 1): print(f"题目{i} - detailId: {question.get('detailId')}, goalId: {question.get('goalId')}") else: print("❌ 题目生成失败") else: print(f"❌ API调用失败: {response_1.status_code}") except Exception as e: print(f"❌ 测试用例1异常: {e}") # 等待一下,让日志输出完整 time.sleep(2) # 测试用例2: 使用null goalId(模拟Android应用的错误情况) print("\n--- 测试用例2: 使用null goalId ---") test_data_2 = { "userId": 1, "goalId": None, # 模拟goalId为null的情况 "subject": "数学", "knowledgePoint": "测试null goalId", "difficulty": "简单", "type": "选择题", "totalQuantity": 1 } print(f"请求数据: {json.dumps(test_data_2, ensure_ascii=False, indent=2)}") try: response_2 = requests.post( f"{base_url}/api/coze/generate-questions", json=test_data_2, timeout=30 ) if response_2.status_code == 200: result_2 = response_2.json() print(f"\n响应结果: {json.dumps(result_2, ensure_ascii=False, indent=2)}") if result_2.get('success'): questions = result_2.get('questions', []) print(f"\n✅ 成功生成 {len(questions)} 道题目") for i, question in enumerate(questions, 1): print(f"题目{i} - detailId: {question.get('detailId')}, goalId: {question.get('goalId')}") else: print("❌ 题目生成失败(预期结果,因为goalId为null)") else: print(f"❌ API调用失败: {response_2.status_code}") except Exception as e: print(f"❌ 测试用例2异常: {e}") # 测试用例3: 不传goalId字段 print("\n--- 测试用例3: 不传goalId字段 ---") test_data_3 = { "userId": 1, # 不包含goalId字段 "subject": "数学", "knowledgePoint": "测试缺失goalId", "difficulty": "简单", "type": "选择题", "totalQuantity": 1 } print(f"请求数据: {json.dumps(test_data_3, ensure_ascii=False, indent=2)}") try: response_3 = requests.post( f"{base_url}/api/coze/generate-questions", json=test_data_3, timeout=30 ) if response_3.status_code == 200: result_3 = response_3.json() print(f"\n响应结果: {json.dumps(result_3, ensure_ascii=False, indent=2)}") if result_3.get('success'): questions = result_3.get('questions', []) print(f"\n✅ 成功生成 {len(questions)} 道题目") for i, question in enumerate(questions, 1): print(f"题目{i} - detailId: {question.get('detailId')}, goalId: {question.get('goalId')}") else: print("❌ 题目生成失败(预期结果,因为缺失goalId)") else: print(f"❌ API调用失败: {response_3.status_code}") except Exception as e: print(f"❌ 测试用例3异常: {e}") print("\n=== 测试完成,请查看服务器日志中的🔍标记的详细goalId信息 ===") print("注意观察日志中goalId的值变化,特别是:") print("1. 接收请求时的goalId值") print("2. 设置实体goalId前后的值") print("3. 数据库保存前后的goalId值") print("4. 转换响应对象时的goalId值") print("5. 返回给前端的goalId值") if __name__ == "__main__": test_goalid_logging()