Test variants

This commit is contained in:
Adrian Woźniak 2022-11-10 16:57:48 +01:00
parent 6a4c38593f
commit 1d0ab456b9
No known key found for this signature in database
GPG Key ID: 0012845A89C7352B
5 changed files with 53 additions and 38 deletions

View File

@ -905,9 +905,9 @@ pub mod v2 {
use serde::{Deserialize, Serialize};
pub use crate::{
Days, FileName, LocalPath, PhotoId, Price, ProductCategory, ProductId, ProductLongDesc,
ProductName, ProductPhotoId, ProductShortDesc, Quantity, QuantityUnit, RecordId, StockId,
UniqueName,
Day, Days, FileName, LocalPath, PhotoId, Price, ProductCategory, ProductId,
ProductLongDesc, ProductName, ProductPhotoId, ProductShortDesc, Quantity, QuantityUnit,
RecordId, StockId, UniqueName,
};
#[cfg_attr(feature = "db", derive(sqlx::Type))]

View File

@ -8,7 +8,7 @@ pub enum Error {
#[error("Failed to fetch all photo")]
All,
#[error("Failed to fetch photos for products")]
PhotosForProducts,
PhotosForProductVariants,
}
pub type Result<T> = std::result::Result<T, Error>;
@ -20,7 +20,7 @@ pub struct AllPhotos {
}
impl AllPhotos {
pub async fn run(self, pool: &mut PgT<'_>) -> Result<Vec<Photo>> {
pub async fn run(self, t: &mut PgT<'_>) -> Result<Vec<Photo>> {
sqlx::query_as(
r#"
SELECT id, local_path, file_name, unique_name
@ -31,7 +31,7 @@ LIMIT $1 OFFSET $2
)
.bind(self.limit)
.bind(self.offset)
.fetch_all(pool)
.fetch_all(t)
.await
.map_err(|e| {
tracing::error!("{e:?}");
@ -75,42 +75,38 @@ RETURNING id, local_path, file_name, unique_name
//######################################
#[derive(Debug)]
pub struct PhotosForProducts {
pub product_ids: Vec<ProductId>,
pub struct PhotosForProductVariants {
pub product_variant_ids: Vec<ProductVariantId>,
}
impl PhotosForProducts {
pub async fn run(self, pool: &mut PgT<'_>) -> Result<Vec<model::ProductLinkedPhoto>> {
tracing::debug!("all product ids {:?}", self.product_ids);
let res: Vec<model::ProductLinkedPhoto> = db_utils::MultiLoad::new(
impl PhotosForProductVariants {
pub async fn run(self, pool: &mut PgT<'_>) -> Result<Vec<ProductLinkedPhoto>> {
tracing::debug!("all product ids {:?}", self.product_variant_ids);
let res: Vec<ProductLinkedPhoto> = db_utils::MultiLoad::new(
pool,
r#"
SELECT photos.id AS photo_id,
photos.local_path AS local_path,
photos.file_name AS file_name,
product_photos.product_id AS product_id,
product_photos.product_variant_id AS product_variant_id,
photos.unique_name AS unique_name
FROM photos
INNER JOIN product_photos
ON photos.id = product_photos.photo_id
INNER JOIN product_variants
ON product_variants.id = product_photos.product_variant_id
INNER JOIN products
ON product_variants.product_id = products.id
WHERE
"#,
" products.id =",
" product_photos.product_variant_id =",
)
.with_sorting("photos.id ASC")
.allow_over_max()
.with_size(1000)
.with_size(2000)
.load(
self.product_ids.len(),
self.product_ids.into_iter().map(|id| *id),
self.product_variant_ids.len(),
self.product_variant_ids.into_iter().map(|id| *id),
|e| {
tracing::error!("{}", e);
dbg!(e);
Error::PhotosForProducts
Error::PhotosForProductVariants
},
)
.await?;
@ -126,6 +122,8 @@ mod tests {
use model::Day;
use uuid::Uuid;
use crate::db::Database;
pub struct NoOpts;
impl UpdateConfig for NoOpts {}
@ -192,7 +190,7 @@ mod tests {
.unwrap()
}
#[actix::test]
#[tokio::test]
async fn create_photo() {
testx::db_t_ref!(t);
@ -201,7 +199,7 @@ mod tests {
testx::db_rollback!(t);
}
#[actix::test]
#[tokio::test]
async fn all() {
testx::db_t_ref!(t);
@ -209,13 +207,19 @@ mod tests {
let p2 = test_photo(&mut t, None, None, None).await;
let p3 = test_photo(&mut t, None, None, None).await;
let all = all_photos(AllPhotos, &mut t).await.unwrap();
let all = AllPhotos {
limit: 1000,
offset: 0,
}
.run(&mut t)
.await
.unwrap();
testx::db_rollback!(t);
assert_eq!(all, vec![p1, p2, p3]);
}
#[actix::test]
#[tokio::test]
async fn products_photos() {
testx::db_t_ref!(t);
@ -237,8 +241,8 @@ mod tests {
let p8 = test_product_photo(&mut t, product_variant_3.id).await;
let p9 = test_product_photo(&mut t, product_variant_3.id).await;
let mut all = PhotosForProducts {
product_ids: vec![product_1.id, product_3.id],
let mut all = PhotosForProductVariants {
product_variant_ids: vec![product_variant_1.id, product_variant_3.id],
}
.run(&mut t)
.await

View File

@ -100,6 +100,7 @@ mod tests {
use uuid::Uuid;
use crate::db::product_variants::CreateProductVariant;
use crate::db::Database;
pub struct NoOpts;
@ -157,7 +158,7 @@ mod tests {
.unwrap()
}
#[actix::test]
#[tokio::test]
async fn create_photo() {
testx::db_t_ref!(t);
@ -166,7 +167,7 @@ mod tests {
testx::db_rollback!(t);
}
#[actix::test]
#[tokio::test]
async fn delete() {
testx::db_t_ref!(t);
@ -182,7 +183,7 @@ mod tests {
assert_ne!(deleted, Some(p3));
}
#[actix::test]
#[tokio::test]
async fn create() {
testx::db_t_ref!(t);

View File

@ -265,8 +265,11 @@ WHERE
mod tests {
use config::UpdateConfig;
use model::v2::*;
use model::Day;
use uuid::Uuid;
use crate::db::Database;
pub struct NoOpts;
impl UpdateConfig for NoOpts {}
@ -310,7 +313,13 @@ mod tests {
let p2 = test_product(&mut t, None, None, None, None, None, None).await;
let p3 = test_product(&mut t, None, None, None, None, None, None).await;
let products = super::all(AllProducts, &mut t).await.unwrap();
let products = AllProducts {
limit: 10000,
offset: 0,
}
.run(&mut t)
.await
.unwrap();
testx::db_rollback!(t);
assert_eq!(products, vec![p1, p2, p3]);

View File

@ -189,9 +189,12 @@ mod tests {
use config::UpdateConfig;
use fake::faker::lorem::en as lorem;
use fake::Fake;
use model::*;
use model::v2::*;
use model::Day;
use uuid::Uuid;
use crate::db::Database;
pub struct NoOpts;
impl UpdateConfig for NoOpts {}
@ -292,12 +295,12 @@ mod tests {
.run(&mut t)
.await
.unwrap();
let reloaded = super::find_stock(FindStock { id: second.id }, &mut t).await;
let reloaded = FindStock { id: second.id }.run(&mut t).await;
testx::db_rollback!(t);
assert_eq!(deleted, Some(second));
assert_ne!(deleted, Some(first));
assert_eq!(reloaded, Err(crate::Error::Stock(super::Error::NotFound)));
assert_eq!(reloaded, Err(super::Error::NotFound));
}
#[tokio::test]
@ -317,9 +320,7 @@ mod tests {
.run(&mut t)
.await
.unwrap();
let reloaded = super::find_stock(FindStock { id: second.id }, &mut t)
.await
.unwrap();
let reloaded = FindStock { id: second.id }.run(&mut t).await.unwrap();
testx::db_rollback!(t);
assert_eq!(