feat: store view tests
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
4580aea18b
commit
2853bc5a75
2 changed files with 119 additions and 76 deletions
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "UPDATE\n cqrs_inventory_store_query\n SET\n version = $1,\n name = $2,\n address = $3,\n store_id = $4,\n owner = $5,\n deleted = $6;",
|
"query": "UPDATE\n cqrs_inventory_store_query\n SET\n version = $1,\n name = $2,\n address = $3,\n owner = $4,\n deleted = $5;",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
|
@ -9,11 +9,10 @@
|
||||||
"Text",
|
"Text",
|
||||||
"Text",
|
"Text",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Uuid",
|
|
||||||
"Bool"
|
"Bool"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nullable": []
|
"nullable": []
|
||||||
},
|
},
|
||||||
"hash": "0ccd86643c13d9d7c793f362aedc2d720d9dda982a0416849bf97291fc645ea5"
|
"hash": "8d62d7b612fd7f323aee586047dec9603f05782f55f72d21fc29954a65622b5d"
|
||||||
}
|
}
|
|
@ -11,7 +11,7 @@ use uuid::Uuid;
|
||||||
use super::errors::*;
|
use super::errors::*;
|
||||||
use super::InventoryDBPostgresAdapter;
|
use super::InventoryDBPostgresAdapter;
|
||||||
use crate::inventory::domain::events::InventoryEvent;
|
use crate::inventory::domain::events::InventoryEvent;
|
||||||
use crate::inventory::domain::store_aggregate::Store;
|
use crate::inventory::domain::store_aggregate::{Store, StoreBuilder};
|
||||||
use crate::utils::parse_aggregate_id::parse_aggregate_id;
|
use crate::utils::parse_aggregate_id::parse_aggregate_id;
|
||||||
|
|
||||||
pub const NEW_STORE_NON_UUID: &str = "new_store_non_uuid-asdfa";
|
pub const NEW_STORE_NON_UUID: &str = "new_store_non_uuid-asdfa";
|
||||||
|
@ -27,6 +27,19 @@ pub struct StoreView {
|
||||||
deleted: bool,
|
deleted: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<StoreView> for Store {
|
||||||
|
fn from(value: StoreView) -> Self {
|
||||||
|
StoreBuilder::default()
|
||||||
|
.name(value.name)
|
||||||
|
.address(value.address)
|
||||||
|
.store_id(value.store_id)
|
||||||
|
.owner(value.owner)
|
||||||
|
.deleted(value.deleted)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This updates the view with events as they are committed.
|
// This updates the view with events as they are committed.
|
||||||
// The logic should be minimal here, e.g., don't calculate the account balance,
|
// The logic should be minimal here, e.g., don't calculate the account balance,
|
||||||
// design the events to carry the balance information instead.
|
// design the events to carry the balance information instead.
|
||||||
|
@ -156,13 +169,11 @@ impl ViewRepository<StoreView, Store> for InventoryDBPostgresAdapter {
|
||||||
version = $1,
|
version = $1,
|
||||||
name = $2,
|
name = $2,
|
||||||
address = $3,
|
address = $3,
|
||||||
store_id = $4,
|
owner = $4,
|
||||||
owner = $5,
|
deleted = $5;",
|
||||||
deleted = $6;",
|
|
||||||
version,
|
version,
|
||||||
view.name,
|
view.name,
|
||||||
view.address,
|
view.address,
|
||||||
view.store_id,
|
|
||||||
view.owner,
|
view.owner,
|
||||||
view.deleted,
|
view.deleted,
|
||||||
)
|
)
|
||||||
|
@ -192,12 +203,21 @@ impl Query<Store> for SimpleLoggingQuery {
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Query<Store> for InventoryDBPostgresAdapter {
|
impl Query<Store> for InventoryDBPostgresAdapter {
|
||||||
async fn dispatch(&self, store_id: &str, events: &[EventEnvelope<Store>]) {
|
async fn dispatch(&self, aggregate_id: &str, events: &[EventEnvelope<Store>]) {
|
||||||
|
println!("Got store ID: {}", aggregate_id);
|
||||||
|
|
||||||
let res = self
|
let res = self
|
||||||
.load_with_context(store_id)
|
.load_with_context(&aggregate_id)
|
||||||
.await
|
.await
|
||||||
.unwrap_or_else(|_| Some((StoreView::default(), ViewContext::new(store_id.into(), 0))));
|
.unwrap_or_else(|_| {
|
||||||
|
println!("generating default");
|
||||||
|
Some((
|
||||||
|
StoreView::default(),
|
||||||
|
ViewContext::new(aggregate_id.into(), 0),
|
||||||
|
))
|
||||||
|
});
|
||||||
let (mut view, view_context): (StoreView, ViewContext) = res.unwrap();
|
let (mut view, view_context): (StoreView, ViewContext) = res.unwrap();
|
||||||
|
|
||||||
for event in events {
|
for event in events {
|
||||||
view.update(event);
|
view.update(event);
|
||||||
}
|
}
|
||||||
|
@ -205,11 +225,6 @@ impl Query<Store> for InventoryDBPostgresAdapter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Our second query, this one will be handled with Postgres `GenericQuery`
|
|
||||||
// which will serialize and persist our view after it is updated. It also
|
|
||||||
// provides a `load` method to deserialize the view on request.
|
|
||||||
//pub type StoreQuery = GenericQuery<InventoryDBPostgresAdapter, StoreView, Store>;
|
|
||||||
//pub type StoreQuery = Query<dyn InventoryDBPostgresAdapter, StoreView, Store>;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
@ -221,31 +236,20 @@ mod tests {
|
||||||
db::migrate::*,
|
db::migrate::*,
|
||||||
inventory::{
|
inventory::{
|
||||||
application::services::{
|
application::services::{
|
||||||
add_category_service::tests::mock_add_category_service,
|
add_store_service::AddStoreServiceBuilder, update_store_service::*,
|
||||||
add_customization_service::tests::mock_add_customization_service,
|
MockInventoryServicesInterface,
|
||||||
add_product_service::tests::mock_add_product_service,
|
|
||||||
add_store_service::AddStoreServiceBuilder,
|
|
||||||
update_category_service::tests::mock_update_category_service,
|
|
||||||
update_customization_service::tests::mock_update_customization_service,
|
|
||||||
update_product_service::tests::mock_update_product_service,
|
|
||||||
update_store_service::tests::mock_update_store_service, InventoryServicesBuilder,
|
|
||||||
},
|
|
||||||
domain::{
|
|
||||||
add_category_command::AddCategoryCommand, add_customization_command,
|
|
||||||
add_product_command::tests::get_command, add_store_command::AddStoreCommand,
|
|
||||||
commands::InventoryCommand,
|
|
||||||
update_category_command::tests::get_update_category_command,
|
|
||||||
update_customization_command::tests::get_update_customization_command,
|
|
||||||
update_product_command, update_store_command::tests::get_update_store_cmd,
|
|
||||||
},
|
},
|
||||||
|
domain::add_store_command::*,
|
||||||
|
domain::commands::InventoryCommand,
|
||||||
|
domain::update_store_command::*,
|
||||||
},
|
},
|
||||||
tests::bdd::IS_NEVER_CALLED,
|
tests::bdd::*,
|
||||||
utils::{random_string::GenerateRandomStringInterface, uuid::tests::UUID},
|
utils::{random_string::GenerateRandomStringInterface, uuid::tests::UUID},
|
||||||
};
|
};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn pg_query() {
|
async fn pg_query_inventory_store_view() {
|
||||||
let settings = crate::settings::tests::get_settings().await;
|
let settings = crate::settings::tests::get_settings().await;
|
||||||
//let settings = crate::settings::Settings::new().unwrap();
|
//let settings = crate::settings::Settings::new().unwrap();
|
||||||
settings.create_db().await;
|
settings.create_db().await;
|
||||||
|
@ -259,61 +263,101 @@ mod tests {
|
||||||
let queries: Vec<Box<dyn Query<Store>>> =
|
let queries: Vec<Box<dyn Query<Store>>> =
|
||||||
vec![Box::new(simple_query), Box::new(db.clone())];
|
vec![Box::new(simple_query), Box::new(db.clone())];
|
||||||
|
|
||||||
let services = InventoryServicesBuilder::default()
|
let mut mock_services = MockInventoryServicesInterface::new();
|
||||||
.add_store(Arc::new(
|
|
||||||
AddStoreServiceBuilder::default()
|
|
||||||
.db_store_id_exists(Arc::new(db.clone()))
|
|
||||||
.db_store_name_exists(Arc::new(db.clone()))
|
|
||||||
.get_uuid(Arc::new(crate::utils::uuid::GenerateUUID {}))
|
|
||||||
.build()
|
|
||||||
.unwrap(),
|
|
||||||
))
|
|
||||||
.add_category(mock_add_category_service(
|
|
||||||
IS_NEVER_CALLED,
|
|
||||||
AddCategoryCommand::new("foo".into(), None, UUID, UUID).unwrap(),
|
|
||||||
))
|
|
||||||
.add_product(mock_add_product_service(IS_NEVER_CALLED, get_command()))
|
|
||||||
.add_customization(mock_add_customization_service(
|
|
||||||
IS_NEVER_CALLED,
|
|
||||||
add_customization_command::tests::get_command(),
|
|
||||||
))
|
|
||||||
.update_product(mock_update_product_service(
|
|
||||||
IS_NEVER_CALLED,
|
|
||||||
update_product_command::tests::get_command(),
|
|
||||||
))
|
|
||||||
.update_customization(mock_update_customization_service(
|
|
||||||
IS_NEVER_CALLED,
|
|
||||||
get_update_customization_command(),
|
|
||||||
))
|
|
||||||
.update_category(mock_update_category_service(
|
|
||||||
IS_NEVER_CALLED,
|
|
||||||
get_update_category_command(),
|
|
||||||
))
|
|
||||||
.update_store(mock_update_store_service(
|
|
||||||
IS_NEVER_CALLED,
|
|
||||||
get_update_store_cmd(),
|
|
||||||
))
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let (cqrs, _store_query): (
|
let db2 = db.clone();
|
||||||
|
mock_services
|
||||||
|
.expect_add_store()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.returning(move || {
|
||||||
|
Arc::new(
|
||||||
|
AddStoreServiceBuilder::default()
|
||||||
|
.db_store_id_exists(Arc::new(db2.clone()))
|
||||||
|
.db_store_name_exists(Arc::new(db2.clone()))
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
let db2 = db.clone();
|
||||||
|
mock_services
|
||||||
|
.expect_update_store()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.returning(move || {
|
||||||
|
Arc::new(
|
||||||
|
UpdateStoreServiceBuilder::default()
|
||||||
|
.db_store_id_exists(Arc::new(db2.clone()))
|
||||||
|
.db_store_name_exists(Arc::new(db2.clone()))
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
let (cqrs, store_query): (
|
||||||
Arc<PostgresCqrs<Store>>,
|
Arc<PostgresCqrs<Store>>,
|
||||||
Arc<dyn ViewRepository<StoreView, Store>>,
|
Arc<dyn ViewRepository<StoreView, Store>>,
|
||||||
) = (
|
) = (
|
||||||
Arc::new(postgres_es::postgres_cqrs(
|
Arc::new(postgres_es::postgres_cqrs(
|
||||||
db.pool.clone(),
|
db.pool.clone(),
|
||||||
queries,
|
queries,
|
||||||
Arc::new(services),
|
Arc::new(mock_services),
|
||||||
)),
|
)),
|
||||||
Arc::new(db.clone()),
|
Arc::new(db.clone()),
|
||||||
);
|
);
|
||||||
|
|
||||||
let rand = crate::utils::random_string::GenerateRandomString {};
|
let rand = crate::utils::random_string::GenerateRandomString {};
|
||||||
let cmd = AddStoreCommand::new(rand.get_random(10), None, UUID).unwrap();
|
let cmd = AddStoreCommandBuilder::default()
|
||||||
cqrs.execute("", InventoryCommand::AddStore(cmd.clone()))
|
.name(rand.get_random(10))
|
||||||
.await
|
.address(None)
|
||||||
|
.owner(UUID)
|
||||||
|
.store_id(UUID)
|
||||||
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
cqrs.execute(
|
||||||
|
&cmd.store_id().to_string(),
|
||||||
|
InventoryCommand::AddStore(cmd.clone()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
settings.drop_db().await;
|
let store = store_query
|
||||||
|
.load(&(*cmd.store_id()).to_string())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let store: Store = store.into();
|
||||||
|
assert_eq!(store.name(), cmd.name());
|
||||||
|
assert_eq!(store.address(), cmd.address());
|
||||||
|
assert_eq!(store.owner(), cmd.owner());
|
||||||
|
assert_eq!(store.store_id(), cmd.store_id());
|
||||||
|
assert!(!store.deleted());
|
||||||
|
|
||||||
|
let update_store_cmd = UpdateStoreCommand::new(
|
||||||
|
rand.get_random(10),
|
||||||
|
Some(rand.get_random(10)),
|
||||||
|
UUID,
|
||||||
|
store,
|
||||||
|
UUID,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
cqrs.execute(
|
||||||
|
&cmd.store_id().to_string(),
|
||||||
|
InventoryCommand::UpdateStore(update_store_cmd.clone()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let store = store_query
|
||||||
|
.load(&(*cmd.store_id()).to_string())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let store: Store = store.into();
|
||||||
|
assert_eq!(store.name(), update_store_cmd.name());
|
||||||
|
assert_eq!(store.address(), update_store_cmd.address());
|
||||||
|
assert_eq!(store.owner(), update_store_cmd.owner());
|
||||||
|
assert_eq!(store.store_id(), update_store_cmd.old_store().store_id());
|
||||||
|
assert!(!store.deleted());
|
||||||
|
|
||||||
|
settings.drop_db().await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue