Wojciech Kozlowski
14a0567fa1
Closes #14 Reviewed-on: https://git.wojciechkozlowski.eu/wojtek/musichoard/pulls/17
61 lines
1.5 KiB
Rust
61 lines
1.5 KiB
Rust
use std::{fs, path::PathBuf};
|
|
|
|
use musichoard::{
|
|
database::{
|
|
json::{JsonDatabase, JsonDatabaseFileBackend},
|
|
Database,
|
|
},
|
|
Artist,
|
|
};
|
|
use once_cell::sync::Lazy;
|
|
use tempfile::NamedTempFile;
|
|
|
|
use crate::COLLECTION;
|
|
|
|
static DATABASE_TEST_FILE: Lazy<PathBuf> =
|
|
Lazy::new(|| fs::canonicalize("./tests/files/database/database.json").unwrap());
|
|
|
|
#[test]
|
|
fn write() {
|
|
let file = NamedTempFile::new().unwrap();
|
|
|
|
let backend = JsonDatabaseFileBackend::new(file.path());
|
|
let mut database = JsonDatabase::new(Box::new(backend));
|
|
|
|
let write_data = COLLECTION.to_owned();
|
|
database.write(&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 read() {
|
|
let backend = JsonDatabaseFileBackend::new(&*DATABASE_TEST_FILE);
|
|
let database = JsonDatabase::new(Box::new(backend));
|
|
|
|
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();
|
|
|
|
let backend = JsonDatabaseFileBackend::new(file.path());
|
|
let mut database = JsonDatabase::new(Box::new(backend));
|
|
|
|
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);
|
|
}
|