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

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

View file

@ -1,6 +1,6 @@
{ {
"db_name": "PostgreSQL", "db_name": "PostgreSQL",
"query": "UPDATE\n cqrs_ordering_product_customizations_query\n SET\n version = $1,\n name = $2,\n customization_id = $3,\n product_id = $4,\n deleted = $5;", "query": "UPDATE\n cqrs_ordering_product_customizations_query\n SET\n version = $1,\n name = $2,\n product_id = $3,\n deleted = $4;",
"describe": { "describe": {
"columns": [], "columns": [],
"parameters": { "parameters": {
@ -8,11 +8,10 @@
"Int8", "Int8",
"Text", "Text",
"Uuid", "Uuid",
"Uuid",
"Bool" "Bool"
] ]
}, },
"nullable": [] "nullable": []
}, },
"hash": "d9c625876e7d398cb48c6278e69b2eb6ad8515e68d5520013634415109309e6e" "hash": "a5a58d14ddbfa78cca3729392faecfab30cc8b01fed9b73b9cc0813750230314"
} }

View file

@ -17,11 +17,6 @@ use crate::utils::parse_aggregate_id::parse_aggregate_id;
pub const NEW_CUSTOMIZATION_NON_UUID: &str = "ordering_new_customization_non_uuid-asdfa"; pub const NEW_CUSTOMIZATION_NON_UUID: &str = "ordering_new_customization_non_uuid-asdfa";
//#[derive(Debug, Default, Serialize, Deserialize)]
//struct Customizations {
// customizations: Vec<CustomizationView>,
//}
#[derive(Debug, Default, Serialize, Deserialize)] #[derive(Debug, Default, Serialize, Deserialize)]
struct CustomizationView { struct CustomizationView {
name: String, name: String,
@ -187,12 +182,10 @@ impl ViewRepository<CustomizationView, Customization> for OrderingDBPostgresAdap
SET SET
version = $1, version = $1,
name = $2, name = $2,
customization_id = $3, product_id = $3,
product_id = $4, deleted = $4;",
deleted = $5;",
version, version,
view.name, view.name,
view.customization_id,
view.product_id, view.product_id,
view.deleted, view.deleted,
) )
@ -225,3 +218,146 @@ impl Query<Customization> 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::{
db::migrate::*,
ordering::{
adapters::output::db::product_id_exists::tests::create_dummy_product_record,
application::services::{
add_customization_service::*, update_customization_service::*,
MockOrderingServicesInterface,
},
domain::{
add_customization_command::*,
commands::OrderingCommand,
product_aggregate::Product,
update_customization_command::{tests::get_update_customization_command, *},
},
},
tests::bdd::*,
utils::{random_string::GenerateRandomStringInterface, uuid::tests::UUID},
};
use std::sync::Arc;
#[actix_rt::test]
async fn pg_query_ordering_customization_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 product = Product::default();
create_dummy_product_record(&product, &db).await;
// let simple_query = super::store_view::SimpleLoggingQuery {};
let queries: Vec<Box<dyn Query<Customization>>> = vec![Box::new(db.clone())];
let mut mock_services = MockOrderingServicesInterface::new();
let db2 = db.clone();
mock_services
.expect_add_customization()
.times(IS_CALLED_ONLY_ONCE.unwrap())
.returning(move || {
Arc::new(
AddCustomizationServiceBuilder::default()
.db_product_id_exists(Arc::new(db2.clone()))
.db_customization_id_exists(Arc::new(db2.clone()))
.db_customization_name_exists_for_product(Arc::new(db2.clone()))
.build()
.unwrap(),
)
});
let db2 = Arc::new(db.clone());
mock_services
.expect_update_customization()
.times(IS_CALLED_ONLY_ONCE.unwrap())
.returning(move || {
Arc::new(
UpdateCustomizationServiceBuilder::default()
.db_product_id_exists(db2.clone())
.db_customization_name_exists_for_product(db2.clone())
.db_customization_id_exists(db2.clone())
.build()
.unwrap(),
)
});
let (cqrs, custmoization_query): (
Arc<PostgresCqrs<Customization>>,
Arc<dyn ViewRepository<CustomizationView, Customization>>,
) = (
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 cmd = AddCustomizationCommandBuilder::default()
.name(rand.get_random(10))
.product_id(product.product_id().clone())
.customization_id(UUID.clone())
.build()
.unwrap();
cqrs.execute(
&cmd.customization_id().to_string(),
OrderingCommand::AddCustomization(cmd.clone()),
)
.await
.unwrap();
let customization = custmoization_query
.load(&(*cmd.customization_id()).to_string())
.await
.unwrap()
.unwrap();
let customization: Customization = customization.into();
assert_eq!(customization.name(), cmd.name());
assert_eq!(customization.customization_id(), cmd.customization_id());
assert_eq!(customization.product_id(), cmd.product_id());
assert!(!customization.deleted());
let update_customization_command = UnvalidatedUpdateCustomizationCommandBuilder::default()
.name(rand.get_random(10))
.old_customization(customization.clone())
.adding_by(UUID.clone())
.build()
.unwrap()
.validate()
.unwrap();
cqrs.execute(
&cmd.customization_id().to_string(),
OrderingCommand::UpdateCustomization(update_customization_command.clone()),
)
.await
.unwrap();
let c = custmoization_query
.load(&(*cmd.customization_id()).to_string())
.await
.unwrap()
.unwrap();
let c: Customization = c.into();
assert_eq!(c.name(), update_customization_command.name());
assert_eq!(
update_customization_command.old_customization(),
&customization
);
assert!(!c.deleted());
settings.drop_db().await;
}
}

View file

@ -95,8 +95,8 @@ pub mod tests {
);", );",
1, 1,
p.name(), p.name(),
p.description().as_ref().unwrap(), p.description().as_ref().map(|s| s.as_str()),
p.image().as_ref().unwrap(), p.image().as_ref().map(|s| s.as_str()),
p.product_id(), p.product_id(),
p.category_id(), p.category_id(),
p.price().major().clone() as i32, p.price().major().clone() as i32,

View file

@ -11,20 +11,16 @@ 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::{self, *}, customization_id_exists::*,
customization_name_exists_for_product::*, customization_name_exists_for_product::*,
product_id_exists::{self, *}, product_id_exists::*,
product_name_exists_for_category::*,
}, },
domain::{ domain::{
add_customization_command::AddCustomizationCommand, add_customization_command::*,
customization_added_event::{self, *}, customization_added_event::*,
customization_aggregate::*, customization_aggregate::*,
product_added_event::{self, ProductAddedEvent, ProductAddedEventBuilder},
product_aggregate::*,
}, },
}; };
use crate::utils::uuid::*;
#[automock] #[automock]
#[async_trait::async_trait] #[async_trait::async_trait]
@ -42,7 +38,6 @@ pub struct AddCustomizationService {
db_product_id_exists: ProductIDExistsDBPortObj, db_product_id_exists: ProductIDExistsDBPortObj,
db_customization_id_exists: CustomizationIDExistsDBPortObj, db_customization_id_exists: CustomizationIDExistsDBPortObj,
db_customization_name_exists_for_product: CustomizationNameExistsForProductDBPortObj, db_customization_name_exists_for_product: CustomizationNameExistsForProductDBPortObj,
get_uuid: GetUUIDInterfaceObj,
} }
#[async_trait::async_trait] #[async_trait::async_trait]
@ -59,25 +54,19 @@ impl AddCustomizationUseCase for AddCustomizationService {
return Err(OrderingError::ProductIDNotFound); return Err(OrderingError::ProductIDNotFound);
} }
let mut customization_id = self.get_uuid.get_uuid();
loop {
if self if self
.db_customization_id_exists .db_customization_id_exists
.customization_id_exists(&customization_id) .customization_id_exists(cmd.customization_id())
.await? .await?
{ {
customization_id = self.get_uuid.get_uuid(); return Err(OrderingError::DuplicateCustomizationID);
continue;
} else {
break;
} }
}
let customization = CustomizationBuilder::default() let customization = CustomizationBuilder::default()
.name(cmd.name().into()) .name(cmd.name().into())
.deleted(false) .deleted(false)
.product_id(*cmd.product_id()) .product_id(*cmd.product_id())
.customization_id(customization_id) .customization_id(*cmd.customization_id())
.build() .build()
.unwrap(); .unwrap();
@ -100,12 +89,11 @@ impl AddCustomizationUseCase for AddCustomizationService {
pub mod tests { pub mod tests {
use super::*; use super::*;
use customization_added_event::tests::get_customization_added_event_from_cmd; use crate::ordering::domain::{
use uuid::Uuid; add_customization_command::tests::*,
customization_added_event::tests::*,
use crate::ordering::domain::add_customization_command::tests::get_command; };
use crate::utils::uuid::tests::UUID; use crate::tests::bdd::*;
use crate::{tests::bdd::*, utils::uuid::tests::mock_get_uuid};
pub fn mock_add_customization_service( pub fn mock_add_customization_service(
times: Option<usize>, times: Option<usize>,
@ -138,13 +126,12 @@ pub mod tests {
.db_customization_name_exists_for_product( .db_customization_name_exists_for_product(
mock_customization_name_exists_for_product_db_port_false(IS_CALLED_ONLY_ONCE), mock_customization_name_exists_for_product_db_port_false(IS_CALLED_ONLY_ONCE),
) )
.get_uuid(mock_get_uuid(IS_CALLED_ONLY_ONCE))
.build() .build()
.unwrap(); .unwrap();
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!(customization_added_events.len(), cmd.customizations().len()); assert_eq!(res.customization().customization_id(), cmd.customization_id());
} }
#[actix_rt::test] #[actix_rt::test]
@ -159,7 +146,6 @@ pub mod tests {
.db_customization_name_exists_for_product( .db_customization_name_exists_for_product(
mock_customization_name_exists_for_product_db_port_true(IS_CALLED_ONLY_ONCE), mock_customization_name_exists_for_product_db_port_true(IS_CALLED_ONLY_ONCE),
) )
.get_uuid(mock_get_uuid(IS_CALLED_ONLY_ONCE))
.build() .build()
.unwrap(); .unwrap();
@ -179,7 +165,6 @@ pub mod tests {
.db_customization_name_exists_for_product( .db_customization_name_exists_for_product(
mock_customization_name_exists_for_product_db_port_false(IS_NEVER_CALLED), mock_customization_name_exists_for_product_db_port_false(IS_NEVER_CALLED),
) )
.get_uuid(mock_get_uuid(IS_NEVER_CALLED))
.build() .build()
.unwrap(); .unwrap();

View file

@ -16,28 +16,26 @@ pub enum AddCustomizationCommandError {
#[derive( #[derive(
Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder, Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder,
)] )]
pub struct UnvalidatedAddCustomizationCommand { #[builder(build_fn(validate = "Self::validate"))]
name: String,
product_id: Uuid,
}
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)]
pub struct AddCustomizationCommand { pub struct AddCustomizationCommand {
#[builder(setter(custom))]
name: String, name: String,
product_id: Uuid, product_id: Uuid,
customization_id: Uuid,
} }
impl UnvalidatedAddCustomizationCommand { impl AddCustomizationCommandBuilder {
pub fn validate(self) -> Result<AddCustomizationCommand, AddCustomizationCommandError> { pub fn name(&mut self, name: String) -> &mut Self {
let name = self.name.trim().to_owned(); self.name = Some(name.trim().to_owned());
if name.is_empty() { self
return Err(AddCustomizationCommandError::NameIsEmpty); }
}
Ok(AddCustomizationCommand { fn validate(&self) -> Result<(), String> {
name, let name = self.name.as_ref().unwrap().trim().to_owned();
product_id: self.product_id, if name.is_empty() {
}) return Err(AddCustomizationCommandError::NameIsEmpty.to_string());
}
Ok(())
} }
} }
@ -48,13 +46,12 @@ pub mod tests {
use crate::utils::uuid::tests::UUID; use crate::utils::uuid::tests::UUID;
pub fn get_command() -> AddCustomizationCommand { pub fn get_command() -> AddCustomizationCommand {
UnvalidatedAddCustomizationCommandBuilder::default() AddCustomizationCommandBuilder::default()
.name("foo".into()) .name("foo".into())
.product_id(UUID.clone()) .product_id(UUID.clone())
.customization_id(UUID.clone())
.build() .build()
.unwrap() .unwrap()
.validate()
.unwrap()
} }
#[test] #[test]
@ -62,12 +59,11 @@ pub mod tests {
let name = "foo"; let name = "foo";
let product_id = UUID; let product_id = UUID;
let cmd = UnvalidatedAddCustomizationCommandBuilder::default() let cmd = AddCustomizationCommandBuilder::default()
.name(name.into()) .name(name.into())
.product_id(product_id.clone()) .product_id(product_id.clone())
.customization_id(UUID.clone())
.build() .build()
.unwrap()
.validate()
.unwrap(); .unwrap();
assert_eq!(cmd.name(), name); assert_eq!(cmd.name(), name);
@ -78,14 +74,12 @@ pub mod tests {
fn test_cmd_name_is_empty() { fn test_cmd_name_is_empty() {
let product_id = UUID; let product_id = UUID;
assert_eq!( assert!(AddCustomizationCommandBuilder::default()
UnvalidatedAddCustomizationCommandBuilder::default() .name("".into())
.name("".into()) .product_id(product_id.clone())
.product_id(product_id.clone()) .customization_id(UUID.clone())
.build() .build()
.unwrap() .is_err(),);
.validate(),
Err(AddCustomizationCommandError::NameIsEmpty)
);
} }
} }