food diary bug fix, blocking calorie count

This commit is contained in:
Joren de Graaf
2026-10-18 00:03:47 +02:00
parent e3b14d6823
commit a8418cdba7
8 changed files with 150 additions and 41 deletions
+44
View File
@@ -172,6 +172,50 @@ class FoodFoundationTests(TestCase):
self.assertContains(response, 'date=2026-10-02')
self.assertContains(response, 'date=2026-10-04')
def test_goals_and_partial_nutrition_across_recent_dates(self):
UserSettings.objects.create(user=self.user, daily_calorie_goal=2050,
protein_goal_g=Decimal('132'), carbohydrate_goal_g=Decimal('250'),
fat_goal_g=Decimal('48'), fibre_goal_g=Decimal('40'))
complete = {'calories_kcal': Decimal('300'), 'protein_g': Decimal('20'),
'carbohydrates_g': Decimal('30'), 'fat_g': Decimal('10'),
'fibre_g': Decimal('5'), 'sugars_g': Decimal('4'), 'salt_g': Decimal('1')}
for day in (15, 16, 17):
FoodLogEntry.objects.create(user=self.user, date=date(2026, 10, day),
meal=MealType.LUNCH, product_name_snapshot='Known lunch',
amount=Decimal('100'), unit='g', **complete)
unknown = FoodLogEntry.objects.create(user=self.user, date=date(2026, 10, 16),
meal=MealType.DINNER, product_name_snapshot='Unknown meatballs',
amount=Decimal('160'), unit='g')
FoodLogEntry.objects.create(user=self.user, date=date(2026, 10, 17),
meal=MealType.DINNER, product_name_snapshot='Partial dinner',
amount=Decimal('100'), unit='g', protein_g=Decimal('8'), fibre_g=Decimal('2'))
before = list(FoodLogEntry.objects.filter(user=self.user).values_list('pk', 'calories_kcal'))
self.client.force_login(self.user)
for day in (15, 16, 17):
selected = date(2026, 10, day)
context = diary_context(self.user, selected)
self.assertEqual(context['calorie_goal'], 2050)
self.assertEqual(context['totals']['calories_kcal'], Decimal('300'))
self.assertEqual(context['totals']['protein_g'], Decimal('20') if day != 17 else Decimal('28'))
self.assertEqual(context['nutrition_incomplete']['calories_kcal'], day != 15)
self.assertEqual(context['calorie_remaining'], Decimal('1750') if day == 15 else None)
response = self.client.get(reverse('food:diary'), {'date': selected.isoformat()})
self.assertContains(response, 'Goal: 2050 kcal')
self.assertNotContains(response, 'No calorie goal')
self.assertContains(response, '132.0 g goal')
self.assertContains(response, '250.0 g goal')
self.assertContains(response, '48.0 g goal')
if day == 15:
self.assertContains(response, '1750 kcal remaining')
self.assertNotContains(response, 'Nutrition incomplete')
else:
self.assertContains(response, 'At least 300 kcal eaten')
self.assertContains(response, 'Nutrition incomplete')
self.assertContains(response, 'food entry to review')
self.assertContains(self.client.get(reverse('food:diary'), {'date': '2026-10-16'}),
f'href="#food-entry-{unknown.pk}"')
self.assertEqual(before, list(FoodLogEntry.objects.filter(user=self.user).values_list('pk', 'calories_kcal')))
def test_bad_date_and_user_isolation(self):
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,