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());

View File

@ -1 +1 @@
{"V20240210":[{"name":"Аркона","sort":"Arkona","musicbrainz":"https://musicbrainz.org/artist/baad262d-55ef-427a-83c7-f7530964f212","properties":{"Bandcamp":["https://arkonamoscow.bandcamp.com/"],"MusicButler":["https://www.musicbutler.io/artist-page/283448581"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/arkona/download-streaming-albums"]}},{"name":"Eluveitie","sort":null,"musicbrainz":"https://musicbrainz.org/artist/8000598a-5edb-401c-8e6d-36b167feaf38","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/269358403"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/eluveitie/download-streaming-albums"]}},{"name":"Frontside","sort":null,"musicbrainz":"https://musicbrainz.org/artist/3a901353-fccd-4afd-ad01-9c03f451b490","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/826588800"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/frontside/download-streaming-albums"]}},{"name":"Heavens Basement","sort":"Heavens Basement","musicbrainz":"https://musicbrainz.org/artist/c2c4d56a-d599-4a18-bd2f-ae644e2198cc","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/291158685"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/heaven-s-basement/download-streaming-albums"]}},{"name":"Metallica","sort":null,"musicbrainz":"https://musicbrainz.org/artist/65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/3996865"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/metallica/download-streaming-albums"]}}]}
{"V20240210":[{"name":"Аркона","sort":"Arkona","musicbrainz":"https://musicbrainz.org/artist/baad262d-55ef-427a-83c7-f7530964f212","properties":{"Bandcamp":["https://arkonamoscow.bandcamp.com/"],"MusicButler":["https://www.musicbutler.io/artist-page/283448581"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/arkona/download-streaming-albums"]}},{"name":"Eluveitie","sort":null,"musicbrainz":"https://musicbrainz.org/artist/8000598a-5edb-401c-8e6d-36b167feaf38","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/269358403"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/eluveitie/download-streaming-albums"]}},{"name":"Frontside","sort":null,"musicbrainz":"https://musicbrainz.org/artist/3a901353-fccd-4afd-ad01-9c03f451b490","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/826588800"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/frontside/download-streaming-albums"]}},{"name":"Heavens Basement","sort":"Heavens Basement","musicbrainz":"https://musicbrainz.org/artist/c2c4d56a-d599-4a18-bd2f-ae644e2198cc","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/291158685"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/heaven-s-basement/download-streaming-albums"]}},{"name":"Metallica","sort":null,"musicbrainz":"https://musicbrainz.org/artist/65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/3996865"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/metallica/download-streaming-albums"]}}]}