Add unit tests
This commit is contained in:
parent
115373667f
commit
2d25b15f3c
@ -20,6 +20,19 @@ pub trait IDatabase {
|
|||||||
fn save<S: Serialize + 'static>(&mut self, collection: &S) -> Result<(), SaveError>;
|
fn save<S: Serialize + 'static>(&mut self, collection: &S) -> Result<(), SaveError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Non-implementation defined to make generics easier.
|
||||||
|
pub struct NoDatabase {}
|
||||||
|
|
||||||
|
impl IDatabase for NoDatabase {
|
||||||
|
fn load<D: DeserializeOwned + 'static>(&self, _collection: &mut D) -> Result<(), LoadError> {
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save<S: Serialize + 'static>(&mut self, _collection: &S) -> Result<(), SaveError> {
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Error type for database calls.
|
/// Error type for database calls.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum LoadError {
|
pub enum LoadError {
|
||||||
@ -76,7 +89,23 @@ impl From<std::io::Error> for SaveError {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use std::io;
|
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<String> = vec![];
|
||||||
|
_ = database.load(&mut collection);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn no_database_save() {
|
||||||
|
let mut database = NoDatabase {};
|
||||||
|
let collection: Vec<String> = vec![];
|
||||||
|
_ = database.save(&collection);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn errors() {
|
fn errors() {
|
||||||
|
31
src/lib.rs
31
src/lib.rs
@ -616,7 +616,7 @@ mod tests {
|
|||||||
use mockall::predicate;
|
use mockall::predicate;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use crate::{database::MockIDatabase, library::MockILibrary};
|
use crate::{database::{MockIDatabase, NoDatabase}, library::{MockILibrary, NoLibrary}};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
@ -961,6 +961,18 @@ mod tests {
|
|||||||
music_hoard.save_to_database().unwrap();
|
music_hoard.save_to_database().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn library_not_provided() {
|
||||||
|
let library: Option<NoLibrary> = 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]
|
#[test]
|
||||||
fn library_error() {
|
fn library_error() {
|
||||||
let mut library = MockILibrary::new();
|
let mut library = MockILibrary::new();
|
||||||
@ -983,6 +995,23 @@ mod tests {
|
|||||||
assert_eq!(actual_err.to_string(), expected_err.to_string());
|
assert_eq!(actual_err.to_string(), expected_err.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn database_not_provided() {
|
||||||
|
let library = Some(MockILibrary::new());
|
||||||
|
let database: Option<NoDatabase> = 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]
|
#[test]
|
||||||
fn database_load_error() {
|
fn database_load_error() {
|
||||||
let library = MockILibrary::new();
|
let library = MockILibrary::new();
|
||||||
|
@ -17,6 +17,15 @@ pub trait ILibrary {
|
|||||||
fn list(&mut self, query: &Query) -> Result<Vec<Item>, Error>;
|
fn list(&mut self, query: &Query) -> Result<Vec<Item>, Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Non-implementation defined to make generics easier.
|
||||||
|
pub struct NoLibrary {}
|
||||||
|
|
||||||
|
impl ILibrary for NoLibrary {
|
||||||
|
fn list(&mut self, _query: &Query) -> Result<Vec<Item>,Error> {
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// An item from the library. An item corresponds to an individual file (usually a single track).
|
/// An item from the library. An item corresponds to an individual file (usually a single track).
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct Item {
|
pub struct Item {
|
||||||
@ -127,7 +136,14 @@ impl From<Utf8Error> for Error {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use std::io;
|
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]
|
#[test]
|
||||||
fn query() {
|
fn query() {
|
||||||
|
Loading…
Reference in New Issue
Block a user