review: remove Default for Entity
This commit is contained in:
@@ -6,9 +6,10 @@ pub fn run(row: &rusqlite::Row) -> rusqlite::Result<Entity> {
|
||||
let ref_date: Option<u64> = row.get(2)?;
|
||||
let entity = Entity {
|
||||
id,
|
||||
state: EntityState::Loaded,
|
||||
class,
|
||||
ref_date,
|
||||
..Entity::default()
|
||||
attributes: std::collections::HashMap::new(),
|
||||
};
|
||||
Ok(entity)
|
||||
}
|
||||
|
||||
@@ -56,18 +56,3 @@ pub fn run(path: &path::Path, catalog: &Catalog) {
|
||||
}
|
||||
let _ = transaction.commit();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod unit_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_call() {
|
||||
let tempfile = tempfile::NamedTempFile::new().unwrap();
|
||||
let path = tempfile.path();
|
||||
let entity = Entity::new("test");
|
||||
let mut catalog = Catalog::new("");
|
||||
catalog.insert(entity);
|
||||
sqlite::init::db(path);
|
||||
run(path, &catalog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,10 @@ impl From<Value> for Entity {
|
||||
};
|
||||
Entity {
|
||||
id,
|
||||
..Entity::default()
|
||||
ref_date: None,
|
||||
state: EntityState::Unknown,
|
||||
class: String::new(),
|
||||
attributes: std::collections::HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::*;
|
||||
|
||||
#[derive(Debug, Default, PartialEq, Clone)]
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct O {
|
||||
pub id: String,
|
||||
pub state: EntityState,
|
||||
@@ -16,24 +16,22 @@ impl EAV for Entity {
|
||||
}
|
||||
|
||||
impl O {
|
||||
pub fn new(class: &str) -> Self {
|
||||
pub fn new<T>() -> Self
|
||||
where
|
||||
T: EAV,
|
||||
{
|
||||
Self {
|
||||
id: short_uuid::short!().to_string(),
|
||||
class: String::from(class),
|
||||
..Entity::default()
|
||||
id: String::new(),
|
||||
state: EntityState::New,
|
||||
ref_date: None,
|
||||
class: T::class().to_string(),
|
||||
attributes: std::collections::HashMap::<String, Attribute>::new(),
|
||||
}
|
||||
}
|
||||
pub fn with_id(mut self, id: &str) -> Self {
|
||||
self.id = id.to_string();
|
||||
self
|
||||
}
|
||||
pub fn with_class<T>(mut self) -> Self
|
||||
where
|
||||
T: EAV,
|
||||
{
|
||||
self.class = T::class().to_string();
|
||||
self
|
||||
}
|
||||
pub fn with_ref_date(mut self, ref_date: u64) -> Self {
|
||||
self.ref_date = Some(ref_date);
|
||||
self
|
||||
|
||||
@@ -2,5 +2,6 @@
|
||||
pub enum E {
|
||||
#[default]
|
||||
New,
|
||||
Unknown,
|
||||
Loaded,
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::*;
|
||||
#[test]
|
||||
pub fn check_001() {
|
||||
let entity = Entity::new("class");
|
||||
assert_eq!(entity.class, String::from("class"));
|
||||
assert_eq!(entity.attributes.len(), 0);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::*;
|
||||
#[test]
|
||||
pub fn check_001() {
|
||||
let entity = Entity::new("class").with_attribute("a001", 0u64);
|
||||
assert_eq!(
|
||||
entity.attributes.get("a001"),
|
||||
Some(&Attribute {
|
||||
id: "a001".to_string(),
|
||||
value: Value::UnsignedInt(0)
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,7 @@ mod tests {
|
||||
}
|
||||
impl From<Product> for Entity {
|
||||
fn from(value: Product) -> Entity {
|
||||
Entity::default()
|
||||
.with_class::<Product>()
|
||||
Entity::new::<Product>()
|
||||
.with_id(&value.id)
|
||||
.with_attribute("name", value.name)
|
||||
.with_attribute("price", value.price)
|
||||
@@ -33,7 +32,7 @@ mod tests {
|
||||
#[test]
|
||||
fn check_001() {
|
||||
// Demonstrates the costruction of a new entity instance
|
||||
let _product = Entity::new("product")
|
||||
let _product = Entity::new::<Product>()
|
||||
.with_attribute("name", "laptop")
|
||||
.with_attribute("price", 200000u64)
|
||||
.with_attribute("discount", 2.5f64)
|
||||
@@ -42,7 +41,7 @@ mod tests {
|
||||
#[test]
|
||||
fn check_002() {
|
||||
// Demonstrate attribute value reading
|
||||
let product = Entity::new("product")
|
||||
let product = Entity::new::<Product>()
|
||||
.with_attribute("name", "laptop")
|
||||
.with_attribute("price", 200000u64);
|
||||
let name = product.value_of("name");
|
||||
@@ -53,7 +52,7 @@ mod tests {
|
||||
#[test]
|
||||
fn check_003() {
|
||||
// Demonstrate attribute value setting
|
||||
let mut product = Entity::new("product");
|
||||
let mut product = Entity::new::<Product>();
|
||||
// new product
|
||||
product.set("price", 200000u64);
|
||||
// set price
|
||||
@@ -71,48 +70,48 @@ mod tests {
|
||||
}
|
||||
#[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))
|
||||
);
|
||||
// // 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);
|
||||
// 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() {
|
||||
@@ -167,27 +166,27 @@ mod tests {
|
||||
}
|
||||
#[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,);
|
||||
// // 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() {
|
||||
|
||||
@@ -1,4 +1,2 @@
|
||||
pub mod entity_new;
|
||||
pub mod entity_with_attribute;
|
||||
pub mod intended_use;
|
||||
pub mod rusty_budger_use;
|
||||
|
||||
@@ -18,8 +18,7 @@ mod tests {
|
||||
}
|
||||
impl From<OperationToCategory> for Entity {
|
||||
fn from(value: OperationToCategory) -> Entity {
|
||||
Entity::default()
|
||||
.with_class::<OperationToCategory>()
|
||||
Entity::new::<OperationToCategory>()
|
||||
.with_id(&value.id)
|
||||
.with_attribute("operation_id", value.operation_id)
|
||||
.with_attribute("category_id", value.category_id)
|
||||
@@ -48,8 +47,7 @@ mod tests {
|
||||
}
|
||||
impl From<Category> for Entity {
|
||||
fn from(value: Category) -> Entity {
|
||||
Entity::default()
|
||||
.with_class::<Category>()
|
||||
Entity::new::<Category>()
|
||||
.with_id(&value.id)
|
||||
.with_attribute("label", value.label)
|
||||
}
|
||||
@@ -78,8 +76,7 @@ mod tests {
|
||||
}
|
||||
impl From<Operation> for Entity {
|
||||
fn from(value: Operation) -> Entity {
|
||||
Entity::default()
|
||||
.with_class::<Operation>()
|
||||
Entity::new::<Operation>()
|
||||
.with_id(&value.id)
|
||||
.with_attribute("date", value.date)
|
||||
.with_attribute("amount", value.amount)
|
||||
|
||||
Reference in New Issue
Block a user