130 lines
8.0 KiB
Python
130 lines
8.0 KiB
Python
from datetime import date
|
|
from decimal import Decimal
|
|
|
|
from django.contrib.auth import get_user_model
|
|
from django.test import TestCase
|
|
from django.urls import reverse
|
|
|
|
from .models import FoodFavourite, FoodLogEntry, FoodPortion, FoodProduct, MealType, Unit
|
|
|
|
|
|
class RepeatLoggingTests(TestCase):
|
|
def setUp(self):
|
|
self.user = get_user_model().objects.create_user(username='repeat-one', password='test-password')
|
|
self.other = get_user_model().objects.create_user(username='repeat-two', password='test-password')
|
|
self.client.force_login(self.user)
|
|
self.product = FoodProduct.objects.create(owner=self.user, product_name='Greek yoghurt',
|
|
base_unit=Unit.GRAM, energy_kcal_100=Decimal('58'), protein_100=Decimal('6'))
|
|
|
|
def test_favourite_toggle_is_user_specific_and_htmx_updates(self):
|
|
url = reverse('food:toggle-favourite', args=[self.product.pk])
|
|
payload = {'date': '2026-10-03', 'meal': 'breakfast', 'tab': 'favourites', 'is_list': '1'}
|
|
response = self.client.post(url, payload, HTTP_HX_REQUEST='true')
|
|
self.assertContains(response, 'aria-pressed="true"')
|
|
self.assertTrue(FoodFavourite.objects.filter(user=self.user, product=self.product).exists())
|
|
self.assertFalse(FoodFavourite.objects.filter(user=self.other, product=self.product).exists())
|
|
response = self.client.post(url, payload, HTTP_HX_REQUEST='true')
|
|
self.assertEqual(response.content, b'')
|
|
self.client.force_login(self.other)
|
|
self.assertEqual(self.client.post(url, payload).status_code, 404)
|
|
|
|
def test_recent_products_are_unique_and_ordered_by_last_use(self):
|
|
other_product = FoodProduct.objects.create(owner=self.user, product_name='Soup')
|
|
for product in (self.product, other_product, self.product):
|
|
FoodLogEntry.objects.create(user=self.user, date=date(2026, 10, 3), meal=MealType.LUNCH,
|
|
product=product, product_name_snapshot=product.product_name, amount=100, unit=Unit.GRAM)
|
|
response = self.client.get(reverse('food:add'), {'tab': 'recent'})
|
|
self.assertEqual(response.status_code, 200)
|
|
html = response.content.decode()
|
|
self.assertEqual(html.count('<strong>Greek yoghurt</strong>'), 1)
|
|
self.assertLess(html.index('<strong>Greek yoghurt</strong>'), html.index('<strong>Soup</strong>'))
|
|
|
|
def test_custom_portion_calculation_and_snapshot(self):
|
|
portion = FoodPortion.objects.create(user=self.user, product=self.product,
|
|
label='My bowl', amount=Decimal('300'), unit=Unit.GRAM, is_default=True)
|
|
response = self.client.get(reverse('food:add'), {'product': self.product.pk, 'meal': 'breakfast'})
|
|
self.assertContains(response, 'My bowl')
|
|
self.assertContains(response, 'value="portion:%s"' % portion.pk)
|
|
response = self.client.post(reverse('food:add'), {'date': '2026-10-03', 'product': self.product.pk,
|
|
'meal': 'breakfast', 'amount': '100', 'unit': 'g', 'portion': f'portion:{portion.pk}'})
|
|
self.assertEqual(response.status_code, 302)
|
|
entry = FoodLogEntry.objects.get(user=self.user)
|
|
self.assertEqual(entry.amount, Decimal('300'))
|
|
self.assertEqual(entry.calories_kcal, Decimal('174'))
|
|
self.assertEqual(entry.protein_g, Decimal('18'))
|
|
self.assertEqual(entry.portion_label_snapshot, 'My bowl')
|
|
portion.label = 'Renamed bowl'
|
|
portion.save()
|
|
entry.refresh_from_db()
|
|
self.assertEqual(entry.portion_label_snapshot, 'My bowl')
|
|
|
|
def test_save_new_default_portion(self):
|
|
old = FoodPortion.objects.create(user=self.user, product=self.product,
|
|
label='Old bowl', amount=Decimal('200'), unit=Unit.GRAM, is_default=True)
|
|
response = self.client.post(reverse('food:add'), {'date': '2026-10-03', 'product': self.product.pk,
|
|
'meal': 'breakfast', 'amount': '250', 'unit': 'g', 'portion': 'custom',
|
|
'save_portion': 'on', 'new_portion_label': 'New bowl', 'make_default': 'on'})
|
|
self.assertEqual(response.status_code, 302)
|
|
old.refresh_from_db()
|
|
self.assertFalse(old.is_default)
|
|
new = FoodPortion.objects.get(user=self.user, product=self.product, label='New bowl')
|
|
self.assertTrue(new.is_default)
|
|
self.assertEqual(new.amount, Decimal('250'))
|
|
self.assertEqual(FoodLogEntry.objects.get(user=self.user).portion_label_snapshot, 'New bowl')
|
|
|
|
def test_manual_food_basis_and_optional_nutrients(self):
|
|
response = self.client.post(reverse('food:manual'), {'date': '2026-10-03', 'meal': 'dinner',
|
|
'product_name': 'Homemade soup', 'brand': '', 'barcode': '', 'basis_amount': '250',
|
|
'base_unit': 'ml', 'energy_kcal_100': '150', 'carbohydrates_100': '20',
|
|
'protein_100': '5', 'fat_100': '2'})
|
|
self.assertEqual(response.status_code, 302)
|
|
product = FoodProduct.objects.get(owner=self.user, product_name='Homemade soup')
|
|
self.assertEqual(product.source, FoodProduct.Source.MANUAL)
|
|
self.assertEqual(product.energy_kcal_100, Decimal('60'))
|
|
self.assertEqual(product.carbohydrates_100, Decimal('8'))
|
|
self.assertIsNone(product.fibre_100)
|
|
response = self.client.post(reverse('food:add'), {'date': '2026-10-03', 'product': product.pk,
|
|
'meal': 'dinner', 'amount': '250', 'unit': 'ml', 'portion': 'custom'})
|
|
self.assertEqual(response.status_code, 302)
|
|
entry = FoodLogEntry.objects.get(user=self.user)
|
|
self.assertEqual(entry.calories_kcal, Decimal('150'))
|
|
self.assertIsNone(entry.fibre_g)
|
|
|
|
def test_cannot_use_another_users_portion(self):
|
|
other_product = FoodProduct.objects.create(owner=self.other, product_name='Private')
|
|
other_portion = FoodPortion.objects.create(user=self.other, product=other_product,
|
|
label='Private bowl', amount=300, unit=Unit.GRAM)
|
|
response = self.client.post(reverse('food:add'), {'date': '2026-10-03', 'product': self.product.pk,
|
|
'meal': 'breakfast', 'amount': '100', 'unit': 'g', 'portion': f'portion:{other_portion.pk}'})
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertFalse(FoodLogEntry.objects.filter(user=self.user).exists())
|
|
|
|
def test_local_search_ranks_exact_and_favourites_first(self):
|
|
favourite = FoodProduct.objects.create(owner=self.user, product_name='Greek yoghurt vanilla')
|
|
ordinary = FoodProduct.objects.create(owner=self.user, product_name='Vanilla Greek yoghurt')
|
|
FoodFavourite.objects.create(user=self.user, product=favourite)
|
|
response = self.client.get(reverse('food:add'), {'q': 'Greek yoghurt'})
|
|
html = response.content.decode()
|
|
self.assertLess(html.index('<strong>Greek yoghurt</strong>'),
|
|
html.index('<strong>Greek yoghurt vanilla</strong>'))
|
|
self.assertLess(html.index('<strong>Greek yoghurt vanilla</strong>'),
|
|
html.index('<strong>Vanilla Greek yoghurt</strong>'))
|
|
|
|
def test_invalid_custom_portion_stays_on_form_with_htmx(self):
|
|
response = self.client.post(reverse('food:add'), {'date': '2026-10-03', 'product': self.product.pk,
|
|
'meal': 'breakfast', 'amount': '250', 'unit': 'g', 'portion': 'custom',
|
|
'save_portion': 'on'}, HTTP_HX_REQUEST='true')
|
|
self.assertContains(response, 'Enter a label for the saved portion')
|
|
self.assertContains(response, 'id="food-workspace"')
|
|
self.assertNotContains(response, '<!doctype html>')
|
|
self.assertNotIn('HX-Push-Url', response)
|
|
|
|
def test_add_and_edit_use_settings_switches(self):
|
|
add = self.client.get(reverse('food:add'), {'product': self.product.pk})
|
|
self.assertContains(add, 'settings-switch__track', count=2)
|
|
entry = FoodLogEntry.objects.create(user=self.user, date=date(2026, 10, 3),
|
|
meal=MealType.BREAKFAST, product=self.product, product_name_snapshot='Greek yoghurt',
|
|
amount=100, unit=Unit.GRAM)
|
|
edit = self.client.get(reverse('food:edit', args=[entry.pk]))
|
|
self.assertContains(edit, 'settings-switch__track', count=2)
|