Add nested inventory package tracking
This commit is contained in:
+118
-1
@@ -1,10 +1,14 @@
|
||||
from unittest.mock import patch
|
||||
from decimal import Decimal
|
||||
import json
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase
|
||||
from django.urls import reverse
|
||||
|
||||
from .models import FoodProduct, InventoryLocation, KitchenInventoryItem
|
||||
from .api import create_token
|
||||
from .models import FoodProduct, InventoryLocation, KitchenInventoryItem, InventoryConsumption
|
||||
from .services.inventory_consumption import StockError, consume, preview, undo
|
||||
|
||||
|
||||
class KitchenInventoryTests(TestCase):
|
||||
@@ -68,3 +72,116 @@ class KitchenInventoryTests(TestCase):
|
||||
self.assertEqual(list(KitchenInventoryItem.objects.filter(product=self.product)
|
||||
.values_list('quantity', 'original_amount', 'remaining_amount', 'inventory_unit')),
|
||||
[(1, 6, 6, 'pieces')] * 3)
|
||||
|
||||
def test_opened_roll_pack_tracks_pieces_not_packs(self):
|
||||
response = self.client.post(reverse('food:inventory-add'), {
|
||||
'product': self.product.pk, 'quantity': 1, 'pieces_per_package': 12,
|
||||
'remaining_pieces': 10, 'location_name': 'Bread box',
|
||||
})
|
||||
self.assertEqual(response.status_code, 302, response.content[:500])
|
||||
item = KitchenInventoryItem.objects.get(user=self.user)
|
||||
self.assertEqual((item.quantity, item.original_amount, item.remaining_amount,
|
||||
item.inventory_unit, item.remaining_pieces, item.opened),
|
||||
(1, 12, 10, 'pieces', 10, True))
|
||||
page = self.client.get(reverse('food:inventory'))
|
||||
self.assertContains(page, '10 pieces')
|
||||
self.assertContains(page, '1 package')
|
||||
operation = consume(self.user, self.product, 2, 'pieces', package_id=item.api_id)
|
||||
item.refresh_from_db()
|
||||
self.assertEqual((item.remaining_pieces, item.remaining_amount), (8, 8))
|
||||
undo(self.user, operation.pk)
|
||||
item.refresh_from_db()
|
||||
self.assertEqual((item.remaining_pieces, item.remaining_amount), (10, 10))
|
||||
|
||||
def test_weighed_sausages_piece_and_mass_deductions(self):
|
||||
location = InventoryLocation.objects.create(user=self.user, name='Pantry')
|
||||
item = KitchenInventoryItem.objects.create(user=self.user, product=self.product,
|
||||
location=location, original_amount=Decimal('230'), remaining_amount=Decimal('230'),
|
||||
inventory_unit='g', pieces_per_package=10, remaining_pieces=10,
|
||||
piece_amount=Decimal('23'))
|
||||
self.assertEqual(preview(self.user, self.product, 6, 'pieces')['deductions'][0]['after'], 92)
|
||||
operation = consume(self.user, self.product, 6, 'pieces', package_id=item.api_id)
|
||||
item.refresh_from_db()
|
||||
self.assertEqual((item.quantity, item.remaining_pieces, item.remaining_amount, item.opened),
|
||||
(1, 4, Decimal('92'), True))
|
||||
self.assertEqual(operation.lines.get().amount, Decimal('138'))
|
||||
consume(self.user, self.product, 46, 'g', package_id=item.api_id)
|
||||
item.refresh_from_db()
|
||||
self.assertEqual((item.remaining_pieces, item.remaining_amount), (2, Decimal('46')))
|
||||
with self.assertRaisesMessage(StockError, 'whole pieces'):
|
||||
consume(self.user, self.product, 20, 'g', package_id=item.api_id)
|
||||
item.refresh_from_db()
|
||||
self.assertEqual((item.remaining_pieces, item.remaining_amount), (2, Decimal('46')))
|
||||
self.assertEqual(InventoryConsumption.objects.filter(user=self.user).count(), 2)
|
||||
|
||||
def test_api_converts_legacy_count_and_deducts_sausages(self):
|
||||
_, token = create_token(self.user, 'inventory test', {'inventory:read', 'inventory:write'})
|
||||
headers = {'HTTP_AUTHORIZATION': f'Bearer {token}'}
|
||||
location = InventoryLocation.objects.create(user=self.user, name='Pantry')
|
||||
legacy = KitchenInventoryItem.objects.create(user=self.user, product=self.product,
|
||||
location=location, quantity=10)
|
||||
url = f'/api/v1/kitchen/items/{legacy.api_id}/'
|
||||
missing_count = self.client.patch(url, data=json.dumps({'pieces_per_package': 12}),
|
||||
content_type='application/json', **headers)
|
||||
self.assertEqual(missing_count.status_code, 400)
|
||||
converted = self.client.patch(url, data=json.dumps({'quantity': 1,
|
||||
'pieces_per_package': 12, 'remaining_pieces': 10, 'opened': True}),
|
||||
content_type='application/json', **headers)
|
||||
self.assertEqual(converted.status_code, 200, converted.content[:500])
|
||||
self.assertEqual(converted.json()['remaining_pieces'], 10)
|
||||
self.assertEqual(KitchenInventoryItem.objects.filter(user=self.user).count(), 1)
|
||||
self.assertEqual(converted.json()['unit'], 'pieces')
|
||||
result = self.client.post('/api/v1/kitchen/consumption/',
|
||||
data=json.dumps({'product_id': str(self.product.api_id), 'amount': 2,
|
||||
'unit': 'pieces', 'package_id': str(legacy.api_id)}),
|
||||
content_type='application/json', HTTP_IDEMPOTENCY_KEY='rolls-2', **headers)
|
||||
self.assertEqual(result.status_code, 200, result.content[:500])
|
||||
legacy.refresh_from_db()
|
||||
self.assertEqual(legacy.remaining_pieces, 8)
|
||||
|
||||
def test_piece_conversion_requires_explicit_amount(self):
|
||||
location = InventoryLocation.objects.create(user=self.user, name='Pantry')
|
||||
item = KitchenInventoryItem.objects.create(user=self.user, product=self.product,
|
||||
location=location, original_amount=Decimal('230'), remaining_amount=Decimal('230'),
|
||||
inventory_unit='g', pieces_per_package=10, remaining_pieces=10)
|
||||
with self.assertRaisesMessage(StockError, 'explicit amount per piece'):
|
||||
preview(self.user, self.product, 6, 'pieces', package_id=item.api_id)
|
||||
|
||||
def test_api_corrects_one_legacy_can_without_creating_stock(self):
|
||||
_, token = create_token(self.user, 'can correction', {'inventory:read', 'inventory:write'})
|
||||
headers = {'HTTP_AUTHORIZATION': f'Bearer {token}'}
|
||||
location = InventoryLocation.objects.create(user=self.user, name='Pantry')
|
||||
can = KitchenInventoryItem.objects.create(user=self.user, product=self.product,
|
||||
location=location, quantity=1, notes='10 sausages; 230 g drained weight')
|
||||
other = KitchenInventoryItem.objects.create(user=self.other,
|
||||
product=FoodProduct.objects.create(owner=self.other, product_name='Other stock'),
|
||||
location=InventoryLocation.objects.create(user=self.other, name='Other pantry'), quantity=3)
|
||||
response = self.client.patch(f'/api/v1/kitchen/items/{can.api_id}/',
|
||||
data=json.dumps({'quantity': 1, 'original_amount': '230', 'unit': 'g',
|
||||
'pieces_per_package': 10, 'remaining_pieces': 4, 'piece_amount': '23',
|
||||
'opened': True}), content_type='application/json', **headers)
|
||||
self.assertEqual(response.status_code, 200, response.content[:500])
|
||||
can.refresh_from_db()
|
||||
other.refresh_from_db()
|
||||
self.assertEqual((can.remaining_amount, can.remaining_pieces, can.quantity),
|
||||
(Decimal('92'), 4, 1))
|
||||
self.assertEqual(other.quantity, 3)
|
||||
stock = self.client.get('/api/v1/kitchen/stock/',
|
||||
{'product_id': str(self.product.api_id)}, **headers).json()['products'][0]
|
||||
self.assertEqual(Decimal(stock['totals']['g']), Decimal('92'))
|
||||
self.assertEqual(stock['totals']['pieces'], '4')
|
||||
|
||||
def test_api_piece_update_rollback_on_invalid_metadata(self):
|
||||
_, token = create_token(self.user, 'atomic correction', {'inventory:read', 'inventory:write'})
|
||||
location = InventoryLocation.objects.create(user=self.user, name='Pantry')
|
||||
can = KitchenInventoryItem.objects.create(user=self.user, product=self.product,
|
||||
location=location, original_amount=Decimal('230'), remaining_amount=Decimal('230'),
|
||||
inventory_unit='g', pieces_per_package=10, remaining_pieces=10,
|
||||
piece_amount=Decimal('23'))
|
||||
response = self.client.patch(f'/api/v1/kitchen/items/{can.api_id}/',
|
||||
data=json.dumps({'remaining_pieces': 4, 'location': ''}),
|
||||
content_type='application/json', HTTP_AUTHORIZATION=f'Bearer {token}')
|
||||
self.assertEqual(response.status_code, 400)
|
||||
can.refresh_from_db()
|
||||
self.assertEqual((can.remaining_amount, can.remaining_pieces), (Decimal('230'), 10))
|
||||
self.assertFalse(InventoryConsumption.objects.filter(user=self.user).exists())
|
||||
|
||||
Reference in New Issue
Block a user