From 2d25b15f3c8898d9fc07edab6a21d983b6da950f Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sun, 7 Jan 2024 11:04:06 +0100 Subject: [PATCH] Add unit tests --- src/database/mod.rs | 31 ++++++++++++++++++++++++++++++- src/lib.rs | 31 ++++++++++++++++++++++++++++++- src/library/mod.rs | 18 +++++++++++++++++- 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/database/mod.rs b/src/database/mod.rs index 3846314..f2747e1 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -20,6 +20,19 @@ pub trait IDatabase { fn save(&mut self, collection: &S) -> Result<(), SaveError>; } +/// Non-implementation defined to make generics easier. +pub struct NoDatabase {} + +impl IDatabase for NoDatabase { + fn load(&self, _collection: &mut D) -> Result<(), LoadError> { + panic!() + } + + fn save(&mut self, _collection: &S) -> Result<(), SaveError> { + panic!() + } +} + /// Error type for database calls. #[derive(Debug)] pub enum LoadError { @@ -76,7 +89,23 @@ impl From for SaveError { mod tests { use std::io; - use super::{LoadError, SaveError}; + use super::{LoadError, SaveError, NoDatabase, IDatabase}; + + #[test] + #[should_panic] + fn no_database_load() { + let database = NoDatabase {}; + let mut collection: Vec = vec![]; + _ = database.load(&mut collection); + } + + #[test] + #[should_panic] + fn no_database_save() { + let mut database = NoDatabase {}; + let collection: Vec = vec![]; + _ = database.save(&collection); + } #[test] fn errors() { diff --git a/src/lib.rs b/src/lib.rs index 1aa823c..2653aac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -616,7 +616,7 @@ mod tests { use mockall::predicate; use once_cell::sync::Lazy; - use crate::{database::MockIDatabase, library::MockILibrary}; + use crate::{database::{MockIDatabase, NoDatabase}, library::{MockILibrary, NoLibrary}}; use super::*; @@ -961,6 +961,18 @@ mod tests { music_hoard.save_to_database().unwrap(); } + #[test] + fn library_not_provided() { + let library: Option = None; + let database = Some(MockIDatabase::new()); + let mut music_hoard = MusicHoard::new(library, database); + + let actual_err = music_hoard.rescan_library().unwrap_err(); + let expected_err = Error::LibraryError(String::from("library not provided")); + + assert_eq!(actual_err, expected_err); + } + #[test] fn library_error() { let mut library = MockILibrary::new(); @@ -983,6 +995,23 @@ mod tests { assert_eq!(actual_err.to_string(), expected_err.to_string()); } + #[test] + fn database_not_provided() { + let library = Some(MockILibrary::new()); + let database: Option = None; + let mut music_hoard = MusicHoard::new(library, database); + + let expected_err = Error::DatabaseError(String::from("database not provided")); + + let actual_err = music_hoard.load_from_database().unwrap_err(); + assert_eq!(actual_err, expected_err); + assert_eq!(actual_err.to_string(), expected_err.to_string()); + + let actual_err = music_hoard.save_to_database().unwrap_err(); + assert_eq!(actual_err, expected_err); + assert_eq!(actual_err.to_string(), expected_err.to_string()); + } + #[test] fn database_load_error() { let library = MockILibrary::new(); diff --git a/src/library/mod.rs b/src/library/mod.rs index 3e4ce7d..e94ef94 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -17,6 +17,15 @@ pub trait ILibrary { fn list(&mut self, query: &Query) -> Result, Error>; } +/// Non-implementation defined to make generics easier. +pub struct NoLibrary {} + +impl ILibrary for NoLibrary { + fn list(&mut self, _query: &Query) -> Result,Error> { + panic!() + } +} + /// An item from the library. An item corresponds to an individual file (usually a single track). #[derive(Debug, PartialEq, Eq, Hash)] pub struct Item { @@ -127,7 +136,14 @@ impl From for Error { mod tests { use std::io; - use super::{Error, Field, Query}; + use super::{Error, Field, Query, NoLibrary, ILibrary}; + + #[test] + #[should_panic] + fn no_library_list() { + let mut library = NoLibrary {}; + _ = library.list(&Query::default()); + } #[test] fn query() {