from datetime import date from decimal import Decimal from django.contrib.auth import get_user_model from django.core.exceptions import ValidationError from django.db import IntegrityError, transaction from django.test import TestCase from django.urls import reverse from .models import FoodFavourite, FoodLogEntry, FoodPortion, FoodProduct, MealType, Unit class FoodFoundationTests(TestCase): def setUp(self): self.user = get_user_model().objects.create_user(username='food-one', password='test-password') self.other = get_user_model().objects.create_user(username='food-two', password='test-password') self.product = FoodProduct.objects.create(owner=self.user, product_name='Yoghurt', barcode='123', base_unit=Unit.GRAM) def test_meals_are_fixed(self): self.assertEqual(list(MealType.values), ['breakfast', 'lunch', 'dinner', 'snacks']) def test_missing_nutrients_remain_null(self): self.product.refresh_from_db() self.assertIsNone(self.product.energy_kcal_100) self.assertIsNone(self.product.protein_100) def test_favourite_and_portion_uniqueness(self): FoodFavourite.objects.create(user=self.user, product=self.product) FoodPortion.objects.create(user=self.user, product=self.product, label='My bowl', amount=Decimal('300'), unit=Unit.GRAM) with self.assertRaises(IntegrityError), transaction.atomic(): FoodFavourite.objects.create(user=self.user, product=self.product) with self.assertRaises(IntegrityError), transaction.atomic(): FoodPortion.objects.create(user=self.user, product=self.product, label='My bowl', amount=Decimal('200'), unit=Unit.GRAM) def test_log_snapshot_survives_product_edit(self): entry = FoodLogEntry.objects.create(user=self.user, date=date(2026, 10, 3), meal=MealType.BREAKFAST, product=self.product, product_name_snapshot='Yoghurt', amount=Decimal('300'), unit=Unit.GRAM, calories_kcal=Decimal('174')) self.product.product_name = 'Renamed yoghurt' self.product.energy_kcal_100 = Decimal('100') self.product.save() entry.refresh_from_db() self.assertEqual(entry.product_name_snapshot, 'Yoghurt') self.assertEqual(entry.calories_kcal, Decimal('174')) def test_cross_user_product_reference_rejected(self): for item in ( FoodFavourite(user=self.other, product=self.product), FoodPortion(user=self.other, product=self.product, label='Bowl', amount=Decimal('100'), unit=Unit.GRAM), FoodLogEntry(user=self.other, product=self.product, date=date(2026, 10, 3), meal=MealType.LUNCH, product_name_snapshot='Yoghurt', amount=Decimal('100'), unit=Unit.GRAM), ): with self.assertRaises(ValidationError): item.full_clean() def test_food_route_requires_login(self): self.assertEqual(self.client.get(reverse('food:diary')).status_code, 302) self.client.force_login(self.user) self.assertEqual(self.client.get(reverse('food:diary')).status_code, 200) def test_food_goals_reject_negative_values(self): from planner.forms import FoodGoalForm form = FoodGoalForm(data={'daily_calorie_goal': '-1', 'carbohydrate_goal_g': '-1', 'protein_goal_g': '10', 'fat_goal_g': '0'}) self.assertFalse(form.is_valid())