diff --git a/01.workspace/heave/src/tst/intended_use.rs b/01.workspace/heave/src/tst/intended_use.rs index 77c1b32..5b6f313 100644 --- a/01.workspace/heave/src/tst/intended_use.rs +++ b/01.workspace/heave/src/tst/intended_use.rs @@ -1,219 +1,71 @@ #[cfg(test)] mod tests { use crate::*; - #[derive(Debug, Clone, PartialEq)] + #[derive(Clone, Debug, PartialEq)] struct Product { - pub id: String, - pub name: String, - pub price: u64, + id: String, + name: String, + model: Option, + price: u64, + in_stock: bool, } impl EAV for Product { fn class() -> &'static str { "product" } } - impl From for Entity { - fn from(value: Product) -> Entity { - Entity::new::() - .with_id(&value.id) - .with_attribute("name", value.name) - .with_attribute("price", value.price) - } - } impl From for Product { fn from(entity: Entity) -> Self { Product { id: entity.id.clone(), name: entity.unwrap("name"), - price: entity.unwrap("price"), + model: entity.unwrap_opt("model"), + price: entity.unwrap_or("price", 0), + in_stock: entity.unwrap("in_stock"), } } } - #[test] - fn check_001() { - // Demonstrates the costruction of a new entity instance - let _product = Entity::new::() - .with_attribute("name", "laptop") - .with_attribute("price", 200000u64) - .with_attribute("discount", 2.5f64) - .with_attribute("in_stock", true); + impl From for Entity { + fn from(value: Product) -> Self { + let mut entity = Entity::new::() + .with_id(&value.id) + .with_attribute("name", value.name) + .with_attribute("price", value.price) + .with_attribute("in_stock", value.in_stock); + if let Some(model) = value.model { + entity.set("model", model); + } + entity + } } #[test] - fn check_002() { - // Demonstrate attribute value reading - let product = Entity::new::() - .with_attribute("name", "laptop") - .with_attribute("price", 200000u64); - let name = product.value_of("name"); - let price = product.value_of("price"); - assert_eq!(name, Some(&Value::Text(String::from("laptop")))); - assert_eq!(price, Some(&Value::UnsignedInt(200000))); - } - #[test] - fn check_003() { - // Demonstrate attribute value setting - let mut product = Entity::new::(); - // new product - product.set("price", 200000u64); - // set price - let price = product.value_of("price"); - assert_eq!(price, Some(&Value::UnsignedInt(200000))); - // set in stock - product.set("in_stock", true); - let in_stock = product.value_of("in_stock"); - assert_eq!(in_stock, Some(&Value::Bool(true))); - // set in stock again - let prev_in_stock = product.set("in_stock", false); - let in_stock = product.value_of("in_stock"); - assert_eq!(prev_in_stock, Some(Value::Bool(true))); - assert_eq!(in_stock, Some(&Value::Bool(false))); - } - #[test] - fn check_004() { - // // new entities - // let product = Entity::new::(); - // let category = Entity::new::(); - // // id clones for testing purposes - // let product_id = product.id.clone(); - // let category_id = category.id.clone(); - // // new relation as entity - // let product_has_category = Entity::new("product_has_category") - // .with_attribute("product", product) - // .with_attribute("category", category); - // assert_eq!( - // product_has_category.value_of("product"), - // Some(&Value::Text(product_id)) - // ); - // assert_eq!( - // product_has_category.value_of("category"), - // Some(&Value::Text(category_id)) - // ); - } - #[test] - fn check_005() { - // let tag = Entity::new("tag").with_attribute("label", "new"); - // let tag_id = tag.id.clone(); - // let entity = Entity::new("product") - // .with_attribute("name", "laptop") - // .with_attribute("price", 200000u64) - // .with_attribute("delta", -50i64) - // .with_attribute("in_stock", true) - // .with_attribute("discount", 5.2f64) - // .with_attribute("tag", tag); - // let name: String = entity.unwrap("name"); - // let price: u64 = entity.unwrap("price"); - // let delta: i64 = entity.unwrap("delta"); - // let in_stock: bool = entity.unwrap("in_stock"); - // let discount: f64 = entity.unwrap("discount"); - // let tag: Entity = entity.unwrap("tag"); - // assert_eq!(name, "laptop".to_string()); - // assert_eq!(price, 200000u64); - // assert_eq!(delta, -50i64); - // assert!(in_stock); - // assert_eq!(discount, 5.2f64); - // assert_eq!(tag.id, tag_id); - } - #[test] - fn check_006() { - let product = Product { - id: "abcdef".to_string(), - name: "laptop".to_string(), - price: 200000u64, - }; - let expected_product = product.clone(); - let entity: Entity = product.into(); - let converted_product = Product::from(entity); - assert_eq!(expected_product, converted_product); - } - #[test] - fn check_007() { - let mut catalog = Catalog::new(""); - let product = Product { - id: short_uuid::short!().to_string(), - name: "laptop".to_string(), - price: 200000u64, - }; - catalog.insert(product); - } - #[test] - fn check_008() { - let mut catalog = Catalog::new(""); - let product01 = Product { - id: short_uuid::short!().to_string(), - name: "laptop".to_string(), - price: 200000u64, - }; - let product02 = Product { - id: short_uuid::short!().to_string(), - name: "desktop".to_string(), - price: 150000u64, - }; - let products = vec![product01, product02]; - catalog.insert_many(products); - } - #[test] - fn check_009() { - let mut catalog = Catalog::new(""); - let product = Product { - id: short_uuid::short!().to_string(), - name: "laptop".to_string(), - price: 200000u64, - }; - let expected_product = product.clone(); - catalog.insert(product); - let read_product: Product = catalog.get(&expected_product.id).unwrap(); - assert_eq!(read_product, expected_product); - } - #[test] - fn check_010() { - // // demonstrates load entity by id - // let tempfile = tempfile::NamedTempFile::new().unwrap(); - // let path = tempfile.path().to_str().unwrap(); - // // insert a new entity - // let mut catalog = Catalog::new(path); - // catalog.init(); - // let entity = Entity::new("test") - // .with_attribute("int", Value::SignedInt(50)) - // .with_attribute("string", Value::Text("text".to_string())); - // let id = entity.id.clone(); - // let expected_entity = Entity { - // state: EntityState::Loaded, - // ..entity.clone() - // }; - // catalog.insert(entity); - // catalog.persist(); - // // read the entity in a new catalog - // let mut catalog = Catalog::new(path); - // catalog.load_by_id(&id); - // let entity: Entity = catalog.get(&id).unwrap(); - // assert_eq!(expected_entity, entity,); - } - #[test] - fn check_011() { - // demonstrate load by class + /// # Example 1 + /// + /// - How to create a new empty catalog + /// - How to insert a new entity + /// - How to read the entity by id + fn scenario_001() { + // create a new named temp file let tempfile = tempfile::NamedTempFile::new().unwrap(); let path = tempfile.path(); - let mut catalog = Catalog::new(path.to_str().unwrap()); - catalog.init(); - let product_01 = Product { + let path = path.to_str().unwrap(); + // initialize a new empty catalog given an existing path + let mut catalog = Catalog::new(path); + // create a new object + let product = Product { id: short_uuid::short!().to_string(), - name: "laptop".to_string(), - price: 200000u64, + name: String::from("PenguinX Laptop"), + model: None, + price: 135000, + in_stock: true, }; - let product_02 = Product { - id: short_uuid::short!().to_string(), - name: "desktop".to_string(), - price: 300000u64, - }; - let expected_value_01 = product_01.clone(); - catalog.insert(product_01); - catalog.insert(product_02); - catalog.persist(); - // new empty catalog - let mut catalog = Catalog::new(path.to_str().unwrap()); - catalog.load_by_class::(); - assert_eq!(catalog.items.len(), 2); - let loaded_value_01: Product = catalog.get(&expected_value_01.id).unwrap(); - assert_eq!(loaded_value_01, expected_value_01); + // save some info for late comparison + let original_product = product.clone(); + // insert the new object into catalog consuming it + catalog.insert(product); + // read value from catalog using the original key + let read_product = catalog.get::(&original_product.id).unwrap(); + // assert equality between original and read + assert_eq!(original_product, read_product); } }