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

This commit is contained in:
Aravinth Manivannan 2024-09-24 12:39:58 +05:30
parent 5a050fde0e
commit df64f434be
Signed by: realaravinth
GPG key ID: F8F50389936984FF
12 changed files with 318 additions and 140 deletions

View file

@ -1,24 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "UPDATE\n cqrs_ordering_line_item_query\n SET\n version = $1,\n product_name = $2,\n product_id = $3,\n line_item_id = $4,\n quantity_minor_unit = $5,\n quantity_minor_number = $6,\n quantity_major_unit = $7,\n quantity_major_number = $8,\n created_time = $9,\n kot_id = $10,\n deleted = $11;",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8",
"Text",
"Uuid",
"Uuid",
"Text",
"Int4",
"Text",
"Int4",
"Timestamptz",
"Uuid",
"Bool"
]
},
"nullable": []
},
"hash": "c348d55dd91acb0d4697c433f61866b288fec93971bf9ab41faec21680e50f71"
}

View file

@ -0,0 +1,23 @@
{
"db_name": "PostgreSQL",
"query": "UPDATE\n cqrs_ordering_line_item_query\n SET\n version = $1,\n product_name = $2,\n product_id = $3,\n quantity_minor_unit = $4,\n quantity_minor_number = $5,\n quantity_major_unit = $6,\n quantity_major_number = $7,\n created_time = $8,\n kot_id = $9,\n deleted = $10;",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8",
"Text",
"Uuid",
"Text",
"Int4",
"Text",
"Int4",
"Timestamptz",
"Uuid",
"Bool"
]
},
"nullable": []
},
"hash": "e61db066fe7e66879f8ce8ccd3da94d89c30f8f5d633004ba16a6e971e041a14"
}

View file

@ -219,7 +219,6 @@ impl Query<Customization> for OrderingDBPostgresAdapter {
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View file

@ -36,7 +36,7 @@ pub mod tests {
// use crate::ordering::domain::add_product_command::tests::get_customizations; // use crate::ordering::domain::add_product_command::tests::get_customizations;
use crate::ordering::domain::kot_aggregate::*; use crate::ordering::domain::kot_aggregate::*;
async fn create_dummy_kot(kot: &Kot, db: &OrderingDBPostgresAdapter) { pub async fn create_dummy_kot(kot: &Kot, db: &OrderingDBPostgresAdapter) {
sqlx::query!( sqlx::query!(
"INSERT INTO cqrs_ordering_kot_query ( "INSERT INTO cqrs_ordering_kot_query (
version, version,

View file

@ -271,18 +271,16 @@ impl ViewRepository<LineItemView, LineItem> for OrderingDBPostgresAdapter {
version = $1, version = $1,
product_name = $2, product_name = $2,
product_id = $3, product_id = $3,
line_item_id = $4, quantity_minor_unit = $4,
quantity_minor_unit = $5, quantity_minor_number = $5,
quantity_minor_number = $6, quantity_major_unit = $6,
quantity_major_unit = $7, quantity_major_number = $7,
quantity_major_number = $8, created_time = $8,
created_time = $9, kot_id = $9,
kot_id = $10, deleted = $10;",
deleted = $11;",
version, version,
view.product_name, view.product_name,
view.product_id, view.product_id,
view.line_item_id,
view.quantity_minor_unit, view.quantity_minor_unit,
view.quantity_minor_number, view.quantity_minor_number,
view.quantity_major_unit, view.quantity_major_unit,
@ -320,3 +318,216 @@ impl Query<LineItem> for OrderingDBPostgresAdapter {
self.update_view(view, view_context).await.unwrap(); self.update_view(view, view_context).await.unwrap();
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use postgres_es::PostgresCqrs;
use crate::{
ordering::{
application::services::{
add_line_item_service::AddLineItemServiceBuilder, delete_line_item_service::*,
update_line_item_service::*, MockOrderingServicesInterface,
},
domain::{
add_line_item_command::*, kot_aggregate::Kot, commands::OrderingCommand,
delete_line_item_command::DeleteLineItemCommandBuilder,
update_line_item_command::*,
},
},
db::migrate::*,
tests::bdd::*,
types::quantity::*,
utils::{
random_string::GenerateRandomStringInterface,
uuid::{tests::UUID, *},
},
};
use std::sync::Arc;
#[actix_rt::test]
async fn pg_query_ordering_line_item_view() {
let settings = crate::settings::tests::get_settings().await;
//let settings = crate::settings::Settings::new().unwrap();
settings.create_db().await;
let db = crate::db::sqlx_postgres::Postgres::init(&settings.database.url).await;
db.migrate().await;
let db = OrderingDBPostgresAdapter::new(db.pool.clone());
let queries: Vec<Box<dyn Query<LineItem>>> = vec![Box::new(db.clone())];
let mut mock_services = MockOrderingServicesInterface::new();
let kot = Kot::default();
crate::ordering::adapters::output::db::kot_id_exists::tests::create_dummy_kot(
&kot, &db,
)
.await;
let db2 = db.clone();
mock_services
.expect_add_line_item()
.times(IS_CALLED_ONLY_ONCE.unwrap())
.returning(move || {
Arc::new(
AddLineItemServiceBuilder::default()
.db_line_item_id_exists(Arc::new(db2.clone()))
.db_kot_id_exists(Arc::new(db2.clone()))
.build()
.unwrap(),
)
});
let db2 = db.clone();
mock_services
.expect_update_line_item()
.times(IS_CALLED_ONLY_ONCE.unwrap())
.returning(move || {
Arc::new(
UpdateLineItemServiceBuilder::default()
.db_line_item_id_exists(Arc::new(db2.clone()))
.db_kot_id_exists(Arc::new(db2.clone()))
.build()
.unwrap(),
)
});
let db2 = db.clone();
mock_services
.expect_delete_line_item()
.times(IS_CALLED_ONLY_ONCE.unwrap())
.returning(move || {
Arc::new(
DeleteLineItemServiceBuilder::default()
.db_line_item_id_exists(Arc::new(db2.clone()))
.build()
.unwrap(),
)
});
let (cqrs, line_item_query): (
Arc<PostgresCqrs<LineItem>>,
Arc<dyn ViewRepository<LineItemView, LineItem>>,
) = (
Arc::new(postgres_es::postgres_cqrs(
db.pool.clone(),
queries,
Arc::new(mock_services),
)),
Arc::new(db.clone()),
);
let rand = crate::utils::random_string::GenerateRandomString {};
let uuid = GenerateUUID {};
let line_item_id = uuid.get_uuid();
let cmd = AddLineItemCommandBuilder::default()
.product_name(rand.get_random(10))
.adding_by(UUID)
.quantity(Quantity::get_quantity())
.product_id(UUID)
.kot_id(*kot.kot_id())
.line_item_id(line_item_id)
.build()
.unwrap();
cqrs.execute(
&cmd.line_item_id().to_string(),
OrderingCommand::AddLineItem(cmd.clone()),
)
.await
.unwrap();
let line_item = line_item_query
.load(&(*cmd.line_item_id()).to_string())
.await
.unwrap()
.unwrap();
let line_item: LineItem = line_item.into();
assert_eq!(line_item.line_item_id(), cmd.line_item_id());
assert_eq!(line_item.product_name(), cmd.product_name());
assert_eq!(line_item.product_id(), cmd.product_id());
assert_eq!(line_item.quantity(), cmd.quantity());
assert!(!line_item.deleted());
let update_line_item_cmd = UnvalidatedUpdateLineItemCommandBuilder::default()
.product_name(rand.get_random(10))
.adding_by(UUID)
.quantity(Quantity::get_quantity())
.product_id(UUID)
.kot_id(*kot.kot_id())
.old_line_item(line_item.clone())
.build()
.unwrap()
.validate()
.unwrap();
cqrs.execute(
&cmd.line_item_id().to_string(),
OrderingCommand::UpdateLineItem(update_line_item_cmd.clone()),
)
.await
.unwrap();
let line_item = line_item_query
.load(&(*cmd.line_item_id()).to_string())
.await
.unwrap()
.unwrap();
let line_item: LineItem = line_item.into();
assert_eq!(
line_item.line_item_id(),
update_line_item_cmd.old_line_item().line_item_id()
);
assert_eq!(
line_item.product_name(),
update_line_item_cmd.product_name()
);
assert_eq!(line_item.product_id(), update_line_item_cmd.product_id());
assert_eq!(line_item.quantity(), update_line_item_cmd.quantity());
assert!(!line_item.deleted());
// delete
let delete_line_item_cmd = DeleteLineItemCommandBuilder::default()
.line_item(line_item.clone())
.adding_by(UUID)
.build()
.unwrap();
cqrs.execute(
&cmd.line_item_id().to_string(),
OrderingCommand::DeleteLineItem(delete_line_item_cmd.clone()),
)
.await
.unwrap();
let deleted_line_item = line_item_query
.load(&(*cmd.line_item_id()).to_string())
.await
.unwrap()
.unwrap();
let deleted_line_item: LineItem = deleted_line_item.into();
assert_eq!(
deleted_line_item.line_item_id(),
delete_line_item_cmd.line_item().line_item_id()
);
assert_eq!(
deleted_line_item.product_name(),
delete_line_item_cmd.line_item().product_name()
);
assert_eq!(
deleted_line_item.product_id(),
delete_line_item_cmd.line_item().product_id()
);
assert_eq!(
deleted_line_item.quantity(),
delete_line_item_cmd.line_item().quantity()
);
assert!(deleted_line_item.deleted());
settings.drop_db().await;
}
}

View file

@ -216,7 +216,6 @@ impl Query<Store> for OrderingDBPostgresAdapter {
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -224,6 +223,7 @@ mod tests {
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_store_service::AddStoreServiceBuilder, update_store_service::*,
@ -233,7 +233,6 @@ mod tests {
domain::commands::OrderingCommand, domain::commands::OrderingCommand,
domain::update_store_command::*, domain::update_store_command::*,
}, },
db::migrate::*,
tests::bdd::*, tests::bdd::*,
utils::{random_string::GenerateRandomStringInterface, uuid::tests::UUID}, utils::{random_string::GenerateRandomStringInterface, uuid::tests::UUID},
}; };

View file

@ -11,14 +11,10 @@ use mockall::*;
use super::errors::*; use super::errors::*;
use crate::ordering::{ use crate::ordering::{
application::port::output::db::{ application::port::output::db::{
customization_id_exists::*, customization_id_exists::*, customization_name_exists_for_product::*, product_id_exists::*,
customization_name_exists_for_product::*,
product_id_exists::*,
}, },
domain::{ domain::{
add_customization_command::*, add_customization_command::*, customization_added_event::*, customization_aggregate::*,
customization_added_event::*,
customization_aggregate::*,
}, },
}; };
@ -90,8 +86,7 @@ pub mod tests {
use super::*; use super::*;
use crate::ordering::domain::{ use crate::ordering::domain::{
add_customization_command::tests::*, add_customization_command::tests::*, customization_added_event::tests::*,
customization_added_event::tests::*,
}; };
use crate::tests::bdd::*; use crate::tests::bdd::*;
@ -131,7 +126,10 @@ pub mod tests {
let res = s.add_customization(cmd.clone()).await.unwrap(); let res = s.add_customization(cmd.clone()).await.unwrap();
assert_eq!(res.customization().name(), cmd.name()); assert_eq!(res.customization().name(), cmd.name());
assert_eq!(res.customization().customization_id(), cmd.customization_id()); assert_eq!(
res.customization().customization_id(),
cmd.customization_id()
);
} }
#[actix_rt::test] #[actix_rt::test]

View file

@ -15,7 +15,6 @@ use crate::ordering::{
application::port::output::db::line_item_id_exists::*, application::port::output::db::line_item_id_exists::*,
domain::{add_line_item_command::*, line_item_added_event::*, line_item_aggregate::*}, domain::{add_line_item_command::*, line_item_added_event::*, line_item_aggregate::*},
}; };
use crate::utils::uuid::*;
#[automock] #[automock]
#[async_trait::async_trait] #[async_trait::async_trait]
@ -29,7 +28,6 @@ pub type AddLineItemServiceObj = Arc<dyn AddLineItemUseCase>;
pub struct AddLineItemService { pub struct AddLineItemService {
db_line_item_id_exists: LineItemIDExistsDBPortObj, db_line_item_id_exists: LineItemIDExistsDBPortObj,
db_kot_id_exists: KotIDExistsDBPortObj, db_kot_id_exists: KotIDExistsDBPortObj,
get_uuid: GetUUIDInterfaceObj,
} }
#[async_trait::async_trait] #[async_trait::async_trait]
@ -39,19 +37,12 @@ impl AddLineItemUseCase for AddLineItemService {
return Err(OrderingError::KotIDNotFound); return Err(OrderingError::KotIDNotFound);
} }
let mut line_item_id = self.get_uuid.get_uuid();
loop {
if self if self
.db_line_item_id_exists .db_line_item_id_exists
.line_item_id_exists(&line_item_id) .line_item_id_exists(cmd.line_item_id())
.await? .await?
{ {
line_item_id = self.get_uuid.get_uuid(); return Err(OrderingError::DuplicateLineItemID);
continue;
} else {
break;
}
} }
let line_item = LineItemBuilder::default() let line_item = LineItemBuilder::default()
@ -59,7 +50,7 @@ impl AddLineItemUseCase for AddLineItemService {
.product_name(cmd.product_name().into()) .product_name(cmd.product_name().into())
.product_id(*cmd.product_id()) .product_id(*cmd.product_id())
.kot_id(*cmd.kot_id()) .kot_id(*cmd.kot_id())
.line_item_id(line_item_id) .line_item_id(*cmd.line_item_id())
.quantity(cmd.quantity().clone()) .quantity(cmd.quantity().clone())
.deleted(false) .deleted(false)
.build() .build()
@ -79,7 +70,7 @@ pub mod tests {
use crate::ordering::domain::line_item_added_event::tests::get_added_line_item_event_from_command; use crate::ordering::domain::line_item_added_event::tests::get_added_line_item_event_from_command;
use crate::utils::uuid::tests::UUID; use crate::utils::uuid::tests::UUID;
use crate::{tests::bdd::*, utils::uuid::tests::mock_get_uuid}; use crate::tests::bdd::*;
pub fn mock_add_line_item_service( pub fn mock_add_line_item_service(
times: Option<usize>, times: Option<usize>,
@ -106,7 +97,6 @@ pub mod tests {
let s = AddLineItemServiceBuilder::default() let s = AddLineItemServiceBuilder::default()
.db_line_item_id_exists(mock_line_item_id_exists_db_port_false(IS_CALLED_ONLY_ONCE)) .db_line_item_id_exists(mock_line_item_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
.db_kot_id_exists(mock_kot_id_exists_db_port_true(IS_CALLED_ONLY_ONCE)) .db_kot_id_exists(mock_kot_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
.get_uuid(mock_get_uuid(IS_CALLED_ONLY_ONCE))
.build() .build()
.unwrap(); .unwrap();
@ -117,6 +107,7 @@ pub mod tests {
assert_eq!(res.line_item().quantity(), cmd.quantity()); assert_eq!(res.line_item().quantity(), cmd.quantity());
assert_eq!(res.line_item().quantity(), cmd.quantity()); assert_eq!(res.line_item().quantity(), cmd.quantity());
assert_eq!(res.line_item().created_time(), cmd.created_time()); assert_eq!(res.line_item().created_time(), cmd.created_time());
assert_eq!(res.line_item().line_item_id(), cmd.line_item_id());
assert!(!res.line_item().deleted()); assert!(!res.line_item().deleted());
assert_eq!(res.added_by_user(), cmd.adding_by()); assert_eq!(res.added_by_user(), cmd.adding_by());
} }
@ -128,7 +119,6 @@ pub mod tests {
let s = AddLineItemServiceBuilder::default() let s = AddLineItemServiceBuilder::default()
.db_line_item_id_exists(mock_line_item_id_exists_db_port_false(IS_NEVER_CALLED)) .db_line_item_id_exists(mock_line_item_id_exists_db_port_false(IS_NEVER_CALLED))
.db_kot_id_exists(mock_kot_id_exists_db_port_false(IS_CALLED_ONLY_ONCE)) .db_kot_id_exists(mock_kot_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
.get_uuid(mock_get_uuid(IS_NEVER_CALLED))
.build() .build()
.unwrap(); .unwrap();

View file

@ -35,8 +35,11 @@ pub struct AddStoreService {
#[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> {
if self
if self.db_store_id_exists.store_id_exists(cmd.store_id()).await? { .db_store_id_exists
.store_id_exists(cmd.store_id())
.await?
{
return Err(OrderingError::DuplicateStoreID); return Err(OrderingError::DuplicateStoreID);
} }

View file

@ -82,4 +82,3 @@ pub mod tests {
.is_err(),); .is_err(),);
} }
} }

View file

@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use time::OffsetDateTime; use time::OffsetDateTime;
use uuid::Uuid; use uuid::Uuid;
use crate::types::quantity::*; use crate::types::{currency::*, quantity::*};
use crate::utils::string::empty_string_err; use crate::utils::string::empty_string_err;
#[derive(Debug, Error, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Error, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
@ -19,52 +19,41 @@ pub enum AddLineItemCommandError {
} }
#[derive( #[derive(
Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder, Clone, Debug, Builder, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters,
)] )]
pub struct UnvalidatedAddLineItemCommand { #[builder(build_fn(validate = "Self::validate"))]
adding_by: Uuid, pub struct AddLineItemCommand {
#[builder(default = "OffsetDateTime::now_utc()")] #[builder(default = "OffsetDateTime::now_utc()")]
created_time: OffsetDateTime, created_time: OffsetDateTime,
#[builder(setter(custom))]
product_name: String, product_name: String,
product_id: Uuid, product_id: Uuid,
kot_id: Uuid, kot_id: Uuid,
quantity: Quantity, line_item_id: Uuid,
}
impl UnvalidatedAddLineItemCommand {
pub fn validate(self) -> Result<AddLineItemCommand, AddLineItemCommandError> {
let product_name = empty_string_err(
self.product_name,
AddLineItemCommandError::ProductNameIsEmpty,
)?;
if self.quantity.is_empty() {
return Err(AddLineItemCommandError::QuantityIsEmpty);
}
Ok(AddLineItemCommand {
created_time: self.created_time,
product_name,
product_id: self.product_id,
kot_id: self.kot_id,
quantity: self.quantity,
adding_by: self.adding_by,
})
}
}
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)]
pub struct AddLineItemCommand {
created_time: OffsetDateTime,
product_name: String,
product_id: Uuid,
kot_id: Uuid,
quantity: Quantity, quantity: Quantity,
adding_by: Uuid, adding_by: Uuid,
} }
impl AddLineItemCommandBuilder {
pub fn product_name(&mut self, product_name: String) -> &mut Self {
self.product_name = Some(product_name.trim().to_owned());
self
}
fn validate(&self) -> Result<(), String> {
let product_name = self.product_name.as_ref().unwrap().trim().to_owned();
if product_name.is_empty() {
return Err(AddLineItemCommandError::ProductNameIsEmpty.to_string());
}
if self.quantity.as_ref().unwrap().is_empty() {
return Err(AddLineItemCommandError::QuantityIsEmpty.to_string());
}
Ok(())
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use time::macros::datetime; use time::macros::datetime;
@ -81,17 +70,16 @@ mod tests {
let adding_by = UUID; let adding_by = UUID;
let quantity = Quantity::get_quantity(); let quantity = Quantity::get_quantity();
UnvalidatedAddLineItemCommandBuilder::default() AddLineItemCommandBuilder::default()
.product_name(product_name.into()) .product_name(product_name.into())
.adding_by(adding_by) .adding_by(adding_by)
.created_time(datetime!(1970-01-01 0:00 UTC)) .created_time(datetime!(1970-01-01 0:00 UTC))
.quantity(quantity.clone()) .quantity(quantity.clone())
.product_id(product_id) .product_id(product_id)
.kot_id(kot_id) .kot_id(kot_id)
.line_item_id(UUID)
.build() .build()
.unwrap() .unwrap()
.validate()
.unwrap()
} }
} }
@ -103,15 +91,14 @@ mod tests {
let adding_by = UUID; let adding_by = UUID;
let quantity = Quantity::get_quantity(); let quantity = Quantity::get_quantity();
let cmd = UnvalidatedAddLineItemCommandBuilder::default() let cmd = AddLineItemCommandBuilder::default()
.product_name(product_name.into()) .product_name(product_name.into())
.adding_by(adding_by) .adding_by(adding_by)
.quantity(quantity.clone()) .quantity(quantity.clone())
.product_id(product_id) .product_id(product_id)
.kot_id(kot_id) .kot_id(kot_id)
.line_item_id(UUID)
.build() .build()
.unwrap()
.validate()
.unwrap(); .unwrap();
assert_eq!(cmd.quantity(), &quantity); assert_eq!(cmd.quantity(), &quantity);
@ -128,18 +115,15 @@ mod tests {
let adding_by = UUID; let adding_by = UUID;
let quantity = Quantity::get_quantity(); let quantity = Quantity::get_quantity();
assert_eq!( assert!(AddLineItemCommandBuilder::default()
UnvalidatedAddLineItemCommandBuilder::default()
.product_name(product_name.into()) .product_name(product_name.into())
.adding_by(adding_by) .adding_by(adding_by)
.quantity(quantity.clone()) .quantity(quantity.clone())
.product_id(product_id) .product_id(product_id)
.line_item_id(UUID)
.kot_id(kot_id) .kot_id(kot_id)
.build() .build()
.unwrap() .is_err());
.validate(),
Err(AddLineItemCommandError::ProductNameIsEmpty)
);
} }
#[test] #[test]
@ -151,17 +135,13 @@ mod tests {
// minor = 0; major = 0; // minor = 0; major = 0;
let quantity = Quantity::default(); let quantity = Quantity::default();
assert_eq!( assert!(AddLineItemCommandBuilder::default()
UnvalidatedAddLineItemCommandBuilder::default()
.product_name(product_name.into()) .product_name(product_name.into())
.adding_by(adding_by) .adding_by(adding_by)
.quantity(quantity.clone()) .quantity(quantity.clone())
.product_id(product_id) .product_id(product_id)
.kot_id(kot_id) .kot_id(kot_id)
.build() .build()
.unwrap() .is_err());
.validate(),
Err(AddLineItemCommandError::QuantityIsEmpty)
);
} }
} }