food diary improvements

This commit is contained in:
Joren de Graaf
2026-10-17 12:12:41 +02:00
parent 76c76bc281
commit e3b14d6823
9 changed files with 258 additions and 20 deletions
+27
View File
@@ -39,6 +39,33 @@ class FoodApiTests(TestCase):
data = json.dumps(data)
return getattr(self.client, method)(path, data=data, **args)
def test_atomic_food_log_and_inventory_keeps_historical_snapshot(self):
old = FoodLogEntry.objects.create(user=self.a, product=self.product_a,
date=timezone.localdate() - timedelta(days=1), meal='dinner',
product_name_snapshot='Pasta', amount=Decimal('100'), unit='g',
calories_kcal=Decimal('432'))
location = InventoryLocation.objects.create(user=self.a, name='Pantry')
package = KitchenInventoryItem.objects.create(user=self.a, product=self.product_a,
location=location, original_amount=Decimal('700'), remaining_amount=Decimal('600'),
inventory_unit='g', opened=True)
payload = {'meal': 'lunch', 'product_id': str(self.product_a.api_id),
'amount': '90', 'unit': 'g', 'inventory_package_id': str(package.api_id)}
path = '/api/v1/food/log/'
self.assertEqual(self.call('post', path, payload).status_code, 400)
first = self.call('post', path, payload, HTTP_IDEMPOTENCY_KEY='food-stock-1')
self.assertEqual(first.status_code, 201, first.content)
self.assertIn('inventory_consumption_id', first.json())
self.assertEqual(self.call('post', path, payload, HTTP_IDEMPOTENCY_KEY='food-stock-1').status_code, 200)
package.refresh_from_db()
self.assertEqual(package.remaining_amount, Decimal('510'))
self.assertEqual(InventoryConsumption.objects.filter(user=self.a).count(), 1)
self.assertEqual(FoodLogEntry.objects.filter(user=self.a).count(), 2)
shortage = self.call('post', path, {**payload, 'amount': '600'}, HTTP_IDEMPOTENCY_KEY='food-stock-2')
self.assertEqual(shortage.status_code, 409)
self.assertEqual(FoodLogEntry.objects.filter(user=self.a).count(), 2)
old.refresh_from_db()
self.assertEqual(old.calories_kcal, Decimal('432'))
def test_bulk_inventory_update_preview_partial_atomic_and_retry(self):
location = InventoryLocation.objects.create(user=self.a, name='Pantry')
items = [KitchenInventoryItem.objects.create(user=self.a, product=self.product_a, location=location)