#!/usr/bin/env python3 """ API测试脚本 用于验证新创建的接口是否正常工作 """ import requests import json from datetime import datetime # API基础URL BASE_URL = "http://localhost:8000/api" def test_dashboard_apis(): """测试仪表板相关接口""" print("=== 测试仪表板接口 ===") # 测试KPI接口 try: response = requests.get(f"{BASE_URL}/dashboard/kpi") print(f"KPI接口: {response.status_code}") if response.status_code == 200: print(f"KPI数据: {response.json()}") except Exception as e: print(f"KPI接口错误: {e}") # 测试告警趋势接口 try: response = requests.get(f"{BASE_URL}/dashboard/alarm-trend?days=7") print(f"告警趋势接口: {response.status_code}") if response.status_code == 200: print(f"告警趋势数据: {response.json()}") except Exception as e: print(f"告警趋势接口错误: {e}") # 测试摄像头统计接口 try: response = requests.get(f"{BASE_URL}/dashboard/camera-stats") print(f"摄像头统计接口: {response.status_code}") if response.status_code == 200: print(f"摄像头统计数据: {response.json()}") except Exception as e: print(f"摄像头统计接口错误: {e}") def test_monitor_apis(): """测试监控相关接口""" print("\n=== 测试监控接口 ===") # 测试监控列表接口 try: response = requests.get(f"{BASE_URL}/monitors?page=1&size=10") print(f"监控列表接口: {response.status_code}") if response.status_code == 200: data = response.json() print(f"监控列表: 总数={data.get('total', 0)}") except Exception as e: print(f"监控列表接口错误: {e}") def test_alarm_apis(): """测试告警相关接口""" print("\n=== 测试告警接口 ===") # 测试告警列表接口 try: response = requests.get(f"{BASE_URL}/alarms?page=1&size=10") print(f"告警列表接口: {response.status_code}") if response.status_code == 200: data = response.json() print(f"告警列表: 总数={data.get('total', 0)}") except Exception as e: print(f"告警列表接口错误: {e}") # 测试告警统计接口 try: response = requests.get(f"{BASE_URL}/alarms/stats") print(f"告警统计接口: {response.status_code}") if response.status_code == 200: print(f"告警统计数据: {response.json()}") except Exception as e: print(f"告警统计接口错误: {e}") def test_scene_apis(): """测试场景相关接口""" print("\n=== 测试场景接口 ===") # 测试场景列表接口 try: response = requests.get(f"{BASE_URL}/scenes") print(f"场景列表接口: {response.status_code}") if response.status_code == 200: data = response.json() print(f"场景列表: 数量={len(data.get('scenes', []))}") except Exception as e: print(f"场景列表接口错误: {e}") def test_auth_apis(): """测试认证相关接口""" print("\n=== 测试认证接口 ===") # 测试登录接口 try: login_data = { "username": "admin", "password": "admin123" } response = requests.post(f"{BASE_URL}/auth/login", data=login_data) print(f"登录接口: {response.status_code}") if response.status_code == 200: print("登录成功") else: print(f"登录失败: {response.text}") except Exception as e: print(f"登录接口错误: {e}") def test_upload_apis(): """测试上传相关接口""" print("\n=== 测试上传接口 ===") # 测试上传统计接口 try: response = requests.get(f"{BASE_URL}/upload/stats") print(f"上传统计接口: {response.status_code}") if response.status_code == 200: print(f"上传统计数据: {response.json()}") except Exception as e: print(f"上传统计接口错误: {e}") def main(): """主测试函数""" print("开始API测试...") print(f"测试时间: {datetime.now()}") print(f"API基础URL: {BASE_URL}") # 测试各个模块的接口 test_dashboard_apis() test_monitor_apis() test_alarm_apis() test_scene_apis() test_auth_apis() test_upload_apis() print("\n=== 测试完成 ===") if __name__ == "__main__": main()