diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-09-23 08:33:06 +0200 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-09-23 08:34:45 +0200 |
| commit | 2dc74d621399be454abbbff892fb46204ddc6e7b (patch) | |
| tree | f9525527fc09c465d4e2e4a4f665bfd444b889f8 /crates/rocie-server/tests/products | |
| parent | feat: Provide basic barcode handling support (diff) | |
| download | server-2dc74d621399be454abbbff892fb46204ddc6e7b.zip | |
feat(treewide): Add tests and barcode buying/consuming
Diffstat (limited to 'crates/rocie-server/tests/products')
| -rw-r--r-- | crates/rocie-server/tests/products/barcode.rs | 185 | ||||
| -rw-r--r-- | crates/rocie-server/tests/products/mod.rs | 69 | ||||
| -rw-r--r-- | crates/rocie-server/tests/products/register.rs | 33 |
3 files changed, 287 insertions, 0 deletions
diff --git a/crates/rocie-server/tests/products/barcode.rs b/crates/rocie-server/tests/products/barcode.rs new file mode 100644 index 0000000..c267006 --- /dev/null +++ b/crates/rocie-server/tests/products/barcode.rs @@ -0,0 +1,185 @@ +use rocie_client::{ + apis::{ + api_get_inventory_api::amount_by_id, + api_set_barcode_api::{buy_barcode, consume_barcode}, + }, + models::UnitAmount, +}; + +use crate::{ + products::create_associated_barcode, + testenv::{TestEnv, log::request}, +}; + +#[tokio::test] +async fn test_barcode_buy() { + let env = TestEnv::new(module_path!(), 8087); + + let barcode_id = 23; + let unit_value = 1; + + let (unit_id, product_id) = + create_associated_barcode(&env, "Liter", "Milk", unit_value, barcode_id).await; + + request!(env, buy_barcode(i32::try_from(barcode_id).unwrap())); + + let product_amount = request!(env, amount_by_id(product_id.to_string().as_str())); + + assert_eq!(product_amount.product_id, product_id); + assert_eq!(product_amount.amount.unit, unit_id); + assert_eq!(product_amount.amount.value, i64::from(unit_value)); +} + +#[tokio::test] +async fn test_barcode_consume() { + let env = TestEnv::new(module_path!(), 8083); + + let barcode_id = 23; + let unit_value = 1; + + let (unit_id, product_id) = + create_associated_barcode(&env, "Liter", "Milk", unit_value, barcode_id).await; + + request!(env, buy_barcode(i32::try_from(barcode_id).unwrap())); + + request!( + env, + consume_barcode( + i32::try_from(barcode_id).unwrap(), + UnitAmount { + unit: unit_id, + value: 1, + }, + ) + ); + + let product_amount = request!(env, amount_by_id(product_id.to_string().as_str())); + + assert_eq!(product_amount.product_id, product_id); + assert_eq!(product_amount.amount.unit, unit_id); + assert_eq!(product_amount.amount.value, 0); +} + +#[tokio::test] +async fn test_barcode_consume_error() { + let env = TestEnv::new(module_path!(), 8084); + + let barcode_id = 23; + let unit_value = 1; + + let (unit_id, product_id) = + create_associated_barcode(&env, "Liter", "Milk", unit_value, barcode_id).await; + + request!(env, buy_barcode(i32::try_from(barcode_id).unwrap())); + + request!( + @expect_error "We should not be able to consume more than available." + env, + consume_barcode( + i32::try_from(barcode_id).unwrap(), + UnitAmount { + unit: unit_id, + value: i64::from(unit_value + 1), + }, + ) + ); + + // Test, that the error does not actually go into the db. + let product_amount = request!(env, amount_by_id(product_id.to_string().as_str())); + + assert_eq!(product_amount.product_id, product_id); + assert_eq!(product_amount.amount.unit, unit_id); + assert_eq!(product_amount.amount.value, i64::from(unit_value)); +} + +#[tokio::test] +async fn test_barcode_consume_error_other() { + let env = TestEnv::new(module_path!(), 8085); + + let barcode_id = 23; + let unit_value = 1; + + let (unit_id, product_id) = + create_associated_barcode(&env, "Liter", "Milk", unit_value, barcode_id).await; + + request!(env, buy_barcode(i32::try_from(barcode_id).unwrap())); + + request!( + env, + consume_barcode( + i32::try_from(barcode_id).unwrap(), + UnitAmount { + unit: unit_id, + value: 1, + }, + ) + ); + + request!( + @expect_error "We already consumed everything, we bought" + env, + consume_barcode( + i32::try_from(barcode_id).unwrap(), + UnitAmount { + unit: unit_id, + value: 1, + }, + ) + ); + + // Test, that the error does not actually go into the db. + let product_amount = request!(env, amount_by_id(product_id.to_string().as_str())); + + assert_eq!(product_amount.product_id, product_id); + assert_eq!(product_amount.amount.unit, unit_id); + assert_eq!(product_amount.amount.value, i64::from(0)); +} + +#[tokio::test] +async fn test_barcode_multiple_buy_and_consume() { + let env = TestEnv::new(module_path!(), 8086); + + let barcode_id = 23; + let unit_value = 1; + + let (unit_id, product_id) = + create_associated_barcode(&env, "Liter", "Milk", unit_value, barcode_id).await; + + request!(env, buy_barcode(i32::try_from(barcode_id).unwrap())); + + request!(env, buy_barcode(i32::try_from(barcode_id).unwrap())); + + env.log("Bought both barcodes"); + + request!( + env, + consume_barcode( + i32::try_from(barcode_id).unwrap(), + UnitAmount { + unit: unit_id, + value: 1, + }, + ) + ); + + env.log("Consumed first barcode"); + + request!( + env, + consume_barcode( + i32::try_from(barcode_id).unwrap(), + UnitAmount { + unit: unit_id, + value: 1, + }, + ) + ); + + env.log("Consumed second barcode"); + + let product_amount = request!(env, amount_by_id(product_id.to_string().as_str())); + + assert_eq!(product_amount.product_id, product_id); + assert_eq!(product_amount.amount.unit, unit_id); + assert_eq!(product_amount.amount.value, i64::from(0)); +} diff --git a/crates/rocie-server/tests/products/mod.rs b/crates/rocie-server/tests/products/mod.rs new file mode 100644 index 0000000..e96ecc9 --- /dev/null +++ b/crates/rocie-server/tests/products/mod.rs @@ -0,0 +1,69 @@ +use rocie_client::{ + apis::{ + api_get_product_api::product_by_id, + api_set_product_api::{associate_barcode, register_product}, + api_set_unit_api::register_unit, + }, + models::{Barcode, Product, ProductStub, UnitAmount, UnitStub}, +}; +use uuid::Uuid; + +use crate::testenv::{TestEnv, log::request}; + +mod barcode; +mod register; + +async fn create_product(env: &TestEnv, name: &str) -> Uuid { + request!( + env, + register_product(ProductStub { + description: Some(None), + name: name.to_owned(), + parent: None, + },) + ) +} +async fn create_unit(env: &TestEnv, name: &str) -> Uuid { + request!( + env, + register_unit(UnitStub { + description: Some(None), + full_name_plural: name.to_owned(), + full_name_singular: name.to_owned(), + short_name: name.to_owned(), + },) + ) +} + +async fn create_associated_barcode( + env: &TestEnv, + unit_name: &str, + product_name: &str, + barcode_value: u32, + barcode_id: u32, +) -> (Uuid, Uuid) { + let unit_id = create_unit(env, unit_name).await; + let product_id = create_product(env, product_name).await; + + request!( + env, + associate_barcode( + product_id.to_string().as_str(), + Barcode { + amount: Box::new(UnitAmount { + unit: unit_id, + value: i64::from(barcode_value), + }), + id: i32::try_from(barcode_id).unwrap(), + }, + ) + ); + + (unit_id, product_id) +} + +async fn get_product(env: &TestEnv, id: Uuid) -> Product { + product_by_id(&env.config, id.to_string().as_str()) + .await + .unwrap() +} diff --git a/crates/rocie-server/tests/products/register.rs b/crates/rocie-server/tests/products/register.rs new file mode 100644 index 0000000..34f7b5c --- /dev/null +++ b/crates/rocie-server/tests/products/register.rs @@ -0,0 +1,33 @@ +use rocie_client::{ + apis::{api_get_product_api::product_by_id, api_set_product_api::register_product}, + models::ProductStub, +}; + +use crate::testenv::{TestEnv, log::request}; + +#[tokio::test] +async fn test_product_register_roundtrip() { + let env = TestEnv::new(module_path!(), 8081); + + let id = request!( + env, + register_product(ProductStub { + description: Some(Some("A soy based alternative to milk".to_owned())), + name: "Soy drink".to_owned(), + parent: None, + },) + ); + + let product = request!(env, product_by_id(id.to_string().as_str())); + + assert_eq!(&product.name, "Soy drink"); + assert_eq!( + product.description, + Some(Some("A soy based alternative to milk".to_owned())) + ); + assert_eq!(product.id, id,); + // assert_eq!( + // product.parent, + // None, + // ); +} |
