From 8a9030bf610dade67c2ca955b924ed73777e594e Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Wed, 15 Jan 2025 21:42:28 +0100 Subject: [PATCH] Tests --- README.md | 9 +++++- tests/database/sql.rs | 13 ++------- .../database/{database.db => complete.db} | Bin tests/files/database/partial.db | Bin 0 -> 28672 bytes tests/lib.rs | 27 ++++++++++++++++-- 5 files changed, 35 insertions(+), 14 deletions(-) rename tests/files/database/{database.db => complete.db} (100%) create mode 100644 tests/files/database/partial.db 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 0000000000000000000000000000000000000000..aae6c9a14671b08fe303fd0d820766698a147f13 GIT binary patch literal 28672 zcmeI4Pi)&%9LMc6PP%2WDFaHVV3w&r$Eh=kKkW!G=*qUX6I&I+i~Z7?ICf^c zEu*Tm79=<#Bo16TAi)UdAv;BM$t6&2+va#6>ojXp}BEnA8noc2YSnSr_(q! zTD~XA{Yu3W&rf00e*l5C8%|;NMH&QJcItd_e5T0Hj89A!`Jjxy{~*6}^9;|#{7mtg8Gh<%@|~TW9NB%9 zX;jLF&ObT*^i+5m|7h{C@!824{W#;#uJ#R@r|;d*5Y)o<$c@Bbh|)DSGrZZ_;OI)8BW77>d3iL_O3K=Cq&kwX|@C8V$HVnO>4K4`CO@;@$A&; zbF;w`^_IwA+;;&L5($p2$kL1Y`Jgmhnhza5mj3%3WTSrCmjRf00e*l5C8%|00;m9An@-eaG35JI-vT>Cgl@g$)P* z0U!VbfB+Bx0zd!=0D*lFSbK(v9jQ_4<7Y@3v1u9_S`nu0Iu=d}d8L}GW;8{_)dCiC zg^VU5jAgNqDc~`L^OY>B2&*f?nMK#oCKgHRzayNS6BazL;hs#VmzS4E>wZPn#?=8&X{6+K%e!RK>1oRtcyru80owjfE0npb;^e4!bN=!$;ox9@dV z0&AE0nw?vG%pt#Aa19-YLCUC*kz_>{tD2^hAgj8F^o%4Hw2WNMDN=>>w;$xb`=+{i zt8Si+^f+kLY+g}SHIpITyLKrO1XnZ{m#{$+^uyrD1sSEFB2mG5Ma*R+O;mAS7v+3K z%8ykss>evLStU}YNM5KZ>fOT!zleRXx?qm64DA8))D zMp;2f&*rnbs3^E9=CWf*RAp^Utd1#irjXB7vr@LFE<{evwoo_!MuZ7u=^Cmxwm!Cj zUEOvydwI0dx}f<8`=ok%O=ML$ms1s0B0cW^|DR#6QQSKBDEk|Gjk`fb*xQre?BjI@ zmjVGG00e*l5C8%|00;m9AOHk_z~4up(4ROwoH`OAzw#cscly|w72$&at8(FFel=JO zB9x}$Ukc;?vJ}FcFlj7dp)?UsNSmeu=ZU_-dkdJKH0BpPi)cY%OUka6Zfv$xD#jAS z!zsCW`+-@jLUg2>zW_#PoNF2XDIB_^$wwO5Z;0KqWbv#ejDiG zQvBP{gg}>RiB6E!MvrcWVlUZS@3U1U2<;AX$EkkyI>n8!-?7)Zceo$9DEA3hCT+k5 z1b_e#00KY&2mk>f00e*l5C8%|;GaU^9)_ej(Crc&`cE({ld+-pvq|g5E6#^c`b}R72lN Rq>0?c5NY7Rw7EkR_yafj!7u;- literal 0 HcmV?d00001 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()); }