2025-01-12 11:33:57 +01:00
|
|
|
#![cfg(feature = "database-sqlite")]
|
2024-02-10 20:58:40 +01:00
|
|
|
#![cfg(feature = "library-beets")]
|
|
|
|
|
2023-04-10 00:13:18 +02:00
|
|
|
mod database;
|
|
|
|
mod library;
|
|
|
|
|
2023-05-21 22:28:51 +02:00
|
|
|
mod testlib;
|
2023-04-10 00:13:18 +02:00
|
|
|
|
2025-01-12 12:01:16 +01:00
|
|
|
use std::{fs, path::PathBuf};
|
|
|
|
|
2024-02-10 20:58:40 +01:00
|
|
|
use musichoard::{
|
2024-03-16 16:57:50 +01:00
|
|
|
external::{
|
2025-01-12 11:55:56 +01:00
|
|
|
database::sql::{backend::SqlDatabaseSqliteBackend, SqlDatabase},
|
2024-03-16 16:57:50 +01:00
|
|
|
library::beets::{executor::BeetsLibraryProcessExecutor, BeetsLibrary},
|
|
|
|
},
|
2024-03-09 22:52:03 +01:00
|
|
|
IMusicHoardBase, IMusicHoardDatabase, IMusicHoardLibrary, MusicHoard,
|
2024-02-10 20:58:40 +01:00
|
|
|
};
|
2025-01-12 12:01:16 +01:00
|
|
|
use tempfile::NamedTempFile;
|
2023-05-21 22:28:51 +02:00
|
|
|
|
2024-01-22 23:01:34 +01:00
|
|
|
use crate::testlib::COLLECTION;
|
|
|
|
|
2025-01-12 12:01:16 +01:00
|
|
|
fn copy_file_into_temp<P: Into<PathBuf>>(path: P) -> NamedTempFile {
|
|
|
|
let temp = NamedTempFile::new().unwrap();
|
|
|
|
fs::copy(path.into(), temp.path()).unwrap();
|
|
|
|
temp
|
|
|
|
}
|
|
|
|
|
2023-05-21 22:28:51 +02:00
|
|
|
#[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);
|
|
|
|
|
2025-01-12 12:01:16 +01:00
|
|
|
let file = copy_file_into_temp(&*database::sql::DATABASE_TEST_FILE);
|
|
|
|
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
|
2025-01-12 11:55:56 +01:00
|
|
|
let database = SqlDatabase::new(backend).unwrap();
|
2023-05-21 22:28:51 +02:00
|
|
|
|
2025-01-05 11:06:05 +01:00
|
|
|
let mut music_hoard = MusicHoard::new(database, library);
|
2023-05-21 22:28:51 +02:00
|
|
|
|
|
|
|
music_hoard.rescan_library().unwrap();
|
2024-03-01 09:00:52 +01:00
|
|
|
music_hoard.reload_database().unwrap();
|
2023-05-21 22:28:51 +02:00
|
|
|
|
|
|
|
assert_eq!(music_hoard.get_collection(), &*COLLECTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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);
|
|
|
|
|
2025-01-12 12:01:16 +01:00
|
|
|
let file = copy_file_into_temp(&*database::sql::DATABASE_TEST_FILE);
|
|
|
|
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
|
2025-01-12 11:55:56 +01:00
|
|
|
let database = SqlDatabase::new(backend).unwrap();
|
2023-05-21 22:28:51 +02:00
|
|
|
|
2025-01-05 11:06:05 +01:00
|
|
|
let mut music_hoard = MusicHoard::new(database, library);
|
2023-05-21 22:28:51 +02:00
|
|
|
|
2024-03-01 09:00:52 +01:00
|
|
|
music_hoard.reload_database().unwrap();
|
2023-05-21 22:28:51 +02:00
|
|
|
music_hoard.rescan_library().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(music_hoard.get_collection(), &*COLLECTION);
|
|
|
|
}
|