aboutsummaryrefslogtreecommitdiffstats
path: root/crates/rocie-server/tests/products
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-10-08 11:58:49 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-10-08 11:58:49 +0200
commit9204e472e4f714c84237bca5ebe740080a589917 (patch)
treed17f31240b24fcb2c47eec21edf6a9f512d80123 /crates/rocie-server/tests/products
parentfeat(crates/rocie-server/unit-property): Init (diff)
downloadserver-9204e472e4f714c84237bca5ebe740080a589917.zip
test(crates/rocie-server/testenv/init): Automatically choose the port and wait for server start
This avoids issues regarding a race condition between server start and our start of requests and removes the requirement for specifying free ports in the test files.
Diffstat (limited to 'crates/rocie-server/tests/products')
-rw-r--r--crates/rocie-server/tests/products/barcode.rs163
-rw-r--r--crates/rocie-server/tests/products/mod.rs44
-rw-r--r--crates/rocie-server/tests/products/register.rs27
3 files changed, 199 insertions, 35 deletions
diff --git a/crates/rocie-server/tests/products/barcode.rs b/crates/rocie-server/tests/products/barcode.rs
index 480dcb9..8e1bf42 100644
--- a/crates/rocie-server/tests/products/barcode.rs
+++ b/crates/rocie-server/tests/products/barcode.rs
@@ -2,24 +2,28 @@ use rocie_client::{
apis::{
api_get_inventory_api::amount_by_id,
api_set_barcode_api::{buy_barcode, consume_barcode},
+ api_set_product_api::{associate_barcode, register_product},
+ api_set_unit_api::register_unit,
+ api_set_unit_property_api::register_unit_property,
},
- models::{BarcodeId, UnitAmount},
+ models::{Barcode, BarcodeId, ProductStub, UnitAmount, UnitPropertyStub, UnitStub},
};
use crate::{
+ _testenv::init::function_name,
products::create_associated_barcode,
testenv::{TestEnv, log::request},
};
#[tokio::test]
async fn test_barcode_buy() {
- let env = TestEnv::new(module_path!(), 8087);
+ let env = TestEnv::new(function_name!());
let barcode_id = 23;
let unit_value = 1;
let (unit_id, product_id) =
- create_associated_barcode(&env, "Liter", "Milk", unit_value, barcode_id).await;
+ create_associated_barcode(&env, "Liter", "Milk", "Volume", unit_value, barcode_id).await;
request!(env, buy_barcode(BarcodeId { value: barcode_id }));
@@ -32,13 +36,20 @@ async fn test_barcode_buy() {
#[tokio::test]
async fn test_barcode_consume() {
- let env = TestEnv::new(module_path!(), 8083);
+ let env = TestEnv::new(function_name!());
let barcode_id = BarcodeId { value: 23 };
let unit_value = 1;
- let (unit_id, product_id) =
- create_associated_barcode(&env, "Liter", "Milk", unit_value, barcode_id.value).await;
+ let (unit_id, product_id) = create_associated_barcode(
+ &env,
+ "Liter",
+ "Milk",
+ "Volume",
+ unit_value,
+ barcode_id.value,
+ )
+ .await;
request!(env, buy_barcode(barcode_id));
@@ -62,13 +73,20 @@ async fn test_barcode_consume() {
#[tokio::test]
async fn test_barcode_consume_error() {
- let env = TestEnv::new(module_path!(), 8084);
+ let env = TestEnv::new(function_name!());
let barcode_id = BarcodeId { value: 23 };
let unit_value = 1;
- let (unit_id, product_id) =
- create_associated_barcode(&env, "Liter", "Milk", unit_value, barcode_id.value).await;
+ let (unit_id, product_id) = create_associated_barcode(
+ &env,
+ "Liter",
+ "Milk",
+ "Volume",
+ unit_value,
+ barcode_id.value,
+ )
+ .await;
request!(env, buy_barcode(barcode_id));
@@ -94,13 +112,20 @@ async fn test_barcode_consume_error() {
#[tokio::test]
async fn test_barcode_consume_error_other() {
- let env = TestEnv::new(module_path!(), 8085);
+ let env = TestEnv::new(function_name!());
let barcode_id = BarcodeId { value: 23 };
let unit_value = 1;
- let (unit_id, product_id) =
- create_associated_barcode(&env, "Liter", "Milk", unit_value, barcode_id.value).await;
+ let (unit_id, product_id) = create_associated_barcode(
+ &env,
+ "Liter",
+ "Milk",
+ "Volume",
+ unit_value,
+ barcode_id.value,
+ )
+ .await;
request!(env, buy_barcode(barcode_id));
@@ -137,13 +162,20 @@ async fn test_barcode_consume_error_other() {
#[tokio::test]
async fn test_barcode_multiple_buy_and_consume() {
- let env = TestEnv::new(module_path!(), 8086);
+ let env = TestEnv::new(function_name!());
let barcode_id = BarcodeId { value: 23 };
let unit_value = 1;
- let (unit_id, product_id) =
- create_associated_barcode(&env, "Liter", "Milk", unit_value, barcode_id.value).await;
+ let (unit_id, product_id) = create_associated_barcode(
+ &env,
+ "Liter",
+ "Milk",
+ "Volume",
+ unit_value,
+ barcode_id.value,
+ )
+ .await;
request!(env, buy_barcode(barcode_id));
@@ -183,3 +215,104 @@ async fn test_barcode_multiple_buy_and_consume() {
assert_eq!(product_amount.amount.unit, unit_id);
assert_eq!(product_amount.amount.value, 0);
}
+
+#[tokio::test]
+async fn test_barcode_fill_up() {
+ let env = TestEnv::new(function_name!());
+
+ let milk_id = BarcodeId { value: 23 };
+ let bread_id = BarcodeId { value: 24 };
+ let nuts_id = BarcodeId { value: 25 };
+
+ let _ = create_associated_barcode(&env, "Liter", "Milk", "Volume", 2, milk_id.value).await;
+ let _ = create_associated_barcode(&env, "Kilogram", "Bread", "Mass", 2, bread_id.value).await;
+ let _ = create_associated_barcode(&env, "Piece", "Nut", "Quantity", 2, nuts_id.value).await;
+
+ request!(env, buy_barcode(milk_id));
+ request!(env, buy_barcode(bread_id));
+ request!(env, buy_barcode(nuts_id));
+}
+
+#[tokio::test]
+async fn test_barcode_associate_false_unit() {
+ let env = TestEnv::new(function_name!());
+
+ let unit_property_mass = request!(
+ env,
+ register_unit_property(UnitPropertyStub {
+ description: None,
+ name: "Mass".to_owned()
+ })
+ );
+ let unit_property_volume = request!(
+ env,
+ register_unit_property(UnitPropertyStub {
+ description: None,
+ name: "Volume".to_owned()
+ })
+ );
+
+ let product = request!(
+ env,
+ register_product(ProductStub {
+ description: None,
+ name: "Milk".to_owned(),
+ parent: None,
+ unit_property: unit_property_mass,
+ })
+ );
+
+ let unit_mass = request!(
+ env,
+ register_unit(UnitStub {
+ description: None,
+ full_name_plural: "Grams".to_owned(),
+ full_name_singular: "Gram".to_owned(),
+ short_name: "g".to_owned(),
+ unit_property: unit_property_mass
+ })
+ );
+ let unit_volume = request!(
+ env,
+ register_unit(UnitStub {
+ description: None,
+ full_name_plural: "Liters".to_owned(),
+ full_name_singular: "Liter".to_owned(),
+ short_name: "L".to_owned(),
+ unit_property: unit_property_volume
+ })
+ );
+
+ let barcode_id_1 = BarcodeId { value: 23 };
+ let barcode_id_2 = BarcodeId { value: 24 };
+ let value = 1;
+
+ request!(
+ env,
+ associate_barcode(
+ product,
+ Barcode {
+ amount: UnitAmount {
+ unit: unit_mass,
+ value,
+ },
+ id: barcode_id_1,
+ },
+ )
+ );
+
+ request!(@expect_error
+ "Should not work, as we registered the base product with unit_property_mass"
+ env,
+ associate_barcode(
+ product,
+ Barcode {
+ amount: UnitAmount {
+ unit: unit_volume,
+ value,
+ },
+ id: barcode_id_2,
+ },
+ )
+ );
+}
diff --git a/crates/rocie-server/tests/products/mod.rs b/crates/rocie-server/tests/products/mod.rs
index 2ae52fa..b39a07c 100644
--- a/crates/rocie-server/tests/products/mod.rs
+++ b/crates/rocie-server/tests/products/mod.rs
@@ -1,10 +1,13 @@
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,
+ api_set_unit_property_api::register_unit_property,
+ },
+ models::{
+ Barcode, BarcodeId, ProductId, ProductStub, UnitAmount, UnitId, UnitPropertyId,
+ UnitPropertyStub, UnitStub,
},
- models::{Barcode, BarcodeId, Product, ProductId, ProductStub, UnitAmount, UnitId, UnitStub},
};
use crate::testenv::{TestEnv, log::request};
@@ -12,17 +15,24 @@ use crate::testenv::{TestEnv, log::request};
mod barcode;
mod register;
-async fn create_product(env: &TestEnv, name: &str) -> ProductId {
- request!(
+async fn create_product(
+ env: &TestEnv,
+ unit_property: UnitPropertyId,
+ name: &str,
+) -> (ProductId, UnitPropertyId) {
+ let product_id = request!(
env,
register_product(ProductStub {
description: Some(None),
name: name.to_owned(),
parent: None,
- },)
- )
+ unit_property
+ })
+ );
+
+ (product_id, unit_property)
}
-async fn create_unit(env: &TestEnv, name: &str) -> UnitId {
+async fn create_unit(env: &TestEnv, name: &str, unit_property: UnitPropertyId) -> UnitId {
request!(
env,
register_unit(UnitStub {
@@ -30,7 +40,8 @@ async fn create_unit(env: &TestEnv, name: &str) -> UnitId {
full_name_plural: name.to_owned(),
full_name_singular: name.to_owned(),
short_name: name.to_owned(),
- },)
+ unit_property
+ })
)
}
@@ -38,11 +49,20 @@ async fn create_associated_barcode(
env: &TestEnv,
unit_name: &str,
product_name: &str,
+ unit_property_name: &str,
barcode_value: u32,
barcode_id: u32,
) -> (UnitId, ProductId) {
- let unit_id = create_unit(env, unit_name).await;
- let product_id = create_product(env, product_name).await;
+ let unit_property = request!(
+ env,
+ register_unit_property(UnitPropertyStub {
+ description: None,
+ name: unit_property_name.to_owned()
+ })
+ );
+
+ let (product_id, unit_property) = create_product(env, unit_property, product_name).await;
+ let unit_id = create_unit(env, unit_name, unit_property).await;
let barcode_id = BarcodeId { value: barcode_id };
request!(
@@ -61,7 +81,3 @@ async fn create_associated_barcode(
(unit_id, product_id)
}
-
-async fn get_product(env: &TestEnv, id: ProductId) -> Product {
- product_by_id(&env.config, id).await.unwrap()
-}
diff --git a/crates/rocie-server/tests/products/register.rs b/crates/rocie-server/tests/products/register.rs
index 15abec4..4284bd1 100644
--- a/crates/rocie-server/tests/products/register.rs
+++ b/crates/rocie-server/tests/products/register.rs
@@ -1,13 +1,27 @@
use rocie_client::{
- apis::{api_get_product_api::product_by_id, api_set_product_api::register_product},
- models::ProductStub,
+ apis::{
+ api_get_product_api::product_by_id, api_set_product_api::register_product,
+ api_set_unit_property_api::register_unit_property,
+ },
+ models::{ProductStub, UnitPropertyStub},
};
-use crate::testenv::{TestEnv, log::request};
+use crate::{
+ _testenv::init::function_name,
+ testenv::{TestEnv, log::request},
+};
#[tokio::test]
async fn test_product_register_roundtrip() {
- let env = TestEnv::new(module_path!(), 8081);
+ let env = TestEnv::new(function_name!());
+
+ let unit_property = request!(
+ env,
+ register_unit_property(UnitPropertyStub {
+ description: Some(Some("The total mass of a product".to_owned())),
+ name: "Mass".to_owned()
+ })
+ );
let id = request!(
env,
@@ -15,7 +29,8 @@ async fn test_product_register_roundtrip() {
description: Some(Some("A soy based alternative to milk".to_owned())),
name: "Soy drink".to_owned(),
parent: None,
- },)
+ unit_property,
+ })
);
let product = request!(env, product_by_id(id));
@@ -25,7 +40,7 @@ async fn test_product_register_roundtrip() {
product.description,
Some(Some("A soy based alternative to milk".to_owned()))
);
- assert_eq!(product.id, id,);
+ assert_eq!(product.id, id);
// assert_eq!(
// product.parent,
// None,