test: rewrite intended use scenario 1 with some new features
This commit is contained in:
@@ -1,219 +1,71 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::*;
|
use crate::*;
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
struct Product {
|
struct Product {
|
||||||
pub id: String,
|
id: String,
|
||||||
pub name: String,
|
name: String,
|
||||||
pub price: u64,
|
model: Option<String>,
|
||||||
|
price: u64,
|
||||||
|
in_stock: bool,
|
||||||
}
|
}
|
||||||
impl EAV for Product {
|
impl EAV for Product {
|
||||||
fn class() -> &'static str {
|
fn class() -> &'static str {
|
||||||
"product"
|
"product"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl From<Product> for Entity {
|
|
||||||
fn from(value: Product) -> Entity {
|
|
||||||
Entity::new::<Product>()
|
|
||||||
.with_id(&value.id)
|
|
||||||
.with_attribute("name", value.name)
|
|
||||||
.with_attribute("price", value.price)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<Entity> for Product {
|
impl From<Entity> for Product {
|
||||||
fn from(entity: Entity) -> Self {
|
fn from(entity: Entity) -> Self {
|
||||||
Product {
|
Product {
|
||||||
id: entity.id.clone(),
|
id: entity.id.clone(),
|
||||||
name: entity.unwrap("name"),
|
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]
|
impl From<Product> for Entity {
|
||||||
fn check_001() {
|
fn from(value: Product) -> Self {
|
||||||
// Demonstrates the costruction of a new entity instance
|
let mut entity = Entity::new::<Product>()
|
||||||
let _product = Entity::new::<Product>()
|
.with_id(&value.id)
|
||||||
.with_attribute("name", "laptop")
|
.with_attribute("name", value.name)
|
||||||
.with_attribute("price", 200000u64)
|
.with_attribute("price", value.price)
|
||||||
.with_attribute("discount", 2.5f64)
|
.with_attribute("in_stock", value.in_stock);
|
||||||
.with_attribute("in_stock", true);
|
if let Some(model) = value.model {
|
||||||
|
entity.set("model", model);
|
||||||
|
}
|
||||||
|
entity
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn check_002() {
|
/// # Example 1
|
||||||
// Demonstrate attribute value reading
|
///
|
||||||
let product = Entity::new::<Product>()
|
/// - How to create a new empty catalog
|
||||||
.with_attribute("name", "laptop")
|
/// - How to insert a new entity
|
||||||
.with_attribute("price", 200000u64);
|
/// - How to read the entity by id
|
||||||
let name = product.value_of("name");
|
fn scenario_001() {
|
||||||
let price = product.value_of("price");
|
// create a new named temp file
|
||||||
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::<Product>();
|
|
||||||
// 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::<Product>();
|
|
||||||
// let category = Entity::new::<Category>();
|
|
||||||
// // 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
|
|
||||||
let tempfile = tempfile::NamedTempFile::new().unwrap();
|
let tempfile = tempfile::NamedTempFile::new().unwrap();
|
||||||
let path = tempfile.path();
|
let path = tempfile.path();
|
||||||
let mut catalog = Catalog::new(path.to_str().unwrap());
|
let path = path.to_str().unwrap();
|
||||||
catalog.init();
|
// initialize a new empty catalog given an existing path
|
||||||
let product_01 = Product {
|
let mut catalog = Catalog::new(path);
|
||||||
|
// create a new object
|
||||||
|
let product = Product {
|
||||||
id: short_uuid::short!().to_string(),
|
id: short_uuid::short!().to_string(),
|
||||||
name: "laptop".to_string(),
|
name: String::from("PenguinX Laptop"),
|
||||||
price: 200000u64,
|
model: None,
|
||||||
|
price: 135000,
|
||||||
|
in_stock: true,
|
||||||
};
|
};
|
||||||
let product_02 = Product {
|
// save some info for late comparison
|
||||||
id: short_uuid::short!().to_string(),
|
let original_product = product.clone();
|
||||||
name: "desktop".to_string(),
|
// insert the new object into catalog consuming it
|
||||||
price: 300000u64,
|
catalog.insert(product);
|
||||||
};
|
// read value from catalog using the original key
|
||||||
let expected_value_01 = product_01.clone();
|
let read_product = catalog.get::<Product>(&original_product.id).unwrap();
|
||||||
catalog.insert(product_01);
|
// assert equality between original and read
|
||||||
catalog.insert(product_02);
|
assert_eq!(original_product, read_product);
|
||||||
catalog.persist();
|
|
||||||
// new empty catalog
|
|
||||||
let mut catalog = Catalog::new(path.to_str().unwrap());
|
|
||||||
catalog.load_by_class::<Product>();
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user