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 {
|
async fn test_product_variant(product_id: ProductId, t: &mut PgT<'_>) -> ProductVariant {
|
||||||
CreateProductVariant {
|
CreateProductVariant {
|
||||||
product_id,
|
product_id,
|
||||||
name: ProductName::new(format!("{}", Uuid::new_v4())),
|
name: ProductVariantName::new(format!("{}", Uuid::new_v4())),
|
||||||
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
||||||
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
||||||
price: Default::default(),
|
price: Default::default(),
|
||||||
|
@ -16,7 +16,7 @@ macro_rules! begin_t {
|
|||||||
match $db.pool().begin().await {
|
match $db.pool().begin().await {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
tracing::error!("{}", e);
|
tracing::error!("{}", e);
|
||||||
return Err(detailed_products::Error::InternalServerError);
|
return Err($err);
|
||||||
}
|
}
|
||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
}
|
}
|
||||||
|
@ -2,6 +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 crate::begin_t;
|
use crate::begin_t;
|
||||||
use crate::db::Database;
|
use crate::db::Database;
|
||||||
@ -14,25 +15,36 @@ pub async fn create_product(
|
|||||||
) -> create_product::Output {
|
) -> create_product::Output {
|
||||||
let mut t = begin_t!(db, Error::InternalServerError);
|
let mut t = begin_t!(db, Error::InternalServerError);
|
||||||
|
|
||||||
let res = inner_create_product(input, &mut t, _mqtt, _config).await;
|
match inner_create_product(input, &mut t, Some(_mqtt), Some(_config)).await {
|
||||||
|
Ok(res) => {
|
||||||
t.commit().await.ok();
|
if let Err(e) = t.commit().await {
|
||||||
|
tracing::error!("{}", e);
|
||||||
res
|
return Err(Error::InternalServerError);
|
||||||
|
}
|
||||||
|
return Ok(res);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("{}", e);
|
||||||
|
t.rollback().await.ok();
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn inner_create_product(
|
async fn inner_create_product(
|
||||||
input: create_product::Input,
|
input: create_product::Input,
|
||||||
t: &mut PgT<'_>,
|
t: &mut PgT<'_>,
|
||||||
_mqtt: AsyncClient,
|
_mqtt: Option<AsyncClient>,
|
||||||
_config: SharedAppConfig,
|
_config: Option<SharedAppConfig>,
|
||||||
) -> create_product::Output {
|
) -> create_product::Output {
|
||||||
|
use create_product::*;
|
||||||
|
|
||||||
let dbm = crate::db::CreateProduct {
|
let dbm = crate::db::CreateProduct {
|
||||||
name: input.product.name.clone(),
|
name: input.product.name.clone(),
|
||||||
category: input.product.category,
|
category: input.product.category,
|
||||||
deliver_days_flag: input.product.deliver_days_flag,
|
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,
|
Ok(product) => product,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
tracing::error!("{}", e);
|
tracing::error!("{}", e);
|
||||||
@ -42,12 +54,15 @@ async fn inner_create_product(
|
|||||||
|
|
||||||
let dbm = crate::db::CreateProductVariant {
|
let dbm = crate::db::CreateProductVariant {
|
||||||
product_id: product.id,
|
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,
|
short_description: input.product.short_description,
|
||||||
long_description: input.product.long_description,
|
long_description: input.product.long_description,
|
||||||
price: input.product.price,
|
price: input.product.price,
|
||||||
};
|
};
|
||||||
let variant = match dbm.run(t).await {
|
let variant = match dbm.run(&mut *t).await {
|
||||||
Ok(variant) => variant,
|
Ok(variant) => variant,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
tracing::warn!("{}", e);
|
tracing::warn!("{}", e);
|
||||||
@ -60,13 +75,31 @@ async fn inner_create_product(
|
|||||||
quantity: input.stock.quantity,
|
quantity: input.stock.quantity,
|
||||||
quantity_unit: input.stock.quantity_unit,
|
quantity_unit: input.stock.quantity_unit,
|
||||||
};
|
};
|
||||||
let stock = match dbm.run(t).await {
|
let stock = match dbm.run(&mut *t).await {
|
||||||
Ok(stock) => stock,
|
Ok(stock) => stock,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
tracing::warn!("{}", e);
|
tracing::warn!("{}", e);
|
||||||
return Err(Error::CreateVariantStock(variant.id));
|
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(
|
pub async fn update_product(
|
||||||
@ -86,3 +119,46 @@ pub async fn delete_product(
|
|||||||
) -> delete_product::Output {
|
) -> delete_product::Output {
|
||||||
todo!()
|
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 {
|
async fn test_product_variant(product_id: ProductId, t: &mut PgT<'_>) -> ProductVariant {
|
||||||
CreateProductVariant {
|
CreateProductVariant {
|
||||||
product_id,
|
product_id,
|
||||||
name: ProductName::new(format!("{}", Uuid::new_v4())),
|
name: ProductVariantName::new(format!("{}", Uuid::new_v4())),
|
||||||
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
||||||
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
||||||
price: Default::default(),
|
price: Default::default(),
|
||||||
|
@ -122,7 +122,7 @@ mod tests {
|
|||||||
async fn test_product_variant(product_id: ProductId, t: &mut PgT<'_>) -> ProductVariant {
|
async fn test_product_variant(product_id: ProductId, t: &mut PgT<'_>) -> ProductVariant {
|
||||||
CreateProductVariant {
|
CreateProductVariant {
|
||||||
product_id,
|
product_id,
|
||||||
name: ProductName::new(format!("{}", Uuid::new_v4())),
|
name: ProductVariantName::new(format!("{}", Uuid::new_v4())),
|
||||||
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
||||||
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
||||||
price: Default::default(),
|
price: Default::default(),
|
||||||
|
@ -16,7 +16,7 @@ pub type Result<T> = std::result::Result<T, Error>;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct CreateProductVariant {
|
pub struct CreateProductVariant {
|
||||||
pub product_id: ProductId,
|
pub product_id: ProductId,
|
||||||
pub name: ProductName,
|
pub name: ProductVariantName,
|
||||||
pub short_description: ProductShortDesc,
|
pub short_description: ProductShortDesc,
|
||||||
pub long_description: ProductLongDesc,
|
pub long_description: ProductLongDesc,
|
||||||
pub price: Price,
|
pub price: Price,
|
||||||
@ -136,7 +136,7 @@ mod tests {
|
|||||||
async fn test_product_variant(product_id: ProductId, t: &mut PgT<'_>) -> ProductVariant {
|
async fn test_product_variant(product_id: ProductId, t: &mut PgT<'_>) -> ProductVariant {
|
||||||
CreateProductVariant {
|
CreateProductVariant {
|
||||||
product_id,
|
product_id,
|
||||||
name: ProductName::new(format!("{}", Uuid::new_v4())),
|
name: ProductVariantName::new(format!("{}", Uuid::new_v4())),
|
||||||
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
||||||
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
||||||
price: Default::default(),
|
price: Default::default(),
|
||||||
@ -170,7 +170,7 @@ mod tests {
|
|||||||
let product = test_product(&mut t, None, None, None).await;
|
let product = test_product(&mut t, None, None, None).await;
|
||||||
let dbm = CreateProductVariant {
|
let dbm = CreateProductVariant {
|
||||||
product_id: product.id,
|
product_id: product.id,
|
||||||
name: ProductName::new(format!("{}", Uuid::new_v4())),
|
name: ProductVariantName::new(format!("{}", Uuid::new_v4())),
|
||||||
short_description: ProductShortDesc::new("aosdjajsodjaoisdjoajs"),
|
short_description: ProductShortDesc::new("aosdjajsodjaoisdjoajs"),
|
||||||
long_description: ProductLongDesc::new("jsa a98dh 9ahsd ha89shd 98aus 98asu "),
|
long_description: ProductLongDesc::new("jsa a98dh 9ahsd ha89shd 98aus 98asu "),
|
||||||
price: Default::default(),
|
price: Default::default(),
|
||||||
|
@ -214,7 +214,7 @@ mod tests {
|
|||||||
async fn test_product_variant(product_id: ProductId, t: &mut PgT<'_>) -> ProductVariant {
|
async fn test_product_variant(product_id: ProductId, t: &mut PgT<'_>) -> ProductVariant {
|
||||||
CreateProductVariant {
|
CreateProductVariant {
|
||||||
product_id,
|
product_id,
|
||||||
name: ProductName::new(format!("{}", Uuid::new_v4())),
|
name: ProductVariantName::new(format!("{}", Uuid::new_v4())),
|
||||||
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
short_description: ProductShortDesc::new(format!("{}", Uuid::new_v4())),
|
||||||
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
long_description: ProductLongDesc::new(format!("{}", Uuid::new_v4())),
|
||||||
price: Default::default(),
|
price: Default::default(),
|
||||||
|
Loading…
Reference in New Issue
Block a user