This commit is contained in:
Wojciech Kozlowski 2025-01-15 21:42:28 +01:00
parent aea710eb32
commit 8a9030bf61
5 changed files with 35 additions and 14 deletions

View File

@ -8,7 +8,7 @@
This feature requires the `sqlite` library.
Either install system libraries: with
Either install system libraries with:
On Fedora:
``` sh
@ -17,6 +17,13 @@ sudo dnf install sqlite-devel
Or use a bundled version by enabling the `database-sqlite-bundled` feature.
To run the tests you will also need `sqldiff`.
On Fedora:
``` sh
sudo dnf install sqlite-tools
```
#### musicbrainz-api
This feature requires the `openssl` system library.

View File

@ -1,7 +1,3 @@
use std::{fs, path::PathBuf};
use once_cell::sync::Lazy;
use musichoard::{
collection::{artist::Artist, Collection},
external::database::sql::{backend::SqlDatabaseSqliteBackend, SqlDatabase},
@ -9,10 +5,7 @@ use musichoard::{
};
use tempfile::NamedTempFile;
use crate::{copy_file_into_temp, testlib::COLLECTION};
pub static DATABASE_TEST_FILE: Lazy<PathBuf> =
Lazy::new(|| fs::canonicalize("./tests/files/database/database.db").unwrap());
use crate::{copy_file_into_temp, testlib::COLLECTION, COMPLETE_DB_TEST_FILE};
fn expected() -> Collection {
let mut expected = COLLECTION.to_owned();
@ -47,7 +40,7 @@ fn save() {
#[test]
fn load() {
let backend = SqlDatabaseSqliteBackend::new(&*DATABASE_TEST_FILE).unwrap();
let backend = SqlDatabaseSqliteBackend::new(&*COMPLETE_DB_TEST_FILE).unwrap();
let mut database = SqlDatabase::new(backend).unwrap();
let read_data: Vec<Artist> = database.load().unwrap();
@ -74,7 +67,7 @@ fn reverse() {
#[test]
fn reverse_with_reset() {
let file = copy_file_into_temp(&*DATABASE_TEST_FILE);
let file = copy_file_into_temp(&*COMPLETE_DB_TEST_FILE);
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
let mut database = SqlDatabase::new(backend).unwrap();

Binary file not shown.

View File

@ -6,7 +6,7 @@ mod library;
mod testlib;
use std::{fs, path::PathBuf};
use std::{ffi::OsStr, fs, path::PathBuf, process::Command};
use musichoard::{
external::{
@ -15,16 +15,33 @@ use musichoard::{
},
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
@ -37,7 +54,7 @@ fn merge_library_then_database() {
.config(Some(&*library::beets::BEETS_TEST_CONFIG_PATH));
let library = BeetsLibrary::new(executor);
let file = copy_file_into_temp(&*database::sql::DATABASE_TEST_FILE);
let file = copy_file_into_temp(&*PARTIAL_DB_TEST_FILE);
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
let database = SqlDatabase::new(backend).unwrap();
@ -47,6 +64,8 @@ fn merge_library_then_database() {
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]
@ -61,7 +80,7 @@ fn merge_database_then_library() {
.config(Some(&*library::beets::BEETS_TEST_CONFIG_PATH));
let library = BeetsLibrary::new(executor);
let file = copy_file_into_temp(&*database::sql::DATABASE_TEST_FILE);
let file = copy_file_into_temp(&*PARTIAL_DB_TEST_FILE);
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
let database = SqlDatabase::new(backend).unwrap();
@ -71,4 +90,6 @@ fn merge_database_then_library() {
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());
}