fix: change mutability to ref while loading and inserting many

This commit is contained in:
2026-02-06 09:24:57 +01:00
parent edc9d64744
commit 5215a3a724
11 changed files with 45 additions and 45 deletions

View File

@@ -55,7 +55,7 @@ fn main() {
// Define the path for the SQLite database file.
let db_path = "./using_filters.sqlite3";
// Create a new `Catalog` instance with the specified database path.
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
// Initialize the catalog, which sets up the database.
catalog.init().unwrap();
// Create some component instances.

View File

@@ -98,7 +98,7 @@ fn main() -> Result<(), FailedTo> {
// 1. Initialize and Persist Data
println!("== 1. Storing different product types ==");
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
catalog.init()?;
let products_to_add = vec![

View File

@@ -19,7 +19,7 @@ impl Catalog {
///
/// Returns `Err(FailedTo)` if any of the underlying `upsert` operations fail.
/// This could be due to issues like an object not having a valid ID.
pub fn insert_many(&mut self, objects: Vec<impl EAV>) -> Result<(), FailedTo> {
pub fn insert_many(&self, objects: Vec<impl EAV>) -> Result<(), FailedTo> {
for object in objects {
self.upsert(object)?;
}

View File

@@ -23,7 +23,7 @@ impl Catalog {
///
/// Returns `Err(FailedTo::LoadFromDB)` if there is an issue loading entities
/// from the database.
pub fn load<T>(&mut self) -> Result<(), FailedTo>
pub fn load<T>(&self) -> Result<(), FailedTo>
where
T: EAV,
{

View File

@@ -11,7 +11,7 @@ mod tests {
}
#[test]
fn get_by_finds_item() {
let mut catalog = catalog("get_by_finds_item");
let catalog = catalog("get_by_finds_item");
let item1 = Item {
id: "1".to_string(),
name: "one".to_string(),
@@ -33,7 +33,7 @@ mod tests {
}
#[test]
fn get_by_returns_none_when_no_match() {
let mut catalog = catalog("get_by_returns_none_when_no_match");
let catalog = catalog("get_by_returns_none_when_no_match");
let item1 = Item {
id: "1".to_string(),
name: "one".to_string(),
@@ -51,7 +51,7 @@ mod tests {
}
#[test]
fn get_by_multiple_matches() {
let mut catalog = catalog("get_by_multiple_matches");
let catalog = catalog("get_by_multiple_matches");
let item1 = Item {
id: "1".to_string(),
name: "match".to_string(),

View File

@@ -4,7 +4,7 @@ mod tests {
#[test]
fn insert_many_should_add_all_entities() {
// 'insert_many()': Should add all provided entities to the 'items' map.
let mut catalog = Catalog::new("dummy.db");
let catalog = Catalog::new("dummy.db");
let items = vec![
Item {
id: "item-1".to_string(),

View File

@@ -43,7 +43,7 @@ mod tests {
std::fs::remove_file(path).unwrap();
}
// 1. 'init' -> 'insert_many' -> 'persist'
let mut catalog1 = Catalog::new(db_path);
let catalog1 = Catalog::new(db_path);
catalog1.init().unwrap();
let items_to_insert = vec![
Item {
@@ -68,7 +68,7 @@ mod tests {
let _ = catalog1.insert_many(items_to_insert.clone());
catalog1.persist().unwrap();
// 2. new catalog -> 'load_by_class' -> 'list_by_class'
let mut catalog2 = Catalog::new(db_path);
let catalog2 = Catalog::new(db_path);
catalog2.load::<Item>().unwrap();
let mut loaded_items: Vec<Item> = catalog2
.list::<Item>()

View File

@@ -35,7 +35,7 @@ mod tests {
let _ = catalog1.upsert(item2.clone());
catalog1.persist().unwrap();
// 2. Create a new catalog and load the items by class
let mut catalog2 = Catalog::new(db_path);
let catalog2 = Catalog::new(db_path);
let result = catalog2.load::<Item>();
assert!(result.is_ok());
// 3. Verify that all items of that class were loaded
@@ -84,7 +84,7 @@ mod tests {
let _ = catalog1.upsert(item_in_db.clone());
catalog1.persist().unwrap();
// 2. Create a new catalog with 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()),
@@ -128,7 +128,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 from the empty DB.
let result = catalog.load::<Item>();
@@ -164,7 +164,7 @@ mod tests {
// Using a directory as a path should cause a failure.
let invalid_path = "target/test_dbs/a_directory_for_load_class_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::<Item>();
assert!(result.is_err());

View File

@@ -10,7 +10,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -57,7 +57,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -96,7 +96,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -183,7 +183,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -230,7 +230,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -277,7 +277,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -324,7 +324,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -383,7 +383,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -430,7 +430,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -489,7 +489,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -536,7 +536,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -595,7 +595,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -642,7 +642,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -701,7 +701,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -783,7 +783,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -845,7 +845,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -917,7 +917,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -980,7 +980,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -1042,7 +1042,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -1080,7 +1080,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -1117,7 +1117,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -1152,7 +1152,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -1189,7 +1189,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -1224,7 +1224,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -1261,7 +1261,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -1296,7 +1296,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -1333,7 +1333,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
@@ -1368,7 +1368,7 @@ mod tests {
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
let catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {

View File

@@ -166,7 +166,7 @@ mod tests {
let _ = catalog_setup.upsert(item_to_keep.clone());
catalog_setup.persist().unwrap();
// 2. Manipulation: Load the data and perform mixed operations.
let mut catalog_ops = Catalog::new(db_path);
let catalog_ops = Catalog::new(db_path);
catalog_ops.load::<Item>().unwrap(); // Load all items
// A new item to be inserted.
let item_to_add = Item {
@@ -196,7 +196,7 @@ mod tests {
// 3. Execution: Persist all the changes in one go.
catalog_ops.persist().unwrap();
// 4. Verification: Load into a new catalog and check the final state of the DB.
let mut catalog_verify = Catalog::new(db_path);
let catalog_verify = Catalog::new(db_path);
catalog_verify.load::<Item>().unwrap();
// Check total count
assert_eq!(catalog_verify.len().unwrap(), 3);

View File

@@ -39,7 +39,7 @@ mod tests {
let total_items = catalog.with_items(|items| Ok(items.len())).unwrap();
assert_eq!(total_items, 1000);
catalog.persist().unwrap();
let mut new_catalog = Catalog::new(db_path);
let new_catalog = Catalog::new(db_path);
new_catalog.load::<Item>().unwrap();
let total_items_after_load = new_catalog.with_items(|items| Ok(items.len())).unwrap();
assert_eq!(total_items_after_load, 1000);