food diary improvements
This commit is contained in:
+71
-1
@@ -12,7 +12,7 @@ from planner.models import UserSettings
|
||||
|
||||
from .services.diary import diary_context
|
||||
|
||||
from .models import FoodFavourite, FoodLogAttachment, FoodLogEntry, FoodPortion, FoodProduct, MealType, Unit
|
||||
from .models import FoodFavourite, FoodLogAttachment, FoodLogEntry, FoodPortion, FoodProduct, MealType, Unit, InventoryLocation, KitchenInventoryItem, InventoryConsumption
|
||||
|
||||
|
||||
class FoodFoundationTests(TestCase):
|
||||
@@ -205,6 +205,76 @@ class FoodFoundationTests(TestCase):
|
||||
self.assertContains(response, '0 kcal eaten')
|
||||
self.assertFalse(FoodLogEntry.objects.filter(pk=entry.pk).exists())
|
||||
|
||||
def test_inventory_portion_preserves_old_diary_and_retries(self):
|
||||
import uuid
|
||||
self.product.energy_kcal_100 = Decimal('100')
|
||||
self.product.save(update_fields=['energy_kcal_100'])
|
||||
historical = FoodLogEntry.objects.create(user=self.user, product=self.product,
|
||||
date=date(2026, 10, 3), meal=MealType.BREAKFAST,
|
||||
product_name_snapshot='Yoghurt', amount=Decimal('100'), unit='g',
|
||||
calories_kcal=Decimal('275'))
|
||||
location = InventoryLocation.objects.create(user=self.user, name='Fridge')
|
||||
package = KitchenInventoryItem.objects.create(user=self.user, product=self.product,
|
||||
location=location, original_amount=Decimal('360'), remaining_amount=Decimal('270'),
|
||||
inventory_unit='g', opened=True)
|
||||
portion = FoodPortion.objects.create(user=self.user, product=self.product,
|
||||
label='cup', amount=Decimal('90'), unit='g')
|
||||
self.client.force_login(self.user)
|
||||
payload = {'date': '2026-10-04', 'product': self.product.pk, 'meal': MealType.LUNCH,
|
||||
'portion': f'portion:{portion.pk}', 'unit': 'g', 'inventory_package': str(package.api_id),
|
||||
'submission_id': str(uuid.uuid4())}
|
||||
response = self.client.post(reverse('food:add'), payload)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
package.refresh_from_db()
|
||||
self.assertEqual(package.remaining_amount, Decimal('180'))
|
||||
self.assertEqual(InventoryConsumption.objects.filter(user=self.user).count(), 1)
|
||||
self.client.post(reverse('food:add'), payload)
|
||||
self.assertEqual(FoodLogEntry.objects.filter(user=self.user).count(), 2)
|
||||
package.refresh_from_db()
|
||||
self.assertEqual(package.remaining_amount, Decimal('180'))
|
||||
self.assertEqual(diary_context(self.user, date(2026, 10, 3))['totals']['calories_kcal'], Decimal('275'))
|
||||
historical.refresh_from_db()
|
||||
self.assertEqual(historical.calories_kcal, Decimal('275'))
|
||||
|
||||
def test_inventory_shortage_does_not_log_food(self):
|
||||
import uuid
|
||||
location = InventoryLocation.objects.create(user=self.user, name='Fridge')
|
||||
package = KitchenInventoryItem.objects.create(user=self.user, product=self.product,
|
||||
location=location, original_amount=Decimal('700'), remaining_amount=Decimal('60'),
|
||||
inventory_unit='g', opened=True)
|
||||
self.client.force_login(self.user)
|
||||
response = self.client.post(reverse('food:add'), {'date': '2026-10-04',
|
||||
'product': self.product.pk, 'meal': MealType.LUNCH, 'portion': 'custom',
|
||||
'amount': '100', 'unit': 'g', 'inventory_package': str(package.api_id),
|
||||
'submission_id': str(uuid.uuid4())})
|
||||
self.assertContains(response, 'Insufficient stock')
|
||||
self.assertFalse(FoodLogEntry.objects.filter(user=self.user).exists())
|
||||
package.refresh_from_db()
|
||||
self.assertEqual(package.remaining_amount, Decimal('60'))
|
||||
|
||||
def test_multiple_configured_pieces_use_exact_saved_weight(self):
|
||||
import uuid
|
||||
self.product.energy_kcal_100 = Decimal('200')
|
||||
self.product.save(update_fields=['energy_kcal_100'])
|
||||
piece = FoodPortion.objects.create(user=self.user, product=self.product,
|
||||
label='meatball', amount=Decimal('25'), unit='g')
|
||||
location = InventoryLocation.objects.create(user=self.user, name='Freezer')
|
||||
package = KitchenInventoryItem.objects.create(user=self.user, product=self.product,
|
||||
location=location, original_amount=Decimal('250'), remaining_amount=Decimal('125'),
|
||||
inventory_unit='g', opened=True)
|
||||
self.client.force_login(self.user)
|
||||
response = self.client.post(reverse('food:add'), {'date': '2026-10-04',
|
||||
'product': self.product.pk, 'meal': MealType.DINNER,
|
||||
'portion': f'portion:{piece.pk}', 'portion_count': '3', 'unit': 'g',
|
||||
'inventory_package': str(package.api_id), 'submission_id': str(uuid.uuid4())})
|
||||
self.assertEqual(response.status_code, 302)
|
||||
entry = FoodLogEntry.objects.get(user=self.user)
|
||||
self.assertEqual(entry.amount, Decimal('75'))
|
||||
self.assertEqual(entry.calories_kcal, Decimal('150'))
|
||||
self.assertEqual(entry.portion_label_snapshot, '3 × meatball')
|
||||
package.refresh_from_db()
|
||||
self.assertEqual(package.remaining_amount, Decimal('50'))
|
||||
|
||||
def test_cannot_edit_or_delete_another_users_entry(self):
|
||||
entry = FoodLogEntry.objects.create(user=self.other, date=date(2026, 10, 3), meal=MealType.LUNCH,
|
||||
product_name_snapshot='Private food', amount=Decimal('100'), unit=Unit.GRAM)
|
||||
|
||||
Reference in New Issue
Block a user