#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 酒店管理系统 - 按钮功能测试脚本 用于验证所有按钮功能是否正常工作 """ import requests import json import time from typing import Dict, List, Tuple # 配置 API_BASE_URL = "http://127.0.0.1:8000" FRONTEND_URL = "http://127.0.0.1:8080/frontend/index.html" # 测试数据 TEST_RESULTS = [] def test_api_endpoint(method: str, endpoint: str, data: Dict = None) -> Tuple[bool, str]: """测试API端点""" try: url = f"{API_BASE_URL}{endpoint}" if method == "GET": response = requests.get(url) elif method == "POST": response = requests.post(url, json=data) elif method == "PUT": response = requests.put(url, json=data) elif method == "DELETE": response = requests.delete(url) if response.status_code in [200, 201]: return True, f"✅ {method} {endpoint} - 成功" else: return False, f"❌ {method} {endpoint} - 状态码: {response.status_code}" except Exception as e: return False, f"❌ {method} {endpoint} - 错误: {str(e)}" def run_api_tests(): """运行所有API测试""" print("\n" + "="*60) print("🔍 酒店管理系统 - 按钮功能验证") print("="*60 + "\n") # 测试基本连接 print("1️⃣ 测试后端连接...") try: response = requests.get(f"{API_BASE_URL}/health") if response.status_code == 200: print("✅ 后端服务正常运行") else: print("❌ 后端服务响应异常") return False except Exception as e: print(f"❌ 无法连接到后端服务: {e}") return False # 测试各个API端点 print("\n2️⃣ 测试API端点...") endpoints = [ ("GET", "/api/rooms", None, "获取房间列表"), ("GET", "/api/guests", None, "获取宾客列表"), ("GET", "/api/employees", None, "获取员工列表"), ("GET", "/api/reservations", None, "获取预订列表"), ("GET", "/api/bills", None, "获取账单列表"), ] for method, endpoint, data, description in endpoints: success, message = test_api_endpoint(method, endpoint, data) TEST_RESULTS.append((success, message, description)) print(f" {message}") # 测试业务流程API print("\n3️⃣ 测试业务流程API...") business_endpoints = [ ("POST", "/api/checkin", {"name": "测试用户", "phone": "13800138000", "id_card": "110000000000000000", "room_id": 1, "nights": 1, "checkin_date": "2024-01-01T10:00", "price": 200}, "入住登记"), ("POST", "/api/checkout", {"order_id": "TEST001", "room_id": 1, "room_fee": 200, "additional_fee": 0, "payment_method": "cash", "remarks": ""}, "退房结账"), ("POST", "/api/services", {"room_id": 1, "service_type": "客房服务", "description": "测试", "priority": "中"}, "服务请求"), ("POST", "/api/reservations", {"guest_name": "测试", "phone": "13800138000", "checkin_date": "2024-01-05", "checkout_date": "2024-01-07", "room_type": "标间", "channel": "电话预约"}, "预订"), ("POST", "/api/bills/TEST001", {}, "账单支付确认"), ] for method, endpoint, data, description in business_endpoints: success, message = test_api_endpoint(method, endpoint, data) TEST_RESULTS.append((success, message, description)) # 不显示详细错误,因为某些端点可能因为业务逻辑而失败 status = "✅" if success else "⚠️" print(f" {status} {description}") return True def check_frontend_methods(): """检查前端是否包含所有必要的方法""" print("\n4️⃣ 检查前端方法定义...") required_methods = [ 'showCheckinModal', 'showCheckoutModal', 'showGuestModal', 'showEmployeeModal', 'showReservationModal', 'showServiceModal', 'viewGuestDetails', 'markRoomCleaning', 'filterRooms', 'updateRoomStatus', 'confirmPayment', 'completeService', 'confirmReservation', 'cancelReservation', 'formatDate', 'getRoomCardStyle', 'getStatusText' ] try: # 这里我们无法直接测试JavaScript,但我们可以验证文件是否存在和有合理的大小 with open('frontend/index.html', 'r', encoding='utf-8') as f: content = f.read() missing_methods = [] for method in required_methods: if method not in content: missing_methods.append(method) if not missing_methods: print("✅ 所有必要方法都已定义") return True else: print(f"⚠️ 缺失方法: {', '.join(missing_methods[:3])}...") return False except Exception as e: print(f"❌ 无法检查前端文件: {e}") return False def print_summary(): """打印测试总结""" print("\n" + "="*60) print("📊 测试总结") print("="*60) success_count = sum(1 for success, _, _ in TEST_RESULTS if success) total_count = len(TEST_RESULTS) print(f"\n总测试数: {total_count}") print(f"成功: {success_count}") print(f"失败: {total_count - success_count}") print(f"成功率: {success_count/total_count*100:.1f}%") if success_count == total_count: print("\n🎉 所有测试通过! 按钮功能已完全实现") else: print("\n⚠️ 某些测试失败,请检查服务器日志") print("\n访问地址: " + FRONTEND_URL) print("\n" + "="*60) def main(): """主函数""" try: run_api_tests() check_frontend_methods() print_summary() except KeyboardInterrupt: print("\n\n测试被中断") except Exception as e: print(f"\n❌ 测试出错: {e}") if __name__ == "__main__": main()