Add unit tests
Some checks failed
Cargo CI / Lint (pull_request) Failing after 58s
Cargo CI / Build and Test (pull_request) Successful in 1m16s

This commit is contained in:
Wojciech Kozlowski 2024-01-07 11:04:06 +01:00
parent 115373667f
commit 2d25b15f3c
3 changed files with 77 additions and 3 deletions

View File

@ -20,6 +20,19 @@ pub trait IDatabase {
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.
#[derive(Debug)]
pub enum LoadError {
@ -76,7 +89,23 @@ impl From<std::io::Error> 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<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]
fn errors() {

View File

@ -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<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]
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<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]
fn database_load_error() {
let library = MockILibrary::new();

View File

@ -17,6 +17,15 @@ pub trait ILibrary {
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).
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct Item {
@ -127,7 +136,14 @@ impl From<Utf8Error> 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() {