2025-01-11 14:12:40 +01:00
|
|
|
use std::{fs, path::PathBuf};
|
|
|
|
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
|
|
|
use musichoard::{
|
2025-01-11 15:47:07 +01:00
|
|
|
collection::{artist::Artist, Collection},
|
2025-01-11 14:12:40 +01:00
|
|
|
external::database::sql::{backend::SqlDatabaseSqliteBackend, SqlDatabase},
|
|
|
|
interface::database::IDatabase,
|
|
|
|
};
|
2025-01-11 15:47:07 +01:00
|
|
|
use tempfile::NamedTempFile;
|
2025-01-11 14:12:40 +01:00
|
|
|
|
|
|
|
use crate::testlib::COLLECTION;
|
|
|
|
|
|
|
|
pub static DATABASE_TEST_FILE: Lazy<PathBuf> =
|
|
|
|
Lazy::new(|| fs::canonicalize("./tests/files/database/database.db").unwrap());
|
|
|
|
|
2025-01-11 15:47:07 +01:00
|
|
|
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-11 14:12:40 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn save() {
|
2025-01-11 15:47:07 +01:00
|
|
|
let file = NamedTempFile::new().unwrap();
|
2025-01-11 14:12:40 +01:00
|
|
|
|
2025-01-11 15:47:07 +01:00
|
|
|
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
|
2025-01-11 14:12:40 +01:00
|
|
|
let mut database = SqlDatabase::new(backend).unwrap();
|
|
|
|
|
|
|
|
let write_data = COLLECTION.to_owned();
|
|
|
|
database.save(&write_data).unwrap();
|
|
|
|
}
|
|
|
|
|
2025-01-11 15:47:07 +01:00
|
|
|
#[test]
|
|
|
|
fn load() {
|
|
|
|
let backend = SqlDatabaseSqliteBackend::new(&*DATABASE_TEST_FILE).unwrap();
|
|
|
|
let mut database = SqlDatabase::new(backend).unwrap();
|
2025-01-11 14:12:40 +01:00
|
|
|
|
2025-01-11 15:47:07 +01:00
|
|
|
let read_data: Vec<Artist> = database.load().unwrap();
|
2025-01-11 14:12:40 +01:00
|
|
|
|
2025-01-11 15:47:07 +01:00
|
|
|
let expected = expected();
|
|
|
|
assert_eq!(read_data, expected);
|
|
|
|
}
|
2025-01-11 14:12:40 +01:00
|
|
|
|
2025-01-11 15:47:07 +01:00
|
|
|
#[test]
|
|
|
|
fn reverse() {
|
|
|
|
let file = NamedTempFile::new().unwrap();
|
2025-01-11 14:12:40 +01:00
|
|
|
|
2025-01-11 15:47:07 +01:00
|
|
|
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
|
|
|
|
let mut database = SqlDatabase::new(backend).unwrap();
|
2025-01-11 14:12:40 +01:00
|
|
|
|
2025-01-11 15:47:07 +01:00
|
|
|
let write_data = COLLECTION.to_owned();
|
|
|
|
database.save(&write_data).unwrap();
|
|
|
|
let read_data: Vec<Artist> = database.load().unwrap();
|
2025-01-11 14:12:40 +01:00
|
|
|
|
2025-01-11 15:47:07 +01:00
|
|
|
// Not all data is saved into database.
|
|
|
|
let expected = expected();
|
|
|
|
assert_eq!(read_data, expected);
|
|
|
|
}
|