feat: ordering: store ID is provided by caller & store view tests
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Aravinth Manivannan 2024-09-24 12:20:06 +05:30
parent 272e8f68c6
commit d265412d06
Signed by: realaravinth
GPG key ID: F8F50389936984FF
7 changed files with 249 additions and 273 deletions

View file

@ -1,6 +1,6 @@
{ {
"db_name": "PostgreSQL", "db_name": "PostgreSQL",
"query": "UPDATE\n cqrs_ordering_store_query\n SET\n version = $1,\n name = $2,\n address = $3,\n store_id = $4,\n owner = $5,\n deleted = $6;", "query": "UPDATE\n cqrs_ordering_store_query\n SET\n version = $1,\n name = $2,\n address = $3,\n owner = $4,\n deleted = $5;",
"describe": { "describe": {
"columns": [], "columns": [],
"parameters": { "parameters": {
@ -9,11 +9,10 @@
"Text", "Text",
"Text", "Text",
"Uuid", "Uuid",
"Uuid",
"Bool" "Bool"
] ]
}, },
"nullable": [] "nullable": []
}, },
"hash": "d0580ff6dc77150cb00186302f20460dc3be59be1f8f5588bdc3d0f4489eb613" "hash": "8a7958c4f8419e1fd95b2d0c75a3bab76f5962f37e58c7dee4e5f9341dca8c0e"
} }

View file

@ -1,86 +1,3 @@
//// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
////
//// SPDX-License-Identifier: AGPL-3.0-or-later
//
//use derive_getters::Getters;
//use derive_more::{Display, Error};
//use serde::{Deserialize, Serialize};
//use uuid::Uuid;
//
//#[derive(Debug, Error, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
//pub enum AddStoreCommandError {
// NameIsEmpty,
//}
//
//#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)]
//pub struct AddStoreCommand {
// name: String,
// address: Option<String>,
// owner: Uuid,
//}
//
//impl AddStoreCommand {
// pub fn new(
// name: String,
// address: Option<String>,
// owner: Uuid,
// ) -> Result<Self, AddStoreCommandError> {
// let address: Option<String> = if let Some(address) = address {
// let address = address.trim();
// if address.is_empty() {
// None
// } else {
// Some(address.to_owned())
// }
// } else {
// None
// };
//
// let name = name.trim().to_owned();
// if name.is_empty() {
// return Err(AddStoreCommandError::NameIsEmpty);
// }
//
// Ok(Self {
// name,
// address,
// owner,
// })
// }
//}
//
//#[cfg(test)]
//mod tests {
// use crate::utils::uuid::tests::UUID;
//
// use super::*;
//
// #[test]
// fn test_cmd() {
// let name = "foo";
// let address = "bar";
// let owner = UUID;
//
// // address = None
// let cmd = AddStoreCommand::new(name.into(), None, owner).unwrap();
// assert_eq!(cmd.name(), name);
// assert_eq!(cmd.address(), &None);
// assert_eq!(cmd.owner(), &owner);
//
// // address = Some
// let cmd = AddStoreCommand::new(name.into(), Some(address.into()), owner).unwrap();
// assert_eq!(cmd.name(), name);
// assert_eq!(cmd.address(), &Some(address.to_owned()));
// assert_eq!(cmd.owner(), &owner);
//
// // AddStoreCommandError::NameIsEmpty
// assert_eq!(
// AddStoreCommand::new("".into(), Some(address.into()), owner),
// Err(AddStoreCommandError::NameIsEmpty)
// )
// }
//}
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net> // SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
// //
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later

View file

@ -11,7 +11,7 @@ use uuid::Uuid;
use super::errors::*; use super::errors::*;
use super::OrderingDBPostgresAdapter; use super::OrderingDBPostgresAdapter;
use crate::ordering::domain::events::OrderingEvent; use crate::ordering::domain::events::OrderingEvent;
use crate::ordering::domain::store_aggregate::Store; use crate::ordering::domain::store_aggregate::*;
use crate::utils::parse_aggregate_id::parse_aggregate_id; use crate::utils::parse_aggregate_id::parse_aggregate_id;
pub const NEW_STORE_NON_UUID: &str = "ordering_new_store_non_uuid-asdfa"; pub const NEW_STORE_NON_UUID: &str = "ordering_new_store_non_uuid-asdfa";
@ -27,6 +27,19 @@ pub struct StoreView {
deleted: bool, deleted: bool,
} }
impl From<StoreView> for Store {
fn from(value: StoreView) -> Self {
StoreBuilder::default()
.name(value.name)
.address(value.address)
.store_id(value.store_id)
.owner(value.owner)
.deleted(value.deleted)
.build()
.unwrap()
}
}
// This updates the view with events as they are committed. // This updates the view with events as they are committed.
// The logic should be minimal here, e.g., don't calculate the account balance, // The logic should be minimal here, e.g., don't calculate the account balance,
// design the events to carry the balance information instead. // design the events to carry the balance information instead.
@ -156,13 +169,11 @@ impl ViewRepository<StoreView, Store> for OrderingDBPostgresAdapter {
version = $1, version = $1,
name = $2, name = $2,
address = $3, address = $3,
store_id = $4, owner = $4,
owner = $5, deleted = $5;",
deleted = $6;",
version, version,
view.name, view.name,
view.address, view.address,
view.store_id,
view.owner, view.owner,
view.deleted, view.deleted,
) )
@ -205,108 +216,139 @@ impl Query<Store> for OrderingDBPostgresAdapter {
} }
} }
// Our second query, this one will be handled with Postgres `GenericQuery`
// which will serialize and persist our view after it is updated. It also
// provides a `load` method to deserialize the view on request.
//pub type StoreQuery = GenericQuery<OrderingDBPostgresAdapter, StoreView, Store>;
//pub type StoreQuery = Query<dyn OrderingDBPostgresAdapter, StoreView, Store>;
//#[cfg(test)] #[cfg(test)]
//mod tests { mod tests {
// use super::*; use super::*;
//
// use postgres_es::PostgresCqrs; use postgres_es::PostgresCqrs;
//
// use crate::{ use crate::{
// db::migrate::*, ordering::{
// ordering::{ application::services::{
// application::services::{ add_store_service::AddStoreServiceBuilder, update_store_service::*,
// add_category_service::tests::mock_add_category_service, add_customization_service::tests::mock_add_customization_service, add_line_item_service::tests::mock_add_line_item_service, add_product_service::tests::mock_add_product_service, add_store_service::AddStoreServiceBuilder, update_category_service::tests::mock_update_category_service, update_customization_service::tests::mock_update_customization_service, update_product_service::tests::mock_update_product_service, update_store_service::tests::mock_update_store_service, OrderingServicesBuilder MockOrderingServicesInterface,
// }, },
// domain::{ domain::add_store_command::*,
// add_category_command::AddCategoryCommand, add_customization_command, domain::commands::OrderingCommand,
// add_product_command::tests::get_command, add_store_command::AddStoreCommand, domain::update_store_command::*,
// commands::OrderingCommand, },
// update_category_command::tests::get_update_category_command, db::migrate::*,
// update_customization_command::tests::get_update_customization_command, tests::bdd::*,
// update_product_command, update_store_command::tests::get_update_store_cmd, utils::{random_string::GenerateRandomStringInterface, uuid::tests::UUID},
// }, };
// }, use std::sync::Arc;
// tests::bdd::IS_NEVER_CALLED,
// utils::{random_string::GenerateRandomStringInterface, uuid::tests::UUID}, #[actix_rt::test]
// }; async fn pg_query_ordering_store_view() {
// use std::sync::Arc; let settings = crate::settings::tests::get_settings().await;
// //let settings = crate::settings::Settings::new().unwrap();
// #[actix_rt::test] settings.create_db().await;
// async fn pg_query() {
// let settings = crate::settings::tests::get_settings().await; let db = crate::db::sqlx_postgres::Postgres::init(&settings.database.url).await;
// //let settings = crate::settings::Settings::new().unwrap(); db.migrate().await;
// settings.create_db().await; let db = OrderingDBPostgresAdapter::new(db.pool.clone());
//
// let db = crate::db::sqlx_postgres::Postgres::init(&settings.database.url).await; let simple_query = SimpleLoggingQuery {};
// db.migrate().await;
// let db = OrderingDBPostgresAdapter::new(db.pool.clone()); let queries: Vec<Box<dyn Query<Store>>> =
// vec![Box::new(simple_query), Box::new(db.clone())];
// let simple_query = SimpleLoggingQuery {};
// let mut mock_services = MockOrderingServicesInterface::new();
// let queries: Vec<Box<dyn Query<Store>>> =
// vec![Box::new(simple_query), Box::new(db.clone())]; let db2 = db.clone();
// mock_services
// let services = OrderingServicesBuilder::default() .expect_add_store()
// .add_store(Arc::new( .times(IS_CALLED_ONLY_ONCE.unwrap())
// AddStoreServiceBuilder::default() .returning(move || {
// .db_store_id_exists(Arc::new(db.clone())) Arc::new(
// .db_store_name_exists(Arc::new(db.clone())) AddStoreServiceBuilder::default()
// .get_uuid(Arc::new(crate::utils::uuid::GenerateUUID {})) .db_store_id_exists(Arc::new(db2.clone()))
// .build() .db_store_name_exists(Arc::new(db2.clone()))
// .unwrap(), .build()
// )) .unwrap(),
// .add_category(mock_add_category_service( )
// IS_NEVER_CALLED, });
// AddCategoryCommand::new("foo".into(), None, UUID, UUID).unwrap(),
// )) let db2 = db.clone();
// .add_product(mock_add_product_service(IS_NEVER_CALLED, get_command())) mock_services
// .add_customization(mock_add_customization_service( .expect_update_store()
// IS_NEVER_CALLED, .times(IS_CALLED_ONLY_ONCE.unwrap())
// add_customization_command::tests::get_command(), .returning(move || {
// )) Arc::new(
// .update_product(mock_update_product_service( UpdateStoreServiceBuilder::default()
// IS_NEVER_CALLED, .db_store_id_exists(Arc::new(db2.clone()))
// update_product_command::tests::get_command(), .db_store_name_exists(Arc::new(db2.clone()))
// )) .build()
// .update_customization(mock_update_customization_service( .unwrap(),
// IS_NEVER_CALLED, )
// get_update_customization_command(), });
// ))
// .update_category(mock_update_category_service( let (cqrs, store_query): (
// IS_NEVER_CALLED, Arc<PostgresCqrs<Store>>,
// get_update_category_command(), Arc<dyn ViewRepository<StoreView, Store>>,
// )) ) = (
// .update_store(mock_update_store_service( Arc::new(postgres_es::postgres_cqrs(
// IS_NEVER_CALLED, db.pool.clone(),
// get_update_store_cmd(), queries,
// )) Arc::new(mock_services),
// .build() )),
// .unwrap(); Arc::new(db.clone()),
// );
// let (cqrs, _store_query): (
// Arc<PostgresCqrs<Store>>, let rand = crate::utils::random_string::GenerateRandomString {};
// Arc<dyn ViewRepository<StoreView, Store>>, let cmd = AddStoreCommandBuilder::default()
// ) = ( .name(rand.get_random(10))
// Arc::new(postgres_es::postgres_cqrs( .address(None)
// db.pool.clone(), .owner(UUID)
// queries, .store_id(UUID)
// Arc::new(services), .build()
// )), .unwrap();
// Arc::new(db.clone()), cqrs.execute(
// ); &cmd.store_id().to_string(),
// OrderingCommand::AddStore(cmd.clone()),
// let rand = crate::utils::random_string::GenerateRandomString {}; )
// let cmd = AddStoreCommand::new(rand.get_random(10), None, UUID).unwrap(); .await
// cqrs.execute("", OrderingCommand::AddStore(cmd.clone())) .unwrap();
// .await
// .unwrap(); let store = store_query
// .load(&(*cmd.store_id()).to_string())
// settings.drop_db().await; .await
// } .unwrap()
//} .unwrap();
let store: Store = store.into();
assert_eq!(store.name(), cmd.name());
assert_eq!(store.address(), cmd.address());
assert_eq!(store.owner(), cmd.owner());
assert_eq!(store.store_id(), cmd.store_id());
assert!(!store.deleted());
let update_store_cmd = UpdateStoreCommand::new(
rand.get_random(10),
Some(rand.get_random(10)),
UUID,
store,
UUID,
)
.unwrap();
cqrs.execute(
&cmd.store_id().to_string(),
OrderingCommand::UpdateStore(update_store_cmd.clone()),
)
.await
.unwrap();
let store = store_query
.load(&(*cmd.store_id()).to_string())
.await
.unwrap()
.unwrap();
let store: Store = store.into();
assert_eq!(store.name(), update_store_cmd.name());
assert_eq!(store.address(), update_store_cmd.address());
assert_eq!(store.owner(), update_store_cmd.owner());
assert_eq!(store.store_id(), update_store_cmd.old_store().store_id());
assert!(!store.deleted());
settings.drop_db().await;
}
}

View file

@ -12,12 +12,11 @@ use super::errors::*;
use crate::ordering::{ use crate::ordering::{
application::port::output::db::{store_id_exists::*, store_name_exists::*}, application::port::output::db::{store_id_exists::*, store_name_exists::*},
domain::{ domain::{
add_store_command::AddStoreCommand, add_store_command::*,
store_added_event::{StoreAddedEvent, StoreAddedEventBuilder}, store_added_event::{StoreAddedEvent, StoreAddedEventBuilder},
store_aggregate::*, store_aggregate::*,
}, },
}; };
use crate::utils::uuid::*;
#[automock] #[automock]
#[async_trait::async_trait] #[async_trait::async_trait]
@ -31,28 +30,21 @@ pub type AddStoreServiceObj = Arc<dyn AddStoreUseCase>;
pub struct AddStoreService { pub struct AddStoreService {
db_store_id_exists: StoreIDExistsDBPortObj, db_store_id_exists: StoreIDExistsDBPortObj,
db_store_name_exists: StoreNameExistsDBPortObj, db_store_name_exists: StoreNameExistsDBPortObj,
get_uuid: GetUUIDInterfaceObj,
} }
#[async_trait::async_trait] #[async_trait::async_trait]
impl AddStoreUseCase for AddStoreService { impl AddStoreUseCase for AddStoreService {
async fn add_store(&self, cmd: AddStoreCommand) -> OrderingResult<StoreAddedEvent> { async fn add_store(&self, cmd: AddStoreCommand) -> OrderingResult<StoreAddedEvent> {
let mut store_id = self.get_uuid.get_uuid();
loop { if self.db_store_id_exists.store_id_exists(cmd.store_id()).await? {
if self.db_store_id_exists.store_id_exists(&store_id).await? { return Err(OrderingError::DuplicateStoreID);
store_id = self.get_uuid.get_uuid();
continue;
} else {
break;
} }
}
let store = StoreBuilder::default() let store = StoreBuilder::default()
.name(cmd.name().into()) .name(cmd.name().into())
.address(cmd.address().as_ref().map(|s| s.to_string())) .address(cmd.address().as_ref().map(|s| s.to_string()))
.owner(*cmd.owner()) .owner(*cmd.owner())
.store_id(store_id) .store_id(*cmd.store_id())
.build() .build()
.unwrap(); .unwrap();
@ -64,7 +56,7 @@ impl AddStoreUseCase for AddStoreService {
.name(store.name().into()) .name(store.name().into())
.address(store.address().as_ref().map(|s| s.to_string())) .address(store.address().as_ref().map(|s| s.to_string()))
.owner(*cmd.owner()) .owner(*cmd.owner())
.store_id(store_id) .store_id(*cmd.store_id())
.build() .build()
.unwrap()) .unwrap())
} }
@ -87,7 +79,7 @@ pub mod tests {
.name(cmd.name().into()) .name(cmd.name().into())
.address(cmd.address().as_ref().map(|s| s.to_string())) .address(cmd.address().as_ref().map(|s| s.to_string()))
.owner(*cmd.owner()) .owner(*cmd.owner())
.store_id(UUID) .store_id(*cmd.store_id())
.build() .build()
.unwrap(); .unwrap();
@ -108,13 +100,17 @@ pub mod tests {
let address = "bar"; let address = "bar";
let owner = UUID; let owner = UUID;
// address = None let cmd = AddStoreCommandBuilder::default()
let cmd = AddStoreCommand::new(name.into(), Some(address.into()), owner).unwrap(); .name(name.into())
.address(Some(address.into()))
.owner(owner)
.store_id(UUID)
.build()
.unwrap();
let s = AddStoreServiceBuilder::default() let s = AddStoreServiceBuilder::default()
.db_store_id_exists(mock_store_id_exists_db_port_false(IS_CALLED_ONLY_ONCE)) .db_store_id_exists(mock_store_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
.db_store_name_exists(mock_store_name_exists_db_port_false(IS_CALLED_ONLY_ONCE)) .db_store_name_exists(mock_store_name_exists_db_port_false(IS_CALLED_ONLY_ONCE))
.get_uuid(mock_get_uuid(IS_CALLED_ONLY_ONCE))
.build() .build()
.unwrap(); .unwrap();
@ -122,7 +118,7 @@ pub mod tests {
assert_eq!(res.name(), cmd.name()); assert_eq!(res.name(), cmd.name());
assert_eq!(res.address(), cmd.address()); assert_eq!(res.address(), cmd.address());
assert_eq!(res.owner(), cmd.owner()); assert_eq!(res.owner(), cmd.owner());
assert_eq!(res.store_id(), &UUID); assert_eq!(res.store_id(), cmd.store_id());
} }
#[actix_rt::test] #[actix_rt::test]
@ -131,13 +127,17 @@ pub mod tests {
let address = "bar"; let address = "bar";
let owner = UUID; let owner = UUID;
// address = None let cmd = AddStoreCommandBuilder::default()
let cmd = AddStoreCommand::new(name.into(), Some(address.into()), owner).unwrap(); .name(name.into())
.address(Some(address.into()))
.owner(owner)
.store_id(UUID)
.build()
.unwrap();
let s = AddStoreServiceBuilder::default() let s = AddStoreServiceBuilder::default()
.db_store_id_exists(mock_store_id_exists_db_port_false(IS_CALLED_ONLY_ONCE)) .db_store_id_exists(mock_store_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
.db_store_name_exists(mock_store_name_exists_db_port_true(IS_CALLED_ONLY_ONCE)) .db_store_name_exists(mock_store_name_exists_db_port_true(IS_CALLED_ONLY_ONCE))
.get_uuid(mock_get_uuid(IS_CALLED_ONLY_ONCE))
.build() .build()
.unwrap(); .unwrap();

View file

@ -26,52 +26,37 @@ pub enum OrderingError {
ProductIDNotFound, ProductIDNotFound,
DuplicateCustomizationName, DuplicateCustomizationName,
CustomizationIDNotFound, CustomizationIDNotFound,
DuplicateKotID,
DuplicateOrderID,
DuplicateStoreID,
DuplicateCustomizationID,
DuplicateProductID,
DuplicateCategoryID,
DuplicateLineItemID,
} }
// //
impl From<OrderingDBError> for OrderingError { impl From<OrderingDBError> for OrderingError {
fn from(value: OrderingDBError) -> Self { fn from(value: OrderingDBError) -> Self {
match value { match value {
OrderingDBError::DuplicateLineItemID => { OrderingDBError::DuplicateLineItemID => Self::DuplicateLineItemID,
error!("DuplicateLineItemID");
Self::InternalError
}
OrderingDBError::LineItemIDNotFound => OrderingError::LineItemIDNotFound, OrderingDBError::LineItemIDNotFound => OrderingError::LineItemIDNotFound,
OrderingDBError::DuplicateOrderID => { OrderingDBError::DuplicateOrderID => Self::DuplicateOrderID,
error!("DuplicateOrderID");
Self::InternalError
}
OrderingDBError::DuplicateStoreName => Self::DuplicateStoreName, OrderingDBError::DuplicateStoreName => Self::DuplicateStoreName,
OrderingDBError::DuplicateStoreID => { OrderingDBError::DuplicateStoreID => Self::DuplicateStoreID,
error!("DuplicateStoreID");
Self::InternalError
}
OrderingDBError::StoreIDNotFound => OrderingError::StoreIDNotFound, OrderingDBError::StoreIDNotFound => OrderingError::StoreIDNotFound,
OrderingDBError::OrderIDNotFound => OrderingError::OrderIDNotFound, OrderingDBError::OrderIDNotFound => OrderingError::OrderIDNotFound,
OrderingDBError::DuplicateKotID => Self::DuplicateKotID,
OrderingDBError::DuplicateKotID => {
error!("DuplicateKotID");
Self::InternalError
}
OrderingDBError::KotIDNotFound => OrderingError::KotIDNotFound, OrderingDBError::KotIDNotFound => OrderingError::KotIDNotFound,
OrderingDBError::InternalError => Self::InternalError, OrderingDBError::InternalError => Self::InternalError,
OrderingDBError::DuplicateCategoryName => Self::DuplicateCategoryName, OrderingDBError::DuplicateCategoryName => Self::DuplicateCategoryName,
OrderingDBError::DuplicateCategoryID => { OrderingDBError::DuplicateCategoryID => Self::DuplicateCategoryID,
error!("DuplicateCategoryID");
Self::InternalError
}
OrderingDBError::CategoryIDNotFound => OrderingError::CategoryIDNotFound, OrderingDBError::CategoryIDNotFound => OrderingError::CategoryIDNotFound,
OrderingDBError::DuplicateProductName => Self::DuplicateProductName, OrderingDBError::DuplicateProductName => Self::DuplicateProductName,
OrderingDBError::DuplicateProductID => { OrderingDBError::DuplicateProductID => Self::DuplicateProductID,
error!("DuplicateProductID");
Self::InternalError
}
OrderingDBError::ProductIDNotFound => OrderingError::ProductIDNotFound, OrderingDBError::ProductIDNotFound => OrderingError::ProductIDNotFound,
OrderingDBError::DuplicateCustomizationName => Self::DuplicateCustomizationName, OrderingDBError::DuplicateCustomizationName => Self::DuplicateCustomizationName,
OrderingDBError::DuplicateCustomizationID => { OrderingDBError::DuplicateCustomizationID => Self::DuplicateCustomizationID,
error!("DuplicateCustomizationID");
Self::InternalError
}
OrderingDBError::CustomizationIDNotFound => OrderingError::CustomizationIDNotFound, OrderingDBError::CustomizationIDNotFound => OrderingError::CustomizationIDNotFound,
} }
} }

View file

@ -2,6 +2,7 @@
// //
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
use derive_builder::Builder;
use derive_getters::Getters; use derive_getters::Getters;
use derive_more::{Display, Error}; use derive_more::{Display, Error};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -12,46 +13,52 @@ pub enum AddStoreCommandError {
NameIsEmpty, NameIsEmpty,
} }
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)] #[derive(
Clone, Builder, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters,
)]
#[builder(build_fn(validate = "Self::validate"))]
pub struct AddStoreCommand { pub struct AddStoreCommand {
#[builder(setter(custom))]
name: String, name: String,
#[builder(setter(custom))]
address: Option<String>, address: Option<String>,
store_id: Uuid,
owner: Uuid, owner: Uuid,
} }
impl AddStoreCommand { impl AddStoreCommandBuilder {
pub fn new( pub fn address(&mut self, address: Option<String>) -> &mut Self {
name: String, self.address = if let Some(address) = address {
address: Option<String>,
owner: Uuid,
) -> Result<Self, AddStoreCommandError> {
let address: Option<String> = if let Some(address) = address {
let address = address.trim(); let address = address.trim();
if address.is_empty() { if address.is_empty() {
None Some(None)
} else { } else {
Some(address.to_owned()) Some(Some(address.to_owned()))
} }
} else { } else {
None Some(None)
}; };
self
}
let name = name.trim().to_owned(); pub fn name(&mut self, name: String) -> &mut Self {
self.name = Some(name.trim().to_owned());
self
}
fn validate(&self) -> Result<(), String> {
let name = self.name.as_ref().unwrap().trim().to_owned();
if name.is_empty() { if name.is_empty() {
return Err(AddStoreCommandError::NameIsEmpty); return Err(AddStoreCommandError::NameIsEmpty.to_string());
} }
Ok(())
Ok(Self {
name,
address,
owner,
})
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::utils::uuid::tests::UUID; use crate::tests::bdd::*;
use crate::utils::uuid::tests::*;
use super::*; use super::*;
@ -62,21 +69,41 @@ mod tests {
let owner = UUID; let owner = UUID;
// address = None // address = None
let cmd = AddStoreCommand::new(name.into(), None, owner).unwrap(); let cmd = AddStoreCommandBuilder::default()
.name(name.into())
.address(None)
.owner(owner)
.store_id(UUID)
.build()
.unwrap();
assert_eq!(cmd.name(), name); assert_eq!(cmd.name(), name);
assert_eq!(cmd.address(), &None); assert_eq!(cmd.address(), &None);
assert_eq!(cmd.owner(), &owner); assert_eq!(cmd.owner(), &owner);
assert_eq!(*cmd.store_id(), UUID);
// address = Some // address = Some
let cmd = AddStoreCommand::new(name.into(), Some(address.into()), owner).unwrap(); let cmd = AddStoreCommandBuilder::default()
.name(name.into())
.address(Some(address.into()))
.owner(owner)
.store_id(UUID)
.build()
.unwrap();
assert_eq!(cmd.name(), name); assert_eq!(cmd.name(), name);
assert_eq!(cmd.address(), &Some(address.to_owned())); assert_eq!(cmd.address(), &Some(address.to_owned()));
assert_eq!(cmd.owner(), &owner); assert_eq!(cmd.owner(), &owner);
assert_eq!(*cmd.store_id(), UUID);
// AddStoreCommandError::NameIsEmpty // AddStoreCommandError::NameIsEmpty
assert_eq!(
AddStoreCommand::new("".into(), Some(address.into()), owner), assert!(AddStoreCommandBuilder::default()
Err(AddStoreCommandError::NameIsEmpty) .name("".into())
) .address(Some(address.into()))
.owner(owner)
.store_id(UUID)
.build()
.is_err())
} }
} }

View file

@ -113,7 +113,13 @@ mod tests {
.unwrap(); .unwrap();
let expected = OrderingEvent::StoreAdded(expected); let expected = OrderingEvent::StoreAdded(expected);
let cmd = AddStoreCommand::new(name.into(), address.clone(), owner).unwrap(); let cmd = AddStoreCommandBuilder::default()
.name(name.into())
.address(address.clone())
.owner(owner)
.store_id(UUID)
.build()
.unwrap();
let mut services = MockOrderingServicesInterface::new(); let mut services = MockOrderingServicesInterface::new();
services services