Add mocks to json database tests

This commit is contained in:
Wojciech Kozlowski 2023-04-12 20:52:21 +02:00
parent 670e3f4cad
commit 5efc3ab0c5

View File

@ -6,9 +6,13 @@ use std::path::{Path, PathBuf};
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
#[cfg(test)]
use mockall::automock;
use super::{Database, DatabaseRead, DatabaseWrite}; use super::{Database, DatabaseRead, DatabaseWrite};
/// Trait for the JSON database backend. /// Trait for the JSON database backend.
#[cfg_attr(test, automock)]
pub trait JsonDatabaseBackend { pub trait JsonDatabaseBackend {
/// Read the JSON string from the backend. /// Read the JSON string from the backend.
fn read(&self) -> Result<String, std::io::Error>; fn read(&self) -> Result<String, std::io::Error>;
@ -81,25 +85,12 @@ impl JsonDatabaseBackend for JsonDatabaseFileBackend {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use mockall::predicate;
use super::*; use super::*;
use crate::{tests::COLLECTION, Artist, TrackFormat}; use crate::{tests::COLLECTION, Artist, TrackFormat};
struct JsonDatabaseTestBackend {
json: String,
}
impl JsonDatabaseBackend for JsonDatabaseTestBackend {
fn read(&self) -> Result<String, std::io::Error> {
Ok(self.json.clone())
}
fn write(&mut self, json: &str) -> Result<(), std::io::Error> {
assert_eq!(json, self.json);
Ok(())
}
}
fn artist_to_json(artist: &Artist) -> String { fn artist_to_json(artist: &Artist) -> String {
let album_artist = &artist.id.name; let album_artist = &artist.id.name;
@ -167,9 +158,14 @@ mod tests {
#[test] #[test]
fn write() { fn write() {
let write_data = COLLECTION.to_owned(); let write_data = COLLECTION.to_owned();
let backend = JsonDatabaseTestBackend { let input = artists_to_json(&write_data);
json: artists_to_json(&write_data),
}; let mut backend = MockJsonDatabaseBackend::new();
backend
.expect_write()
.with(predicate::eq(input))
.times(1)
.return_once(|_| Ok(()));
JsonDatabase::new(Box::new(backend)) JsonDatabase::new(Box::new(backend))
.write(&write_data) .write(&write_data)
@ -179,9 +175,10 @@ mod tests {
#[test] #[test]
fn read() { fn read() {
let expected = COLLECTION.to_owned(); let expected = COLLECTION.to_owned();
let backend = JsonDatabaseTestBackend { let result = Ok(artists_to_json(&expected));
json: artists_to_json(&expected),
}; let mut backend = MockJsonDatabaseBackend::new();
backend.expect_read().times(1).return_once(|| result);
let mut read_data: Vec<Artist> = vec![]; let mut read_data: Vec<Artist> = vec![];
JsonDatabase::new(Box::new(backend)) JsonDatabase::new(Box::new(backend))
@ -194,9 +191,17 @@ mod tests {
#[test] #[test]
fn reverse() { fn reverse() {
let expected = COLLECTION.to_owned(); let expected = COLLECTION.to_owned();
let backend = JsonDatabaseTestBackend { let input = artists_to_json(&expected);
json: artists_to_json(&expected), let result = Ok(input.clone());
};
let mut backend = MockJsonDatabaseBackend::new();
backend
.expect_write()
.with(predicate::eq(input))
.times(1)
.return_once(|_| Ok(()));
backend.expect_read().times(1).return_once(|| result);
let mut database = JsonDatabase::new(Box::new(backend)); let mut database = JsonDatabase::new(Box::new(backend));
let write_data = COLLECTION.to_owned(); let write_data = COLLECTION.to_owned();