Re-add save tests
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 1m3s
Cargo CI / Lint (pull_request) Successful in 43s

This commit is contained in:
Wojciech Kozlowski 2024-02-10 20:08:36 +01:00
parent 6b8c7c40fa
commit 0d8736a93e
4 changed files with 42 additions and 24 deletions

View File

@ -69,6 +69,8 @@ pub mod testmod;
mod tests {
use std::collections::HashMap;
use mockall::predicate;
use crate::core::{
collection::{artist::Artist, Collection},
testmod::FULL_COLLECTION,
@ -85,7 +87,20 @@ mod tests {
expected
}
// FIXME: Re-add save() test.
#[test]
fn save() {
let write_data = FULL_COLLECTION.to_owned();
let input = DATABASE_JSON.to_owned();
let mut backend = MockIJsonDatabaseBackend::new();
backend
.expect_write()
.with(predicate::eq(input))
.times(1)
.return_once(|_| Ok(()));
JsonDatabase::new(backend).save(&write_data).unwrap();
}
#[test]
fn load() {
@ -102,25 +117,16 @@ mod tests {
#[test]
fn reverse() {
// Saving is non-deterministic due to HashMap, but regardless of how the data ends up being
// saved, loading it again should always yield the exact same data as was input.
// FIXME: remove and replace with mocks now that indeterminism is not a problem.
struct MockIJsonDatabaseBackend {
data: Option<String>,
}
let input = DATABASE_JSON.to_owned();
let result = Ok(input.clone());
impl IJsonDatabaseBackend for MockIJsonDatabaseBackend {
fn write(&mut self, json: &str) -> Result<(), std::io::Error> {
let _ = self.data.insert(json.to_owned());
Ok(())
}
fn read(&self) -> Result<String, std::io::Error> {
Ok(self.data.as_ref().unwrap().clone())
}
}
let backend = MockIJsonDatabaseBackend { data: None };
let mut backend = MockIJsonDatabaseBackend::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(backend);
let write_data = FULL_COLLECTION.to_owned();

View File

@ -1,5 +1,5 @@
pub static DATABASE_JSON: &str = "{\
\"V20240210\":
\"V20240210\":\
[\
{\
\"name\":\"album_artist a\",\

View File

@ -24,7 +24,21 @@ fn expected() -> Collection {
expected
}
// FIXME: Re-add save test.
#[test]
fn save() {
let file = NamedTempFile::new().unwrap();
let backend = JsonDatabaseFileBackend::new(file.path());
let mut database = JsonDatabase::new(backend);
let write_data = COLLECTION.to_owned();
database.save(&write_data).unwrap();
let expected = fs::read_to_string(&*DATABASE_TEST_FILE).unwrap();
let actual = fs::read_to_string(file.path()).unwrap();
assert_eq!(actual, expected);
}
#[test]
fn load() {
@ -39,8 +53,6 @@ fn load() {
#[test]
fn reverse() {
// Saving is non-deterministic due to HashMap, but regardless of how the data ends up being
// saved, loading it again should always yield the exact same data as was input.
let file = NamedTempFile::new().unwrap();
let backend = JsonDatabaseFileBackend::new(file.path());