From 2b5b04aa3d1be877d327ae62953adfcd4c204748 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 5 Jun 2025 20:41:52 -0400 Subject: [PATCH 1/9] Added new status and made changes to song queue status code --- migrations/20250420185217_init_migration.sql | 2 +- src/callers/song.rs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/migrations/20250420185217_init_migration.sql b/migrations/20250420185217_init_migration.sql index e5a6d6a..68cdfea 100644 --- a/migrations/20250420185217_init_migration.sql +++ b/migrations/20250420185217_init_migration.sql @@ -5,7 +5,7 @@ CREATE EXTENSION IF NOT EXISTS pgcrypto; CREATE TABLE IF NOT EXISTS "songQueue" ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), filename TEXT NOT NULL, - status TEXT CHECK (status IN ('pending', 'processing', 'done')), + status TEXT CHECK (status IN ('pending', 'ready', 'processing', 'done')), data BYTEA NULL ); diff --git a/src/callers/song.rs b/src/callers/song.rs index f1e4ac3..ed9b13b 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -148,11 +148,12 @@ pub mod response { pub mod status { pub const PENDING: &str = "pending"; + pub const READY: &str = "ready"; pub const PROCESSING: &str = "processing"; pub const DONE: &str = "done"; pub async fn is_valid(status: &str) -> bool { - status == PENDING || status == PROCESSING || status == DONE + status == PENDING || status == PROCESSING || status == DONE || status == READY } } @@ -396,7 +397,7 @@ mod song_queue { "#, ) .bind(super::status::PROCESSING) - .bind(super::status::PENDING) + .bind(super::status::READY) .fetch_one(pool) .await .map_err(|e| { -- 2.47.3 From 82b9ca3cb008d75bf4b61fa2865d534099e982a0 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 7 Jun 2025 13:09:16 -0400 Subject: [PATCH 2/9] Saving changes --- src/main.rs | 681 +++++++++++++++++++++++++++++----------------------- 1 file changed, 385 insertions(+), 296 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9d68922..88ef7fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -508,8 +508,65 @@ mod tests { } } } + + pub async fn queue_song_and_coverart_flow(app: &axum::Router) -> Result { + match queue_song_flow(&app).await { + Ok(response) => { + let resp = super::get_resp_data::< + crate::callers::metadata::response::fetch_metadata::Response, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Data should not be empty"); + let song_queue_id = resp.data[0].song_queue_id; + + match super::create_song_req(&app, &song_queue_id).await { + Ok(response) => { + let resp = super::get_resp_data::< + crate::callers::song::response::create_metadata::Response, + >(response) + .await; + assert_eq!( + false, + resp.data.is_empty(), + "No songs found, Response {:?}", + resp + ); + let song = &resp.data[0]; + let song_id = song.id; + assert_eq!( + false, + song_id.is_nil(), + "Song id should not be nil {:?}", + song + ); + + eprintln!("Song: {:?}", song); + + match queue_coverart_flow(&app, &song_queue_id).await { + Ok(response) => { + Ok(response) + } + Err(err) => { + assert!(false, "Error: {:?}", err); + Err(err) + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + Err(err) + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + Err(err) + } + } + } } + pub async fn resp_to_bytes( response: axum::response::Response, ) -> Result { @@ -577,121 +634,363 @@ mod tests { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } - #[tokio::test] - async fn test_song_fetch_queue_item() { - let tm_pool = db_mgr::get_pool().await.unwrap(); - let db_name = db_mgr::generate_db_name().await; + mod special_area { - match db_mgr::create_database(&tm_pool, &db_name).await { - Ok(_) => { - println!("Success"); - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - } + use super::*; - let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); - db::migrations(&pool).await; + use std::io::Write; - let app = init::app(pool).await; + #[tokio::test] + async fn test_song_fetch_queue_item() { + let tm_pool = db_mgr::get_pool().await.unwrap(); + let db_name = db_mgr::generate_db_name().await; - // Send request - match song_queue_req(&app).await { - Ok(response) => { - 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"); - - match fetch_queue_req(&app).await { - Ok(response) => { - 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) => { - assert!(false, "Error: {:?}", err); - } + match db_mgr::create_database(&tm_pool, &db_name).await { + Ok(_) => { + println!("Success"); + } + Err(err) => { + assert!(false, "Error: {:?}", err); } } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - }; - let _ = db_mgr::drop_database(&tm_pool, &db_name).await; - } + let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); + db::migrations(&pool).await; - #[tokio::test] - async fn test_song_fetch_queue_data() { - let tm_pool = db_mgr::get_pool().await.unwrap(); - let db_name = db_mgr::generate_db_name().await; + let app = init::app(pool).await; - match db_mgr::create_database(&tm_pool, &db_name).await { - Ok(_) => { - println!("Success"); - } - Err(err) => { - assert!(false, "Error: {:?}", err); + match sequence_flow::queue_song_and_coverart_flow(&app).await { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::coverart::response::fetch_coverart_no_data::Response, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + + let resp_coverart_queue_id = resp.data[0].id; + + let payload = serde_json::json!({ + "coverart_queue_id": resp_coverart_queue_id + }); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } } + + + + // Send request + match song_queue_req(&app).await { + Ok(response) => { + 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"); + + match fetch_queue_req(&app).await { + Ok(response) => { + 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) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }; + + let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } - let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); - db::migrations(&pool).await; + #[tokio::test] + async fn test_update_song_from_queue() { + let tm_pool = db_mgr::get_pool().await.unwrap(); + let db_name = db_mgr::generate_db_name().await; - let app = init::app(pool).await; + match db_mgr::create_database(&tm_pool, &db_name).await { + Ok(_) => { + println!("Success"); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } - // Send request - match song_queue_req(&app).await { - Ok(response) => { - 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 pool = db_mgr::connect_to_db(&db_name).await.unwrap(); + db::migrations(&pool).await; - match fetch_queue_req(&app).await { - Ok(response) => { - 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"); - let id = resp.data[0].id; + let app = init::app(pool).await; + + // Send request + match song_queue_req(&app).await { + Ok(response) => { + 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"); + + match fetch_queue_req(&app).await { + Ok(response) => { + 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"); + let id = resp.data[0].id; + + match fetch_queue_data_req(&app, &id).await { + Ok(response) => match resp_to_bytes(response).await { + Ok(bytes) => { + assert_eq!( + false, + bytes.is_empty(), + "Queued data should not be empty" + ); + + let temp_file = tempfile::tempdir() + .expect("Could not create test directory"); + let test_dir = String::from(temp_file.path().to_str().unwrap()); + let new_file = format!("{}/new_file.flac", test_dir); + + let mut file = std::fs::File::create(&new_file).unwrap(); + file.write_all(&bytes).unwrap(); + + let mut form = MultipartForm::default(); + let _ = form.add_file("flac", new_file); + + // Create request + let content_type = form.content_type(); + let body = MultipartBody::from(form); + + let raw_uri = + String::from(crate::callers::endpoints::QUEUESONGUPDATE); + let end_index = raw_uri.len() - 5; + + let uri = format!( + "{}/{}", + (&raw_uri[..end_index]).to_string(), + id.to_string() + ); + + match app + .clone() + .oneshot( + axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(uri) + .header( + axum::http::header::CONTENT_TYPE, + content_type, + ) + .body(axum::body::Body::from_stream(body)) + .unwrap(), + ) + .await + { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::song::response::update_song_queue::Response, + >(response) + .await; + assert_eq!( + false, + resp.data.is_empty(), + "Should not be empty" + ); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }, + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }; + + let _ = db_mgr::drop_database(&tm_pool, &db_name).await; + } + + #[tokio::test] + async fn test_song_fetch_queue_data() { + let tm_pool = db_mgr::get_pool().await.unwrap(); + let db_name = db_mgr::generate_db_name().await; + + match db_mgr::create_database(&tm_pool, &db_name).await { + Ok(_) => { + println!("Success"); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + + let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); + db::migrations(&pool).await; + + let app = init::app(pool).await; + + // Send request + match song_queue_req(&app).await { + Ok(response) => { + 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"); + + match fetch_queue_req(&app).await { + Ok(response) => { + 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"); + let id = resp.data[0].id; + + match fetch_queue_data_req(&app, &id).await { + Ok(response) => match resp_to_bytes(response).await { + Ok(bytes) => { + assert_eq!( + false, + bytes.is_empty(), + "Queued data should not be empty" + ); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }, + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }; + + let _ = db_mgr::drop_database(&tm_pool, &db_name).await; + } + + #[tokio::test] + async fn test_song_queue_update_status() { + let tm_pool = db_mgr::get_pool().await.unwrap(); + let db_name = db_mgr::generate_db_name().await; + + match db_mgr::create_database(&tm_pool, &db_name).await { + Ok(_) => { + println!("Success"); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + + let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); + db::migrations(&pool).await; + + let app = init::app(pool).await; + + // Send request + match song_queue_req(&app).await { + Ok(response) => { + 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"); + + match fetch_queue_req(&app).await { + Ok(response) => { + 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"); + + let old = &resp.data[0].status; + let done = crate::callers::song::status::DONE; + let payload = serde_json::json!({ + "id": &resp.data[0].id, + "status": done, + }); + + match app + .clone() + .oneshot( + axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(crate::callers::endpoints::QUEUESONG) + .header(axum::http::header::CONTENT_TYPE, "application/json") + .body(axum::body::Body::from(payload.to_string())) + .unwrap(), + ) + .await + { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::song::response::update_status::Response, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + let changed_status = &resp.data[0]; - match fetch_queue_data_req(&app, &id).await { - Ok(response) => match resp_to_bytes(response).await { - Ok(bytes) => { assert_eq!( - false, - bytes.is_empty(), - "Queued data should not be empty" + *old, changed_status.old_status, + "Old status does not match" + ); + assert_eq!( + done, changed_status.new_status, + "New status does not match" ); } Err(err) => { assert!(false, "Error: {:?}", err); } - }, - Err(err) => { - assert!(false, "Error: {:?}", err); } } - } - Err(err) => { - assert!(false, "Error: {:?}", err); + Err(err) => { + assert!(false, "Error: {:?}", err); + } } } - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - }; + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }; - let _ = db_mgr::drop_database(&tm_pool, &db_name).await; + let _ = db_mgr::drop_database(&tm_pool, &db_name).await; + } } + + #[tokio::test] async fn test_song_metadata_queue() { let tm_pool = db_mgr::get_pool().await.unwrap(); @@ -885,94 +1184,6 @@ mod tests { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } - #[tokio::test] - async fn test_song_queue_update_status() { - let tm_pool = db_mgr::get_pool().await.unwrap(); - let db_name = db_mgr::generate_db_name().await; - - match db_mgr::create_database(&tm_pool, &db_name).await { - Ok(_) => { - println!("Success"); - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - } - - let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); - db::migrations(&pool).await; - - let app = init::app(pool).await; - - // Send request - match song_queue_req(&app).await { - Ok(response) => { - 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"); - - match fetch_queue_req(&app).await { - Ok(response) => { - 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"); - - let old = &resp.data[0].status; - let done = crate::callers::song::status::DONE; - let payload = serde_json::json!({ - "id": &resp.data[0].id, - "status": done, - }); - - match app - .clone() - .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(crate::callers::endpoints::QUEUESONG) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .body(axum::body::Body::from(payload.to_string())) - .unwrap(), - ) - .await - { - Ok(response) => { - let resp = get_resp_data::< - crate::callers::song::response::update_status::Response, - >(response) - .await; - assert_eq!(false, resp.data.is_empty(), "Should not be empty"); - let changed_status = &resp.data[0]; - - assert_eq!( - *old, changed_status.old_status, - "Old status does not match" - ); - assert_eq!( - done, changed_status.new_status, - "New status does not match" - ); - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - } - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - } - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - }; - - let _ = db_mgr::drop_database(&tm_pool, &db_name).await; - } #[tokio::test] async fn test_fetch_coverart_queue_without_data() { @@ -1130,128 +1341,6 @@ mod tests { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } - #[tokio::test] - async fn test_update_song_from_queue() { - let tm_pool = db_mgr::get_pool().await.unwrap(); - let db_name = db_mgr::generate_db_name().await; - - match db_mgr::create_database(&tm_pool, &db_name).await { - Ok(_) => { - println!("Success"); - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - } - - let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); - db::migrations(&pool).await; - - let app = init::app(pool).await; - - // Send request - match song_queue_req(&app).await { - Ok(response) => { - 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"); - - match fetch_queue_req(&app).await { - Ok(response) => { - 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"); - let id = resp.data[0].id; - - match fetch_queue_data_req(&app, &id).await { - Ok(response) => match resp_to_bytes(response).await { - Ok(bytes) => { - assert_eq!( - false, - bytes.is_empty(), - "Queued data should not be empty" - ); - - let temp_file = tempfile::tempdir() - .expect("Could not create test directory"); - let test_dir = String::from(temp_file.path().to_str().unwrap()); - let new_file = format!("{}/new_file.flac", test_dir); - - let mut file = std::fs::File::create(&new_file).unwrap(); - file.write_all(&bytes).unwrap(); - - let mut form = MultipartForm::default(); - let _ = form.add_file("flac", new_file); - - // Create request - let content_type = form.content_type(); - let body = MultipartBody::from(form); - - let raw_uri = - String::from(crate::callers::endpoints::QUEUESONGUPDATE); - let end_index = raw_uri.len() - 5; - - let uri = format!( - "{}/{}", - (&raw_uri[..end_index]).to_string(), - id.to_string() - ); - - match app - .clone() - .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(uri) - .header( - axum::http::header::CONTENT_TYPE, - content_type, - ) - .body(axum::body::Body::from_stream(body)) - .unwrap(), - ) - .await - { - Ok(response) => { - let resp = get_resp_data::< - crate::callers::song::response::update_song_queue::Response, - >(response) - .await; - assert_eq!( - false, - resp.data.is_empty(), - "Should not be empty" - ); - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - } - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - }, - Err(err) => { - assert!(false, "Error: {:?}", err); - } - } - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - } - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - }; - - let _ = db_mgr::drop_database(&tm_pool, &db_name).await; - } #[tokio::test] async fn test_create_song() { -- 2.47.3 From fe89320d778244363ae8d3408f82b5dec5b056b9 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 7 Jun 2025 13:21:44 -0400 Subject: [PATCH 3/9] Got a test working --- src/main.rs | 177 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 104 insertions(+), 73 deletions(-) diff --git a/src/main.rs b/src/main.rs index 88ef7fd..fdbb7e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -564,6 +564,49 @@ mod tests { } } } + + // pub async + /* + let done = crate::callers::song::status::DONE; + let payload = serde_json::json!({ + "id": &resp.data[0].id, + "status": done, + }); + + match app + .clone() + .oneshot( + axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(crate::callers::endpoints::QUEUESONG) + .header(axum::http::header::CONTENT_TYPE, "application/json") + .body(axum::body::Body::from(payload.to_string())) + .unwrap(), + ) + .await + { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::song::response::update_status::Response, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + let changed_status = &resp.data[0]; + + assert_eq!( + *old, changed_status.old_status, + "Old status does not match" + ); + assert_eq!( + done, changed_status.new_status, + "New status does not match" + ); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + */ } @@ -736,89 +779,77 @@ mod tests { assert_eq!(false, resp.data.is_empty(), "Should not be empty"); assert_eq!(false, resp.data[0].is_nil(), "Should not be empty"); - match fetch_queue_req(&app).await { - Ok(response) => { - 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"); - let id = resp.data[0].id; + let id = &resp.data[0]; - match fetch_queue_data_req(&app, &id).await { - Ok(response) => match resp_to_bytes(response).await { - Ok(bytes) => { + match fetch_queue_data_req(&app, &id).await { + Ok(response) => match resp_to_bytes(response).await { + Ok(bytes) => { + assert_eq!( + false, + bytes.is_empty(), + "Queued data should not be empty" + ); + + let temp_file = tempfile::tempdir() + .expect("Could not create test directory"); + let test_dir = String::from(temp_file.path().to_str().unwrap()); + let new_file = format!("{}/new_file.flac", test_dir); + + let mut file = std::fs::File::create(&new_file).unwrap(); + file.write_all(&bytes).unwrap(); + + let mut form = MultipartForm::default(); + let _ = form.add_file("flac", new_file); + + // Create request + let content_type = form.content_type(); + let body = MultipartBody::from(form); + + let raw_uri = + String::from(crate::callers::endpoints::QUEUESONGUPDATE); + let end_index = raw_uri.len() - 5; + + let uri = format!( + "{}/{}", + (&raw_uri[..end_index]).to_string(), + id.to_string() + ); + + match app + .clone() + .oneshot( + axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(uri) + .header( + axum::http::header::CONTENT_TYPE, + content_type, + ) + .body(axum::body::Body::from_stream(body)) + .unwrap(), + ) + .await + { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::song::response::update_song_queue::Response, + >(response) + .await; assert_eq!( false, - bytes.is_empty(), - "Queued data should not be empty" + resp.data.is_empty(), + "Should not be empty" ); - - let temp_file = tempfile::tempdir() - .expect("Could not create test directory"); - let test_dir = String::from(temp_file.path().to_str().unwrap()); - let new_file = format!("{}/new_file.flac", test_dir); - - let mut file = std::fs::File::create(&new_file).unwrap(); - file.write_all(&bytes).unwrap(); - - let mut form = MultipartForm::default(); - let _ = form.add_file("flac", new_file); - - // Create request - let content_type = form.content_type(); - let body = MultipartBody::from(form); - - let raw_uri = - String::from(crate::callers::endpoints::QUEUESONGUPDATE); - let end_index = raw_uri.len() - 5; - - let uri = format!( - "{}/{}", - (&raw_uri[..end_index]).to_string(), - id.to_string() - ); - - match app - .clone() - .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(uri) - .header( - axum::http::header::CONTENT_TYPE, - content_type, - ) - .body(axum::body::Body::from_stream(body)) - .unwrap(), - ) - .await - { - Ok(response) => { - let resp = get_resp_data::< - crate::callers::song::response::update_song_queue::Response, - >(response) - .await; - assert_eq!( - false, - resp.data.is_empty(), - "Should not be empty" - ); - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - } } Err(err) => { assert!(false, "Error: {:?}", err); } - }, - Err(err) => { - assert!(false, "Error: {:?}", err); } } - } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }, Err(err) => { assert!(false, "Error: {:?}", err); } -- 2.47.3 From f9d7589f25395992e0ba06d3a89c97664433bb3f Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 7 Jun 2025 13:27:18 -0400 Subject: [PATCH 4/9] Got another test working --- src/main.rs | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index fdbb7e8..3f36c4f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -889,34 +889,21 @@ mod tests { 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 id = resp.data[0]; - match fetch_queue_req(&app).await { - Ok(response) => { - 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"); - let id = resp.data[0].id; - - match fetch_queue_data_req(&app, &id).await { - Ok(response) => match resp_to_bytes(response).await { - Ok(bytes) => { - assert_eq!( - false, - bytes.is_empty(), - "Queued data should not be empty" - ); - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - }, - Err(err) => { - assert!(false, "Error: {:?}", err); - } + match fetch_queue_data_req(&app, &id).await { + Ok(response) => match resp_to_bytes(response).await { + Ok(bytes) => { + assert_eq!( + false, + bytes.is_empty(), + "Queued data should not be empty" + ); } - } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }, Err(err) => { assert!(false, "Error: {:?}", err); } -- 2.47.3 From e20745b71ba3e53ba197c01dddc91b144a039623 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 7 Jun 2025 14:16:21 -0400 Subject: [PATCH 5/9] Got the last test working --- src/main.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3f36c4f..fec0e50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -509,12 +509,13 @@ mod tests { } } - pub async fn queue_song_and_coverart_flow(app: &axum::Router) -> Result { + // Returns coverart response and song_queue_id + pub async fn queue_song_and_coverart_flow(app: &axum::Router) -> Result<(axum::response::Response, uuid::Uuid), std::convert::Infallible> { match queue_song_flow(&app).await { - Ok(response) => { + Ok(song_response) => { let resp = super::get_resp_data::< crate::callers::metadata::response::fetch_metadata::Response, - >(response) + >(song_response) .await; assert_eq!(false, resp.data.is_empty(), "Data should not be empty"); let song_queue_id = resp.data[0].song_queue_id; @@ -544,7 +545,7 @@ mod tests { match queue_coverart_flow(&app, &song_queue_id).await { Ok(response) => { - Ok(response) + Ok((response, song_queue_id)) } Err(err) => { assert!(false, "Error: {:?}", err); @@ -703,10 +704,10 @@ mod tests { let app = init::app(pool).await; match sequence_flow::queue_song_and_coverart_flow(&app).await { - Ok(response) => { + Ok((resp_one, song_queue_id)) => { let resp = get_resp_data::< crate::callers::coverart::response::fetch_coverart_no_data::Response, - >(response) + >(resp_one) .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); @@ -715,6 +716,60 @@ mod tests { let payload = serde_json::json!({ "coverart_queue_id": resp_coverart_queue_id }); + + let old = crate::callers::song::status::PENDING; + let target_status = crate::callers::song::status::READY; + let payload = serde_json::json!({ + "id": &song_queue_id, + "status": target_status, + }); + + match app + .clone() + .oneshot( + axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(crate::callers::endpoints::QUEUESONG) + .header(axum::http::header::CONTENT_TYPE, "application/json") + .body(axum::body::Body::from(payload.to_string())) + .unwrap(), + ) + .await + { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::song::response::update_status::Response, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + let changed_status = &resp.data[0]; + + assert_eq!( + *old, changed_status.old_status, + "Old status does not match" + ); + assert_eq!( + target_status, changed_status.new_status, + "New status does not match" + ); + + match fetch_queue_req(&app).await { + Ok(response) => { + 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) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } } Err(err) => { assert!(false, "Error: {:?}", err); @@ -724,6 +779,7 @@ mod tests { // Send request + /* match song_queue_req(&app).await { Ok(response) => { let resp = @@ -748,6 +804,7 @@ mod tests { assert!(false, "Error: {:?}", err); } }; + */ let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } @@ -936,7 +993,27 @@ mod tests { let app = init::app(pool).await; + match sequence_flow::queue_song_and_coverart_flow(&app).await { + Ok((resp_one, resp_two)) => { + let resp = get_resp_data::< + crate::callers::coverart::response::fetch_coverart_no_data::Response, + >(resp_one) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + + let resp_coverart_queue_id = resp.data[0].id; + + let payload = serde_json::json!({ + "coverart_queue_id": resp_coverart_queue_id + }); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + // Send request + /* match song_queue_req(&app).await { Ok(response) => { let resp = @@ -1002,6 +1079,7 @@ mod tests { assert!(false, "Error: {:?}", err); } }; + */ let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } -- 2.47.3 From c75b8e3401b5591ffe743bf44503f86b6a723d64 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 7 Jun 2025 14:16:47 -0400 Subject: [PATCH 6/9] Code cleanup --- src/main.rs | 102 ++++++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 56 deletions(-) diff --git a/src/main.rs b/src/main.rs index fec0e50..109ff69 100644 --- a/src/main.rs +++ b/src/main.rs @@ -510,7 +510,9 @@ mod tests { } // Returns coverart response and song_queue_id - pub async fn queue_song_and_coverart_flow(app: &axum::Router) -> Result<(axum::response::Response, uuid::Uuid), std::convert::Infallible> { + pub async fn queue_song_and_coverart_flow( + app: &axum::Router, + ) -> Result<(axum::response::Response, uuid::Uuid), std::convert::Infallible> { match queue_song_flow(&app).await { Ok(song_response) => { let resp = super::get_resp_data::< @@ -544,9 +546,7 @@ mod tests { eprintln!("Song: {:?}", song); match queue_coverart_flow(&app, &song_queue_id).await { - Ok(response) => { - Ok((response, song_queue_id)) - } + Ok(response) => Ok((response, song_queue_id)), Err(err) => { assert!(false, "Error: {:?}", err); Err(err) @@ -566,51 +566,50 @@ mod tests { } } - // pub async - /* - let done = crate::callers::song::status::DONE; - let payload = serde_json::json!({ - "id": &resp.data[0].id, - "status": done, - }); + // pub async + /* + let done = crate::callers::song::status::DONE; + let payload = serde_json::json!({ + "id": &resp.data[0].id, + "status": done, + }); - match app - .clone() - .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(crate::callers::endpoints::QUEUESONG) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .body(axum::body::Body::from(payload.to_string())) - .unwrap(), - ) - .await - { - Ok(response) => { - let resp = get_resp_data::< - crate::callers::song::response::update_status::Response, - >(response) - .await; - assert_eq!(false, resp.data.is_empty(), "Should not be empty"); - let changed_status = &resp.data[0]; + match app + .clone() + .oneshot( + axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(crate::callers::endpoints::QUEUESONG) + .header(axum::http::header::CONTENT_TYPE, "application/json") + .body(axum::body::Body::from(payload.to_string())) + .unwrap(), + ) + .await + { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::song::response::update_status::Response, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + let changed_status = &resp.data[0]; - assert_eq!( - *old, changed_status.old_status, - "Old status does not match" - ); - assert_eq!( - done, changed_status.new_status, - "New status does not match" - ); - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } + assert_eq!( + *old, changed_status.old_status, + "Old status does not match" + ); + assert_eq!( + done, changed_status.new_status, + "New status does not match" + ); } - */ + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + */ } - pub async fn resp_to_bytes( response: axum::response::Response, ) -> Result { @@ -776,8 +775,6 @@ mod tests { } } - - // Send request /* match song_queue_req(&app).await { @@ -847,8 +844,8 @@ mod tests { "Queued data should not be empty" ); - let temp_file = tempfile::tempdir() - .expect("Could not create test directory"); + let temp_file = + tempfile::tempdir().expect("Could not create test directory"); let test_dir = String::from(temp_file.path().to_str().unwrap()); let new_file = format!("{}/new_file.flac", test_dir); @@ -878,10 +875,7 @@ mod tests { axum::http::Request::builder() .method(axum::http::Method::PATCH) .uri(uri) - .header( - axum::http::header::CONTENT_TYPE, - content_type, - ) + .header(axum::http::header::CONTENT_TYPE, content_type) .body(axum::body::Body::from_stream(body)) .unwrap(), ) @@ -1085,8 +1079,6 @@ mod tests { } } - - #[tokio::test] async fn test_song_metadata_queue() { let tm_pool = db_mgr::get_pool().await.unwrap(); @@ -1280,7 +1272,6 @@ mod tests { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } - #[tokio::test] async fn test_fetch_coverart_queue_without_data() { let tm_pool = db_mgr::get_pool().await.unwrap(); @@ -1437,7 +1428,6 @@ mod tests { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } - #[tokio::test] async fn test_create_song() { let tm_pool = db_mgr::get_pool().await.unwrap(); -- 2.47.3 From c2e40fe02af3f972752c35d2bc78f7141b89894b Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 7 Jun 2025 14:25:32 -0400 Subject: [PATCH 7/9] More minor changes --- src/main.rs | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 109ff69..35d3d10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -153,7 +153,6 @@ pub async fn root() -> &'static str { mod tests { use crate::db; - use std::io::Write; use std::usize; use common_multipart_rfc7578::client::multipart::{ @@ -710,11 +709,7 @@ mod tests { .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); - let resp_coverart_queue_id = resp.data[0].id; - - let payload = serde_json::json!({ - "coverart_queue_id": resp_coverart_queue_id - }); + let _resp_coverart_queue_id = resp.data[0].id; let old = crate::callers::song::status::PENDING; let target_status = crate::callers::song::status::READY; @@ -988,18 +983,55 @@ mod tests { let app = init::app(pool).await; match sequence_flow::queue_song_and_coverart_flow(&app).await { - Ok((resp_one, resp_two)) => { + Ok((resp_one, song_queue_id)) => { let resp = get_resp_data::< crate::callers::coverart::response::fetch_coverart_no_data::Response, >(resp_one) .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); - let resp_coverart_queue_id = resp.data[0].id; + let _resp_coverart_queue_id = resp.data[0].id; + let old = crate::callers::song::status::PENDING; + let done = crate::callers::song::status::READY; let payload = serde_json::json!({ - "coverart_queue_id": resp_coverart_queue_id + "id": song_queue_id, + "status": done, }); + + match app + .clone() + .oneshot( + axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(crate::callers::endpoints::QUEUESONG) + .header(axum::http::header::CONTENT_TYPE, "application/json") + .body(axum::body::Body::from(payload.to_string())) + .unwrap(), + ) + .await + { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::song::response::update_status::Response, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + let changed_status = &resp.data[0]; + + assert_eq!( + *old, changed_status.old_status, + "Old status does not match" + ); + assert_eq!( + done, changed_status.new_status, + "New status does not match" + ); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } } Err(err) => { assert!(false, "Error: {:?}", err); -- 2.47.3 From b092fa00e6f02a08bec7db660e8e0f720efc0931 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 7 Jun 2025 14:29:01 -0400 Subject: [PATCH 8/9] More minor changes --- src/main.rs | 140 ---------------------------------------------------- 1 file changed, 140 deletions(-) diff --git a/src/main.rs b/src/main.rs index 35d3d10..dfc9965 100644 --- a/src/main.rs +++ b/src/main.rs @@ -564,49 +564,6 @@ mod tests { } } } - - // pub async - /* - let done = crate::callers::song::status::DONE; - let payload = serde_json::json!({ - "id": &resp.data[0].id, - "status": done, - }); - - match app - .clone() - .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(crate::callers::endpoints::QUEUESONG) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .body(axum::body::Body::from(payload.to_string())) - .unwrap(), - ) - .await - { - Ok(response) => { - let resp = get_resp_data::< - crate::callers::song::response::update_status::Response, - >(response) - .await; - assert_eq!(false, resp.data.is_empty(), "Should not be empty"); - let changed_status = &resp.data[0]; - - assert_eq!( - *old, changed_status.old_status, - "Old status does not match" - ); - assert_eq!( - done, changed_status.new_status, - "New status does not match" - ); - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - } - */ } pub async fn resp_to_bytes( @@ -768,35 +725,7 @@ mod tests { Err(err) => { assert!(false, "Error: {:?}", err); } - } - - // Send request - /* - match song_queue_req(&app).await { - Ok(response) => { - 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"); - - match fetch_queue_req(&app).await { - Ok(response) => { - 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) => { - assert!(false, "Error: {:?}", err); - } - } - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } }; - */ let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } @@ -1038,75 +967,6 @@ mod tests { } } - // Send request - /* - match song_queue_req(&app).await { - Ok(response) => { - 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"); - - match fetch_queue_req(&app).await { - Ok(response) => { - 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"); - - let old = &resp.data[0].status; - let done = crate::callers::song::status::DONE; - let payload = serde_json::json!({ - "id": &resp.data[0].id, - "status": done, - }); - - match app - .clone() - .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(crate::callers::endpoints::QUEUESONG) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .body(axum::body::Body::from(payload.to_string())) - .unwrap(), - ) - .await - { - Ok(response) => { - let resp = get_resp_data::< - crate::callers::song::response::update_status::Response, - >(response) - .await; - assert_eq!(false, resp.data.is_empty(), "Should not be empty"); - let changed_status = &resp.data[0]; - - assert_eq!( - *old, changed_status.old_status, - "Old status does not match" - ); - assert_eq!( - done, changed_status.new_status, - "New status does not match" - ); - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - } - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - } - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - }; - */ - let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } } -- 2.47.3 From 6ee9164578de08dca5d071fca565969febef9c53 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 7 Jun 2025 14:43:58 -0400 Subject: [PATCH 9/9] Minor changes --- src/main.rs | 53 +++++++++++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/src/main.rs b/src/main.rs index dfc9965..1fee55c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -407,6 +407,25 @@ mod tests { app.clone().oneshot(req).await } + async fn update_song_queue_status_req( + app: &axum::Router, + song_queue_id: &uuid::Uuid, + ) -> Result { + let payload = serde_json::json!({ + "id": &song_queue_id, + "status": crate::callers::song::status::READY + }); + + let req = axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(crate::callers::endpoints::QUEUESONG) + .header(axum::http::header::CONTENT_TYPE, "application/json") + .body(axum::body::Body::from(payload.to_string())) + .unwrap(); + + app.clone().oneshot(req).await + } + async fn get_queued_coverart( app: &axum::Router, coverart_queue_id: &uuid::Uuid, @@ -670,23 +689,8 @@ mod tests { let old = crate::callers::song::status::PENDING; let target_status = crate::callers::song::status::READY; - let payload = serde_json::json!({ - "id": &song_queue_id, - "status": target_status, - }); - match app - .clone() - .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(crate::callers::endpoints::QUEUESONG) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .body(axum::body::Body::from(payload.to_string())) - .unwrap(), - ) - .await - { + match update_song_queue_status_req(&app, &song_queue_id).await { Ok(response) => { let resp = get_resp_data::< crate::callers::song::response::update_status::Response, @@ -923,23 +927,8 @@ mod tests { let old = crate::callers::song::status::PENDING; let done = crate::callers::song::status::READY; - let payload = serde_json::json!({ - "id": song_queue_id, - "status": done, - }); - match app - .clone() - .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(crate::callers::endpoints::QUEUESONG) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .body(axum::body::Body::from(payload.to_string())) - .unwrap(), - ) - .await - { + match update_song_queue_status_req(&app, &song_queue_id).await { Ok(response) => { let resp = get_resp_data::< crate::callers::song::response::update_status::Response, -- 2.47.3