From 793a6a572d62a2ff0aa3c405897abff206516259 Mon Sep 17 00:00:00 2001 From: jakka Date: Sat, 5 Jul 2025 21:18:38 +0300 Subject: added test for file cleaning --- src/db.rs | 31 +++++++++++++++++-------------- src/files.rs | 34 ++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/db.rs b/src/db.rs index 6e1667b..86f2083 100644 --- a/src/db.rs +++ b/src/db.rs @@ -166,7 +166,7 @@ mod tests { let pool = open_db(Some(&dbname), 10).unwrap(); let conn = Database::new(pool.get().unwrap()); for file in filenames { - let _ = conn.insert_file(&file.to_string()); + conn.insert_file(&file.to_string()).unwrap(); } let mut stmt = conn.0.prepare(TOENCODE_QUERY).unwrap(); let mut returned = stmt.query(()).unwrap(); @@ -185,21 +185,24 @@ mod tests { let pool = open_db(Some(&dbname), 10).unwrap(); let conn = Database::new(pool.get().unwrap()); for file in filenames { - let _ = conn.insert_file(Path::new(file).canonicalize().unwrap()); + conn.insert_file(Path::new(file).canonicalize().unwrap()) + .unwrap(); } - let _ = conn.0.execute( - REPLACE_ITEM, - params![ - Path::new("16bit.flac") - .canonicalize() - .unwrap() - .to_str() - .unwrap(), - true, - "" - ], - ); + conn.0 + .execute( + REPLACE_ITEM, + params![ + Path::new("16bit.flac") + .canonicalize() + .unwrap() + .to_str() + .unwrap(), + true, + "" + ], + ) + .unwrap(); conn.update_file( Path::new("16bit.flac") diff --git a/src/files.rs b/src/files.rs index 2b924d8..39c9c9f 100644 --- a/src/files.rs +++ b/src/files.rs @@ -24,13 +24,13 @@ const BAR_TEMPLATE: &str = "{msg:<} [{wide_bar:.green/cyan}] Elapsed: {elapsed} const SPINNER_TEMPLATE: &str = "Removed from db: {pos:.green}"; #[derive(Debug)] -pub struct FileError { +struct FileError { file: PathBuf, error: anyhow::Error, } impl FileError { - pub fn new(file: impl AsRef, error: anyhow::Error) -> Self { + fn new(file: impl AsRef, error: anyhow::Error) -> Self { FileError { file: file.as_ref().to_path_buf(), error, @@ -231,16 +231,38 @@ mod tests { #[test] fn test_index_lots_of_files() { + let dbname = "temp3.db"; let handler = Arc::new(AtomicBool::new(true)); - let pool = open_db(Some("temp3.db"), 10).unwrap(); + let pool = open_db(Some(dbname), 10).unwrap(); index_files_recursively(Path::new("./testfiles"), &pool, handler).unwrap(); - std::fs::remove_file("temp3.db").unwrap(); + std::fs::remove_file(dbname).unwrap(); + } + + #[test] + fn test_clean_files() { + let dbname = "temp4.db"; + let handler = Arc::new(AtomicBool::new(true)); + let pool = open_db(Some(dbname), 10).unwrap(); + let conn = Database::new(pool.get().unwrap()); + let filenames = ["16bit.flac", "24bit.flac", "32bit.flac", "nonexisting.flac"]; + std::fs::copy("32bit.flac", "nonexisting.flac").unwrap(); + for file in filenames { + conn.insert_file(&file.to_string()).unwrap(); + } + + std::fs::remove_file("nonexisting.flac").unwrap(); + + clean_files(&pool, handler).unwrap(); + let counter = conn.init_clean_files().unwrap().len(); + std::fs::remove_file(dbname).unwrap(); + assert!(counter == 3) } #[test] fn test_reencode_lots_of_files() { + let dbname = "temp5.db"; let handler = Arc::new(AtomicBool::new(true)); - let pool = open_db(Some("temp4.db"), 10).unwrap(); + let pool = open_db(Some(dbname), 10).unwrap(); let temp = handler.clone(); index_files_recursively(Path::new("./testfiles"), &pool, temp).unwrap(); let conn = Database::new(pool.get().unwrap()); @@ -249,6 +271,6 @@ mod tests { reencode_files(&pool, handler).unwrap(); let conn = Database::new(pool.get().unwrap()); println!("\n{}", conn.get_toencode_number().unwrap()); - std::fs::remove_file("temp4.db").unwrap(); + std::fs::remove_file(dbname).unwrap(); } } -- cgit v1.3.1