musichoard/tests/lib.rs
2025-01-15 21:42:28 +01:00

96 lines
3.3 KiB
Rust

#![cfg(feature = "database-sqlite")]
#![cfg(feature = "library-beets")]
mod database;
mod library;
mod testlib;
use std::{ffi::OsStr, fs, path::PathBuf, process::Command};
use musichoard::{
external::{
database::sql::{backend::SqlDatabaseSqliteBackend, SqlDatabase},
library::beets::{executor::BeetsLibraryProcessExecutor, BeetsLibrary},
},
IMusicHoardBase, IMusicHoardDatabase, IMusicHoardLibrary, MusicHoard,
};
use once_cell::sync::Lazy;
use tempfile::NamedTempFile;
use crate::testlib::COLLECTION;
pub static PARTIAL_DB_TEST_FILE: Lazy<PathBuf> =
Lazy::new(|| fs::canonicalize("./tests/files/database/partial.db").unwrap());
pub static COMPLETE_DB_TEST_FILE: Lazy<PathBuf> =
Lazy::new(|| fs::canonicalize("./tests/files/database/complete.db").unwrap());
pub fn copy_file_into_temp<P: Into<PathBuf>>(path: P) -> NamedTempFile {
let temp = NamedTempFile::new().unwrap();
fs::copy(path.into(), temp.path()).unwrap();
temp
}
pub fn sqldiff(left: &OsStr, right: &OsStr) {
let output = Command::new("sqldiff")
.arg(left)
.arg(right)
.output()
.unwrap();
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert_eq!(stdout, "");
}
#[test]
fn merge_library_then_database() {
// Acquired the lock on the beets config file. We need to own the underlying object so later we
// create a new one. This is okay as the purpose of the lock is to prevent other tests to access
// the same Beets library at the same time.
let _arc = library::beets::BEETS_TEST_CONFIG.clone();
let _ = &mut _arc.lock().unwrap();
let executor = BeetsLibraryProcessExecutor::default()
.config(Some(&*library::beets::BEETS_TEST_CONFIG_PATH));
let library = BeetsLibrary::new(executor);
let file = copy_file_into_temp(&*PARTIAL_DB_TEST_FILE);
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
let database = SqlDatabase::new(backend).unwrap();
let mut music_hoard = MusicHoard::new(database, library);
music_hoard.rescan_library().unwrap();
music_hoard.reload_database().unwrap();
assert_eq!(music_hoard.get_collection(), &*COLLECTION);
sqldiff(&*COMPLETE_DB_TEST_FILE.as_os_str(), file.path().as_os_str());
}
#[test]
fn merge_database_then_library() {
// Acquired the lock on the beets config file. We need to own the underlying object so later we
// create a new one. This is okay as the purpose of the lock is to prevent other tests to access
// the same Beets library at the same time.
let _arc = library::beets::BEETS_TEST_CONFIG.clone();
let _ = &mut _arc.lock().unwrap();
let executor = BeetsLibraryProcessExecutor::default()
.config(Some(&*library::beets::BEETS_TEST_CONFIG_PATH));
let library = BeetsLibrary::new(executor);
let file = copy_file_into_temp(&*PARTIAL_DB_TEST_FILE);
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
let database = SqlDatabase::new(backend).unwrap();
let mut music_hoard = MusicHoard::new(database, library);
music_hoard.reload_database().unwrap();
music_hoard.rescan_library().unwrap();
assert_eq!(music_hoard.get_collection(), &*COLLECTION);
sqldiff(&*COMPLETE_DB_TEST_FILE.as_os_str(), file.path().as_os_str());
}