From 478977850ad4ea6fbaa0dac4b8e3ceed2f037035 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Tue, 16 Jul 2024 19:39:41 +0530 Subject: [PATCH] feat: test AddCUstomization with cqrs infra --- .../services/add_customization_service.rs | 15 +-- .../domain/customization_added_event.rs | 26 ++++ .../domain/customization_aggregate.rs | 121 ++++++------------ 3 files changed, 68 insertions(+), 94 deletions(-) diff --git a/src/inventory/application/services/add_customization_service.rs b/src/inventory/application/services/add_customization_service.rs index fcdaaee..f715321 100644 --- a/src/inventory/application/services/add_customization_service.rs +++ b/src/inventory/application/services/add_customization_service.rs @@ -100,6 +100,7 @@ impl AddCustomizationUseCase for AddCustomizationService { pub mod tests { use super::*; + use customization_added_event::tests::get_customization_added_event_from_cmd; use uuid::Uuid; use crate::inventory::domain::add_customization_command::tests::get_command; @@ -112,19 +113,7 @@ pub mod tests { ) -> AddCustomizationServiceObj { let mut m = MockAddCustomizationUseCase::new(); - let customization = CustomizationBuilder::default() - .name(cmd.name().into()) - .deleted(false) - .customization_id(UUID.clone()) - .build() - .unwrap(); - - let res = CustomizationAddedEventBuilder::default() - .customization(customization) - .product_id(cmd.product_id().clone()) - .build() - .unwrap(); - + let res = get_customization_added_event_from_cmd(&cmd); if let Some(times) = times { m.expect_add_customization() .times(times) diff --git a/src/inventory/domain/customization_added_event.rs b/src/inventory/domain/customization_added_event.rs index d0913ff..c1ed60b 100644 --- a/src/inventory/domain/customization_added_event.rs +++ b/src/inventory/domain/customization_added_event.rs @@ -16,3 +16,29 @@ pub struct CustomizationAddedEvent { customization: Customization, product_id: Uuid, } + +#[cfg(test)] +pub mod tests { + use crate::inventory::domain::add_customization_command::AddCustomizationCommand; + + use super::*; + + use crate::utils::uuid::tests::UUID; + + pub fn get_customization_added_event_from_cmd( + cmd: &AddCustomizationCommand, + ) -> CustomizationAddedEvent { + let customization = CustomizationBuilder::default() + .name(cmd.name().into()) + .deleted(false) + .customization_id(UUID.clone()) + .build() + .unwrap(); + + CustomizationAddedEventBuilder::default() + .customization(customization) + .product_id(cmd.product_id().clone()) + .build() + .unwrap() + } +} diff --git a/src/inventory/domain/customization_aggregate.rs b/src/inventory/domain/customization_aggregate.rs index 597c2ec..c59adcd 100644 --- a/src/inventory/domain/customization_aggregate.rs +++ b/src/inventory/domain/customization_aggregate.rs @@ -64,84 +64,43 @@ impl Aggregate for Customization { } } -//#[cfg(test)] -//mod aggregate_tests { -// use std::sync::Arc; -// -// use cqrs_es::test::TestFramework; -// -// use super::*; -// use crate::inventory::{ -// application::services::{add_product_service::tests::*, *}, -// domain::{ -// add_product_command::tests::get_command, commands::InventoryCommand, -// events::InventoryEvent, product_added_event::tests::get_event_from_command, -// }, -// }; -// use crate::tests::bdd::*; -// -// type ProductTestFramework = TestFramework; -// -// #[test] -// fn test_create_product() { -// let cmd = get_command(); -// let expected = get_event_from_command(&cmd); -// let expected = InventoryEvent::ProductAdded(expected); -// -// let mut services = MockInventoryServicesInterface::new(); -// services -// .expect_add_product() -// .times(IS_CALLED_ONLY_ONCE.unwrap()) -// .return_const(mock_add_product_service(IS_CALLED_ONLY_ONCE, cmd.clone())); -// -// ProductTestFramework::with(Arc::new(services)) -// .given_no_previous_events() -// .when(InventoryCommand::AddProduct(cmd)) -// .then_expect_events(vec![expected]); -// } -// -// fn test_helper(t: T, str_value: &str) -> bool -// where -// T: ToString + FromStr + std::fmt::Debug + PartialEq, -// ::Err: std::fmt::Debug, -// { -// println!("Testing type: {:?} against value {str_value}", t); -// assert_eq!(t.to_string(), str_value.to_string()); -// -// assert_eq!(T::from_str(str_value).unwrap(), t); -// -// assert_eq!(T::from_str(t.to_string().as_str()).unwrap(), t,); -// -// true -// } -// -// #[test] -// fn currency_to_string_from_str() { -// assert!(test_helper(Currency::INR, INR)); -// } -// -// #[test] -// fn quantity_unit_kilogram() { -// assert!(test_helper(QuantityUnit::Kilogram, KILO_GRAM)); -// } -// -// #[test] -// fn quantity_unit_gram() { -// assert!(test_helper(QuantityUnit::Gram, GRAM)); -// } -// -// #[test] -// fn quantity_unit_discrete_number() { -// assert!(test_helper(QuantityUnit::DiscreteNumber, DISCRETE_NUMBER)); -// } -// -// #[test] -// fn quantity_unit_milli_liter() { -// assert!(test_helper(QuantityUnit::MilliLiter, MILLI_LITER)); -// } -// -// #[test] -// fn quantity_unit_liter() { -// assert!(test_helper(QuantityUnit::Liter, LITER)); -// } -//} +#[cfg(test)] +mod aggregate_tests { + use std::sync::Arc; + + use cqrs_es::test::TestFramework; + + use super::*; + use crate::inventory::{ + application::services::{add_customization_service::tests::*, *}, + domain::{ + add_customization_command, commands::InventoryCommand, + customization_added_event::tests::get_customization_added_event_from_cmd, + events::InventoryEvent, + }, + }; + use crate::tests::bdd::*; + + type CustomizationTestFramework = TestFramework; + + #[test] + fn test_create_customization() { + let cmd = add_customization_command::tests::get_command(); + let expected = get_customization_added_event_from_cmd(&cmd); + let expected = InventoryEvent::CustomizationAdded(expected); + + let mut services = MockInventoryServicesInterface::new(); + services + .expect_add_customization() + .times(IS_CALLED_ONLY_ONCE.unwrap()) + .return_const(mock_add_customization_service( + IS_CALLED_ONLY_ONCE, + cmd.clone(), + )); + + CustomizationTestFramework::with(Arc::new(services)) + .given_no_previous_events() + .when(InventoryCommand::AddCustomization(cmd)) + .then_expect_events(vec![expected]); + } +}