Ironing out some kinks

This commit is contained in:
kdeng00
2025-05-23 18:44:43 -04:00
parent 3416f0db16
commit 64ac7da3a0
2 changed files with 123 additions and 21 deletions
+25 -21
View File
@@ -111,7 +111,7 @@ pub mod response {
} }
pub mod create_metadata { pub mod create_metadata {
#[derive(Default, serde::Deserialize, serde::Serialize)] #[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
pub struct Response { pub struct Response {
pub message: String, pub message: String,
pub data: Vec<icarus_models::song::Song> pub data: Vec<icarus_models::song::Song>
@@ -136,33 +136,36 @@ mod song {
// TODO: Change first parameter of return value from string to a time type // TODO: Change first parameter of return value from string to a time type
pub async fn insert(pool: &sqlx::PgPool, song: &icarus_models::song::Song) -> Result<(String, uuid::Uuid), sqlx::Error> { pub async fn insert(pool: &sqlx::PgPool, song: &icarus_models::song::Song) -> Result<(String, uuid::Uuid), sqlx::Error> {
// $11, $12, $13, $14, $15) RETURNING date_created, id;
let result = sqlx::query( let result = sqlx::query(
r#" r#"
INSERT INTO "song" (title, artist, album_artist, album, genre, year, INSERT INTO "song" (title, artist, album_artist, album, genre, year, track, disc, track_count, disc_count, duration, audio_type, filename, directory, user_id)
track, disc, track_count, disc_count, duration, audio_type, filename, VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) RETURNING id;
directory, user_id) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
$11, $12, $13, $14, $15) RETURNING date_created, id;
"# "#
) )
.bind(song.title.clone()) .bind(&song.title)
.bind(song.artist.clone()) .bind(&song.artist)
.bind(song.album_artist.clone()) .bind(&song.album_artist)
.bind(song.album.clone()) .bind(&song.album)
.bind(song.genre.clone()) .bind(&song.genre)
.bind(song.year) .bind(&song.year)
.bind(song.track) .bind(&song.track)
.bind(song.disc) .bind(&song.disc)
.bind(song.track_count) .bind(&song.track_count)
.bind(song.disc_count) .bind(&song.disc_count)
.bind(song.duration) .bind(&song.duration)
.bind(song.audio_type.clone()) .bind(&song.audio_type)
.bind(song.filename.clone()) .bind(&song.filename)
.bind(song.directory.clone()) .bind(&song.directory)
.bind(song.user_id) .bind(&song.user_id)
.fetch_one(pool) .fetch_one(pool)
.await .await
.map_err(|e| { .map_err(|e| {
eprintln!("Error inserting query: {:?}", e); eprintln!("Error inserting query: {:?}", e);
eprintln!("Year: {:?}", song.year);
eprintln!("Song: {:?}", song);
}); });
match result { match result {
@@ -171,7 +174,8 @@ mod song {
.try_get("id") .try_get("id")
.map_err(|_e| sqlx::Error::RowNotFound) .map_err(|_e| sqlx::Error::RowNotFound)
.unwrap(); .unwrap();
let date_created = row.try_get("date_created").map_err(|_e| sqlx::Error::RowNotFound).unwrap(); // let date_created = row.try_get("date_created").map_err(|_e| sqlx::Error::RowNotFound).unwrap();
let date_created = String::from("2025-01-01");
Ok((date_created, id)) Ok((date_created, id))
} }
+98
View File
@@ -1195,5 +1195,103 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_create_song() { async fn test_create_song() {
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::<crate::callers::song::response::Response>(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 queue_metadata_req(&app, &resp.data[0]).await
{
Ok(response) => {
let resp =
get_resp_data::<crate::callers::song::response::Response>(response)
.await;
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
let id = resp.data[0];
match fetch_metadata_queue_req(&app, &id).await
{
Ok(response) => {
let resp = 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 payload = serde_json::json!({
"title": "Power of Soul",
"artist": "Jimmi Hendrix",
"album_artist": "Jimmi Hendrix",
"album": "Machine Gun",
"genre": "Psychadelic Rock",
"date": "2016-01-01",
"track": 1,
"disc": 1,
"track_count": 11,
"disc_count": 1,
"duration": 330,
"audio_type": "flac",
"user_id": "d6e159c1-9648-4c85-81e5-52f502ff53e4",
"song_queue_id": id
});
match app.clone().oneshot(
axum::http::Request::builder()
.method(axum::http::Method::POST)
.uri(crate::callers::endpoints::CREATESONG)
.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::create_metadata::Response>(response).await;
assert_eq!(false, resp.data.is_empty(), "No songs found, Response {:?}", resp);
}
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;
} }
} }