musichoard/tests/database/json.rs

61 lines
1.5 KiB
Rust
Raw Normal View History

2023-04-10 20:01:23 +02:00
use std::{fs, path::PathBuf};
use musichoard::{
database::{
json::{DatabaseJson, DatabaseJsonFile},
DatabaseRead, DatabaseWrite,
},
Artist,
};
2023-04-10 20:01:23 +02:00
use once_cell::sync::Lazy;
use tempfile::NamedTempFile;
use crate::COLLECTION;
2023-04-10 20:01:23 +02:00
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 = DatabaseJsonFile::new(file.path());
let mut database = DatabaseJson::new(Box::new(backend));
let write_data = COLLECTION.to_owned();
database.write(&write_data).unwrap();
2023-04-10 20:01:23 +02:00
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() {
2023-04-10 20:01:23 +02:00
let backend = DatabaseJsonFile::new(&*DATABASE_TEST_FILE);
let database = DatabaseJson::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 = DatabaseJsonFile::new(file.path());
let mut database = DatabaseJson::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);
}