Add more tests
This commit is contained in:
parent
0d7e72239b
commit
e038eec40e
@ -229,7 +229,7 @@ mod tests {
|
||||
async fn test_product_variant(product_id: ProductId, t: &mut PgT<'_>) -> ProductVariant {
|
||||
CreateProductVariant {
|
||||
product_id,
|
||||
name: ProductName::new(format!("{}", Uuid::new_v4())),
|
||||
name: ProductVariantName::new(format!("{}", Uuid::new_v4())),
|
||||
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
||||
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
||||
price: Default::default(),
|
||||
|
@ -16,7 +16,7 @@ macro_rules! begin_t {
|
||||
match $db.pool().begin().await {
|
||||
Err(e) => {
|
||||
tracing::error!("{}", e);
|
||||
return Err(detailed_products::Error::InternalServerError);
|
||||
return Err($err);
|
||||
}
|
||||
Ok(t) => t,
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ use channels::stocks::{create_product, delete_product, update_product, Error};
|
||||
use channels::AsyncClient;
|
||||
use config::SharedAppConfig;
|
||||
use db_utils::PgT;
|
||||
use model::v2::{DetailedProduct, DetailedProductVariant, ProductVariantName};
|
||||
|
||||
use crate::begin_t;
|
||||
use crate::db::Database;
|
||||
@ -14,25 +15,36 @@ pub async fn create_product(
|
||||
) -> create_product::Output {
|
||||
let mut t = begin_t!(db, Error::InternalServerError);
|
||||
|
||||
let res = inner_create_product(input, &mut t, _mqtt, _config).await;
|
||||
|
||||
t.commit().await.ok();
|
||||
|
||||
res
|
||||
match inner_create_product(input, &mut t, Some(_mqtt), Some(_config)).await {
|
||||
Ok(res) => {
|
||||
if let Err(e) = t.commit().await {
|
||||
tracing::error!("{}", e);
|
||||
return Err(Error::InternalServerError);
|
||||
}
|
||||
return Ok(res);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("{}", e);
|
||||
t.rollback().await.ok();
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn inner_create_product(
|
||||
input: create_product::Input,
|
||||
t: &mut PgT<'_>,
|
||||
_mqtt: AsyncClient,
|
||||
_config: SharedAppConfig,
|
||||
_mqtt: Option<AsyncClient>,
|
||||
_config: Option<SharedAppConfig>,
|
||||
) -> create_product::Output {
|
||||
use create_product::*;
|
||||
|
||||
let dbm = crate::db::CreateProduct {
|
||||
name: input.product.name.clone(),
|
||||
category: input.product.category,
|
||||
deliver_days_flag: input.product.deliver_days_flag,
|
||||
};
|
||||
let product = match dbm.run(t).await {
|
||||
let product = match dbm.run(&mut *t).await {
|
||||
Ok(product) => product,
|
||||
Err(e) => {
|
||||
tracing::error!("{}", e);
|
||||
@ -42,12 +54,15 @@ async fn inner_create_product(
|
||||
|
||||
let dbm = crate::db::CreateProductVariant {
|
||||
product_id: product.id,
|
||||
name: input.product.name,
|
||||
name: input
|
||||
.product
|
||||
.variant_name
|
||||
.unwrap_or_else(|| ProductVariantName::new(input.product.name.as_str())),
|
||||
short_description: input.product.short_description,
|
||||
long_description: input.product.long_description,
|
||||
price: input.product.price,
|
||||
};
|
||||
let variant = match dbm.run(t).await {
|
||||
let variant = match dbm.run(&mut *t).await {
|
||||
Ok(variant) => variant,
|
||||
Err(e) => {
|
||||
tracing::warn!("{}", e);
|
||||
@ -60,13 +75,31 @@ async fn inner_create_product(
|
||||
quantity: input.stock.quantity,
|
||||
quantity_unit: input.stock.quantity_unit,
|
||||
};
|
||||
let stock = match dbm.run(t).await {
|
||||
let stock = match dbm.run(&mut *t).await {
|
||||
Ok(stock) => stock,
|
||||
Err(e) => {
|
||||
tracing::warn!("{}", e);
|
||||
return Err(Error::CreateVariantStock(variant.id));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Details {
|
||||
product: DetailedProduct {
|
||||
id: product.id,
|
||||
name: product.name,
|
||||
category: product.category,
|
||||
deliver_days_flag: product.deliver_days_flag,
|
||||
variants: vec![DetailedProductVariant {
|
||||
id: variant.id,
|
||||
name: variant.name,
|
||||
short_description: variant.short_description,
|
||||
long_description: variant.long_description,
|
||||
price: variant.price,
|
||||
stocks: vec![stock],
|
||||
photos: vec![],
|
||||
}],
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn update_product(
|
||||
@ -86,3 +119,46 @@ pub async fn delete_product(
|
||||
) -> delete_product::Output {
|
||||
todo!()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use config::UpdateConfig;
|
||||
use model::{Days, ProductLongDesc, ProductName, ProductShortDesc, QuantityUnit};
|
||||
|
||||
use super::*;
|
||||
|
||||
pub struct NoOpts;
|
||||
|
||||
impl UpdateConfig for NoOpts {}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_product() {
|
||||
testx::db_t_ref!(t);
|
||||
|
||||
let res = inner_create_product(
|
||||
create_product::Input {
|
||||
product: create_product::ProductInput {
|
||||
name: ProductName::new(format!("{}", uuid::Uuid::new_v4())),
|
||||
variant_name: Some(ProductVariantName::new("variant")),
|
||||
short_description: ProductShortDesc::new("desc"),
|
||||
long_description: ProductLongDesc::new("long description"),
|
||||
category: None,
|
||||
price: Default::default(),
|
||||
deliver_days_flag: Days(vec![]),
|
||||
},
|
||||
stock: create_product::StockInput {
|
||||
quantity: Default::default(),
|
||||
quantity_unit: QuantityUnit::Gram,
|
||||
},
|
||||
},
|
||||
&mut t,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
|
||||
testx::db_rollback!(t);
|
||||
|
||||
res.unwrap();
|
||||
}
|
||||
}
|
||||
|
@ -180,7 +180,7 @@ mod tests {
|
||||
async fn test_product_variant(product_id: ProductId, t: &mut PgT<'_>) -> ProductVariant {
|
||||
CreateProductVariant {
|
||||
product_id,
|
||||
name: ProductName::new(format!("{}", Uuid::new_v4())),
|
||||
name: ProductVariantName::new(format!("{}", Uuid::new_v4())),
|
||||
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
||||
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
||||
price: Default::default(),
|
||||
|
@ -122,7 +122,7 @@ mod tests {
|
||||
async fn test_product_variant(product_id: ProductId, t: &mut PgT<'_>) -> ProductVariant {
|
||||
CreateProductVariant {
|
||||
product_id,
|
||||
name: ProductName::new(format!("{}", Uuid::new_v4())),
|
||||
name: ProductVariantName::new(format!("{}", Uuid::new_v4())),
|
||||
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
||||
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
||||
price: Default::default(),
|
||||
|
@ -16,7 +16,7 @@ pub type Result<T> = std::result::Result<T, Error>;
|
||||
#[derive(Debug)]
|
||||
pub struct CreateProductVariant {
|
||||
pub product_id: ProductId,
|
||||
pub name: ProductName,
|
||||
pub name: ProductVariantName,
|
||||
pub short_description: ProductShortDesc,
|
||||
pub long_description: ProductLongDesc,
|
||||
pub price: Price,
|
||||
@ -136,7 +136,7 @@ mod tests {
|
||||
async fn test_product_variant(product_id: ProductId, t: &mut PgT<'_>) -> ProductVariant {
|
||||
CreateProductVariant {
|
||||
product_id,
|
||||
name: ProductName::new(format!("{}", Uuid::new_v4())),
|
||||
name: ProductVariantName::new(format!("{}", Uuid::new_v4())),
|
||||
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
||||
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
||||
price: Default::default(),
|
||||
@ -170,7 +170,7 @@ mod tests {
|
||||
let product = test_product(&mut t, None, None, None).await;
|
||||
let dbm = CreateProductVariant {
|
||||
product_id: product.id,
|
||||
name: ProductName::new(format!("{}", Uuid::new_v4())),
|
||||
name: ProductVariantName::new(format!("{}", Uuid::new_v4())),
|
||||
short_description: ProductShortDesc::new("aosdjajsodjaoisdjoajs"),
|
||||
long_description: ProductLongDesc::new("jsa a98dh 9ahsd ha89shd 98aus 98asu "),
|
||||
price: Default::default(),
|
||||
|
@ -214,7 +214,7 @@ mod tests {
|
||||
async fn test_product_variant(product_id: ProductId, t: &mut PgT<'_>) -> ProductVariant {
|
||||
CreateProductVariant {
|
||||
product_id,
|
||||
name: ProductName::new(format!("{}", Uuid::new_v4())),
|
||||
name: ProductVariantName::new(format!("{}", Uuid::new_v4())),
|
||||
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
||||
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
||||
price: Default::default(),
|
||||
|
Loading…
Reference in New Issue
Block a user