From c2efeb9e444dce8847350ba5dbddd9407252c4ed Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Sat, 1 Jul 2017 15:52:35 -0700 Subject: [PATCH] Added unit test for editing config --- src/config.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/ddns.rs | 3 ++- src/vfs.rs | 2 +- 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index f1c2aee..bafabfa 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,6 +7,7 @@ use std::io::Read; use std::path; use toml; +use db::DB; use db::ConnectionSource; use db::{ddns_config, misc_settings, mount_points, users}; use ddns::DDNSConfig; @@ -22,13 +23,13 @@ pub struct MiscSettings { pub index_album_art_pattern: String, } -#[derive(Serialize, Deserialize)] +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct ConfigUser { pub name: String, pub password: String, } -#[derive(Serialize, Deserialize)] +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct Config { pub album_art_pattern: Option, pub reindex_every_n_seconds: Option, @@ -138,12 +139,14 @@ pub fn ammend(db: &T, new_config: &Config) -> Result<()> let connection = connection.deref(); if let Some(ref mount_dirs) = new_config.mount_dirs { + diesel::delete(mount_points::table).execute(connection)?; diesel::insert(mount_dirs) .into(mount_points::table) .execute(connection)?; } if let Some(ref config_users) = new_config.users { + diesel::delete(users::table).execute(connection)?; for config_user in config_users { let new_user = User::new(&config_user.name, &config_user.password); diesel::insert(&new_user) @@ -164,6 +167,15 @@ pub fn ammend(db: &T, new_config: &Config) -> Result<()> .execute(connection)?; } + if let Some(ref ydns) = new_config.ydns { + use self::ddns_config::dsl::*; + diesel::update(ddns_config) + .set((host.eq(ydns.host.clone()), + username.eq(ydns.username.clone()), + password.eq(ydns.password.clone()))) + .execute(connection)?; + } + Ok(()) } @@ -175,6 +187,64 @@ fn clean_path_string(path_string: &str) -> path::PathBuf { path::Path::new(&path_string).iter().collect() } +fn _get_test_db(name: &str) -> DB { + let mut db_path = path::PathBuf::new(); + db_path.push("test"); + db_path.push(name); + if db_path.exists() { + fs::remove_file(&db_path).unwrap(); + } + + let db = DB::new(&db_path).unwrap(); + db +} + +#[test] +fn test_ammend() { + let db = _get_test_db("ammend.sqlite"); + + let initial_config = Config { + album_art_pattern: Some("file\\.png".into()), + reindex_every_n_seconds: Some(123), + mount_dirs: Some(vec![MountPoint { + source: "C:\\Music".into(), + name: "root".into(), + }]), + users: Some(vec![ConfigUser { + name: "Teddy🐻".into(), + password: "".into(), + }]), + ydns: Some(DDNSConfig { + host: "🐻🐻🐻.ydns.eu".into(), + username: "be🐻r".into(), + password: "yummy🐇".into(), + }), + }; + + let final_config = Config { + album_art_pattern: Some("🖼️\\.jpg".into()), + reindex_every_n_seconds: Some(7734), + mount_dirs: Some(vec![MountPoint { + source: "/home/music".into(), + name: "🎵📁".into(), + }]), + users: Some(vec![ConfigUser { + name: "Kermit🐸".into(), + password: "".into(), + }]), + ydns: Some(DDNSConfig { + host: "🐸🐸🐸.ydns.eu".into(), + username: "kfr🐸g".into(), + password: "tasty🐞".into(), + }), + }; + + ammend(&db, &initial_config).unwrap(); + ammend(&db, &final_config).unwrap(); + let db_config = read(&db).unwrap(); + assert_eq!(db_config, final_config); +} + #[test] fn test_clean_path_string() { let mut correct_path = path::PathBuf::new(); @@ -198,5 +268,4 @@ fn test_clean_path_string() { assert_eq!(correct_path, clean_path_string(r#"/usr\some\path\\\\"#)); assert_eq!(correct_path, clean_path_string(r#"/usr\some/path//"#)); } - } diff --git a/src/ddns.rs b/src/ddns.rs index b1fe6b7..f0c6acc 100644 --- a/src/ddns.rs +++ b/src/ddns.rs @@ -10,7 +10,8 @@ use db::{ConnectionSource, DB}; use db::ddns_config; use errors; -#[derive(Debug, Deserialize, Queryable, Serialize)] +#[derive(Debug, Deserialize, Insertable, PartialEq, Queryable, Serialize)] +#[table_name="ddns_config"] pub struct DDNSConfig { pub host: String, pub username: String, diff --git a/src/vfs.rs b/src/vfs.rs index 7f10fe3..a6c3c7e 100644 --- a/src/vfs.rs +++ b/src/vfs.rs @@ -29,7 +29,7 @@ impl VFSSource for DB { } } -#[derive(Debug, Deserialize, Insertable, Queryable, Serialize)] +#[derive(Debug, Deserialize, Insertable, PartialEq, Queryable, Serialize)] #[table_name="mount_points"] pub struct MountPoint { pub source: String,