fix: change mutability to ref while loading by id

This commit is contained in:
2026-02-06 09:52:17 +01:00
parent 5db868b192
commit 313248b1a5
4 changed files with 15 additions and 15 deletions

View File

@@ -25,7 +25,7 @@ impl Catalog {
/// ///
/// Returns `Err(FailedTo::LoadFromDB)` if there is an issue loading the entity /// Returns `Err(FailedTo::LoadFromDB)` if there is an issue loading the entity
/// from the database. /// 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); let path = path::Path::new(&self.path);
self.on_items(|items| { self.on_items(|items| {
let entity = sqlite::load::by_id(path, id).map_err(|_| FailedTo::LoadFromDB)?; let entity = sqlite::load::by_id(path, id).map_err(|_| FailedTo::LoadFromDB)?;

View File

@@ -25,7 +25,7 @@ mod tests {
let _ = catalog1.upsert(item_to_insert.clone()); let _ = catalog1.upsert(item_to_insert.clone());
catalog1.persist().unwrap(); catalog1.persist().unwrap();
// 2. create a new catalog instance -> 'load_by_id' -> 'get' // 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(); catalog2.load_by_id("item-1").unwrap();
let loaded_item: Option<Item> = catalog2.get("item-1").unwrap(); let loaded_item: Option<Item> = catalog2.get("item-1").unwrap();
// 3. verify data integrity // 3. verify data integrity
@@ -109,14 +109,14 @@ mod tests {
let _ = catalog1.upsert(item_to_delete.clone()); let _ = catalog1.upsert(item_to_delete.clone());
catalog1.persist().unwrap(); catalog1.persist().unwrap();
// 2. 'load_by_id' to confirm it's there // 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(); catalog2.load_by_id("item-to-delete").unwrap();
assert!(catalog2.get::<Item>("item-to-delete").unwrap().is_some()); assert!(catalog2.get::<Item>("item-to-delete").unwrap().is_some());
// 3. 'delete' -> 'persist' // 3. 'delete' -> 'persist'
catalog2.delete("item-to-delete"); catalog2.delete("item-to-delete");
catalog2.persist().unwrap(); catalog2.persist().unwrap();
// 4. 'load_by_id' should now return nothing // 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(); catalog3.load_by_id("item-to-delete").unwrap();
let loaded_item: Option<Item> = catalog3.get("item-to-delete").unwrap(); let loaded_item: Option<Item> = catalog3.get("item-to-delete").unwrap();
assert!(loaded_item.is_none()); assert!(loaded_item.is_none());
@@ -152,7 +152,7 @@ mod tests {
// 2. Thread 1: Loads, updates name, and persists. // 2. Thread 1: Loads, updates name, and persists.
let db_path_arc1 = std::sync::Arc::clone(&db_path_arc); let db_path_arc1 = std::sync::Arc::clone(&db_path_arc);
let handle1 = std::thread::spawn(move || { 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(); catalog1.load_by_id("item-1").unwrap();
let mut item = catalog1.get::<Item>("item-1").unwrap().unwrap(); let mut item = catalog1.get::<Item>("item-1").unwrap().unwrap();
item.name = "Updated by Thread 1".to_string(); item.name = "Updated by Thread 1".to_string();
@@ -162,7 +162,7 @@ mod tests {
// 3. Thread 2: Loads, updates price, and persists. // 3. Thread 2: Loads, updates price, and persists.
let db_path_arc2 = std::sync::Arc::clone(&db_path_arc); let db_path_arc2 = std::sync::Arc::clone(&db_path_arc);
let handle2 = std::thread::spawn(move || { 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(); catalog2.load_by_id("item-1").unwrap();
let mut item = catalog2.get::<Item>("item-1").unwrap().unwrap(); let mut item = catalog2.get::<Item>("item-1").unwrap().unwrap();
item.price = 200; item.price = 200;
@@ -172,7 +172,7 @@ mod tests {
handle1.join().unwrap(); handle1.join().unwrap();
handle2.join().unwrap(); handle2.join().unwrap();
// 4. Verification: Load the data and check the final state. // 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(); catalog_verify.load_by_id("item-1").unwrap();
let final_item: Item = catalog_verify.get("item-1").unwrap().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. // The final state depends on which thread persisted last. One update will have been lost.

View File

@@ -25,7 +25,7 @@ mod tests {
let _ = catalog1.upsert(item_to_persist.clone()); let _ = catalog1.upsert(item_to_persist.clone());
catalog1.persist().unwrap(); catalog1.persist().unwrap();
// 2. Create a new, empty catalog instance for the same DB. // 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()); assert!(catalog2.is_empty().unwrap());
// 3. Load the item by its ID. // 3. Load the item by its ID.
let result = catalog2.load_by_id("item-1"); let result = catalog2.load_by_id("item-1");
@@ -80,7 +80,7 @@ mod tests {
let _ = catalog1.upsert(item_in_db.clone()); let _ = catalog1.upsert(item_in_db.clone());
catalog1.persist().unwrap(); catalog1.persist().unwrap();
// 2. Create a new catalog and add a *different* in-memory version of the same item. // 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 { let item_in_memory = Item {
id: "item-1".to_string(), id: "item-1".to_string(),
subclass: Some("subitem".to_string()), subclass: Some("subitem".to_string()),
@@ -135,7 +135,7 @@ mod tests {
std::fs::remove_file(path).unwrap(); std::fs::remove_file(path).unwrap();
} }
// 1. Create an empty, initialized database. // 1. Create an empty, initialized database.
let mut catalog = Catalog::new(db_path); let catalog = Catalog::new(db_path);
catalog.init().unwrap(); catalog.init().unwrap();
// 2. Attempt to load an ID that does not exist. // 2. Attempt to load an ID that does not exist.
let result = catalog.load_by_id("nonexistent-id"); let result = catalog.load_by_id("nonexistent-id");
@@ -151,7 +151,7 @@ mod tests {
// Using a directory as a path should cause a failure. // Using a directory as a path should cause a failure.
let invalid_path = "target/test_dbs/a_directory_for_load_fail"; let invalid_path = "target/test_dbs/a_directory_for_load_fail";
std::fs::create_dir_all(invalid_path).unwrap(); 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. // Attempt to load from the invalid path.
let result = catalog.load_by_id("any-id"); let result = catalog.load_by_id("any-id");
assert!(result.is_err()); assert!(result.is_err());

View File

@@ -25,7 +25,7 @@ mod tests {
let _ = catalog1.upsert(item1.clone()); let _ = catalog1.upsert(item1.clone());
assert!(catalog1.persist().is_ok()); assert!(catalog1.persist().is_ok());
// 2. Create a new catalog and load the item to verify it was persisted // 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()); assert!(catalog2.load_by_id("item-1").is_ok());
// 3. Get the item and assert it's the same as the one we inserted // 3. Get the item and assert it's the same as the one we inserted
let loaded_item: Option<Item> = catalog2.get("item-1").unwrap(); let loaded_item: Option<Item> = catalog2.get("item-1").unwrap();
@@ -59,7 +59,7 @@ mod tests {
catalog1.delete(&item1.id); catalog1.delete(&item1.id);
assert!(catalog1.persist().is_ok()); assert!(catalog1.persist().is_ok());
// 3. Create a new catalog and try to load the deleted item. // 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()); assert!(catalog2.load_by_id(&item1.id).is_ok());
// 4. Assert that the item was not found. // 4. Assert that the item was not found.
let loaded_item: Option<Item> = catalog2.get(&item1.id).unwrap(); let loaded_item: Option<Item> = catalog2.get(&item1.id).unwrap();
@@ -91,7 +91,7 @@ mod tests {
let _ = catalog1.upsert(original_item.clone()); let _ = catalog1.upsert(original_item.clone());
catalog1.persist().unwrap(); catalog1.persist().unwrap();
// 2. Load it into a new catalog to simulate a separate session. // 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(); catalog2.load_by_id("item-1").unwrap();
// 3. Upsert updated data for the same item. This should mark it as 'Updated'. // 3. Upsert updated data for the same item. This should mark it as 'Updated'.
let updated_item = Item { let updated_item = Item {
@@ -113,7 +113,7 @@ mod tests {
// 4. Persist the changes. // 4. Persist the changes.
catalog2.persist().unwrap(); catalog2.persist().unwrap();
// 5. Load the data into a third catalog to verify the update was written to the DB. // 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(); catalog3.load_by_id("item-1").unwrap();
let loaded_item: Item = catalog3.get("item-1").unwrap().unwrap(); let loaded_item: Item = catalog3.get("item-1").unwrap().unwrap();
// 6. Assert that the loaded item has the updated values. // 6. Assert that the loaded item has the updated values.