From d0b6cc292762df93db89ec3bbea252d6e9dc7900 Mon Sep 17 00:00:00 2001 From: davidemazzocchi Date: Fri, 10 Oct 2025 10:23:08 +0200 Subject: [PATCH] test: add tests implementations for catalog init --- 01.workspace/heave/src/str/catalog.rs | 39 ++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/01.workspace/heave/src/str/catalog.rs b/01.workspace/heave/src/str/catalog.rs index 630e5ec..7c61c0d 100644 --- a/01.workspace/heave/src/str/catalog.rs +++ b/01.workspace/heave/src/str/catalog.rs @@ -248,19 +248,52 @@ mod tests { #[test] fn init_should_create_db_file_if_not_exists() { // Should create the SQLite database file if it doesn't exist. - todo!(); + let db_path = "target/test_dbs/init_should_create_db_file_if_not_exists.db"; + let path = std::path::Path::new(db_path); + // Ensure the directory exists + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + // Ensure the file does not exist before the test + if path.exists() { + std::fs::remove_file(path).unwrap(); + } + let catalog = Catalog::new(db_path); + let result = catalog.init(); + assert!(result.is_ok()); + assert!(path.exists()); + // Clean up the created file + std::fs::remove_file(path).unwrap(); } #[test] fn init_should_not_fail_if_db_file_exists() { // Should not fail if the database file already exists. - todo!(); + let db_path = "target/test_dbs/init_should_not_fail_if_db_file_exists.db"; + let path = std::path::Path::new(db_path); + // Ensure the directory exists + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + // Create the DB file first + let catalog = Catalog::new(db_path); + catalog.init().unwrap(); + // Calling init() again should not fail + let result = catalog.init(); + assert!(result.is_ok()); + assert!(path.exists()); + // Clean up + std::fs::remove_file(path).unwrap(); } #[test] fn init_should_return_error_for_invalid_path() { // Should return an error for an invalid path or permissions issue. - todo!(); + // Using a directory as a path should fail. + let invalid_path = "target/test_dbs/an_invalid_path_dir"; + std::fs::create_dir_all(invalid_path).unwrap(); + let catalog = Catalog::new(invalid_path); + let result = catalog.init(); + assert!(result.is_err()); + assert_eq!(result.unwrap_err(), FailedTo::InitDatabase); + // Clean up + std::fs::remove_dir_all(invalid_path).unwrap(); } // ## 'insert()' & 'insert_many()'