fix&feat: ordering: Aggregate creator provides ID #114
2 changed files with 190 additions and 12 deletions
|
@ -236,3 +236,187 @@ impl Query<Order> for OrderingDBPostgresAdapter {
|
|||
self.update_view(view, view_context).await.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use postgres_es::PostgresCqrs;
|
||||
use time::macros::datetime;
|
||||
|
||||
use crate::{
|
||||
db::migrate::*,
|
||||
ordering::{
|
||||
application::services::{
|
||||
add_order_service::*, delete_order_service::DeleteOrderServiceBuilder,
|
||||
update_order_service::*, MockOrderingServicesInterface,
|
||||
},
|
||||
domain::{
|
||||
add_order_command::*, commands::*, delete_order_command::DeleteOrderCommandBuilder,
|
||||
events::*, order_aggregate::*, store_aggregate::*, update_order_command::*,
|
||||
},
|
||||
},
|
||||
tests::bdd::*,
|
||||
utils::{
|
||||
random_string::GenerateRandomStringInterface,
|
||||
uuid::{tests::UUID, *},
|
||||
},
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn pg_query_ordering_order_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 store = Store::default();
|
||||
crate::ordering::adapters::output::db::store_id_exists::tests::create_dummy_store_record(
|
||||
&store, &db,
|
||||
)
|
||||
.await;
|
||||
|
||||
let queries: Vec<Box<dyn Query<Order>>> = vec![Box::new(db.clone())];
|
||||
|
||||
let mut mock_services = MockOrderingServicesInterface::new();
|
||||
|
||||
let db2 = Arc::new(db.clone());
|
||||
mock_services
|
||||
.expect_add_order()
|
||||
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||
.returning(move || {
|
||||
Arc::new(
|
||||
AddOrderServiceBuilder::default()
|
||||
.db_order_id_exists(db2.clone())
|
||||
.db_store_id_exists(db2.clone())
|
||||
.build()
|
||||
.unwrap(),
|
||||
)
|
||||
});
|
||||
|
||||
let db2 = Arc::new(db.clone());
|
||||
mock_services
|
||||
.expect_update_order()
|
||||
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||
.returning(move || {
|
||||
Arc::new(
|
||||
UpdateOrderServiceBuilder::default()
|
||||
.db_order_id_exists(db2.clone())
|
||||
.build()
|
||||
.unwrap(),
|
||||
)
|
||||
});
|
||||
|
||||
let db2 = Arc::new(db.clone());
|
||||
mock_services
|
||||
.expect_delete_order()
|
||||
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||
.returning(move || {
|
||||
Arc::new(
|
||||
DeleteOrderServiceBuilder::default()
|
||||
.db_order_id_exists(db2.clone())
|
||||
.build()
|
||||
.unwrap(),
|
||||
)
|
||||
});
|
||||
|
||||
let (cqrs, order_query): (
|
||||
Arc<PostgresCqrs<Order>>,
|
||||
Arc<dyn ViewRepository<OrderView, Order>>,
|
||||
) = (
|
||||
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 cmd = AddOrderCommandBuilder::default()
|
||||
.customer_name(None)
|
||||
.adding_by(UUID)
|
||||
.created_time(datetime!(1970-01-01 0:00 UTC))
|
||||
.store_id(*store.store_id())
|
||||
.order_id(uuid.get_uuid())
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
cqrs.execute(
|
||||
&cmd.order_id().to_string(),
|
||||
OrderingCommand::AddOrder(cmd.clone()),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let order = order_query
|
||||
.load(&(*cmd.order_id()).to_string())
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let order: Order = order.into();
|
||||
assert_eq!(order.customer_name(), cmd.customer_name());
|
||||
assert_eq!(order.store_id(), cmd.store_id());
|
||||
assert_eq!(order.order_id(), cmd.order_id());
|
||||
assert!(!order.deleted());
|
||||
|
||||
// update
|
||||
let update_order_cmd = UpdateOrderCommandBuilder::default()
|
||||
.customer_name(Some(rand.get_random(10)))
|
||||
.adding_by(UUID)
|
||||
.created_time(datetime!(1970-01-01 0:00 UTC))
|
||||
.old_order(order.clone())
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
cqrs.execute(
|
||||
&cmd.order_id().to_string(),
|
||||
OrderingCommand::UpdateOrder(update_order_cmd.clone()),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let order = order_query
|
||||
.load(&(*cmd.order_id()).to_string())
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let order: Order = order.into();
|
||||
assert_eq!(order.customer_name(), update_order_cmd.customer_name());
|
||||
assert_eq!(order.store_id(), update_order_cmd.old_order().store_id());
|
||||
assert_eq!(order.order_id(), update_order_cmd.old_order().order_id());
|
||||
assert!(!order.deleted());
|
||||
|
||||
let delete_order_cmd = DeleteOrderCommandBuilder::default()
|
||||
.adding_by(UUID)
|
||||
.order(order.clone())
|
||||
.build()
|
||||
.unwrap();
|
||||
cqrs.execute(
|
||||
&cmd.order_id().to_string(),
|
||||
OrderingCommand::DeleteOrder(delete_order_cmd.clone()),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let order = order_query
|
||||
.load(&(*cmd.order_id()).to_string())
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let order: Order = order.into();
|
||||
assert_eq!(
|
||||
order.customer_name(),
|
||||
delete_order_cmd.order().customer_name()
|
||||
);
|
||||
assert_eq!(order.store_id(), delete_order_cmd.order().store_id());
|
||||
assert_eq!(order.order_id(), delete_order_cmd.order().order_id());
|
||||
assert!(order.deleted());
|
||||
|
||||
settings.drop_db().await;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -365,17 +365,13 @@ mod tests {
|
|||
},
|
||||
},
|
||||
domain::{
|
||||
add_product_command::{tests::get_command, *},
|
||||
category_aggregate::{Category, CategoryBuilder},
|
||||
commands::*,
|
||||
events::*,
|
||||
product_aggregate::*,
|
||||
store_aggregate::*,
|
||||
update_product_command::{tests::get_command_with_product, *},
|
||||
add_product_command::tests::get_command, category_aggregate::*, commands::*,
|
||||
product_aggregate::*, store_aggregate::*,
|
||||
update_product_command::tests::get_command_with_product,
|
||||
},
|
||||
},
|
||||
tests::bdd::*,
|
||||
utils::{random_string::GenerateRandomStringInterface, uuid::tests::UUID},
|
||||
utils::uuid::tests::UUID,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
|
@ -451,8 +447,6 @@ mod tests {
|
|||
Arc::new(db.clone()),
|
||||
);
|
||||
|
||||
let rand = crate::utils::random_string::GenerateRandomString {};
|
||||
|
||||
let cmd = get_command();
|
||||
cqrs.execute(
|
||||
&cmd.product_id().to_string(),
|
||||
|
@ -474,7 +468,7 @@ mod tests {
|
|||
assert_eq!(product.quantity(), cmd.quantity());
|
||||
assert_eq!(product.sku_able(), cmd.sku_able());
|
||||
assert_eq!(product.price(), cmd.price());
|
||||
assert!(!store.deleted());
|
||||
assert!(!product.deleted());
|
||||
|
||||
let update_product_cmd = get_command_with_product(product.clone());
|
||||
cqrs.execute(
|
||||
|
@ -499,7 +493,7 @@ mod tests {
|
|||
assert_eq!(product.quantity(), update_product_cmd.quantity());
|
||||
assert_eq!(product.sku_able(), update_product_cmd.sku_able());
|
||||
assert_eq!(product.price(), update_product_cmd.price());
|
||||
assert!(!store.deleted());
|
||||
assert!(!product.deleted());
|
||||
|
||||
settings.drop_db().await;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue