feat: handle comparison "lesser or equal" for signed int contidion

This commit is contained in:
2025-10-19 10:26:34 +02:00
parent 3d6dfef254
commit 45093a5672
2 changed files with 174 additions and 61 deletions

View File

@@ -35,6 +35,9 @@ pub fn run(filter: &Filter) -> Result<String, FailedTo> {
(Comparison::GreaterOrEqual, Condition::SignedInt(_)) => { (Comparison::GreaterOrEqual, Condition::SignedInt(_)) => {
compose_fragment(name, "value_int", ">=", i + 1) compose_fragment(name, "value_int", ">=", i + 1)
} }
(Comparison::LesserOrEqual, Condition::SignedInt(_)) => {
compose_fragment(name, "value_int", "<=", i + 1)
}
_ => todo!(), _ => todo!(),
}; };
statement.push_str(&fragment); statement.push_str(&fragment);

View File

@@ -1817,69 +1817,179 @@ mod tests {
std::fs::remove_file(path).unwrap(); std::fs::remove_file(path).unwrap();
} }
#[test] #[test]
fn load_by_filter_should_load_matching_greater_or_equal_sell_trend_edge_cases() { fn load_by_filter_should_load_matching_greater_or_equal_sell_trend_edge_cases() {
let db_path = "target/test_dbs/lbf_greater_or_equal_sell_trend_edge_cases.db"; let db_path = "target/test_dbs/lbf_greater_or_equal_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 be included.
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());
assert_eq!(catalog1.items.len(), 1);
assert!(catalog1.items.contains_key("item-3"));
// Edge Case 2: No values greater than or equal to filter value.
let mut 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.items.is_empty());
// Edge Case 3: All values greater than or equal to filter value.
let mut 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.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();
} }
let mut catalog_setup = Catalog::new(db_path); #[test]
catalog_setup.init().unwrap(); fn load_by_filter_should_load_matching_lesser_or_equal_sell_trend() {
let items = vec![ let db_path = "target/test_dbs/lbf_matching_lesser_or_equal_sell_trend.db";
Item { let path = std::path::Path::new(db_path);
id: "item-1".to_string(), std::fs::create_dir_all(path.parent().unwrap()).unwrap();
name: "Item One".to_string(), if path.exists() {
price: 100, std::fs::remove_file(path).unwrap();
sell_trend: 10, }
in_stock: true,
}, let mut catalog_setup = Catalog::new(db_path);
Item { catalog_setup.init().unwrap();
id: "item-2".to_string(), let items = vec![
name: "Item Two".to_string(), Item {
price: 200, id: "item-1".to_string(),
sell_trend: -5, name: "Item One".to_string(),
in_stock: false, price: 100,
}, sell_trend: 10,
Item { in_stock: true,
id: "item-3".to_string(), },
name: "Item Three".to_string(), Item {
price: 300, id: "item-2".to_string(),
sell_trend: 20, name: "Item Two".to_string(),
in_stock: true, price: 200,
}, sell_trend: -5,
]; in_stock: false,
catalog_setup.insert_many(items).unwrap(); },
catalog_setup.persist().unwrap(); Item {
id: "item-3".to_string(),
// Edge Case 1: Value equal to filter value should be included. name: "Item Three".to_string(),
let mut catalog1 = Catalog::new(db_path); price: 300,
let filter1 = Filter::new().with_signed_int("sell_trend", Comparison::GreaterOrEqual, 20); sell_trend: 20,
assert!(catalog1.load_by_filter(&filter1).is_ok()); in_stock: true,
assert_eq!(catalog1.items.len(), 1); },
assert!(catalog1.items.contains_key("item-3")); ];
catalog_setup.insert_many(items).unwrap();
// Edge Case 2: No values greater than or equal to filter value. catalog_setup.persist().unwrap();
let mut catalog2 = Catalog::new(db_path);
let filter2 = Filter::new().with_signed_int("sell_trend", Comparison::GreaterOrEqual, 21); let mut catalog = Catalog::new(db_path);
assert!(catalog2.load_by_filter(&filter2).is_ok()); let filter = Filter::new().with_signed_int("sell_trend", Comparison::LesserOrEqual, 10);
assert!(catalog2.items.is_empty()); assert!(catalog.load_by_filter(&filter).is_ok());
// Edge Case 3: All values greater than or equal to filter value. assert_eq!(catalog.items.len(), 2);
let mut catalog3 = Catalog::new(db_path); assert!(catalog.items.contains_key("item-1"));
let filter3 = Filter::new().with_signed_int("sell_trend", Comparison::GreaterOrEqual, -5); assert!(catalog.items.contains_key("item-2"));
assert!(catalog3.load_by_filter(&filter3).is_ok()); assert!(!catalog.items.contains_key("item-3"));
assert_eq!(catalog3.items.len(), 3);
assert!(catalog3.items.contains_key("item-1")); std::fs::remove_file(path).unwrap();
assert!(catalog3.items.contains_key("item-2")); }
assert!(catalog3.items.contains_key("item-3"));
#[test]
std::fs::remove_file(path).unwrap(); fn load_by_filter_should_load_matching_lesser_or_equal_sell_trend_edge_cases() {
} let db_path = "target/test_dbs/lbf_lesser_or_equal_sell_trend_edge_cases.db";
let path = std::path::Path::new(db_path);
// ## Integration Tests std::fs::create_dir_all(path.parent().unwrap()).unwrap();
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 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.