musichoard/tests/database/json.rs
Wojciech Kozlowski c2506657c3
All checks were successful
Cargo CI / Build and Test (push) Successful in 1m0s
Cargo CI / Lint (push) Successful in 43s
Streamline adding new URL types (#122)
Closes #117

Reviewed-on: #122
2024-02-09 18:41:20 +01:00

45 lines
1.2 KiB
Rust

use std::{fs, path::PathBuf};
use once_cell::sync::Lazy;
use tempfile::NamedTempFile;
use musichoard::{
collection::artist::Artist,
database::{
json::{backend::JsonDatabaseFileBackend, JsonDatabase},
IDatabase,
},
};
use crate::testlib::COLLECTION;
pub static DATABASE_TEST_FILE: Lazy<PathBuf> =
Lazy::new(|| fs::canonicalize("./tests/files/database/database.json").unwrap());
#[test]
fn load() {
let backend = JsonDatabaseFileBackend::new(&*DATABASE_TEST_FILE);
let database = JsonDatabase::new(backend);
let read_data: Vec<Artist> = database.load().unwrap();
let expected = COLLECTION.to_owned();
assert_eq!(read_data, expected);
}
#[test]
fn reverse() {
// Saving is non-deterministic due to HashMap, but regardless of how the data ends up being
// saved, loading it again should always yield the exact same data as was input.
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 read_data: Vec<Artist> = database.load().unwrap();
assert_eq!(write_data, read_data);
}