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() {