// #[cfg(test)] // mod tests { // use super::*; // use axum::{ // http::{Request, StatusCode}, // Router, // routing::{get, put}, // body::Body, // }; // use sea_orm::{ // MockDatabase, MockExecResult, DatabaseConnection, DatabaseTransaction, // entity::prelude::*, // QueryFilter, Condition, DbErr, EntityTrait, // }; // use serde_json::{json, Value}; // use tower::ServiceExt; // use std::sync::Arc; // // // 模拟 Book 实体 // #[derive(Clone, Debug, PartialEq, DeriveEntityModel)] // #[sea_orm(table_name = "books")] // pub struct Model { // #[sea_orm(primary_key)] // pub id: i32, // pub title: String, // pub author: String, // } // // #[derive(Copy, Clone, Debug, EnumIter)] // pub enum Relation {} // // impl Related for Entity { // fn to() -> RelationDef { // panic!("No relations defined") // } // } // // // 创建测试用的 Router // async fn setup_router(db: DatabaseConnection) -> Router { // Router::new() // .route("/books/:id", get(get_book_by_id).put(update_book_by_id)) // .route("/books", get(get_all_book)) // .with_state(Arc::new(db)) // } // // // 测试 get_book_by_id // #[tokio::test] // async fn test_get_book_by_id() { // // 设置模拟数据库 // let db = MockDatabase::new(DatabaseBackend::Postgres) // .append_query_results(vec![vec![Model { // id: 1, // title: "Test Book".to_string(), // author: "Test Author".to_string(), // }]]) // .into_connection(); // // let app = setup_router(db).await; // // // 构造请求 // let request = Request::builder() // .uri("/books/1") // .method("GET") // .body(Body::empty()) // .unwrap(); // // // 发送请求 // let response = app.oneshot(request).await.unwrap(); // assert_eq!(response.status(), StatusCode::OK); // // // 解析响应 // let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); // let body: Value = serde_json::from_slice(&body).unwrap(); // assert_eq!( // body, // json!({ // "id": 1, // "title": "Test Book", // "author": "Test Author" // }) // ); // } // // // 测试 get_book_by_id 未找到 // #[tokio::test] // async fn test_get_book_by_id_not_found() { // let db = MockDatabase::new(DatabaseBackend::Postgres) // .append_query_results(vec![vec![] as Vec]) // .into_connection(); // // let app = setup_router(db).await; // // let request = Request::builder() // .uri("/books/999") // .method("GET") // .body(Body::empty()) // .unwrap(); // // let response = app.oneshot(request).await.unwrap(); // assert_eq!(response.status(), StatusCode::NOT_FOUND); // } // // // 测试 update_book_by_id // #[tokio::test] // async fn test_update_book_by_id() { // let db = MockDatabase::new(DatabaseBackend::Postgres) // .append_query_results(vec![vec![Model { // id: 1, // title: "Updated Book".to_string(), // author: "Updated Author".to_string(), // }]]) // .append_exec_results(vec![MockExecResult { // last_insert_id: 1, // rows_affected: 1, // }]) // .into_connection(); // // let app = setup_router(db).await; // // // 构造请求 // let request = Request::builder() // .uri("/books/1") // .method("PUT") // .header("Content-Type", "application/json") // .body(Body::from( // json!({ // "title": "Updated Book", // "author": "Updated Author" // }) // .to_string(), // )) // .unwrap(); // // // 发送请求 // let response = app.oneshot(request).await.unwrap(); // assert_eq!(response.status(), StatusCode::OK); // // // 解析响应 // let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); // let body: Value = serde_json::from_slice(&body).unwrap(); // assert_eq!( // body, // json!({ // "id": 1, // "title": "Updated Book", // "author": "Updated Author" // }) // ); // } // // // 测试 get_all_book // #[tokio::test] // async fn test_get_all_book() { // let db = MockDatabase::new(DatabaseBackend::Postgres) // .append_query_results(vec![vec![ // Model { // id: 1, // title: "Book 1".to_string(), // author: "Author 1".to_string(), // }, // Model { // id: 2, // title: "Book 2".to_string(), // author: "Author 2".to_string(), // }, // ]]) // .into_connection(); // // let app = setup_router(db).await; // // let request = Request::builder() // .uri("/books") // .method("GET") // .body(Body::empty()) // .unwrap(); // // let response = app.oneshot(request).await.unwrap(); // assert_eq!(response.status(), StatusCode::OK); // // let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); // let body: Value = serde_json::from_slice(&body).unwrap(); // assert_eq!( // body, // json!([ // { // "id": 1, // "title": "Book 1", // "author": "Author 1" // }, // { // "id": 2, // "title": "Book 2", // "author": "Author 2" // } // ]) // ); // } // }