2025-01-12 10:24:53 +01:00
|
|
|
use std::{fs, path::PathBuf};
|
|
|
|
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
|
|
|
use musichoard::{
|
|
|
|
collection::{artist::Artist, Collection},
|
|
|
|
external::database::sql::{backend::SqlDatabaseSqliteBackend, SqlDatabase},
|
|
|
|
interface::database::IDatabase,
|
|
|
|
};
|
|
|
|
use tempfile::NamedTempFile;
|
|
|
|
|
2025-01-14 21:49:05 +01:00
|
|
|
use crate::{copy_file_into_temp, testlib::COLLECTION};
|
2025-01-12 10:24:53 +01:00
|
|
|
|
|
|
|
pub static DATABASE_TEST_FILE: Lazy<PathBuf> =
|
|
|
|
Lazy::new(|| fs::canonicalize("./tests/files/database/database.db").unwrap());
|
|
|
|
|
|
|
|
fn expected() -> Collection {
|
|
|
|
let mut expected = COLLECTION.to_owned();
|
|
|
|
for artist in expected.iter_mut() {
|
|
|
|
for album in artist.albums.iter_mut() {
|
|
|
|
album.tracks.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expected
|
|
|
|
}
|
|
|
|
|
2025-01-14 21:49:05 +01:00
|
|
|
#[test]
|
|
|
|
fn reset() {
|
|
|
|
let file = NamedTempFile::new().unwrap();
|
|
|
|
|
|
|
|
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
|
|
|
|
let mut database = SqlDatabase::new(backend).unwrap();
|
|
|
|
|
|
|
|
database.reset().unwrap();
|
|
|
|
}
|
|
|
|
|
2025-01-12 10:24:53 +01:00
|
|
|
#[test]
|
|
|
|
fn save() {
|
|
|
|
let file = NamedTempFile::new().unwrap();
|
|
|
|
|
|
|
|
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
|
|
|
|
let mut database = SqlDatabase::new(backend).unwrap();
|
|
|
|
|
|
|
|
let write_data = COLLECTION.to_owned();
|
|
|
|
database.save(&write_data).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn load() {
|
|
|
|
let backend = SqlDatabaseSqliteBackend::new(&*DATABASE_TEST_FILE).unwrap();
|
|
|
|
let mut database = SqlDatabase::new(backend).unwrap();
|
|
|
|
|
|
|
|
let read_data: Vec<Artist> = database.load().unwrap();
|
|
|
|
|
|
|
|
let expected = expected();
|
|
|
|
assert_eq!(read_data, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reverse() {
|
|
|
|
let file = NamedTempFile::new().unwrap();
|
|
|
|
|
|
|
|
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
|
|
|
|
let mut database = SqlDatabase::new(backend).unwrap();
|
|
|
|
|
|
|
|
let write_data = COLLECTION.to_owned();
|
|
|
|
database.save(&write_data).unwrap();
|
|
|
|
let read_data: Vec<Artist> = database.load().unwrap();
|
|
|
|
|
|
|
|
// Not all data is saved into database.
|
|
|
|
let expected = expected();
|
|
|
|
assert_eq!(read_data, expected);
|
|
|
|
}
|
2025-01-14 21:49:05 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reverse_with_reset() {
|
|
|
|
let file = copy_file_into_temp(&*DATABASE_TEST_FILE);
|
|
|
|
|
|
|
|
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
|
|
|
|
let mut database = SqlDatabase::new(backend).unwrap();
|
|
|
|
|
|
|
|
let expected: Vec<Artist> = database.load().unwrap();
|
|
|
|
database.reset().unwrap();
|
|
|
|
database.save(&expected).unwrap();
|
|
|
|
let read_data: Vec<Artist> = database.load().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(read_data, expected);
|
|
|
|
}
|