fix: change mutability to ref while loading with filter

This commit is contained in:
2026-02-06 09:50:07 +01:00
parent 5215a3a724
commit 5db868b192
4 changed files with 70 additions and 70 deletions

View File

@@ -120,7 +120,7 @@ fn main() {
// Persist the changes to the database.
catalog.persist().unwrap();
// Create a new catalog to ensure we are loading from the database.
let mut new_catalog = Catalog::new(db_path);
let new_catalog = Catalog::new(db_path);
// Create a composite filter.
// We are looking for resistors with a value greater than 1000 that are in stock.
let filter = Filter::new()

View File

@@ -134,7 +134,7 @@ fn main() -> Result<(), FailedTo> {
println!("== 2. Loading products using class and subclass filters ==");
// Load only laptops
let mut laptop_catalog = Catalog::new(db_path);
let laptop_catalog = Catalog::new(db_path);
let laptop_filter = Filter::new()
.with_class(Product::class())
.with_subclass("laptop");
@@ -162,7 +162,7 @@ fn main() -> Result<(), FailedTo> {
println!("== 3. Loading products using attribute filters ==");
// Load expensive products (price > 1000)
let mut expensive_catalog = Catalog::new(db_path);
let expensive_catalog = Catalog::new(db_path);
let expensive_filter = Filter::new()
.with_class(Product::class())
.with_unsigned_int("price", Comparison::Greater, 1000);
@@ -197,7 +197,7 @@ fn main() -> Result<(), FailedTo> {
println!();
println!("== 4. Loading all product types ==");
let mut all_products_catalog = Catalog::new(db_path);
let all_products_catalog = Catalog::new(db_path);
let all_products_filter = Filter::new().with_class(Product::class());
all_products_catalog.load_by_filter(&all_products_filter)?;

View File

@@ -28,7 +28,7 @@ impl Catalog {
///
/// Returns `Err(FailedTo::LoadFromDB)` if there is an issue loading entities
/// from the database based on the provided filter.
pub fn load_by_filter(&mut self, filter: &Filter) -> Result<(), FailedTo> {
pub fn load_by_filter(&self, filter: &Filter) -> Result<(), FailedTo> {
let path = path::Path::new(&self.path);
self.on_items(|items| {
let entities =

View File

@@ -40,7 +40,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_bool("in_stock", true);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
@@ -79,7 +79,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_bool("in_stock", false);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 1);
@@ -118,7 +118,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new();
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
@@ -146,7 +146,7 @@ mod tests {
};
catalog_setup.upsert(item_in_db.clone()).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let item_in_memory = Item {
id: "item-1".to_string(),
subclass: Some("subitem".to_string()),
@@ -168,7 +168,7 @@ mod tests {
// Confirms that the function returns an appropriate error if the database operation fails.
let invalid_path = "target/test_dbs/a_directory_for_lbf_fail";
std::fs::create_dir_all(invalid_path).unwrap();
let mut catalog = Catalog::new(invalid_path);
let catalog = Catalog::new(invalid_path);
let filter = Filter::new();
let result = catalog.load_by_filter(&filter);
assert!(result.is_err());
@@ -213,7 +213,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_signed_int("sell_trend", Comparison::Equal, 10);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
@@ -260,7 +260,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_signed_int("sell_trend", Comparison::Equal, -5);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 1);
@@ -307,7 +307,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_signed_int("sell_trend", Comparison::Greater, 5);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
@@ -355,18 +355,18 @@ mod tests {
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
// Edge Case 1: Value equal to filter value should not be included.
let mut catalog1 = Catalog::new(db_path);
let catalog1 = Catalog::new(db_path);
let filter1 = Filter::new().with_signed_int("sell_trend", Comparison::Greater, 10);
assert!(catalog1.load_by_filter(&filter1).is_ok());
assert_eq!(catalog1.len().unwrap(), 1);
assert!(catalog1.contains_key("item-3").unwrap());
// Edge Case 2: No values greater than filter value.
let mut catalog2 = Catalog::new(db_path);
let catalog2 = Catalog::new(db_path);
let filter2 = Filter::new().with_signed_int("sell_trend", Comparison::Greater, 20);
assert!(catalog2.load_by_filter(&filter2).is_ok());
assert!(catalog2.is_empty().unwrap());
// Edge Case 3: All values greater than filter value.
let mut catalog3 = Catalog::new(db_path);
let catalog3 = Catalog::new(db_path);
let filter3 = Filter::new().with_signed_int("sell_trend", Comparison::Greater, -10);
assert!(catalog3.load_by_filter(&filter3).is_ok());
assert_eq!(catalog3.len().unwrap(), 3);
@@ -413,7 +413,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_signed_int("sell_trend", Comparison::Lesser, 15);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
@@ -461,18 +461,18 @@ mod tests {
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
// Edge Case 1: Value equal to filter value should not be included.
let mut catalog1 = Catalog::new(db_path);
let catalog1 = Catalog::new(db_path);
let filter1 = Filter::new().with_signed_int("sell_trend", Comparison::Lesser, 10);
assert!(catalog1.load_by_filter(&filter1).is_ok());
assert_eq!(catalog1.len().unwrap(), 1);
assert!(catalog1.contains_key("item-2").unwrap());
// Edge Case 2: No values lesser than filter value.
let mut catalog2 = Catalog::new(db_path);
let catalog2 = Catalog::new(db_path);
let filter2 = Filter::new().with_signed_int("sell_trend", Comparison::Lesser, -5);
assert!(catalog2.load_by_filter(&filter2).is_ok());
assert!(catalog2.is_empty().unwrap());
// Edge Case 3: All values lesser than filter value.
let mut catalog3 = Catalog::new(db_path);
let catalog3 = Catalog::new(db_path);
let filter3 = Filter::new().with_signed_int("sell_trend", Comparison::Lesser, 25);
assert!(catalog3.load_by_filter(&filter3).is_ok());
assert_eq!(catalog3.len().unwrap(), 3);
@@ -519,7 +519,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_signed_int("sell_trend", Comparison::GreaterOrEqual, 10);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
@@ -567,18 +567,18 @@ mod tests {
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
// Edge Case 1: Value equal to filter value should be included.
let mut catalog1 = Catalog::new(db_path);
let catalog1 = Catalog::new(db_path);
let filter1 = Filter::new().with_signed_int("sell_trend", Comparison::GreaterOrEqual, 20);
assert!(catalog1.load_by_filter(&filter1).is_ok());
assert_eq!(catalog1.len().unwrap(), 1);
assert!(catalog1.contains_key("item-3").unwrap());
// Edge Case 2: No values greater than or equal to filter value.
let mut catalog2 = Catalog::new(db_path);
let catalog2 = Catalog::new(db_path);
let filter2 = Filter::new().with_signed_int("sell_trend", Comparison::GreaterOrEqual, 21);
assert!(catalog2.load_by_filter(&filter2).is_ok());
assert!(catalog2.is_empty().unwrap());
// Edge Case 3: All values greater than or equal to filter value.
let mut catalog3 = Catalog::new(db_path);
let catalog3 = Catalog::new(db_path);
let filter3 = Filter::new().with_signed_int("sell_trend", Comparison::GreaterOrEqual, -5);
assert!(catalog3.load_by_filter(&filter3).is_ok());
assert_eq!(catalog3.len().unwrap(), 3);
@@ -625,7 +625,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_signed_int("sell_trend", Comparison::LesserOrEqual, 10);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
@@ -673,18 +673,18 @@ mod tests {
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
// Edge Case 1: Value equal to filter value should be included.
let mut catalog1 = Catalog::new(db_path);
let catalog1 = Catalog::new(db_path);
let filter1 = Filter::new().with_signed_int("sell_trend", Comparison::LesserOrEqual, -5);
assert!(catalog1.load_by_filter(&filter1).is_ok());
assert_eq!(catalog1.len().unwrap(), 1);
assert!(catalog1.contains_key("item-2").unwrap());
// Edge Case 2: No values lesser than or equal to filter value.
let mut catalog2 = Catalog::new(db_path);
let catalog2 = Catalog::new(db_path);
let filter2 = Filter::new().with_signed_int("sell_trend", Comparison::LesserOrEqual, -6);
assert!(catalog2.load_by_filter(&filter2).is_ok());
assert!(catalog2.is_empty().unwrap());
// Edge Case 3: All values lesser than or equal to filter value.
let mut catalog3 = Catalog::new(db_path);
let catalog3 = Catalog::new(db_path);
let filter3 = Filter::new().with_signed_int("sell_trend", Comparison::LesserOrEqual, 20);
assert!(catalog3.load_by_filter(&filter3).is_ok());
assert_eq!(catalog3.len().unwrap(), 3);
@@ -732,44 +732,44 @@ mod tests {
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
// Equal
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_unsigned_int("price", Comparison::Equal, 200);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 1);
assert!(catalog.contains_key("item-2").unwrap());
// Greater
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_unsigned_int("price", Comparison::Greater, 200);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 1);
assert!(catalog.contains_key("item-3").unwrap());
// Lesser
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_unsigned_int("price", Comparison::Lesser, 200);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 1);
assert!(catalog.contains_key("item-1").unwrap());
// GreaterOrEqual
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_unsigned_int("price", Comparison::GreaterOrEqual, 200);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
assert!(catalog.contains_key("item-2").unwrap());
assert!(catalog.contains_key("item-3").unwrap());
// LesserOrEqual
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_unsigned_int("price", Comparison::LesserOrEqual, 200);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
assert!(catalog.contains_key("item-1").unwrap());
assert!(catalog.contains_key("item-2").unwrap());
// Edge case: No match
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_unsigned_int("price", Comparison::Equal, 400);
assert!(catalog.load_by_filter(&filter).is_ok());
assert!(catalog.is_empty().unwrap());
// Edge case: All match
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_unsigned_int("price", Comparison::GreaterOrEqual, 100);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 3);
@@ -814,24 +814,24 @@ mod tests {
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
// Exact match
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::IsExactly, "Item One");
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 1);
assert!(catalog.contains_key("item-1").unwrap());
// Partial match should not work
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::IsExactly, "Item");
assert!(catalog.load_by_filter(&filter).is_ok());
assert!(catalog.is_empty().unwrap());
// Case insensitive match
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::IsExactly, "item one");
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 1);
assert!(catalog.contains_key("item-1").unwrap());
// No match
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::IsExactly, "Item Four");
assert!(catalog.load_by_filter(&filter).is_ok());
assert!(catalog.is_empty().unwrap());
@@ -876,33 +876,33 @@ mod tests {
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
// Starts with "Item"
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::StartsWith, "Item");
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
assert!(catalog.contains_key("item-1").unwrap());
assert!(catalog.contains_key("item-2").unwrap());
// Starts with "I"
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::StartsWith, "I");
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
assert!(catalog.contains_key("item-1").unwrap());
assert!(catalog.contains_key("item-2").unwrap());
// No match
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::StartsWith, "Z");
assert!(catalog.load_by_filter(&filter).is_ok());
assert!(catalog.is_empty().unwrap());
// Case insensitive
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::StartsWith, "item");
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
assert!(catalog.contains_key("item-1").unwrap());
assert!(catalog.contains_key("item-2").unwrap());
// Full string match
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::StartsWith, "Item One");
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 1);
@@ -948,24 +948,24 @@ mod tests {
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
// Ends with "one" - case insensitive
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::EndsWith, "one");
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 1);
assert!(catalog.contains_key("item-1").unwrap());
// Ends with "item" - case insensitive
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::EndsWith, "item");
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 1);
assert!(catalog.contains_key("item-3").unwrap());
// No match
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::EndsWith, "Z");
assert!(catalog.load_by_filter(&filter).is_ok());
assert!(catalog.is_empty().unwrap());
// Full string match - case insensitive
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::EndsWith, "item one");
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 1);
@@ -1011,23 +1011,23 @@ mod tests {
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
// Contains "item" - case insensitive
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::Contains, "item");
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 3);
// Contains "THE" - case insensitive
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::Contains, "THE");
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 1);
assert!(catalog.contains_key("item-3").unwrap());
// No match
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::Contains, "Z");
assert!(catalog.load_by_filter(&filter).is_ok());
assert!(catalog.is_empty().unwrap());
// Full string match - case insensitive
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_text("name", Comparison::Contains, "item one");
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 1);
@@ -1063,7 +1063,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_real("discount", Comparison::Equal, 0.10);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
@@ -1101,7 +1101,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_real("discount", Comparison::Greater, 0.15);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
@@ -1133,12 +1133,12 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog1 = Catalog::new(db_path);
let catalog1 = Catalog::new(db_path);
let filter1 = Filter::new().with_real("discount", Comparison::Greater, 0.10);
assert!(catalog1.load_by_filter(&filter1).is_ok());
assert_eq!(catalog1.len().unwrap(), 1);
assert!(catalog1.contains_key("item-2").unwrap());
let mut catalog2 = Catalog::new(db_path);
let catalog2 = Catalog::new(db_path);
let filter2 = Filter::new().with_real("discount", Comparison::Greater, 0.20);
assert!(catalog2.load_by_filter(&filter2).is_ok());
assert!(catalog2.is_empty().unwrap());
@@ -1173,7 +1173,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_real("discount", Comparison::Lesser, 0.15);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
@@ -1205,12 +1205,12 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog1 = Catalog::new(db_path);
let catalog1 = Catalog::new(db_path);
let filter1 = Filter::new().with_real("discount", Comparison::Lesser, 0.20);
assert!(catalog1.load_by_filter(&filter1).is_ok());
assert_eq!(catalog1.len().unwrap(), 1);
assert!(catalog1.contains_key("item-1").unwrap());
let mut catalog2 = Catalog::new(db_path);
let catalog2 = Catalog::new(db_path);
let filter2 = Filter::new().with_real("discount", Comparison::Lesser, 0.10);
assert!(catalog2.load_by_filter(&filter2).is_ok());
assert!(catalog2.is_empty().unwrap());
@@ -1245,7 +1245,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_real("discount", Comparison::GreaterOrEqual, 0.15);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
@@ -1277,12 +1277,12 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog1 = Catalog::new(db_path);
let catalog1 = Catalog::new(db_path);
let filter1 = Filter::new().with_real("discount", Comparison::GreaterOrEqual, 0.20);
assert!(catalog1.load_by_filter(&filter1).is_ok());
assert_eq!(catalog1.len().unwrap(), 1);
assert!(catalog1.contains_key("item-2").unwrap());
let mut catalog2 = Catalog::new(db_path);
let catalog2 = Catalog::new(db_path);
let filter2 = Filter::new().with_real("discount", Comparison::GreaterOrEqual, 0.21);
assert!(catalog2.load_by_filter(&filter2).is_ok());
assert!(catalog2.is_empty().unwrap());
@@ -1317,7 +1317,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new().with_real("discount", Comparison::LesserOrEqual, 0.15);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.len().unwrap(), 2);
@@ -1349,12 +1349,12 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog1 = Catalog::new(db_path);
let catalog1 = Catalog::new(db_path);
let filter1 = Filter::new().with_real("discount", Comparison::LesserOrEqual, 0.10);
assert!(catalog1.load_by_filter(&filter1).is_ok());
assert_eq!(catalog1.len().unwrap(), 1);
assert!(catalog1.contains_key("item-1").unwrap());
let mut catalog2 = Catalog::new(db_path);
let catalog2 = Catalog::new(db_path);
let filter2 = Filter::new().with_real("discount", Comparison::LesserOrEqual, 0.09);
assert!(catalog2.load_by_filter(&filter2).is_ok());
assert!(catalog2.is_empty().unwrap());
@@ -1402,7 +1402,7 @@ mod tests {
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let catalog = Catalog::new(db_path);
let filter = Filter::new()
.with_unsigned_int("price", Comparison::Equal, 100)
.with_real("discount", Comparison::Equal, 0.10)
@@ -1475,7 +1475,7 @@ mod tests {
catalog_setup.upsert(another_item).unwrap();
catalog_setup.persist().unwrap();
// Test 1: Filter by class "item"
let mut catalog1 = Catalog::new(db_path);
let catalog1 = Catalog::new(db_path);
let filter1 = Filter::new().with_class("item");
assert!(catalog1.load_by_filter(&filter1).is_ok());
assert_eq!(catalog1.len().unwrap(), 4);
@@ -1484,20 +1484,20 @@ mod tests {
assert!(catalog1.contains_key("item-3").unwrap());
assert!(catalog1.contains_key("item-4").unwrap());
// Test 2: Filter by class "another_item"
let mut catalog2 = Catalog::new(db_path);
let catalog2 = Catalog::new(db_path);
let filter2 = Filter::new().with_class("another_item");
assert!(catalog2.load_by_filter(&filter2).is_ok());
assert_eq!(catalog2.len().unwrap(), 1);
assert!(catalog2.contains_key("another-1").unwrap());
// Test 3: Filter by class "item" and subclass "sub-a"
let mut catalog3 = Catalog::new(db_path);
let catalog3 = Catalog::new(db_path);
let filter3 = Filter::new().with_class("item").with_subclass("sub-a");
assert!(catalog3.load_by_filter(&filter3).is_ok());
assert_eq!(catalog3.len().unwrap(), 2);
assert!(catalog3.contains_key("item-1").unwrap());
assert!(catalog3.contains_key("item-3").unwrap());
// Test 4: Filter by class "item" and subclass "subitem" (the default)
let mut catalog4 = Catalog::new(db_path);
let catalog4 = Catalog::new(db_path);
let filter4 = Filter::new().with_class("item").with_subclass("subitem");
assert!(catalog4.load_by_filter(&filter4).is_ok());
assert_eq!(catalog4.len().unwrap(), 1);