Files
Django_TasksApp/main/food/test_quantity.py
T

81 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from datetime import date
from decimal import Decimal
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.test import Client
from django.urls import reverse
from .models import FoodLogEntry
from .services.diary import diary_context
class FoodQuantityTests(TestCase):
def setUp(self):
self.user = get_user_model().objects.create_user(username='quantity-owner', password='password')
self.other = get_user_model().objects.create_user(username='quantity-other', password='password')
self.day = date(2026, 10, 3)
self.entry = FoodLogEntry.objects.create(user=self.user, date=self.day, meal='lunch',
product_name_snapshot='Soup', amount=Decimal('250'), unit='ml',
calories_kcal=Decimal('100'), protein_g=Decimal('4'), carbohydrates_g=Decimal('12'),
fat_g=Decimal('2'), fibre_g=None)
self.client.force_login(self.user)
def test_increment_preserves_snapshot_and_updates_day(self):
url = reverse('food:increment', args=[self.entry.pk])
for _ in range(2):
response = self.client.post(url, HTTP_HX_REQUEST='true')
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'hx-swap-oob="outerHTML"')
self.entry.refresh_from_db()
self.assertEqual(self.entry.quantity, Decimal('3.00'))
self.assertEqual(self.entry.calories_kcal, Decimal('100'))
self.assertEqual(diary_context(self.user, self.day)['totals']['calories_kcal'], Decimal('300'))
self.assertEqual(diary_context(self.user, date(2026, 10, 4))['totals']['calories_kcal'], 0)
self.assertEqual(diary_context(self.user, self.day)['totals']['fibre_g'], None)
def test_other_user_cannot_increment(self):
self.client.force_login(self.other)
self.assertEqual(self.client.post(reverse('food:increment', args=[self.entry.pk])).status_code, 404)
self.entry.refresh_from_db()
self.assertEqual(self.entry.quantity, Decimal('1.00'))
def test_fractional_quantity_and_csrf(self):
self.entry.quantity = Decimal('1.25')
self.entry.save(update_fields=['quantity'])
self.assertEqual(diary_context(self.user, self.day)['totals']['calories_kcal'], Decimal('125.00000'))
url = reverse('food:increment', args=[self.entry.pk])
csrf_client = Client(enforce_csrf_checks=True)
csrf_client.force_login(self.user)
self.assertEqual(csrf_client.post(url).status_code, 403)
self.entry.refresh_from_db()
self.assertEqual(self.entry.quantity, Decimal('1.25'))
def test_edit_quantity_and_decrement_floor(self):
edit_url = reverse('food:edit', args=[self.entry.pk])
self.assertContains(self.client.get(edit_url), 'Quantity (portions)')
response = self.client.post(edit_url, {'meal': 'lunch', 'amount': '250', 'unit': 'ml',
'quantity': '2.50'})
self.assertEqual(response.status_code, 302)
self.entry.refresh_from_db()
self.assertEqual(self.entry.quantity, Decimal('2.50'))
self.assertEqual(self.entry.calories_kcal, Decimal('100'))
url = reverse('food:decrement', args=[self.entry.pk])
response = self.client.post(url, HTTP_HX_REQUEST='true')
self.assertContains(response, '1.5×')
self.assertContains(response, 'Remove one portion of Soup')
self.entry.refresh_from_db()
self.assertEqual(self.entry.quantity, Decimal('1.50'))
response = self.client.post(url, HTTP_HX_REQUEST='true')
self.assertContains(response, '0.5×')
self.assertNotContains(response, 'Remove one portion of Soup')
self.assertEqual(self.client.post(url).status_code, 409)
self.entry.refresh_from_db()
self.assertEqual(self.entry.quantity, Decimal('0.50'))
def test_other_user_cannot_decrement(self):
self.entry.quantity = Decimal('2')
self.entry.save(update_fields=['quantity'])
self.client.force_login(self.other)
self.assertEqual(self.client.post(reverse('food:decrement', args=[self.entry.pk])).status_code, 404)