#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 测试答题提交功能 """ import requests import json def create_test_goal_and_questions(): """创建测试目标和题目""" base_url = "http://localhost:8080/api" # 1. 创建测试目标 goal_data = { "userId": 1, "subject": "测试学科", "goalType": "练习", "difficulty": "简单", "goalContent": "测试答题提交功能", "totalQuantity": 2 } print("=== 创建测试目标 ===") response = requests.post(f"{base_url}/goals", json=goal_data) print(f"状态码: {response.status_code}") print(f"响应: {response.text}") if response.status_code != 200: print("创建目标失败") return None, [] goal_response = response.json() goal_id = goal_response['data']['goalId'] print(f"创建的目标ID: {goal_id}") # 2. 为目标生成题目 question_data = { "goalId": goal_id, "userId": 1, "subject": "测试学科", "difficulty": "简单", "type": "选择题", "totalQuantity": 2, "knowledgePoint": "测试知识点" } print("=== 生成题目 ===") generate_response = requests.post(f"{base_url}/coze/generate-questions", json=question_data) print(f"状态码: {generate_response.status_code}") print(f"响应: {generate_response.text}") if generate_response.status_code != 200: print("生成题目失败") return goal_id, [] questions_data = generate_response.json() if 'questions' in questions_data: questions = questions_data['questions'] else: questions = questions_data['data'] print(f"生成的题目数量: {len(questions)}") return goal_id, questions def test_submit_answer(): base_url = "http://localhost:8080" # 创建测试数据 goal_id, questions = create_test_goal_and_questions() if not goal_id or not questions: print("无法创建测试数据,跳过答题测试") return # 使用第一道题目进行测试 question = questions[0] content_id = question['detailId'] correct_answer = question.get('answer', '测试答案') print(f"\n=== 使用题目ID: {content_id} 进行测试 ===") # 测试数据 test_cases = [ { "name": "正常提交正确答案", "data": { "userId": 1, "goalId": goal_id, "contentId": content_id, "userAnswer": correct_answer, "answerDuration": 30 } }, { "name": "正常提交错误答案", "data": { "userId": 1, "goalId": goal_id, "contentId": content_id, "userAnswer": "错误答案", "answerDuration": 25 } }, { "name": "缺少用户答案", "data": { "userId": 1, "goalId": goal_id, "contentId": content_id, "answerDuration": 30 } }, { "name": "无效的题目ID", "data": { "userId": 1, "goalId": goal_id, "contentId": 999999, # 不存在的题目ID "userAnswer": "测试答案", "answerDuration": 30 } }, { "name": "缺少必要参数", "data": { "goalId": goal_id, "contentId": content_id, "userAnswer": "测试答案" } } ] for test_case in test_cases: print(f"\n=== {test_case['name']} ===") try: response = requests.post( f"{base_url}/answers/submit", json=test_case['data'], headers={'Content-Type': 'application/json'}, timeout=10 ) print(f"状态码: {response.status_code}") if response.headers.get('content-type', '').startswith('application/json'): try: response_data = response.json() print(f"响应消息: {response_data.get('message', 'N/A')}") print(f"响应代码: {response_data.get('code', 'N/A')}") if response_data.get('data'): data = response_data['data'] print(f"答题记录ID: {data.get('answerId', 'N/A')}") print(f"是否正确: {data.get('isCorrect', 'N/A')}") print(f"用户答案: {data.get('userAnswer', 'N/A')}") except: print(f"响应内容: {response.text}") else: print(f"响应内容: {response.text}") except requests.exceptions.RequestException as e: print(f"请求失败: {e}") except Exception as e: print(f"发生错误: {e}") if __name__ == "__main__": test_submit_answer()