From ca7defe7248526e93763046b1de6890de58f3ed3 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sat, 21 Sep 2024 16:21:28 +0530 Subject: [PATCH] feat: inventory: product view tests --- ...767f7209a2443fd944420baf1e1cf5c51ccb.json} | 5 +- .../category_name_exists_for_store.rs | 2 +- .../output/db/postgres/product_view.rs | 176 ++++++++++++++++-- 3 files changed, 167 insertions(+), 16 deletions(-) rename .sqlx/{query-e2f9f291a20aac77851774ba8cd37325143a4d98e0980632f097c5885cc71094.json => query-c358d3b79d35668b3475f29f5bf6767f7209a2443fd944420baf1e1cf5c51ccb.json} (50%) diff --git a/.sqlx/query-e2f9f291a20aac77851774ba8cd37325143a4d98e0980632f097c5885cc71094.json b/.sqlx/query-c358d3b79d35668b3475f29f5bf6767f7209a2443fd944420baf1e1cf5c51ccb.json similarity index 50% rename from .sqlx/query-e2f9f291a20aac77851774ba8cd37325143a4d98e0980632f097c5885cc71094.json rename to .sqlx/query-c358d3b79d35668b3475f29f5bf6767f7209a2443fd944420baf1e1cf5c51ccb.json index d8268b4..779fd97 100644 --- a/.sqlx/query-e2f9f291a20aac77851774ba8cd37325143a4d98e0980632f097c5885cc71094.json +++ b/.sqlx/query-c358d3b79d35668b3475f29f5bf6767f7209a2443fd944420baf1e1cf5c51ccb.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "UPDATE\n cqrs_inventory_product_query\n SET\n version = $1,\n name = $2,\n description = $3,\n image = $4,\n product_id = $5,\n category_id = $6,\n price_major = $7,\n price_minor = $8,\n price_currency = $9,\n sku_able = $10,\n quantity_minor_unit = $11,\n quantity_minor_number = $12,\n quantity_major_unit = $13,\n quantity_major_number = $14,\n deleted = $15;", + "query": "UPDATE\n cqrs_inventory_product_query\n SET\n version = $1,\n name = $2,\n description = $3,\n image = $4,\n category_id = $5,\n price_major = $6,\n price_minor = $7,\n price_currency = $8,\n sku_able = $9,\n quantity_minor_unit = $10,\n quantity_minor_number = $11,\n quantity_major_unit = $12,\n quantity_major_number = $13,\n deleted = $14;", "describe": { "columns": [], "parameters": { @@ -10,7 +10,6 @@ "Text", "Text", "Uuid", - "Uuid", "Int4", "Int4", "Text", @@ -24,5 +23,5 @@ }, "nullable": [] }, - "hash": "e2f9f291a20aac77851774ba8cd37325143a4d98e0980632f097c5885cc71094" + "hash": "c358d3b79d35668b3475f29f5bf6767f7209a2443fd944420baf1e1cf5c51ccb" } diff --git a/src/inventory/adapters/output/db/postgres/category_name_exists_for_store.rs b/src/inventory/adapters/output/db/postgres/category_name_exists_for_store.rs index cc30580..bd032d8 100644 --- a/src/inventory/adapters/output/db/postgres/category_name_exists_for_store.rs +++ b/src/inventory/adapters/output/db/postgres/category_name_exists_for_store.rs @@ -49,7 +49,7 @@ pub mod tests { VALUES ($1, $2, $3, $4, $5, $6);", 1, c.name(), - c.description().as_ref().unwrap(), + c.description().as_ref().map(|s| s.as_str()), c.category_id(), c.store_id(), c.deleted().clone(), diff --git a/src/inventory/adapters/output/db/postgres/product_view.rs b/src/inventory/adapters/output/db/postgres/product_view.rs index 066df00..d3e3836 100644 --- a/src/inventory/adapters/output/db/postgres/product_view.rs +++ b/src/inventory/adapters/output/db/postgres/product_view.rs @@ -293,22 +293,20 @@ impl ViewRepository for InventoryDBPostgresAdapter { name = $2, description = $3, image = $4, - product_id = $5, - category_id = $6, - price_major = $7, - price_minor = $8, - price_currency = $9, - sku_able = $10, - quantity_minor_unit = $11, - quantity_minor_number = $12, - quantity_major_unit = $13, - quantity_major_number = $14, - deleted = $15;", + category_id = $5, + price_major = $6, + price_minor = $7, + price_currency = $8, + sku_able = $9, + quantity_minor_unit = $10, + quantity_minor_number = $11, + quantity_major_unit = $12, + quantity_major_number = $13, + deleted = $14;", version, view.name, view.description, view.image, - view.product_id, view.category_id, view.price_major, view.price_minor, @@ -349,3 +347,157 @@ impl Query for InventoryDBPostgresAdapter { self.update_view(view, view_context).await.unwrap(); } } + +#[cfg(test)] +mod tests { + use super::*; + + use postgres_es::PostgresCqrs; + + use crate::{ + db::migrate::*, + inventory::{ + application::{ + port::output::full_text_search::add_product_to_store::*, + services::{ + add_product_service::*, update_product_service::*, + MockInventoryServicesInterface, + }, + }, + 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, *}, + }, + }, + tests::bdd::*, + utils::{random_string::GenerateRandomStringInterface, uuid::tests::UUID}, + }; + use std::sync::Arc; + + #[actix_rt::test] + async fn pg_query_inventory_product_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 = InventoryDBPostgresAdapter::new(db.pool.clone()); + + let store = Store::default(); + crate::inventory::adapters::output::db::postgres::store_id_exists::tests::create_dummy_store_record(&store, &db).await; + let category = CategoryBuilder::default() + .name("fooooo_cat".into()) + .description(None) + .store_id(*store.store_id()) + .category_id(UUID) + .build() + .unwrap(); + crate::inventory::adapters::output::db::postgres::category_name_exists_for_store::tests::create_dummy_category_record(&category, &db).await; + + let queries: Vec>> = vec![Box::new(db.clone())]; + + let mut mock_services = MockInventoryServicesInterface::new(); + + let db2 = Arc::new(db.clone()); + mock_services + .expect_add_product() + .times(IS_CALLED_ONLY_ONCE.unwrap()) + .returning(move || { + Arc::new( + AddProductServiceBuilder::default() + .db_product_name_exists_for_category(db2.clone()) + .db_product_id_exists(db2.clone()) + .db_get_category(db2.clone()) + .db_category_id_exists(db2.clone()) + .fts_add_product(mock_add_product_to_store_fts_port(IGNORE_CALL_COUNT)) + .build() + .unwrap(), + ) + }); + + let db2 = Arc::new(db.clone()); + mock_services + .expect_update_product() + .times(IS_CALLED_ONLY_ONCE.unwrap()) + .returning(move || { + Arc::new( + UpdateProductServiceBuilder::default() + .db_category_id_exists(db2.clone()) + .db_product_name_exists_for_category(db2.clone()) + .db_product_id_exists(db2.clone()) + .build() + .unwrap(), + ) + }); + + let (cqrs, product_query): ( + Arc>, + Arc>, + ) = ( + 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 = get_command(); + cqrs.execute( + &cmd.product_id().to_string(), + InventoryCommand::AddProduct(cmd.clone()), + ) + .await + .unwrap(); + + let product = product_query + .load(&(*cmd.product_id()).to_string()) + .await + .unwrap() + .unwrap(); + let product: Product = product.into(); + assert_eq!(product.name(), cmd.name()); + assert_eq!(product.description(), cmd.description()); + assert_eq!(product.image(), cmd.image()); + assert_eq!(product.product_id(), cmd.product_id()); + assert_eq!(product.quantity(), cmd.quantity()); + assert_eq!(product.sku_able(), cmd.sku_able()); + assert_eq!(product.price(), cmd.price()); + assert!(!store.deleted()); + + let update_product_cmd = get_command_with_product(product.clone()); + cqrs.execute( + &cmd.product_id().to_string(), + InventoryCommand::UpdateProduct(update_product_cmd.clone()), + ) + .await + .unwrap(); + let product = product_query + .load(&(*cmd.product_id()).to_string()) + .await + .unwrap() + .unwrap(); + let product: Product = product.into(); + assert_eq!(product.name(), update_product_cmd.name()); + assert_eq!(product.description(), update_product_cmd.description()); + assert_eq!(product.image(), update_product_cmd.image()); + assert_eq!( + product.product_id(), + update_product_cmd.old_product().product_id() + ); + 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()); + + settings.drop_db().await; + } +}