2023-04-10 20:48:44 +02:00
|
|
|
use std::{fs, path::PathBuf};
|
2023-04-10 00:13:18 +02:00
|
|
|
|
2024-01-22 23:01:34 +01:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use tempfile::NamedTempFile;
|
|
|
|
|
2023-04-10 00:13:18 +02:00
|
|
|
use musichoard::{
|
2024-03-05 23:24:18 +01:00
|
|
|
collection::{album::AlbumDate, artist::Artist, Collection},
|
2024-03-16 16:57:50 +01:00
|
|
|
external::database::json::{backend::JsonDatabaseFileBackend, JsonDatabase},
|
2024-03-09 19:11:59 +01:00
|
|
|
interface::database::IDatabase,
|
2023-04-10 00:13:18 +02:00
|
|
|
};
|
|
|
|
|
2023-05-21 22:28:51 +02:00
|
|
|
use crate::testlib::COLLECTION;
|
2023-04-10 00:13:18 +02:00
|
|
|
|
2023-05-21 22:28:51 +02:00
|
|
|
pub static DATABASE_TEST_FILE: Lazy<PathBuf> =
|
2023-04-10 20:48:44 +02:00
|
|
|
Lazy::new(|| fs::canonicalize("./tests/files/database/database.json").unwrap());
|
|
|
|
|
2024-02-10 20:23:10 +01:00
|
|
|
fn expected() -> Collection {
|
|
|
|
let mut expected = COLLECTION.to_owned();
|
|
|
|
for artist in expected.iter_mut() {
|
2024-03-05 23:24:18 +01:00
|
|
|
for album in artist.albums.iter_mut() {
|
|
|
|
album.date = AlbumDate::default();
|
|
|
|
album.tracks.clear();
|
|
|
|
}
|
2024-02-10 20:23:10 +01:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-04-10 00:13:18 +02:00
|
|
|
#[test]
|
2023-05-20 00:02:39 +02:00
|
|
|
fn load() {
|
2023-04-10 21:36:43 +02:00
|
|
|
let backend = JsonDatabaseFileBackend::new(&*DATABASE_TEST_FILE);
|
2023-04-13 15:29:14 +02:00
|
|
|
let database = JsonDatabase::new(backend);
|
2023-04-10 00:13:18 +02:00
|
|
|
|
2024-01-12 21:34:01 +01:00
|
|
|
let read_data: Vec<Artist> = database.load().unwrap();
|
2023-04-10 00:13:18 +02:00
|
|
|
|
2024-02-10 20:23:10 +01:00
|
|
|
let expected = expected();
|
2023-04-10 00:13:18 +02:00
|
|
|
assert_eq!(read_data, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reverse() {
|
|
|
|
let file = NamedTempFile::new().unwrap();
|
|
|
|
|
2023-04-10 21:36:43 +02:00
|
|
|
let backend = JsonDatabaseFileBackend::new(file.path());
|
2023-04-13 15:29:14 +02:00
|
|
|
let mut database = JsonDatabase::new(backend);
|
2023-04-10 00:13:18 +02:00
|
|
|
|
|
|
|
let write_data = COLLECTION.to_owned();
|
2023-05-20 00:02:39 +02:00
|
|
|
database.save(&write_data).unwrap();
|
2024-01-12 21:34:01 +01:00
|
|
|
let read_data: Vec<Artist> = database.load().unwrap();
|
2023-04-10 00:13:18 +02:00
|
|
|
|
2024-02-10 20:23:10 +01:00
|
|
|
// Album data is not saved into database.
|
|
|
|
let expected = expected();
|
|
|
|
assert_eq!(read_data, expected);
|
2023-04-10 00:13:18 +02:00
|
|
|
}
|