diff --git a/.gitignore b/.gitignore index ea8c4bf..96ef6c0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml index 90b37f5..2e6113e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ license = "MIT" serde = { version = "1.0.218", features = ["derive"] } serde_json = { version = "1.0.139" } rand = { version = "0.9" } +tempfile = { version = "3.19.1" } diff --git a/src/song.rs b/src/song.rs index ed62435..4d319dc 100644 --- a/src/song.rs +++ b/src/song.rs @@ -1,4 +1,3 @@ -// use std::default::Default; use std::io::Read; use crate::constants; @@ -7,7 +6,7 @@ use crate::types; use rand::Rng; use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] pub struct Song { #[serde(skip_serializing_if = "is_zero")] #[serde(alias = "id")] @@ -64,37 +63,6 @@ fn is_dur_not_set(num: &i32) -> bool { *num == 0 } -/* -impl Default for Song { - fn default() -> Self { - Song { - id: 0, - title: String::new(), - artist: String::new(), - album: String::new(), - album_artist: String::new(), - genre: String::new(), - year: 0, - duration: 0, - track: 0, - disc: 0, - disc_count: 0, - track_count: 0, - audio_type: String::new(), - date_created: String::new(), - filename: String::new(), - user_id: 0, - data: Vec::new(), - directory: String::new(), - // album_id: 0, - // artist_id: 0, - // genre_id: 0, - // coverart_id: 0, - } - } -} -*/ - impl Song { pub fn to_metadata_json(&self, pretty: bool) -> Result { if pretty { diff --git a/tests/97h357j59j.flac b/tests/97h357j59j.flac new file mode 100644 index 0000000..b018649 Binary files /dev/null and b/tests/97h357j59j.flac differ diff --git a/tests/album.json b/tests/album.json new file mode 100644 index 0000000..7124099 --- /dev/null +++ b/tests/album.json @@ -0,0 +1,31 @@ +{ + "album": "Sample Tracks! Yes", + "album_artist": "KD", + "genre": "Country", + "year": 2025, + "track_count": 3, + "disc_count": 1, + "tracks": [ + { + "title": "E less", + "artist": "KD", + "disc": 1, + "track": 1, + "duration": 31 + }, + { + "title": "Aaaaaye", + "artist": "KD", + "disc": 1, + "track": 2, + "duration": 33 + }, + { + "title": "Check D out", + "artist": "KD", + "disc": 1, + "track": 3, + "duration": 22 + } + ] +} diff --git a/tests/tests.rs b/tests/tests.rs new file mode 100644 index 0000000..ab96494 --- /dev/null +++ b/tests/tests.rs @@ -0,0 +1,146 @@ +mod song_tests { + use std::fs; + use std::fs::File; + use std::io::{Read, Write}; + use std::path::Path; + + use tempfile::tempdir; + + use icarus_models::song; + use icarus_models::types; + + fn get_tests_directory() -> String { + String::from(env!("CARGO_MANIFEST_DIR").to_owned() + "/tests/") + } + + fn does_directory_exists(directory: &String) -> bool { + let path = Path::new(directory); + if let Ok(dir_i) = fs::metadata(path) { + dir_i.is_dir() + } else { + false + } + } + + fn extract_data_from_file(filepath: &String) -> Result, std::io::Error> { + match std::fs::File::open(filepath) { + Ok(mut file) => { + let mut buffer: Vec = Vec::new(); + let _ = file.read_to_end(&mut buffer); + Ok(buffer) + } + Err(err) => Err(err), + } + } + + #[test] + fn test_song_to_data() { + println!("Test"); + let some_val = true; + + println!("Checking if some_val is true"); + assert_eq!(true, some_val); + + println!("Getting track"); + let mut song = song::Song::default(); + song.directory = get_tests_directory(); + song.filename = String::from("track01.flac"); + + assert!( + does_directory_exists(&song.directory), + "Directory does not exist" + ); + + println!("Directory: {}", song.directory); + + match song.song_path() { + Ok(filepath) => match extract_data_from_file(&filepath) { + Ok(buffer) => { + assert_eq!(buffer.is_empty(), false); + + match song.to_data() { + Ok(song_data) => { + println!("Both files match"); + assert_eq!(buffer, song_data); + } + Err(err) => { + assert!(false, "Error producing song data: {:?}", err); + } + }; + } + Err(err) => { + assert!(false, "Failed to open file: {:?}", err); + } + }, + Err(err) => { + assert!(false, "Could not get song path: {:?}", err); + } + } + } + + #[test] + fn test_song_path_check() { + let mut song = song::Song::default(); + song.directory = get_tests_directory(); + song.filename = String::from("track01.flac"); + + assert!( + does_directory_exists(&song.directory), + "Directory does not exist" + ); + } + + #[test] + fn test_song_generate_filename() { + let mut song = song::Song::default(); + song.directory = get_tests_directory(); + song.filename = String::from("track01.flac"); + + match song.song_path() { + Ok(songpath) => match extract_data_from_file(&songpath) { + Ok(buffer) => { + let mut song_cpy = song.clone(); + let temp_dir = tempdir().expect("Failed to create temp dir"); + song_cpy.directory = match temp_dir.path().to_str() { + Some(s) => String::from(s), + None => String::new(), + }; + + assert_eq!(song.directory.is_empty(), false); + song_cpy.filename = + song.generate_filename(types::MusicTypes::FlacExtension, true); + println!("Directory: {:?}", song_cpy.directory); + println!("File to be created: {:?}", song_cpy.filename); + + let path = match song_cpy.song_path() { + Ok(s_path) => s_path, + Err(err) => { + assert!(false, "Error: {:?}", err); + String::new() + } + }; + + match File::create(path) { + Ok(mut file_cpy) => match file_cpy.write_all(&buffer) { + Ok(success) => { + println!("Success: {:?}", success); + } + Err(err) => { + assert!(false, "Error saving file: {:?}", err); + } + }, + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }; + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }, + Err(err) => { + assert!(false, "Error extracting song data: {:?}", err); + } + } + } +} diff --git a/tests/track01.flac b/tests/track01.flac new file mode 100644 index 0000000..b018649 Binary files /dev/null and b/tests/track01.flac differ diff --git a/tests/track02.flac b/tests/track02.flac new file mode 100644 index 0000000..3dfdbdb Binary files /dev/null and b/tests/track02.flac differ diff --git a/tests/track03.flac b/tests/track03.flac new file mode 100644 index 0000000..451c4a1 Binary files /dev/null and b/tests/track03.flac differ