Use multiple files in tests (#168)

* File renamed

* Filename change in the code as well

* Added more migration sql for tests

* Using different song for a test

* Changes to test and helper functions

* Workflow change

* Code formatting

* Version bump
This commit was merged in pull request #168.
This commit is contained in:
KD
2025-07-30 15:28:47 -04:00
committed by GitHub
parent a0c6ae65a3
commit 9aefeace23
7 changed files with 130 additions and 11 deletions
+1 -1
View File
@@ -68,7 +68,7 @@ jobs:
env:
ROOT_DIRECTORY: "/tmp"
DATABASE_URL: "postgres://${{ secrets.POSTGRES_USER }}:${{ secrets.POSTGRES_PASSWORD }}@localhost:5432/${{ secrets.POSTGRES_DB }}"
run: cargo test --verbose -- --test-threads=1
run: cargo test --verbose --
- name: Run clippy
run: cargo clippy -- -D warnings
Generated
+1 -1
View File
@@ -762,7 +762,7 @@ dependencies = [
[[package]]
name = "icarus"
version = "0.1.99"
version = "0.1.100"
dependencies = [
"axum",
"common-multipart-rfc7578",
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "icarus"
version = "0.1.99"
version = "0.1.100"
edition = "2024"
rust-version = "1.88"
+123 -7
View File
@@ -365,7 +365,7 @@ mod tests {
app: &axum::Router,
) -> Result<axum::response::Response, std::convert::Infallible> {
let mut form = MultipartForm::default();
let _ = form.add_file("jpg", "tests/I/Coverart.jpg");
let _ = form.add_file("jpg", "tests/I/Coverart-1.jpg");
// Create request
let content_type = form.content_type();
@@ -1862,7 +1862,7 @@ mod tests {
let app = super::init::app(pool).await;
let id = test_data::song_id().await.unwrap();
let (id, _, _, _) = test_data::song_id().await.unwrap();
let uri = format!("{}?id={id}", crate::callers::endpoints::GETSONGS);
@@ -1950,8 +1950,29 @@ mod tests {
}
pub mod test_data {
pub async fn song_id() -> Result<uuid::Uuid, uuid::Error> {
uuid::Uuid::parse_str("44cf7940-34ff-489f-9124-d0ec90a55af9")
pub async fn song_id() -> Result<(uuid::Uuid, String, String, String), uuid::Error> {
match uuid::Uuid::parse_str("44cf7940-34ff-489f-9124-d0ec90a55af9") {
Ok(id) => Ok((
id,
String::from("tests/I/"),
String::from("track01.flac"),
String::from("tests/I/Coverart-1.jpg"),
)),
Err(err) => Err(err),
}
}
pub async fn other_song_id() -> Result<(uuid::Uuid, String, String, String), uuid::Error>
{
match uuid::Uuid::parse_str("94cf7940-34ff-489f-9124-d0ec90a55af4") {
Ok(id) => Ok((
id,
String::from("tests/I/"),
String::from("track02.flac"),
String::from("tests/I/Coverart-2.jpg"),
)),
Err(err) => Err(err),
}
}
pub async fn coverart_id() -> Result<uuid::Uuid, uuid::Error> {
@@ -1978,7 +1999,7 @@ mod tests {
let app = super::init::app(pool).await;
let id = test_data::song_id().await.unwrap();
let (id, _, _, _) = test_data::song_id().await.unwrap();
let my_url = crate::callers::endpoints::STREAMSONG;
let last = my_url.len() - 5;
@@ -2034,7 +2055,7 @@ mod tests {
let app = super::init::app(pool).await;
let id = test_data::song_id().await.unwrap();
let (id, _, _, _) = test_data::song_id().await.unwrap();
let uri =
super::format_url_with_value(crate::callers::endpoints::DOWNLOADSONG, &id).await;
@@ -2126,6 +2147,81 @@ mod tests {
let _ = super::db_mgr::drop_database(&tm_pool, &db_name).await;
}
async fn get_test_data(
song_directory: &String,
song_filename: &String,
coverart_path: &String,
) -> Result<(Vec<u8>, Vec<u8>), std::io::Error> {
let song = icarus_models::song::Song {
directory: song_directory.clone(),
filename: song_filename.clone(),
..Default::default()
};
let coverart = icarus_models::coverart::CoverArt {
path: coverart_path.clone(),
..Default::default()
};
match song.to_data() {
Ok(song_data) => match coverart.to_data() {
Ok(coverart_data) => Ok((song_data, coverart_data)),
Err(err) => Err(err),
},
Err(err) => Err(err),
}
}
async fn save_test_again(
song_directory: &String,
song_filename: &String,
song_data: Vec<u8>,
coverart_path: &String,
coverart_data: Vec<u8>,
) -> Result<(), std::io::Error> {
let song = icarus_models::song::Song {
directory: song_directory.clone(),
filename: song_filename.clone(),
..Default::default()
};
let coverart = icarus_models::coverart::CoverArt {
path: coverart_path.clone(),
..Default::default()
};
use std::io::Write;
match song.song_path() {
Ok(song_path) => {
let song_p = std::path::Path::new(&song_path);
match std::fs::File::create(song_p) {
Ok(mut song_file) => match song_file.write_all(&song_data) {
Ok(_) => {}
Err(err) => {
return Err(err);
}
},
Err(err) => {
return Err(err);
}
}
}
Err(err) => {
return Err(err);
}
}
let coverart_p = std::path::Path::new(&coverart.path);
match std::fs::File::create(coverart_p) {
Ok(mut coverart_file) => match coverart_file.write_all(&coverart_data) {
Ok(_) => Ok(()),
Err(err) => Err(err),
},
Err(err) => Err(err),
}
}
#[tokio::test]
async fn test_last_delete_song() {
let tm_pool = super::db_mgr::get_pool().await.unwrap();
@@ -2145,7 +2241,12 @@ mod tests {
let app = super::init::app(pool).await;
let id = test_data::song_id().await.unwrap();
let (id, song_directory, song_filename, coverart_path) =
test_data::other_song_id().await.unwrap();
let (song_data, coverart_data) =
get_test_data(&song_directory, &song_filename, &coverart_path)
.await
.unwrap();
let uri =
super::format_url_with_value(crate::callers::endpoints::DELETESONG, &id).await;
@@ -2174,6 +2275,21 @@ mod tests {
"Song Ids do not match {id:?} {:?}",
song_and_coverart.song.id
);
match save_test_again(
&song_directory,
&song_filename,
song_data,
&coverart_path,
coverart_data,
)
.await
{
Ok(_) => {}
Err(err) => {
assert!(false, "Error: {err:?}");
}
}
}
Err(err) => {
assert!(false, "Error: {err:?}");
@@ -1,3 +1,6 @@
-- Add migration script here
INSERT INTO "song" (id, title, artist, album_artist, album, genre, year, track, disc, track_count, disc_count, duration, audio_type, date_created, filename, directory, user_id) VALUES('44cf7940-34ff-489f-9124-d0ec90a55af9', 'Hypocrite Like The Rest', 'Kuoth', 'Kuoth', 'I', 'Alternative Hip-Hop', 2020, 1, 1, 9, 1, 139, 'flac', '2020-01-01 13:00:00-05', 'track01.flac', 'tests/I', '47491f9b-725a-4ba4-b9a5-711e1be46670');
INSERT INTO "coverart" VALUES('996122cd-5ae9-4013-9934-60768d3006ed', 'I', 'tests/I/Coverart.jpg', '44cf7940-34ff-489f-9124-d0ec90a55af9');
INSERT INTO "coverart" VALUES('996122cd-5ae9-4013-9934-60768d3006ed', 'I', 'tests/I/Coverart-1.jpg', '44cf7940-34ff-489f-9124-d0ec90a55af9');
INSERT INTO "song" (id, title, artist, album_artist, album, genre, year, track, disc, track_count, disc_count, duration, audio_type, date_created, filename, directory, user_id) VALUES('94cf7940-34ff-489f-9124-d0ec90a55af4', 'It''s Too Late', 'Kuoth', 'Kuoth', 'I', 'Alternative Hip-Hop', 2020, 2, 1, 9, 1, 116, 'flac', '2020-01-01 13:01:00-05', 'track02.flac', 'tests/I', '47491f9b-725a-4ba4-b9a5-711e1be46670');
INSERT INTO "coverart" VALUES('d96122cd-5ae9-4013-9934-60768d3006e9', 'I', 'tests/I/Coverart-2.jpg', '94cf7940-34ff-489f-9124-d0ec90a55af4');

Before

Width:  |  Height:  |  Size: 6.7 MiB

After

Width:  |  Height:  |  Size: 6.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 MiB