From 7c5a9c282b156337d7de5ab9d4bacafbafa257f1 Mon Sep 17 00:00:00 2001 From: MrDulfin Date: Sun, 21 Jan 2024 05:24:10 -0500 Subject: [PATCH] added more tests --- .gitignore | 2 +- src/config/config.rs | 77 ++++++++++++++++++++++++++++++++------------ 2 files changed, 57 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 72106f3..a9e4c0b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ # Rust binary output dir target/ - +test-config/ # Rust configuration Cargo.lock diff --git a/src/config/config.rs b/src/config/config.rs index 206287a..bc13836 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -1,7 +1,7 @@ use std::{ path::PathBuf, fs::{File, OpenOptions, self}, - io::{Error, Write, Read}, + io::{Error, Write, Read}, sync::{Arc, RwLock}, }; use serde::{Serialize, Deserialize}; @@ -9,11 +9,14 @@ use serde_json::to_string_pretty; use thiserror::Error; use uuid::Uuid; +use crate::music_storage::library::{MusicLibrary, self}; + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ConfigLibrary { pub name: String, pub path: PathBuf, - pub uuid: Uuid + pub uuid: Uuid, + pub scan_folders: Option>, } impl Default for ConfigLibrary { @@ -22,16 +25,18 @@ impl Default for ConfigLibrary { name: String::new(), path: PathBuf::from("library"), uuid: Uuid::new_v4(), + scan_folders: None, } } } impl ConfigLibrary { - pub fn new(path: PathBuf, name: String) -> Self { + pub fn new(path: PathBuf, name: String, scan_folders: Option>) -> Self { ConfigLibrary { name, path, uuid: Uuid::new_v4(), + scan_folders, } } @@ -43,7 +48,7 @@ impl ConfigLibrary { } } -#[derive(Debug, Default, Serialize, Deserialize)] +#[derive(Debug, Default, Serialize, Deserialize, Clone)] pub struct ConfigLibraries { pub default_library: Uuid, pub library_folder: PathBuf, @@ -84,29 +89,13 @@ impl ConfigLibraries { } } -#[derive(Debug, Default, Serialize, Deserialize)] +#[derive(Debug, Default, Serialize, Deserialize, Clone)] pub struct Config { pub path: PathBuf, pub libraries: ConfigLibraries, pub volume: f32, } -#[test] -fn config_test() { - let _ = Config { - path: PathBuf::from("config_test.json"), - libraries: ConfigLibraries { - libraries: vec![ - ConfigLibrary::new(PathBuf::from("library1"), String::from("library1")), - ConfigLibrary::new(PathBuf::from("library2"), String::from("library2")), - ConfigLibrary::new(PathBuf::from("library3"), String::from("library3")) - ], - ..Default::default() - }, - ..Default::default() - }.write_file(); -} - impl Config { pub fn new() -> Self { Config { @@ -150,3 +139,49 @@ pub enum ConfigError { #[error("There is no Default Library for this Config")] NoDefaultLibrary } + + +#[test] +fn config_test() { + let lib_a = ConfigLibrary::new(PathBuf::from("test-config/library1"), String::from("library1"), None); + let lib_b = ConfigLibrary::new(PathBuf::from("test-config/library2"), String::from("library2"), None); + let lib_c = ConfigLibrary::new(PathBuf::from("test-config/library3"), String::from("library3"), None); + let config = Config { + path: PathBuf::from("test-config/config_test.json"), + libraries: ConfigLibraries { + libraries: vec![ + lib_a.clone(), + lib_b.clone(), + lib_c.clone(), + ], + ..Default::default() + }, + ..Default::default() + }; + config.write_file(); + let arc = Arc::new(RwLock::from(config)); + MusicLibrary::init(arc.clone(), lib_a.uuid.clone()).unwrap(); + MusicLibrary::init(arc.clone(), lib_b.uuid.clone()).unwrap(); + MusicLibrary::init(arc.clone(), lib_c.uuid.clone()).unwrap(); + +} + +#[test] +fn test2() { + let config = Config::read_file(PathBuf::from("test-config/config_test.json")).unwrap(); + let uuid = config.libraries.get_default().unwrap().uuid.clone(); + let mut lib = MusicLibrary::init(Arc::new(RwLock::from(config.clone())), uuid).unwrap(); + lib.scan_folder("test-config/music/").unwrap(); + lib.save(&config).unwrap(); + dbg!(&lib); + dbg!(&config); +} + +#[test] +fn test3() { + let config = Config::read_file(PathBuf::from("test-config/config_test.json")).unwrap(); + let uuid = config.libraries.get_default().unwrap().uuid; + let mut lib = MusicLibrary::init(Arc::new(RwLock::from(config.clone())), uuid).unwrap(); + + dbg!(lib); +} \ No newline at end of file