diff --git a/01.workspace/heave/src/imp/catalog_load_by_id.rs b/01.workspace/heave/src/imp/catalog_load_by_id.rs index a6aefa6..8e8d7de 100644 --- a/01.workspace/heave/src/imp/catalog_load_by_id.rs +++ b/01.workspace/heave/src/imp/catalog_load_by_id.rs @@ -25,7 +25,7 @@ impl Catalog { /// /// Returns `Err(FailedTo::LoadFromDB)` if there is an issue loading the entity /// from the database. - pub fn load_by_id(&mut self, id: &str) -> Result<(), FailedTo> { + pub fn load_by_id(&self, id: &str) -> Result<(), FailedTo> { let path = path::Path::new(&self.path); self.on_items(|items| { let entity = sqlite::load::by_id(path, id).map_err(|_| FailedTo::LoadFromDB)?; diff --git a/01.workspace/heave/src/tst/catalog_integration.rs b/01.workspace/heave/src/tst/catalog_integration.rs index 7c9e815..d1c7a88 100644 --- a/01.workspace/heave/src/tst/catalog_integration.rs +++ b/01.workspace/heave/src/tst/catalog_integration.rs @@ -25,7 +25,7 @@ mod tests { let _ = catalog1.upsert(item_to_insert.clone()); catalog1.persist().unwrap(); // 2. create a new catalog instance -> 'load_by_id' -> 'get' - let mut catalog2 = Catalog::new(db_path); + let catalog2 = Catalog::new(db_path); catalog2.load_by_id("item-1").unwrap(); let loaded_item: Option = catalog2.get("item-1").unwrap(); // 3. verify data integrity @@ -109,14 +109,14 @@ mod tests { let _ = catalog1.upsert(item_to_delete.clone()); catalog1.persist().unwrap(); // 2. 'load_by_id' to confirm it's there - let mut catalog2 = Catalog::new(db_path); + let catalog2 = Catalog::new(db_path); catalog2.load_by_id("item-to-delete").unwrap(); assert!(catalog2.get::("item-to-delete").unwrap().is_some()); // 3. 'delete' -> 'persist' catalog2.delete("item-to-delete"); catalog2.persist().unwrap(); // 4. 'load_by_id' should now return nothing - let mut catalog3 = Catalog::new(db_path); + let catalog3 = Catalog::new(db_path); catalog3.load_by_id("item-to-delete").unwrap(); let loaded_item: Option = catalog3.get("item-to-delete").unwrap(); assert!(loaded_item.is_none()); @@ -152,7 +152,7 @@ mod tests { // 2. Thread 1: Loads, updates name, and persists. let db_path_arc1 = std::sync::Arc::clone(&db_path_arc); let handle1 = std::thread::spawn(move || { - let mut catalog1 = Catalog::new(&db_path_arc1); + let catalog1 = Catalog::new(&db_path_arc1); catalog1.load_by_id("item-1").unwrap(); let mut item = catalog1.get::("item-1").unwrap().unwrap(); item.name = "Updated by Thread 1".to_string(); @@ -162,7 +162,7 @@ mod tests { // 3. Thread 2: Loads, updates price, and persists. let db_path_arc2 = std::sync::Arc::clone(&db_path_arc); let handle2 = std::thread::spawn(move || { - let mut catalog2 = Catalog::new(&db_path_arc2); + let catalog2 = Catalog::new(&db_path_arc2); catalog2.load_by_id("item-1").unwrap(); let mut item = catalog2.get::("item-1").unwrap().unwrap(); item.price = 200; @@ -172,7 +172,7 @@ mod tests { handle1.join().unwrap(); handle2.join().unwrap(); // 4. Verification: Load the data and check the final state. - let mut catalog_verify = Catalog::new(db_path); + let catalog_verify = Catalog::new(db_path); catalog_verify.load_by_id("item-1").unwrap(); let final_item: Item = catalog_verify.get("item-1").unwrap().unwrap(); // The final state depends on which thread persisted last. One update will have been lost. diff --git a/01.workspace/heave/src/tst/catalog_load_by_id.rs b/01.workspace/heave/src/tst/catalog_load_by_id.rs index d421fd0..40f7486 100644 --- a/01.workspace/heave/src/tst/catalog_load_by_id.rs +++ b/01.workspace/heave/src/tst/catalog_load_by_id.rs @@ -25,7 +25,7 @@ mod tests { let _ = catalog1.upsert(item_to_persist.clone()); catalog1.persist().unwrap(); // 2. Create a new, empty catalog instance for the same DB. - let mut catalog2 = Catalog::new(db_path); + let catalog2 = Catalog::new(db_path); assert!(catalog2.is_empty().unwrap()); // 3. Load the item by its ID. let result = catalog2.load_by_id("item-1"); @@ -80,7 +80,7 @@ mod tests { let _ = catalog1.upsert(item_in_db.clone()); catalog1.persist().unwrap(); // 2. Create a new catalog and add a *different* in-memory version of the same item. - let mut catalog2 = Catalog::new(db_path); + let catalog2 = Catalog::new(db_path); let item_in_memory = Item { id: "item-1".to_string(), subclass: Some("subitem".to_string()), @@ -135,7 +135,7 @@ mod tests { std::fs::remove_file(path).unwrap(); } // 1. Create an empty, initialized database. - let mut catalog = Catalog::new(db_path); + let catalog = Catalog::new(db_path); catalog.init().unwrap(); // 2. Attempt to load an ID that does not exist. let result = catalog.load_by_id("nonexistent-id"); @@ -151,7 +151,7 @@ mod tests { // Using a directory as a path should cause a failure. let invalid_path = "target/test_dbs/a_directory_for_load_fail"; std::fs::create_dir_all(invalid_path).unwrap(); - let mut catalog = Catalog::new(invalid_path); + let catalog = Catalog::new(invalid_path); // Attempt to load from the invalid path. let result = catalog.load_by_id("any-id"); assert!(result.is_err()); diff --git a/01.workspace/heave/src/tst/catalog_persist.rs b/01.workspace/heave/src/tst/catalog_persist.rs index df027e5..270df5d 100644 --- a/01.workspace/heave/src/tst/catalog_persist.rs +++ b/01.workspace/heave/src/tst/catalog_persist.rs @@ -25,7 +25,7 @@ mod tests { let _ = catalog1.upsert(item1.clone()); assert!(catalog1.persist().is_ok()); // 2. Create a new catalog and load the item to verify it was persisted - let mut catalog2 = Catalog::new(db_path); + let catalog2 = Catalog::new(db_path); assert!(catalog2.load_by_id("item-1").is_ok()); // 3. Get the item and assert it's the same as the one we inserted let loaded_item: Option = catalog2.get("item-1").unwrap(); @@ -59,7 +59,7 @@ mod tests { catalog1.delete(&item1.id); assert!(catalog1.persist().is_ok()); // 3. Create a new catalog and try to load the deleted item. - let mut catalog2 = Catalog::new(db_path); + let catalog2 = Catalog::new(db_path); assert!(catalog2.load_by_id(&item1.id).is_ok()); // 4. Assert that the item was not found. let loaded_item: Option = catalog2.get(&item1.id).unwrap(); @@ -91,7 +91,7 @@ mod tests { let _ = catalog1.upsert(original_item.clone()); catalog1.persist().unwrap(); // 2. Load it into a new catalog to simulate a separate session. - let mut catalog2 = Catalog::new(db_path); + let catalog2 = Catalog::new(db_path); catalog2.load_by_id("item-1").unwrap(); // 3. Upsert updated data for the same item. This should mark it as 'Updated'. let updated_item = Item { @@ -113,7 +113,7 @@ mod tests { // 4. Persist the changes. catalog2.persist().unwrap(); // 5. Load the data into a third catalog to verify the update was written to the DB. - let mut catalog3 = Catalog::new(db_path); + let catalog3 = Catalog::new(db_path); catalog3.load_by_id("item-1").unwrap(); let loaded_item: Item = catalog3.get("item-1").unwrap().unwrap(); // 6. Assert that the loaded item has the updated values.