2023-04-10 20:48:44 +02:00
|
|
|
use std::{fs, path::PathBuf};
|
2023-04-10 00:13:18 +02:00
|
|
|
|
|
|
|
use musichoard::{
|
|
|
|
database::{
|
2023-04-14 16:21:25 +02:00
|
|
|
json::{backend::JsonDatabaseFileBackend, JsonDatabase},
|
2023-04-13 14:09:59 +02:00
|
|
|
Database,
|
2023-04-10 00:13:18 +02:00
|
|
|
},
|
|
|
|
Artist,
|
|
|
|
};
|
2023-04-10 20:48:44 +02:00
|
|
|
use once_cell::sync::Lazy;
|
2023-04-10 00:13:18 +02:00
|
|
|
use tempfile::NamedTempFile;
|
|
|
|
|
|
|
|
use crate::COLLECTION;
|
|
|
|
|
2023-04-10 20:48:44 +02:00
|
|
|
static DATABASE_TEST_FILE: Lazy<PathBuf> =
|
|
|
|
Lazy::new(|| fs::canonicalize("./tests/files/database/database.json").unwrap());
|
|
|
|
|
2023-04-10 00:13:18 +02:00
|
|
|
#[test]
|
|
|
|
fn write() {
|
|
|
|
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();
|
|
|
|
database.write(&write_data).unwrap();
|
|
|
|
|
2023-04-10 20:48:44 +02:00
|
|
|
let expected = fs::read_to_string(&*DATABASE_TEST_FILE).unwrap();
|
2023-04-10 00:13:18 +02:00
|
|
|
let actual = fs::read_to_string(file.path()).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn read() {
|
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
|
|
|
|
|
|
|
let mut read_data: Vec<Artist> = vec![];
|
|
|
|
database.read(&mut read_data).unwrap();
|
|
|
|
|
|
|
|
let expected = COLLECTION.to_owned();
|
|
|
|
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();
|
|
|
|
database.write(&write_data).unwrap();
|
|
|
|
|
|
|
|
let mut read_data: Vec<Artist> = vec![];
|
|
|
|
database.read(&mut read_data).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(write_data, read_data);
|
|
|
|
}
|