Added inventory amount per food package management

This commit is contained in:
Joren de Graaf
2026-10-11 15:46:22 +02:00
parent 400e8254c0
commit cb12c23732
19 changed files with 927 additions and 35 deletions
+30 -2
View File
@@ -186,14 +186,42 @@ class FoodApiTests(TestCase):
self.assertEqual(len(candidates.json()['candidates']), 2)
self.assertEqual(FoodProduct.objects.filter(owner=self.a, product_name='Yoghurt').count(), 2)
product = FoodProduct.objects.filter(owner=self.a, brand='Store A').first()
invalid = self.call('post', '/api/v1/kitchen/items/', {'product_id': str(product.api_id),
created = self.call('post', '/api/v1/kitchen/items/', {'product_id': str(product.api_id),
'quantity': 2, 'location': 'Fridge', 'opened': True, 'remaining_amount': '350'})
self.assertEqual(invalid.status_code, 400)
self.assertEqual(created.status_code, 201)
self.assertEqual(KitchenInventoryItem.objects.filter(user=self.a, product=product).count(), 2)
valid = self.call('post', '/api/v1/kitchen/items/', {'product_id': str(product.api_id),
'quantity': 1, 'location': 'Fridge', 'opened': True, 'remaining_amount': '350'})
self.assertEqual(valid.status_code, 201, valid.content)
self.assertEqual(valid.json()['remaining_amount'], '350')
def test_inventory_stock_preview_consume_idempotency_undo_and_scopes(self):
product = FoodProduct.objects.create(owner=self.a, product_name='Stock butter',
package_amount=Decimal('250'), package_unit='g')
location = InventoryLocation.objects.create(user=self.a, name='Fridge')
for _ in range(2):
KitchenInventoryItem.objects.create(user=self.a, product=product, location=location,
original_amount=Decimal('250'), remaining_amount=Decimal('250'), inventory_unit='g')
base = '/api/v1/kitchen/'
stock = self.call('get', f'{base}stock/?product_id={product.api_id}').json()
self.assertEqual(stock['products'][0]['totals'], {'g': '500.000'})
payload = {'product_id': str(product.api_id), 'amount': '300', 'unit': 'g'}
preview = self.call('post', f'{base}consumption/preview/', payload).json()
self.assertEqual([row['amount'] for row in preview['deductions']], ['250.000', '50.000'])
self.assertEqual(self.call('post', f'{base}consumption/', payload).status_code, 400)
result = self.call('post', f'{base}consumption/', payload, HTTP_IDEMPOTENCY_KEY='stock-1')
self.assertEqual(result.status_code, 200, result.content)
again = self.call('post', f'{base}consumption/', payload, HTTP_IDEMPOTENCY_KEY='stock-1')
self.assertEqual(result.json(), again.json())
self.assertEqual(self.call('get', f'{base}stock/?product_id={product.api_id}').json()['products'][0]['totals'], {'g': '200.000'})
self.assertEqual(self.call('post', f'{base}consumption/{result.json()["transaction_id"]}/undo/', {}).status_code, 200)
self.assertEqual(self.call('get', f'{base}stock/?product_id={product.api_id}').json()['products'][0]['totals'], {'g': '500.000'})
self.assertEqual(self.call('get', f'{base}stock/?product_id={self.product_b.api_id}').status_code, 404)
_, read_secret = create_token(self.a, 'Inventory read', ['inventory:read'])
self.assertEqual(self.call('post', f'{base}consumption/preview/', payload, token=read_secret).status_code, 200)
self.assertEqual(self.call('post', f'{base}consumption/', payload, token=read_secret,
HTTP_IDEMPOTENCY_KEY='read-cannot-write').status_code, 403)
def test_barcode_mismatch_does_not_reuse_similar_product(self):
FoodProduct.objects.create(owner=self.a, product_name='Milk', brand='Example', barcode='11111',
package_amount=Decimal('1000'), package_unit='ml', package_quantity='1000 ml')