musichoard/tests/database/json.rs
Wojciech Kozlowski 473825b396
Some checks failed
Cargo CI / Build and Test (pull_request) Failing after 1m37s
Cargo CI / Lint (pull_request) Failing after 16s
First draft of musicbrainz interface
Some reorganisation

Remove unnecessary trait

Basic example working

Handle errors

Handle dates

Expand scope of MusicBrainz reference

Type the musicbrainz refs

Explicit constructors for str

Handle MBIDs for albums

Add search to the API

Handle primary and secondary types

Simplify AlbumDate

Passing unit tests

Tests pass

Prevent compiler/clippy warnings

Finish unit tests

Clippy

Remove old deserialize version
2024-03-16 16:49:26 +01:00

70 lines
1.8 KiB
Rust

use std::{fs, path::PathBuf};
use once_cell::sync::Lazy;
use tempfile::NamedTempFile;
use musichoard::{
collection::{album::AlbumDate, artist::Artist, Collection},
external::database::json::{backend::JsonDatabaseFileBackend, JsonDatabase},
interface::database::IDatabase,
};
use crate::testlib::COLLECTION;
pub static DATABASE_TEST_FILE: Lazy<PathBuf> =
Lazy::new(|| fs::canonicalize("./tests/files/database/database.json").unwrap());
fn expected() -> Collection {
let mut expected = COLLECTION.to_owned();
for artist in expected.iter_mut() {
for album in artist.albums.iter_mut() {
album.date = AlbumDate::default();
album.tracks.clear();
}
}
expected
}
#[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() {
let backend = JsonDatabaseFileBackend::new(&*DATABASE_TEST_FILE);
let database = JsonDatabase::new(backend);
let read_data: Vec<Artist> = database.load().unwrap();
let expected = expected();
assert_eq!(read_data, expected);
}
#[test]
fn reverse() {
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 read_data: Vec<Artist> = database.load().unwrap();
// Album data is not saved into database.
let expected = expected();
assert_eq!(read_data, expected);
}