//! 新API功能测试 //! //! 测试新实现的API端点: //! - 冻结/解冻接口(支持金额参数) //! - 三账校验API //! - 账户列表分页和筛选 //! //! 注意:这些测试需要真实数据库连接,运行前请确保: //! 1. MySQL数据库已启动 //! 2. 设置了正确的 DATABASE_URL 环境变量 //! 3. 数据库已初始化(运行了迁移脚本) use reqwest::Client; use serde_json::json; const BASE_URL: &str = "http://localhost:8080"; /// 测试健康检查 #[tokio::test] #[ignore] // 需要服务运行 async fn test_health_check() { let client = Client::new(); let response = client.get(&format!("{}/health", BASE_URL)).send().await; if let Ok(resp) = response { assert_eq!(resp.status(), 200); let body: serde_json::Value = resp.json().await.unwrap(); assert_eq!(body["status"], "ok"); } else { // 服务未运行,跳过测试 println!("⚠️ 服务未运行,跳过健康检查测试"); } } /// 测试账户列表分页 #[tokio::test] #[ignore] async fn test_list_accounts_pagination() { let client = Client::new(); // 测试分页参数 let response = client .get(&format!("{}/api/v1/physical-accounts?page=1&page_size=10", BASE_URL)) .send() .await; if let Ok(resp) = response { assert_eq!(resp.status(), 200); let body: serde_json::Value = resp.json().await.unwrap(); assert!(body["success"].as_bool().unwrap_or(false)); if let Some(data) = body["data"].as_object() { assert!(data.contains_key("data")); assert!(data.contains_key("total")); assert_eq!(data["page"], 1); assert_eq!(data["page_size"], 10); } } else { println!("⚠️ 服务未运行,跳过分页测试"); } } /// 测试账户列表筛选 #[tokio::test] #[ignore] async fn test_list_accounts_filter() { let client = Client::new(); // 测试状态筛选 let response = client .get(&format!("{}/api/v1/physical-accounts?status=active", BASE_URL)) .send() .await; if response.is_ok() { assert_eq!(response.unwrap().status(), 200); } else { println!("⚠️ 服务未运行,跳过筛选测试"); } } /// 测试冻结账户金额 #[tokio::test] #[ignore] async fn test_freeze_account_amount() { let client = Client::new(); let account_id = 1; let response = client .post(&format!("{}/api/v1/physical-accounts/{}/freeze", BASE_URL, account_id)) .json(&json!({ "amount": 100.00 })) .send() .await; if let Ok(resp) = response { let status = resp.status(); assert!(status == 200 || status == 404 || status == 400); } else { println!("⚠️ 服务未运行,跳过冻结测试"); } } /// 测试解冻账户金额 #[tokio::test] #[ignore] async fn test_unfreeze_account_amount() { let client = Client::new(); let account_id = 1; let response = client .post(&format!("{}/api/v1/physical-accounts/{}/unfreeze", BASE_URL, account_id)) .json(&json!({ "amount": 50.00 })) .send() .await; if let Ok(resp) = response { let status = resp.status(); assert!(status == 200 || status == 404 || status == 400); } else { println!("⚠️ 服务未运行,跳过解冻测试"); } } /// 测试三账校验API #[tokio::test] #[ignore] async fn test_three_account_verification() { let client = Client::new(); let account_id = 1; let response = client .get(&format!("{}/api/v1/reconciliation/three-account/{}", BASE_URL, account_id)) .send() .await; if let Ok(resp) = response { if resp.status() == 200 { let body: serde_json::Value = resp.json().await.unwrap(); assert!(body["success"].as_bool().unwrap_or(false)); let data = &body["data"]; assert!(data["physical_account_id"].is_number()); assert!(data["bank_balance"].is_number()); assert!(data["transit_net"].is_number()); assert!(data["ledger_total"].is_number()); assert!(data["is_balanced"].is_boolean()); } else { assert_eq!(resp.status(), 404); } } else { println!("⚠️ 服务未运行,跳过三账校验测试"); } }