""" 测试角色卡更新API """ import requests import json # 测试配置 BASE_URL = "http://localhost:8000/api" def test_update_character(): """测试更新角色卡""" # 1. 先获取所有角色 print("1. 获取角色列表...") response = requests.get(f"{BASE_URL}/characters/") if response.status_code != 200: print(f"❌ 获取角色列表失败: {response.status_code}") return characters = response.json() if not characters: print("❌ 没有角色卡可供测试") return # 选择第一个角色 test_char = characters[0] char_name = test_char['name'] print(f"✅ 找到角色: {char_name}") # 2. 获取角色详情 print(f"\n2. 获取角色 '{char_name}' 的详情...") response = requests.get(f"{BASE_URL}/characters/{char_name}") if response.status_code != 200: print(f"❌ 获取角色详情失败: {response.status_code}") return original_data = response.json() print(f"✅ 原始描述长度: {len(original_data.get('description', ''))}") # 3. 更新角色(添加测试标记) print(f"\n3. 更新角色 '{char_name}'...") update_data = { "description": original_data.get('description', '') + '\n\n[测试更新标记]', "tags": original_data.get('tags', []) + ['test_update'] } response = requests.put( f"{BASE_URL}/characters/{char_name}", json=update_data, headers={'Content-Type': 'application/json'} ) if response.status_code != 200: print(f"❌ 更新失败: {response.status_code}") print(f"响应内容: {response.text}") return result = response.json() print(f"✅ 更新成功: {result.get('success')}") # 4. 验证更新 print(f"\n4. 验证更新结果...") response = requests.get(f"{BASE_URL}/characters/{char_name}") updated_data = response.json() new_desc = updated_data.get('description', '') if '[测试更新标记]' in new_desc: print("✅ 验证成功:描述已更新") else: print("❌ 验证失败:描述未更新") if 'test_update' in updated_data.get('tags', []): print("✅ 验证成功:标签已更新") else: print("❌ 验证失败:标签未更新") # 5. 恢复原始数据 print(f"\n5. 恢复原始数据...") response = requests.put( f"{BASE_URL}/characters/{char_name}", json={ "description": original_data.get('description', ''), "tags": original_data.get('tags', []) }, headers={'Content-Type': 'application/json'} ) if response.status_code == 200: print("✅ 数据已恢复") else: print(f"⚠️ 恢复失败: {response.status_code}") if __name__ == "__main__": try: test_update_character() except Exception as e: print(f"❌ 测试出错: {e}") import traceback traceback.print_exc()