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