test: add tests to catalog.get function

This commit is contained in:
2025-10-13 08:31:05 +02:00
parent c6742fcddd
commit 1079f404fb

View File

@@ -195,7 +195,7 @@ impl Catalog {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
#[derive(Default, PartialEq, Clone)] #[derive(Debug, Default, PartialEq, Clone)]
struct Item { struct Item {
pub id: String, pub id: String,
pub name: String, pub name: String,
@@ -369,32 +369,106 @@ mod tests {
#[test] #[test]
fn get_should_retrieve_and_convert_entity_by_id() { fn get_should_retrieve_and_convert_entity_by_id() {
// Should retrieve an entity by its ID and correctly convert it to the target type 'T'. // Should retrieve an entity by its ID and correctly convert it to the target type 'T'.
todo!(); let mut catalog = Catalog::new("dummy.db");
let item = Item {
id: "item-123".to_string(),
name: "Test Item".to_string(),
price: 100,
in_stock: true,
};
catalog.insert(item.clone());
let retrieved_item: Option<Item> = catalog.get("item-123");
assert_eq!(retrieved_item, Some(item));
} }
#[test] #[test]
fn get_should_return_none_for_nonexistent_id() { fn get_should_return_none_for_nonexistent_id() {
// Should return 'None' if the ID does not exist. // Should return 'None' if the ID does not exist.
todo!(); let mut catalog = Catalog::new("dummy.db");
let item = Item {
id: "item-123".to_string(),
name: "Test Item".to_string(),
price: 100,
in_stock: true,
};
catalog.insert(item.clone());
let retrieved_item: Option<Item> = catalog.get("nonexistent-id");
assert!(retrieved_item.is_none());
} }
// ## 'get_by_class_and_attribute()' // ## 'get_by_class_and_attribute()'
#[test] #[test]
fn get_by_class_and_attribute_should_retrieve_correct_entity() { fn get_by_class_and_attribute_should_retrieve_correct_entity() {
// Should retrieve the first entity matching the class, attribute, and value. // Should retrieve the first entity matching the class, attribute, and value.
todo!(); let mut catalog = Catalog::new("dummy.db");
let item1 = Item {
id: "item-1".to_string(),
name: "Test Item".to_string(),
price: 100,
in_stock: true,
};
let item2 = Item {
id: "item-2".to_string(),
name: "Unique Item".to_string(),
price: 200,
in_stock: false,
};
catalog.insert(item1.clone());
catalog.insert(item2.clone());
let retrieved_item: Option<Item> =
catalog.get_by_class_and_attribute("name", "Unique Item");
assert_eq!(retrieved_item, Some(item2));
} }
#[test] #[test]
fn get_by_class_and_attribute_should_work_with_different_value_types() { fn get_by_class_and_attribute_should_work_with_different_value_types() {
// Should work with different value types (String, i64, f64, bool). // Should work with different value types (String, u64, bool).
todo!(); let mut catalog = Catalog::new("dummy.db");
let item1 = Item {
id: "item-1".to_string(),
name: "Item One".to_string(),
price: 100,
in_stock: true,
};
let item2 = Item {
id: "item-2".to_string(),
name: "Item Two".to_string(),
price: 250,
in_stock: false,
};
catalog.insert(item1.clone());
catalog.insert(item2.clone());
// Test with &str for String attribute
let retrieved_by_name: Option<Item> =
catalog.get_by_class_and_attribute("name", "Item One");
assert_eq!(retrieved_by_name, Some(item1.clone()));
// Test with u64 for price attribute
let retrieved_by_price: Option<Item> = catalog.get_by_class_and_attribute("price", 250u64);
assert_eq!(retrieved_by_price, Some(item2.clone()));
// Test with bool for in_stock attribute
let retrieved_by_stock: Option<Item> = catalog.get_by_class_and_attribute("in_stock", true);
assert_eq!(retrieved_by_stock, Some(item1.clone()));
} }
#[test] #[test]
fn get_by_class_and_attribute_should_return_none_if_no_match() { fn get_by_class_and_attribute_should_return_none_if_no_match() {
// Should return 'None' if no entity matches the criteria. // Should return 'None' if no entity matches the criteria.
todo!(); let mut catalog = Catalog::new("dummy.db");
let item = Item {
id: "item-1".to_string(),
name: "Test Item".to_string(),
price: 100,
in_stock: true,
};
catalog.insert(item.clone());
// Test with a value that doesn't exist
let retrieved_item: Option<Item> =
catalog.get_by_class_and_attribute("name", "Non-existent Name");
assert!(retrieved_item.is_none());
// Test with an attribute that doesn't exist
let retrieved_item_2: Option<Item> =
catalog.get_by_class_and_attribute("non-existent-attribute", "Test Item");
assert!(retrieved_item_2.is_none());
} }
// ## 'list_by_class()' // ## 'list_by_class()'