Fix tests

This commit is contained in:
Adrian Woźniak 2022-11-16 13:36:30 +01:00
parent 0d5e450656
commit 55342d2c6e
No known key found for this signature in database
GPG Key ID: 0012845A89C7352B
5 changed files with 56 additions and 12 deletions

View File

@ -79,7 +79,8 @@ SELECT shopping_cart_items.id as id,
FROM shopping_cart_items FROM shopping_cart_items
LEFT JOIN shopping_carts LEFT JOIN shopping_carts
ON shopping_carts.id = shopping_cart_id ON shopping_carts.id = shopping_cart_id
WHERE shopping_carts.buyer_id = $1 AND shopping_carts.id = $2 WHERE shopping_carts.buyer_id = $1 AND shopping_carts.id = $2
ORDER BY shopping_cart_items.id
"#, "#,
) )
.bind(msg.account_id) .bind(msg.account_id)
@ -93,8 +94,9 @@ SELECT shopping_cart_items.id as id,
shopping_cart_items.quantity_unit as quantity_unit shopping_cart_items.quantity_unit as quantity_unit
FROM shopping_cart_items FROM shopping_cart_items
LEFT JOIN shopping_carts LEFT JOIN shopping_carts
ON shopping_carts.id = shopping_cart_id ON shopping_carts.id = shopping_cart_id
WHERE shopping_carts.buyer_id = $1 WHERE shopping_carts.buyer_id = $1
ORDER BY shopping_cart_items.id
"#, "#,
) )
.bind(msg.account_id), .bind(msg.account_id),

View File

@ -32,6 +32,8 @@ pub enum Error {
CreateProduct, CreateProduct,
#[error("Failed to update product {0:?}")] #[error("Failed to update product {0:?}")]
UpdateProduct(ProductId), UpdateProduct(ProductId),
#[error("Failed to delete product {0:?}")]
DeleteProduct(ProductId),
#[error("Failed to create variant of product {0:?}")] #[error("Failed to create variant of product {0:?}")]
CreateProductVariant(ProductId), CreateProductVariant(ProductId),
#[error("Failed to create stock of variant {0:?}")] #[error("Failed to create stock of variant {0:?}")]

View File

@ -84,6 +84,7 @@ pub mod delete_product {
pub enum Topic { pub enum Topic {
ProductCreated, ProductCreated,
ProductUpdated, ProductUpdated,
ProductDeleted,
} }
impl Topic { impl Topic {
@ -91,6 +92,7 @@ impl Topic {
match self { match self {
Topic::ProductCreated => "product/created", Topic::ProductCreated => "product/created",
Topic::ProductUpdated => "product/updated", Topic::ProductUpdated => "product/updated",
Topic::ProductDeleted => "product/deleted",
} }
} }
} }
@ -106,6 +108,7 @@ impl DeserializePayload for Topic {
match self { match self {
Topic::ProductCreated => bincode::deserialize(bytes.as_ref()).ok(), Topic::ProductCreated => bincode::deserialize(bytes.as_ref()).ok(),
Topic::ProductUpdated => bincode::deserialize(bytes.as_ref()).ok(), Topic::ProductUpdated => bincode::deserialize(bytes.as_ref()).ok(),
Topic::ProductDeleted => bincode::deserialize(bytes.as_ref()).ok(),
} }
} }
} }
@ -120,4 +123,9 @@ impl AsyncClient {
self.publish_or_log(Topic::ProductUpdated, QoS::AtLeastOnce, true, product) self.publish_or_log(Topic::ProductUpdated, QoS::AtLeastOnce, true, product)
.await .await
} }
pub async fn emit_product_deleted(&self, product: &model::v2::ProductId) {
self.publish_or_log(Topic::ProductDeleted, QoS::AtLeastOnce, true, product)
.await
}
} }

View File

@ -2,7 +2,7 @@ use channels::stocks::{create_product, delete_product, update_product, Error};
use channels::AsyncClient; use channels::AsyncClient;
use config::SharedAppConfig; use config::SharedAppConfig;
use db_utils::PgT; use db_utils::PgT;
use model::v2::{DetailedProduct, DetailedProductVariant, ProductVariantName}; use model::v2::*;
use crate::begin_t; use crate::begin_t;
use crate::db::Database; use crate::db::Database;
@ -157,12 +157,44 @@ async fn inner_update_product(
} }
pub async fn delete_product( pub async fn delete_product(
_input: delete_product::Input, input: delete_product::Input,
_db: Database, db: Database,
_mqtt: AsyncClient, mqtt: AsyncClient,
_config: SharedAppConfig, _config: SharedAppConfig,
) -> delete_product::Output { ) -> delete_product::Output {
todo!() let mut t = begin_t!(db, Error::InternalServerError);
let product_id = input.product_id;
let res = inner_delete_product(input, &mut t).await;
match res {
Ok((product_id, _maybe_product)) => {
if let Err(e) = t.commit().await {
tracing::error!("{}", e);
return Err(Error::InternalServerError);
}
mqtt.emit_product_deleted(&product_id).await;
Ok(delete_product::Details { product_id })
}
Err(e) => {
tracing::error!("{}", e);
t.rollback().await.map_err(|e| {
tracing::error!("{}", e);
Error::InternalServerError
})?;
Err(Error::DeleteProduct(product_id))
}
}
}
pub async fn inner_delete_product(
input: delete_product::Input,
t: &mut PgT<'_>,
) -> crate::db::products::Result<(ProductId, Option<Product>)> {
let dbm = crate::db::DeleteProduct {
product_id: input.product_id,
};
dbm.run(t).await.map(|product| (input.product_id, product))
} }
#[cfg(test)] #[cfg(test)]

View File

@ -2,11 +2,11 @@ use config::SharedAppConfig;
use sqlx_core::pool::Pool; use sqlx_core::pool::Pool;
use sqlx_core::postgres::Postgres; use sqlx_core::postgres::Postgres;
mod photos; pub mod photos;
mod product_photos; pub mod product_photos;
mod product_variants; pub mod product_variants;
mod products; pub mod products;
mod stocks; pub mod stocks;
pub use photos::*; pub use photos::*;
pub use product_photos::*; pub use product_photos::*;