Merge pull request 'feat: test AddCUstomization with cqrs infra' (#49) from add-customization-test into master
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

Reviewed-on: #49
This commit is contained in:
Aravinth Manivannan 2024-07-16 19:45:59 +05:30
commit 4243c0346f
3 changed files with 68 additions and 94 deletions

View file

@ -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)

View file

@ -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()
}
}

View file

@ -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<Product>;
//
// #[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: T, str_value: &str) -> bool
// where
// T: ToString + FromStr + std::fmt::Debug + PartialEq,
// <T as FromStr>::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<Customization>;
#[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]);
}
}