From c463bb3fb424e5f5df723e5222c947bb5b3fa5a2 Mon Sep 17 00:00:00 2001 From: phoenix Date: Tue, 15 Apr 2025 22:01:30 -0400 Subject: [PATCH] Added tests --- src/meta.rs | 365 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 363 insertions(+), 2 deletions(-) diff --git a/src/meta.rs b/src/meta.rs index 50840a7..29a1fa2 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -377,8 +377,7 @@ mod tests { let dir = String::from(util::TESTFILEDIRECTORY); let temp_file = tempfile::tempdir().expect("Could not create test directory"); - let mut test_dir = String::from(temp_file.path().to_str().unwrap()); - // test_dir = String::from("tests/sample_tracks3"); + let test_dir = String::from(temp_file.path().to_str().unwrap()); let test_filename = String::from("track08.flac"); let new_filepath = test_dir + "/" + &test_filename; @@ -424,5 +423,367 @@ mod tests { } }; } + + #[test] + fn test_set_artist() { + let filename = util::get_filename(1); + let dir = String::from(util::TESTFILEDIRECTORY); + + let temp_file = tempfile::tempdir().expect("Could not create test directory"); + let test_dir = String::from(temp_file.path().to_str().unwrap()); + let test_filename = String::from("track08.flac"); + let new_filepath = test_dir + "/" + &test_filename; + + match file_exists(&dir, &filename) { + Ok(_) => { + let filepath = get_full_path(&dir, &filename).unwrap(); + + match util::copy_file(&filepath, &new_filepath) { + Ok(_o) => match get_meta(types::Type::Artist, &filepath) { + Ok(artist) => { + let found = artist == "KD"; + assert!(found, "Meta information was not found {:?}", artist); + let new_artist = String::from("Pilot"); + + match set_meta(types::Type::Artist, &new_filepath, &new_artist) { + Ok(m) => { + assert_eq!( + new_artist, m, + "New artist does not match {:?}", + m + ); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }, + Err(err) => { + assert!( + false, + "Error: {:?} source {:?} destination {:?}", + err, filepath, new_filepath + ); + } + }; + } + Err(err) => { + assert!(false, "Error: File does not exist {:?}", err.to_string()); + } + }; + } + + #[test] + fn test_set_album() { + let filename = util::get_filename(1); + let dir = String::from(util::TESTFILEDIRECTORY); + + let temp_file = tempfile::tempdir().expect("Could not create test directory"); + let test_dir = String::from(temp_file.path().to_str().unwrap()); + let test_filename = String::from("track08.flac"); + let new_filepath = test_dir + "/" + &test_filename; + + match file_exists(&dir, &filename) { + Ok(_) => { + let filepath = get_full_path(&dir, &filename).unwrap(); + + match util::copy_file(&filepath, &new_filepath) { + Ok(_o) => match get_meta(types::Type::Album, &filepath) { + Ok(title) => { + let found = title == "Sample Tracks 3"; + assert!(found, "Meta information was not found {:?}", title); + let new_album = String::from("Sample Tracks 3 Remastered"); + + match set_meta(types::Type::Album, &new_filepath, &new_album) { + Ok(m) => { + assert_eq!( + new_album, m, + "New album does not match {:?}", + m + ); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }, + Err(err) => { + assert!( + false, + "Error: {:?} source {:?} destination {:?}", + err, filepath, new_filepath + ); + } + }; + } + Err(err) => { + assert!(false, "Error: File does not exist {:?}", err.to_string()); + } + }; + } + #[test] + fn test_set_album_artist() { + let filename = util::get_filename(1); + let dir = String::from(util::TESTFILEDIRECTORY); + + let temp_file = tempfile::tempdir().expect("Could not create test directory"); + let test_dir = String::from(temp_file.path().to_str().unwrap()); + let test_filename = String::from("track08.flac"); + let new_filepath = test_dir + "/" + &test_filename; + + match file_exists(&dir, &filename) { + Ok(_) => { + let filepath = get_full_path(&dir, &filename).unwrap(); + + match util::copy_file(&filepath, &new_filepath) { + Ok(_o) => match get_meta(types::Type::AlbumArtist, &filepath) { + Ok(album_artist) => { + let found = album_artist == "KD"; + assert!(found, "Meta information was not found {:?}", album_artist); + let new_album_artist = String::from("Bob"); + + match set_meta( + types::Type::AlbumArtist, + &new_filepath, + &new_album_artist, + ) { + Ok(m) => { + assert_eq!( + new_album_artist, m, + "New album artist does not match {:?}", + m + ); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }, + Err(err) => { + assert!( + false, + "Error: {:?} source {:?} destination {:?}", + err, filepath, new_filepath + ); + } + }; + } + Err(err) => { + assert!(false, "Error: File does not exist {:?}", err.to_string()); + } + }; + } + #[test] + fn test_set_date() { + let filename = util::get_filename(1); + let dir = String::from(util::TESTFILEDIRECTORY); + + let temp_file = tempfile::tempdir().expect("Could not create test directory"); + let test_dir = String::from(temp_file.path().to_str().unwrap()); + let test_filename = String::from("track08.flac"); + let new_filepath = test_dir + "/" + &test_filename; + + match file_exists(&dir, &filename) { + Ok(_) => { + let filepath = get_full_path(&dir, &filename).unwrap(); + + match util::copy_file(&filepath, &new_filepath) { + Ok(_o) => match get_meta(types::Type::Date, &filepath) { + Ok(date) => { + let found = date == "2025-04-11"; + assert!(found, "Meta information was not found {:?}", date); + let new_date = String::from("2025-02-01"); + + match set_meta(types::Type::Date, &new_filepath, &new_date) { + Ok(m) => { + assert_eq!(new_date, m, "New date does not match {:?}", m); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }, + Err(err) => { + assert!( + false, + "Error: {:?} source {:?} destination {:?}", + err, filepath, new_filepath + ); + } + }; + } + Err(err) => { + assert!(false, "Error: File does not exist {:?}", err.to_string()); + } + }; + } + #[test] + fn test_set_track() { + let filename = util::get_filename(1); + let dir = String::from(util::TESTFILEDIRECTORY); + + let temp_file = tempfile::tempdir().expect("Could not create test directory"); + let test_dir = String::from(temp_file.path().to_str().unwrap()); + let test_filename = String::from("track08.flac"); + let new_filepath = test_dir + "/" + &test_filename; + + match file_exists(&dir, &filename) { + Ok(_) => { + let filepath = get_full_path(&dir, &filename).unwrap(); + + match util::copy_file(&filepath, &new_filepath) { + Ok(_o) => match get_meta(types::Type::Track, &filepath) { + Ok(track) => { + let found = track == "1"; + assert!(found, "Meta information was not found {:?}", track); + let new_track = String::from("5"); + + match set_meta(types::Type::Track, &new_filepath, &new_track) { + Ok(m) => { + assert_eq!( + new_track, m, + "New track does not match {:?}", + m + ); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }, + Err(err) => { + assert!( + false, + "Error: {:?} source {:?} destination {:?}", + err, filepath, new_filepath + ); + } + }; + } + Err(err) => { + assert!(false, "Error: File does not exist {:?}", err.to_string()); + } + }; + } + #[test] + fn test_set_disc() { + let filename = util::get_filename(1); + let dir = String::from(util::TESTFILEDIRECTORY); + + let temp_file = tempfile::tempdir().expect("Could not create test directory"); + let test_dir = String::from(temp_file.path().to_str().unwrap()); + let test_filename = String::from("track08.flac"); + let new_filepath = test_dir + "/" + &test_filename; + + match file_exists(&dir, &filename) { + Ok(_) => { + let filepath = get_full_path(&dir, &filename).unwrap(); + + match util::copy_file(&filepath, &new_filepath) { + Ok(_o) => match get_meta(types::Type::Disc, &filepath) { + Ok(disc) => { + let found = disc == "1"; + assert!(found, "Meta information was not found {:?}", disc); + let new_disc = String::from("2"); + + match set_meta(types::Type::Disc, &new_filepath, &new_disc) { + Ok(m) => { + assert_eq!(new_disc, m, "New disc does not match {:?}", m); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }, + Err(err) => { + assert!( + false, + "Error: {:?} source {:?} destination {:?}", + err, filepath, new_filepath + ); + } + }; + } + Err(err) => { + assert!(false, "Error: File does not exist {:?}", err.to_string()); + } + }; + } + #[test] + fn test_set_genre() { + let filename = util::get_filename(1); + let dir = String::from(util::TESTFILEDIRECTORY); + + let temp_file = tempfile::tempdir().expect("Could not create test directory"); + let test_dir = String::from(temp_file.path().to_str().unwrap()); + let test_filename = String::from("track08.flac"); + let new_filepath = test_dir + "/" + &test_filename; + + match file_exists(&dir, &filename) { + Ok(_) => { + let filepath = get_full_path(&dir, &filename).unwrap(); + + match util::copy_file(&filepath, &new_filepath) { + Ok(_o) => match get_meta(types::Type::Genre, &filepath) { + Ok(genre) => { + let found = genre == "Metal"; + assert!(found, "Meta information was not found {:?}", genre); + let new_genre = String::from("Blues"); + + match set_meta(types::Type::Genre, &new_filepath, &new_genre) { + Ok(m) => { + assert_eq!( + new_genre, m, + "New genre does not match {:?}", + m + ); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }, + Err(err) => { + assert!( + false, + "Error: {:?} source {:?} destination {:?}", + err, filepath, new_filepath + ); + } + }; + } + Err(err) => { + assert!(false, "Error: File does not exist {:?}", err.to_string()); + } + }; + } } }