chore: run cargo format and delete empty lines in catalog.rs

This commit is contained in:
2025-10-19 10:27:41 +02:00
parent 45093a5672
commit 01a6e2ce48

View File

@@ -26,14 +26,12 @@ impl Catalog {
..O::default() ..O::default()
} }
} }
/// Initializes the database. /// Initializes the database.
pub fn init(&self) -> result::Result<(), FailedTo> { pub fn init(&self) -> result::Result<(), FailedTo> {
let path = path::Path::new(&self.path); let path = path::Path::new(&self.path);
sqlite::init::db(path).map_err(|_| FailedTo::InitDatabase)?; sqlite::init::db(path).map_err(|_| FailedTo::InitDatabase)?;
Ok(()) Ok(())
} }
/// Inserts or updates a single object that implements the `EAV` trait into the catalog. /// Inserts or updates a single object that implements the `EAV` trait into the catalog.
/// ///
/// # Arguments /// # Arguments
@@ -49,7 +47,6 @@ impl Catalog {
self.items.insert(entity.id.clone(), entity); self.items.insert(entity.id.clone(), entity);
Ok(()) Ok(())
} }
/// Inserts multiple objects that implement the `EAV` trait into the catalog. /// Inserts multiple objects that implement the `EAV` trait into the catalog.
/// ///
/// # Arguments /// # Arguments
@@ -61,7 +58,6 @@ impl Catalog {
} }
Ok(()) Ok(())
} }
/// Retrieves an entity by its ID and converts it into a specified type `T`. /// Retrieves an entity by its ID and converts it into a specified type `T`.
/// ///
/// # Arguments /// # Arguments
@@ -80,7 +76,6 @@ impl Catalog {
.map(|e| T::try_from(e.clone()).map_err(|_| FailedTo::ConvertEntity)) .map(|e| T::try_from(e.clone()).map_err(|_| FailedTo::ConvertEntity))
.transpose() .transpose()
} }
/// Retrieves the first entity that matches a given attribute and value. /// Retrieves the first entity that matches a given attribute and value.
/// ///
/// # Arguments /// # Arguments
@@ -108,7 +103,6 @@ impl Catalog {
.map(|item| T::try_from(item.clone()).map_err(|_| FailedTo::ConvertEntity)); .map(|item| T::try_from(item.clone()).map_err(|_| FailedTo::ConvertEntity));
items.next().transpose() items.next().transpose()
} }
/// Returns an iterator over entities of a specific class. /// Returns an iterator over entities of a specific class.
/// ///
/// # Returns /// # Returns
@@ -123,7 +117,6 @@ impl Catalog {
.filter(move |item| item.class == T::class()) .filter(move |item| item.class == T::class())
.map(|item| T::try_from(item.clone()).map_err(|_| FailedTo::ConvertEntity)) .map(|item| T::try_from(item.clone()).map_err(|_| FailedTo::ConvertEntity))
} }
/// Returns an iterator over entities that match a given attribute and value. /// Returns an iterator over entities that match a given attribute and value.
/// ///
/// # Arguments /// # Arguments
@@ -149,7 +142,6 @@ impl Catalog {
.filter(move |item| item.value_of(attribute) == Some(&value)) .filter(move |item| item.value_of(attribute) == Some(&value))
.map(|item| T::try_from(item.clone()).map_err(|_| FailedTo::ConvertEntity)) .map(|item| T::try_from(item.clone()).map_err(|_| FailedTo::ConvertEntity))
} }
/// Schedules an entity for deletion. Actual delition will take place when 'persist' is called. /// Schedules an entity for deletion. Actual delition will take place when 'persist' is called.
/// ///
/// # Arguments /// # Arguments
@@ -161,7 +153,6 @@ impl Catalog {
entity.state = EntityState::ToDelete; entity.state = EntityState::ToDelete;
} }
} }
/// Persists the current state of the catalog to the database. /// Persists the current state of the catalog to the database.
/// ///
/// - new entities will be written onto DB /// - new entities will be written onto DB
@@ -185,7 +176,6 @@ impl Catalog {
.collect(); .collect();
Ok(()) Ok(())
} }
/// Loads an entity by its ID from the database into the catalog. /// Loads an entity by its ID from the database into the catalog.
/// ///
/// # Arguments /// # Arguments
@@ -199,7 +189,6 @@ impl Catalog {
} }
Ok(()) Ok(())
} }
/// Loads all entities of a specific class from the database into the catalog. /// Loads all entities of a specific class from the database into the catalog.
pub fn load_by_class<T>(&mut self) -> Result<(), FailedTo> pub fn load_by_class<T>(&mut self) -> Result<(), FailedTo>
where where
@@ -213,7 +202,6 @@ impl Catalog {
} }
Ok(()) Ok(())
} }
/// Loads all entities matching the filter values /// Loads all entities matching the filter values
/// Different entity class with clashing attribute names might be loaded /// Different entity class with clashing attribute names might be loaded
pub fn load_by_filter(&mut self, filter: &Filter) -> Result<(), FailedTo> { pub fn load_by_filter(&mut self, filter: &Filter) -> Result<(), FailedTo> {
@@ -1706,290 +1694,290 @@ mod tests {
assert!(!catalog.items.contains_key("item-3")); assert!(!catalog.items.contains_key("item-3"));
std::fs::remove_file(path).unwrap(); std::fs::remove_file(path).unwrap();
} }
#[test] #[test]
fn load_by_filter_should_load_matching_lesser_sell_trend_edge_cases() { fn load_by_filter_should_load_matching_lesser_sell_trend_edge_cases() {
let db_path = "target/test_dbs/lbf_lesser_sell_trend_edge_cases.db"; let db_path = "target/test_dbs/lbf_lesser_sell_trend_edge_cases.db";
let path = std::path::Path::new(db_path); let path = std::path::Path::new(db_path);
std::fs::create_dir_all(path.parent().unwrap()).unwrap(); std::fs::create_dir_all(path.parent().unwrap()).unwrap();
if path.exists() { if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
id: "item-1".to_string(),
name: "Item One".to_string(),
price: 100,
sell_trend: 10,
in_stock: true,
},
Item {
id: "item-2".to_string(),
name: "Item Two".to_string(),
price: 200,
sell_trend: -5,
in_stock: false,
},
Item {
id: "item-3".to_string(),
name: "Item Three".to_string(),
price: 300,
sell_trend: 20,
in_stock: true,
},
];
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 filter1 = Filter::new().with_signed_int("sell_trend", Comparison::Lesser, 10);
assert!(catalog1.load_by_filter(&filter1).is_ok());
assert_eq!(catalog1.items.len(), 1);
assert!(catalog1.items.contains_key("item-2"));
// Edge Case 2: No values lesser than filter value.
let mut 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.items.is_empty());
// Edge Case 3: All values lesser than filter value.
let mut 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.items.len(), 3);
assert!(catalog3.items.contains_key("item-1"));
assert!(catalog3.items.contains_key("item-2"));
assert!(catalog3.items.contains_key("item-3"));
std::fs::remove_file(path).unwrap(); std::fs::remove_file(path).unwrap();
} }
#[test] let mut catalog_setup = Catalog::new(db_path);
fn load_by_filter_should_load_matching_greater_or_equal_sell_trend() { catalog_setup.init().unwrap();
let db_path = "target/test_dbs/lbf_matching_greater_or_equal_sell_trend.db"; let items = vec![
let path = std::path::Path::new(db_path); Item {
std::fs::create_dir_all(path.parent().unwrap()).unwrap(); id: "item-1".to_string(),
if path.exists() { name: "Item One".to_string(),
std::fs::remove_file(path).unwrap(); price: 100,
} sell_trend: 10,
in_stock: true,
let mut catalog_setup = Catalog::new(db_path); },
catalog_setup.init().unwrap(); Item {
let items = vec![ id: "item-2".to_string(),
Item { name: "Item Two".to_string(),
id: "item-1".to_string(), price: 200,
name: "Item One".to_string(), sell_trend: -5,
price: 100, in_stock: false,
sell_trend: 10, },
in_stock: true, Item {
}, id: "item-3".to_string(),
Item { name: "Item Three".to_string(),
id: "item-2".to_string(), price: 300,
name: "Item Two".to_string(), sell_trend: 20,
price: 200, in_stock: true,
sell_trend: -5, },
in_stock: false, ];
}, catalog_setup.insert_many(items).unwrap();
Item { catalog_setup.persist().unwrap();
id: "item-3".to_string(),
name: "Item Three".to_string(), // Edge Case 1: Value equal to filter value should not be included.
price: 300, let mut catalog1 = Catalog::new(db_path);
sell_trend: 20, let filter1 = Filter::new().with_signed_int("sell_trend", Comparison::Lesser, 10);
in_stock: true, assert!(catalog1.load_by_filter(&filter1).is_ok());
}, assert_eq!(catalog1.items.len(), 1);
]; assert!(catalog1.items.contains_key("item-2"));
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap(); // Edge Case 2: No values lesser than filter value.
let mut catalog2 = Catalog::new(db_path);
let mut catalog = Catalog::new(db_path); let filter2 = Filter::new().with_signed_int("sell_trend", Comparison::Lesser, -5);
let filter = Filter::new().with_signed_int("sell_trend", Comparison::GreaterOrEqual, 10); assert!(catalog2.load_by_filter(&filter2).is_ok());
assert!(catalog.load_by_filter(&filter).is_ok()); assert!(catalog2.items.is_empty());
assert_eq!(catalog.items.len(), 2); // Edge Case 3: All values lesser than filter value.
assert!(catalog.items.contains_key("item-1")); let mut catalog3 = Catalog::new(db_path);
assert!(!catalog.items.contains_key("item-2")); let filter3 = Filter::new().with_signed_int("sell_trend", Comparison::Lesser, 25);
assert!(catalog.items.contains_key("item-3")); assert!(catalog3.load_by_filter(&filter3).is_ok());
assert_eq!(catalog3.items.len(), 3);
assert!(catalog3.items.contains_key("item-1"));
assert!(catalog3.items.contains_key("item-2"));
assert!(catalog3.items.contains_key("item-3"));
std::fs::remove_file(path).unwrap();
}
#[test]
fn load_by_filter_should_load_matching_greater_or_equal_sell_trend() {
let db_path = "target/test_dbs/lbf_matching_greater_or_equal_sell_trend.db";
let path = std::path::Path::new(db_path);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
if path.exists() {
std::fs::remove_file(path).unwrap(); std::fs::remove_file(path).unwrap();
} }
#[test] let mut catalog_setup = Catalog::new(db_path);
fn load_by_filter_should_load_matching_greater_or_equal_sell_trend_edge_cases() { catalog_setup.init().unwrap();
let db_path = "target/test_dbs/lbf_greater_or_equal_sell_trend_edge_cases.db"; let items = vec![
let path = std::path::Path::new(db_path); Item {
std::fs::create_dir_all(path.parent().unwrap()).unwrap(); id: "item-1".to_string(),
if path.exists() { name: "Item One".to_string(),
std::fs::remove_file(path).unwrap(); price: 100,
} sell_trend: 10,
in_stock: true,
let mut catalog_setup = Catalog::new(db_path); },
catalog_setup.init().unwrap(); Item {
let items = vec![ id: "item-2".to_string(),
Item { name: "Item Two".to_string(),
id: "item-1".to_string(), price: 200,
name: "Item One".to_string(), sell_trend: -5,
price: 100, in_stock: false,
sell_trend: 10, },
in_stock: true, Item {
}, id: "item-3".to_string(),
Item { name: "Item Three".to_string(),
id: "item-2".to_string(), price: 300,
name: "Item Two".to_string(), sell_trend: 20,
price: 200, in_stock: true,
sell_trend: -5, },
in_stock: false, ];
}, catalog_setup.insert_many(items).unwrap();
Item { catalog_setup.persist().unwrap();
id: "item-3".to_string(),
name: "Item Three".to_string(), let mut catalog = Catalog::new(db_path);
price: 300, let filter = Filter::new().with_signed_int("sell_trend", Comparison::GreaterOrEqual, 10);
sell_trend: 20, assert!(catalog.load_by_filter(&filter).is_ok());
in_stock: true,
}, assert_eq!(catalog.items.len(), 2);
]; assert!(catalog.items.contains_key("item-1"));
catalog_setup.insert_many(items).unwrap(); assert!(!catalog.items.contains_key("item-2"));
catalog_setup.persist().unwrap(); assert!(catalog.items.contains_key("item-3"));
// Edge Case 1: Value equal to filter value should be included. std::fs::remove_file(path).unwrap();
let mut 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()); #[test]
assert_eq!(catalog1.items.len(), 1); fn load_by_filter_should_load_matching_greater_or_equal_sell_trend_edge_cases() {
assert!(catalog1.items.contains_key("item-3")); let db_path = "target/test_dbs/lbf_greater_or_equal_sell_trend_edge_cases.db";
let path = std::path::Path::new(db_path);
// Edge Case 2: No values greater than or equal to filter value. std::fs::create_dir_all(path.parent().unwrap()).unwrap();
let mut catalog2 = Catalog::new(db_path); if path.exists() {
let filter2 = Filter::new().with_signed_int("sell_trend", Comparison::GreaterOrEqual, 21); std::fs::remove_file(path).unwrap();
assert!(catalog2.load_by_filter(&filter2).is_ok()); }
assert!(catalog2.items.is_empty());
let mut catalog_setup = Catalog::new(db_path);
// Edge Case 3: All values greater than or equal to filter value. catalog_setup.init().unwrap();
let mut catalog3 = Catalog::new(db_path); let items = vec![
let filter3 = Filter::new().with_signed_int("sell_trend", Comparison::GreaterOrEqual, -5); Item {
assert!(catalog3.load_by_filter(&filter3).is_ok()); id: "item-1".to_string(),
assert_eq!(catalog3.items.len(), 3); name: "Item One".to_string(),
assert!(catalog3.items.contains_key("item-1")); price: 100,
assert!(catalog3.items.contains_key("item-2")); sell_trend: 10,
assert!(catalog3.items.contains_key("item-3")); in_stock: true,
},
std::fs::remove_file(path).unwrap(); Item {
} id: "item-2".to_string(),
name: "Item Two".to_string(),
#[test] price: 200,
fn load_by_filter_should_load_matching_lesser_or_equal_sell_trend() { sell_trend: -5,
let db_path = "target/test_dbs/lbf_matching_lesser_or_equal_sell_trend.db"; in_stock: false,
let path = std::path::Path::new(db_path); },
std::fs::create_dir_all(path.parent().unwrap()).unwrap(); Item {
if path.exists() { id: "item-3".to_string(),
std::fs::remove_file(path).unwrap(); name: "Item Three".to_string(),
} price: 300,
sell_trend: 20,
let mut catalog_setup = Catalog::new(db_path); in_stock: true,
catalog_setup.init().unwrap(); },
let items = vec![ ];
Item { catalog_setup.insert_many(items).unwrap();
id: "item-1".to_string(), catalog_setup.persist().unwrap();
name: "Item One".to_string(),
price: 100, // Edge Case 1: Value equal to filter value should be included.
sell_trend: 10, let mut catalog1 = Catalog::new(db_path);
in_stock: true, let filter1 = Filter::new().with_signed_int("sell_trend", Comparison::GreaterOrEqual, 20);
}, assert!(catalog1.load_by_filter(&filter1).is_ok());
Item { assert_eq!(catalog1.items.len(), 1);
id: "item-2".to_string(), assert!(catalog1.items.contains_key("item-3"));
name: "Item Two".to_string(),
price: 200, // Edge Case 2: No values greater than or equal to filter value.
sell_trend: -5, let mut catalog2 = Catalog::new(db_path);
in_stock: false, let filter2 = Filter::new().with_signed_int("sell_trend", Comparison::GreaterOrEqual, 21);
}, assert!(catalog2.load_by_filter(&filter2).is_ok());
Item { assert!(catalog2.items.is_empty());
id: "item-3".to_string(),
name: "Item Three".to_string(), // Edge Case 3: All values greater than or equal to filter value.
price: 300, let mut catalog3 = Catalog::new(db_path);
sell_trend: 20, let filter3 = Filter::new().with_signed_int("sell_trend", Comparison::GreaterOrEqual, -5);
in_stock: true, assert!(catalog3.load_by_filter(&filter3).is_ok());
}, assert_eq!(catalog3.items.len(), 3);
]; assert!(catalog3.items.contains_key("item-1"));
catalog_setup.insert_many(items).unwrap(); assert!(catalog3.items.contains_key("item-2"));
catalog_setup.persist().unwrap(); assert!(catalog3.items.contains_key("item-3"));
let mut catalog = Catalog::new(db_path); std::fs::remove_file(path).unwrap();
let filter = Filter::new().with_signed_int("sell_trend", Comparison::LesserOrEqual, 10); }
assert!(catalog.load_by_filter(&filter).is_ok());
#[test]
assert_eq!(catalog.items.len(), 2); fn load_by_filter_should_load_matching_lesser_or_equal_sell_trend() {
assert!(catalog.items.contains_key("item-1")); let db_path = "target/test_dbs/lbf_matching_lesser_or_equal_sell_trend.db";
assert!(catalog.items.contains_key("item-2")); let path = std::path::Path::new(db_path);
assert!(!catalog.items.contains_key("item-3")); std::fs::create_dir_all(path.parent().unwrap()).unwrap();
if path.exists() {
std::fs::remove_file(path).unwrap(); std::fs::remove_file(path).unwrap();
} }
#[test] let mut catalog_setup = Catalog::new(db_path);
fn load_by_filter_should_load_matching_lesser_or_equal_sell_trend_edge_cases() { catalog_setup.init().unwrap();
let db_path = "target/test_dbs/lbf_lesser_or_equal_sell_trend_edge_cases.db"; let items = vec![
let path = std::path::Path::new(db_path); Item {
std::fs::create_dir_all(path.parent().unwrap()).unwrap(); id: "item-1".to_string(),
if path.exists() { name: "Item One".to_string(),
std::fs::remove_file(path).unwrap(); price: 100,
} sell_trend: 10,
in_stock: true,
let mut catalog_setup = Catalog::new(db_path); },
catalog_setup.init().unwrap(); Item {
let items = vec![ id: "item-2".to_string(),
Item { name: "Item Two".to_string(),
id: "item-1".to_string(), price: 200,
name: "Item One".to_string(), sell_trend: -5,
price: 100, in_stock: false,
sell_trend: 10, },
in_stock: true, Item {
}, id: "item-3".to_string(),
Item { name: "Item Three".to_string(),
id: "item-2".to_string(), price: 300,
name: "Item Two".to_string(), sell_trend: 20,
price: 200, in_stock: true,
sell_trend: -5, },
in_stock: false, ];
}, catalog_setup.insert_many(items).unwrap();
Item { catalog_setup.persist().unwrap();
id: "item-3".to_string(),
name: "Item Three".to_string(), let mut catalog = Catalog::new(db_path);
price: 300, let filter = Filter::new().with_signed_int("sell_trend", Comparison::LesserOrEqual, 10);
sell_trend: 20, assert!(catalog.load_by_filter(&filter).is_ok());
in_stock: true,
}, assert_eq!(catalog.items.len(), 2);
]; assert!(catalog.items.contains_key("item-1"));
catalog_setup.insert_many(items).unwrap(); assert!(catalog.items.contains_key("item-2"));
catalog_setup.persist().unwrap(); assert!(!catalog.items.contains_key("item-3"));
// Edge Case 1: Value equal to filter value should be included. std::fs::remove_file(path).unwrap();
let mut 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()); #[test]
assert_eq!(catalog1.items.len(), 1); fn load_by_filter_should_load_matching_lesser_or_equal_sell_trend_edge_cases() {
assert!(catalog1.items.contains_key("item-2")); let db_path = "target/test_dbs/lbf_lesser_or_equal_sell_trend_edge_cases.db";
let path = std::path::Path::new(db_path);
// Edge Case 2: No values lesser than or equal to filter value. std::fs::create_dir_all(path.parent().unwrap()).unwrap();
let mut catalog2 = Catalog::new(db_path); if path.exists() {
let filter2 = Filter::new().with_signed_int("sell_trend", Comparison::LesserOrEqual, -6); std::fs::remove_file(path).unwrap();
assert!(catalog2.load_by_filter(&filter2).is_ok()); }
assert!(catalog2.items.is_empty());
let mut catalog_setup = Catalog::new(db_path);
// Edge Case 3: All values lesser than or equal to filter value. catalog_setup.init().unwrap();
let mut catalog3 = Catalog::new(db_path); let items = vec![
let filter3 = Filter::new().with_signed_int("sell_trend", Comparison::LesserOrEqual, 20); Item {
assert!(catalog3.load_by_filter(&filter3).is_ok()); id: "item-1".to_string(),
assert_eq!(catalog3.items.len(), 3); name: "Item One".to_string(),
assert!(catalog3.items.contains_key("item-1")); price: 100,
assert!(catalog3.items.contains_key("item-2")); sell_trend: 10,
assert!(catalog3.items.contains_key("item-3")); in_stock: true,
},
std::fs::remove_file(path).unwrap(); Item {
} id: "item-2".to_string(),
// ## Integration Tests name: "Item Two".to_string(),
price: 200,
sell_trend: -5,
in_stock: false,
},
Item {
id: "item-3".to_string(),
name: "Item Three".to_string(),
price: 300,
sell_trend: 20,
in_stock: true,
},
];
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 filter1 = Filter::new().with_signed_int("sell_trend", Comparison::LesserOrEqual, -5);
assert!(catalog1.load_by_filter(&filter1).is_ok());
assert_eq!(catalog1.items.len(), 1);
assert!(catalog1.items.contains_key("item-2"));
// Edge Case 2: No values lesser than or equal to filter value.
let mut 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.items.is_empty());
// Edge Case 3: All values lesser than or equal to filter value.
let mut 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.items.len(), 3);
assert!(catalog3.items.contains_key("item-1"));
assert!(catalog3.items.contains_key("item-2"));
assert!(catalog3.items.contains_key("item-3"));
std::fs::remove_file(path).unwrap();
}
// ## Integration Tests
#[test] #[test]
fn integration_test_init_insert_persist_load_get() { fn integration_test_init_insert_persist_load_get() {
// Scenario: 'init' -> 'insert' -> 'persist' -> create a new catalog instance -> 'load_by_id' -> 'get' -> verify data integrity. // Scenario: 'init' -> 'insert' -> 'persist' -> create a new catalog instance -> 'load_by_id' -> 'get' -> verify data integrity.