""" Phase 2 功能验证测试 验证活动委托视图、市场深度盘口、合约管理页面和表格排序功能 """ import requests import json from typing import Dict, List BASE_URL = "http://localhost:8000/api/v1" def test_active_orders_api(): """测试活动委托 API""" print("\n=== 测试活动委托 API ===") try: response = requests.get(f"{BASE_URL}/trading/orders/active") if response.status_code == 200: orders = response.json() print(f"✓ 活动委托 API 正常,返回 {len(orders)} 个订单") # 验证活动订单只包含未成交和部分成交的订单 active_statuses = ['submitted', 'pending', 'partial_filled', 'not_traded'] for order in orders: status = order.get('status', '').lower() if status not in active_statuses: print(f"⚠ 警告: 订单 {order.get('order_id')} 状态为 {status},不应出现在活动委托中") return True else: print(f"✗ 活动委托 API 失败: {response.status_code}") return False except Exception as e: print(f"✗ 活动委托 API 测试异常: {e}") return False def test_contract_api(): """测试合约 API""" print("\n=== 测试合约 API ===") try: response = requests.get(f"{BASE_URL}/market/contracts") if response.status_code == 200: data = response.json() contracts = data.get('contracts', []) print(f"✓ 合约 API 正常,返回 {len(contracts)} 个合约") # 验证合约数据结构 if contracts: sample = contracts[0] required_fields = ['vt_symbol', 'symbol', 'exchange', 'name', 'product', 'size', 'pricetick'] missing_fields = [f for f in required_fields if f not in sample] if missing_fields: print(f"⚠ 警告: 合约数据缺少字段: {missing_fields}") else: print("✓ 合约数据结构完整") return True else: print(f"✗ 合约 API 失败: {response.status_code}") return False except Exception as e: print(f"✗ 合约 API 测试异常: {e}") return False def test_tick_data_depth(): """测试行情数据是否包含五档盘口数据""" print("\n=== 测试行情数据深度 ===") try: response = requests.get(f"{BASE_URL}/market/ticks") if response.status_code == 200: data = response.json() ticks = data.get('ticks', []) print(f"✓ 行情 API 正常,返回 {len(ticks)} 个行情") # 验证五档数据 if ticks: sample = ticks[0] depth_fields = [ 'bid_price_1', 'bid_price_2', 'bid_price_3', 'bid_price_4', 'bid_price_5', 'bid_volume_1', 'bid_volume_2', 'bid_volume_3', 'bid_volume_4', 'bid_volume_5', 'ask_price_1', 'ask_price_2', 'ask_price_3', 'ask_price_4', 'ask_price_5', 'ask_volume_1', 'ask_volume_2', 'ask_volume_3', 'ask_volume_4', 'ask_volume_5' ] missing_depth = [f for f in depth_fields if f not in sample or sample[f] is None] if missing_depth: print(f"⚠ 警告: 行情数据缺少深度字段: {missing_depth}") print(" 这可能是由于没有实际的行情数据推送") else: print("✓ 行情数据包含完整的五档盘口数据") return True else: print(f"✗ 行情 API 失败: {response.status_code}") return False except Exception as e: print(f"✗ 行情数据测试异常: {e}") return False def test_trades_api(): """测试成交监控 API""" print("\n=== 测试成交监控 API ===") try: response = requests.get(f"{BASE_URL}/trading/trades") if response.status_code == 200: data = response.json() trades = data.get('trades', []) print(f"✓ 成交监控 API 正常,返回 {len(trades)} 条成交记录") return True else: print(f"✗ 成交监控 API 失败: {response.status_code}") return False except Exception as e: print(f"✗ 成交监控 API 测试异常: {e}") return False def test_accounts_api(): """测试资金监控 API""" print("\n=== 测试资金监控 API ===") try: response = requests.get(f"{BASE_URL}/accounts/") if response.status_code == 200: accounts = response.json() print(f"✓ 资金监控 API 正常,返回 {len(accounts)} 个账户") return True else: print(f"✗ 资金监控 API 失败: {response.status_code}") return False except Exception as e: print(f"✗ 资金监控 API 测试异常: {e}") return False def verify_frontend_files(): """验证前端文件是否包含 Phase 2 的修改""" print("\n=== 验证前端文件 ===") # 检查 index.html try: with open('sanguo_web/templates/index.html', 'r', encoding='utf-8') as f: html_content = f.read() checks = [ ('active_orders page', "currentPage === 'active_orders'"), ('contracts page', "currentPage === 'contracts'"), ('order book', 'order-book'), ('table sort', 'sortTable'), ('market depth display', 'bid_price_5') ] for check_name, check_string in checks: if check_string in html_content: print(f"✓ 前端包含 {check_name}") else: print(f"✗ 前端缺少 {check_name}") return True except Exception as e: print(f"✗ 前端文件验证异常: {e}") return False def main(): """主测试函数""" print("=" * 60) print("Phase 2 功能验证测试") print("=" * 60) results = [] # 测试 API results.append(("活动委托 API", test_active_orders_api())) results.append(("合约管理 API", test_contract_api())) results.append(("行情数据深度", test_tick_data_depth())) results.append(("成交监控 API", test_trades_api())) results.append(("资金监控 API", test_accounts_api())) results.append(("前端文件验证", verify_frontend_files())) # 汇总结果 print("\n" + "=" * 60) print("测试结果汇总") print("=" * 60) passed = sum(1 for _, result in results if result) total = len(results) for name, result in results: status = "✓ 通过" if result else "✗ 失败" print(f"{name}: {status}") print(f"\n总计: {passed}/{total} 通过") if passed == total: print("\n🎉 所有测试通过!Phase 2 功能实现完成。") else: print(f"\n⚠ 有 {total - passed} 个测试失败,请检查相关功能。") if __name__ == "__main__": main()