musichoard/tests/database/json.rs

60 lines
1.5 KiB
Rust

use std::fs;
use musichoard::{
database::{
json::{DatabaseJson, DatabaseJsonFile},
DatabaseRead, DatabaseWrite,
},
Artist,
};
use tempfile::NamedTempFile;
use crate::COLLECTION;
#[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();
let expected_path = fs::canonicalize("./tests/files/database/database.json").unwrap();
let expected = fs::read_to_string(expected_path).unwrap();
let actual = fs::read_to_string(file.path()).unwrap();
assert_eq!(actual, expected);
}
#[test]
fn read() {
let file_path = fs::canonicalize("./tests/files/database/database.json").unwrap();
let backend = DatabaseJsonFile::new(&file_path);
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);
}