From cdbff9c2c590bab942385c37e5275be9ce0528f2 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 26 Apr 2025 16:22:34 -0400 Subject: [PATCH 1/5] Refactoring test --- src/main.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index f2d73dc..21a291d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -197,6 +197,17 @@ mod tests { } } + mod init { + pub async fn app(pool: sqlx::PgPool) -> axum::Router { + let app = crate::init::routes() + .await + .layer(axum::Extension(pool)) + .layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024)) + .layer(tower_http::timeout::TimeoutLayer::new(std::time::Duration::from_secs(300))); + app + } + } + #[tokio::test] async fn test_song_queue() { let tm_pool = db_mgr::get_pool().await.unwrap(); @@ -214,11 +225,7 @@ mod tests { let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); db::migrations(&pool).await; - let app = crate::init::routes() - .await - .layer(axum::Extension(pool)) - .layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024)) - .layer(TimeoutLayer::new(Duration::from_secs(300))); + let app = init::app(pool).await; // Create multipart form let mut form = MultipartForm::default(); -- 2.47.3 From aad8f0d74227311c010faaa0f0b300f42b791731 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 26 Apr 2025 16:26:10 -0400 Subject: [PATCH 2/5] Test refactor --- src/main.rs | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 21a291d..456a9e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,9 +117,8 @@ mod tests { use common_multipart_rfc7578::client::multipart::{ Body as MultipartBody, Form as MultipartForm, }; - use std::{time::Duration, usize}; + use std::{usize}; use tower::ServiceExt; - use tower_http::timeout::TimeoutLayer; mod db_mgr { use std::str::FromStr; @@ -199,12 +198,11 @@ mod tests { mod init { pub async fn app(pool: sqlx::PgPool) -> axum::Router { - let app = crate::init::routes() + crate::init::routes() .await .layer(axum::Extension(pool)) .layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024)) - .layer(tower_http::timeout::TimeoutLayer::new(std::time::Duration::from_secs(300))); - app + .layer(tower_http::timeout::TimeoutLayer::new(std::time::Duration::from_secs(300))) } } @@ -278,11 +276,7 @@ mod tests { let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); db::migrations(&pool).await; - let app = crate::init::routes() - .await - .layer(axum::Extension(pool)) - .layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024)) - .layer(TimeoutLayer::new(Duration::from_secs(300))); + let app = init::app(pool).await; // Create multipart form let mut form = MultipartForm::default(); @@ -356,11 +350,7 @@ mod tests { let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); db::migrations(&pool).await; - let app = crate::init::routes() - .await - .layer(axum::Extension(pool)) - .layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024)) - .layer(TimeoutLayer::new(Duration::from_secs(300))); + let app = init::app(pool).await; // Create multipart form let mut form = MultipartForm::default(); -- 2.47.3 From 773b0780f6f1580647b726dbd4661ed249dc066f Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 26 Apr 2025 16:42:12 -0400 Subject: [PATCH 3/5] Added code to get response content --- src/main.rs | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 456a9e9..ab51e52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -206,6 +206,36 @@ mod tests { } } + async fn song_queue_req(app: &axum::Router) -> Result { + // Create multipart form + let mut form = MultipartForm::default(); + let _ = form.add_file("flac", "tests/Machine_gun/track01.flac"); + + // Create request + let content_type = form.content_type(); + let body = MultipartBody::from(form); + let req = axum::http::Request::builder() + .method(axum::http::Method::POST) + .uri(crate::callers::endpoints::QUEUESONG) + .header(axum::http::header::CONTENT_TYPE, content_type) + .body(axum::body::Body::from_stream(body)) + .unwrap(); + app.clone().oneshot(req).await + } + + pub async fn get_resp_data(response: axum::response::Response) -> Data + + where + Data: for<'a>serde::Deserialize<'a> + { + let body = axum::body::to_bytes(response.into_body(), usize::MAX) + .await + .unwrap(); + let resp: Data = + serde_json::from_slice(&body).unwrap(); + resp + } + #[tokio::test] async fn test_song_queue() { let tm_pool = db_mgr::get_pool().await.unwrap(); @@ -225,29 +255,18 @@ mod tests { let app = init::app(pool).await; - // Create multipart form - let mut form = MultipartForm::default(); - let _ = form.add_file("flac", "tests/Machine_gun/track01.flac"); - - // Create request - let content_type = form.content_type(); - let body = MultipartBody::from(form); - let req = axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::endpoints::QUEUESONG) - .header(axum::http::header::CONTENT_TYPE, content_type) - .body(axum::body::Body::from_stream(body)) - .unwrap(); - // Send request - match app.oneshot(req).await { + match song_queue_req(&app).await { Ok(response) => { + /* let body = axum::body::to_bytes(response.into_body(), usize::MAX) .await .unwrap(); println!("Body: {:?}", body); let resp: crate::callers::song::response::Response = serde_json::from_slice(&body).unwrap(); + */ + let resp = get_resp_data::(response).await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); assert_eq!(false, resp.data[0].is_nil(), "Should not be empty"); } -- 2.47.3 From 70da022a9280ce00bed42c6a9817e9297c5174cb Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 26 Apr 2025 16:50:31 -0400 Subject: [PATCH 4/5] More refactoring --- src/main.rs | 71 ++++++----------------------------------------------- 1 file changed, 7 insertions(+), 64 deletions(-) diff --git a/src/main.rs b/src/main.rs index ab51e52..9c30c17 100644 --- a/src/main.rs +++ b/src/main.rs @@ -224,16 +224,13 @@ mod tests { } pub async fn get_resp_data(response: axum::response::Response) -> Data - where Data: for<'a>serde::Deserialize<'a> { let body = axum::body::to_bytes(response.into_body(), usize::MAX) .await .unwrap(); - let resp: Data = - serde_json::from_slice(&body).unwrap(); - resp + serde_json::from_slice(&body).unwrap() } #[tokio::test] @@ -258,14 +255,6 @@ mod tests { // Send request match song_queue_req(&app).await { Ok(response) => { - /* - let body = axum::body::to_bytes(response.into_body(), usize::MAX) - .await - .unwrap(); - println!("Body: {:?}", body); - let resp: crate::callers::song::response::Response = - serde_json::from_slice(&body).unwrap(); - */ let resp = get_resp_data::(response).await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); assert_eq!(false, resp.data[0].is_nil(), "Should not be empty"); @@ -297,29 +286,10 @@ mod tests { let app = init::app(pool).await; - // Create multipart form - let mut form = MultipartForm::default(); - let _ = form.add_file("flac", "tests/Machine_gun/track01.flac"); - - // Create request - let content_type = form.content_type(); - let body = MultipartBody::from(form); - let req = axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::endpoints::QUEUESONG) - .header(axum::http::header::CONTENT_TYPE, content_type) - .body(axum::body::Body::from_stream(body)) - .unwrap(); - // Send request - match app.clone().oneshot(req).await { + match song_queue_req(&app).await { Ok(response) => { - let body = axum::body::to_bytes(response.into_body(), usize::MAX) - .await - .unwrap(); - println!("Body: {:?}", body); - let resp: crate::callers::song::response::Response = - serde_json::from_slice(&body).unwrap(); + let resp = get_resp_data::(response).await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); assert_eq!(false, resp.data[0].is_nil(), "Should not be empty"); @@ -332,11 +302,7 @@ mod tests { match app.clone().oneshot(fetch_req).await { Ok(response) => { - let body = axum::body::to_bytes(response.into_body(), usize::MAX) - .await - .unwrap(); - let resp: crate::callers::song::response::fetch_queue_song::Response = - serde_json::from_slice(&body).unwrap(); + let resp = get_resp_data::(response).await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); } Err(err) => { @@ -371,29 +337,10 @@ mod tests { let app = init::app(pool).await; - // Create multipart form - let mut form = MultipartForm::default(); - let _ = form.add_file("flac", "tests/Machine_gun/track01.flac"); - - // Create request - let content_type = form.content_type(); - let body = MultipartBody::from(form); - let req = axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::endpoints::QUEUESONG) - .header(axum::http::header::CONTENT_TYPE, content_type) - .body(axum::body::Body::from_stream(body)) - .unwrap(); - // Send request - match app.clone().oneshot(req).await { + match song_queue_req(&app).await { Ok(response) => { - let body = axum::body::to_bytes(response.into_body(), usize::MAX) - .await - .unwrap(); - println!("Body: {:?}", body); - let resp: crate::callers::song::response::Response = - serde_json::from_slice(&body).unwrap(); + let resp = get_resp_data::(response).await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); assert_eq!(false, resp.data[0].is_nil(), "Should not be empty"); let new_payload: serde_json::Value = serde_json::json!( @@ -425,11 +372,7 @@ mod tests { .await { Ok(response) => { - let body = axum::body::to_bytes(response.into_body(), usize::MAX) - .await - .unwrap(); - let resp: crate::callers::song::response::Response = - serde_json::from_slice(&body).unwrap(); + let resp = get_resp_data::(response).await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); } Err(err) => { -- 2.47.3 From ba75441eaea505fe59c39d68fef41b17c5b5a6da Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 26 Apr 2025 16:51:54 -0400 Subject: [PATCH 5/5] Code refactor --- src/main.rs | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9c30c17..f3cc9e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,7 +117,7 @@ mod tests { use common_multipart_rfc7578::client::multipart::{ Body as MultipartBody, Form as MultipartForm, }; - use std::{usize}; + use std::usize; use tower::ServiceExt; mod db_mgr { @@ -202,11 +202,15 @@ mod tests { .await .layer(axum::Extension(pool)) .layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024)) - .layer(tower_http::timeout::TimeoutLayer::new(std::time::Duration::from_secs(300))) + .layer(tower_http::timeout::TimeoutLayer::new( + std::time::Duration::from_secs(300), + )) } } - async fn song_queue_req(app: &axum::Router) -> Result { + async fn song_queue_req( + app: &axum::Router, + ) -> Result { // Create multipart form let mut form = MultipartForm::default(); let _ = form.add_file("flac", "tests/Machine_gun/track01.flac"); @@ -223,14 +227,14 @@ mod tests { app.clone().oneshot(req).await } - pub async fn get_resp_data(response: axum::response::Response) -> Data - where - Data: for<'a>serde::Deserialize<'a> + pub async fn get_resp_data(response: axum::response::Response) -> Data + where + Data: for<'a> serde::Deserialize<'a>, { - let body = axum::body::to_bytes(response.into_body(), usize::MAX) - .await - .unwrap(); - serde_json::from_slice(&body).unwrap() + let body = axum::body::to_bytes(response.into_body(), usize::MAX) + .await + .unwrap(); + serde_json::from_slice(&body).unwrap() } #[tokio::test] @@ -255,7 +259,8 @@ mod tests { // Send request match song_queue_req(&app).await { Ok(response) => { - let resp = get_resp_data::(response).await; + let resp = + get_resp_data::(response).await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); assert_eq!(false, resp.data[0].is_nil(), "Should not be empty"); } @@ -289,7 +294,8 @@ mod tests { // Send request match song_queue_req(&app).await { Ok(response) => { - let resp = get_resp_data::(response).await; + let resp = + get_resp_data::(response).await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); assert_eq!(false, resp.data[0].is_nil(), "Should not be empty"); @@ -302,7 +308,10 @@ mod tests { match app.clone().oneshot(fetch_req).await { Ok(response) => { - let resp = get_resp_data::(response).await; + let resp = get_resp_data::< + crate::callers::song::response::fetch_queue_song::Response, + >(response) + .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); } Err(err) => { @@ -340,7 +349,8 @@ mod tests { // Send request match song_queue_req(&app).await { Ok(response) => { - let resp = get_resp_data::(response).await; + let resp = + get_resp_data::(response).await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); assert_eq!(false, resp.data[0].is_nil(), "Should not be empty"); let new_payload: serde_json::Value = serde_json::json!( @@ -372,7 +382,9 @@ mod tests { .await { Ok(response) => { - let resp = get_resp_data::(response).await; + let resp = + get_resp_data::(response) + .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); } Err(err) => { -- 2.47.3