diff --git a/README.md b/README.md index 15d5d1a..8de3320 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/tests/database/sql.rs b/tests/database/sql.rs index 017167b..7690082 100644 --- a/tests/database/sql.rs +++ b/tests/database/sql.rs @@ -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 = - 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 = 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(); diff --git a/tests/files/database/database.db b/tests/files/database/complete.db similarity index 100% rename from tests/files/database/database.db rename to tests/files/database/complete.db diff --git a/tests/files/database/partial.db b/tests/files/database/partial.db new file mode 100644 index 0000000..aae6c9a Binary files /dev/null and b/tests/files/database/partial.db differ diff --git a/tests/lib.rs b/tests/lib.rs index 6cceef8..5900803 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -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 = + Lazy::new(|| fs::canonicalize("./tests/files/database/partial.db").unwrap()); +pub static COMPLETE_DB_TEST_FILE: Lazy = + Lazy::new(|| fs::canonicalize("./tests/files/database/complete.db").unwrap()); + pub fn copy_file_into_temp>(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()); }