import csv from datetime import date, time from decimal import Decimal from io import StringIO from unittest.mock import patch from django.contrib.auth import get_user_model from django.test import TestCase from django.urls import reverse from .models import FoodLogEntry, FoodProduct, MealType, Unit from .services.diary import diary_context from planner.models import UserSettings class FoodExportTests(TestCase): def setUp(self): self.user = get_user_model().objects.create_user(username='csv-user', password='test-password') self.other = get_user_model().objects.create_user(username='other-csv-user', password='test-password') self.client.force_login(self.user) self.product = FoodProduct.objects.create(owner=self.user, product_name='Changed later') def entry(self, user, day, name, **values): return FoodLogEntry.objects.create(user=user, date=day, time=time(8, 42), meal=MealType.BREAKFAST, product_name_snapshot=name, brand_snapshot='Lidl', barcode_snapshot='1234567890123', portion_label_snapshot='My bowl', amount=Decimal('300'), unit=Unit.GRAM, calories_kcal=Decimal('174'), carbohydrates_g=Decimal('12'), protein_g=Decimal('18'), fat_g=Decimal('6'), source=FoodProduct.Source.MANUAL, **values) def rows(self, period): response = self.client.get(reverse('food:export', args=[period])) self.assertEqual(response.status_code, 200) self.assertTrue(response.streaming) body = b''.join(response.streaming_content).decode('utf-8') return response, list(csv.DictReader(StringIO(body))) @patch('food.views.localdate_for_user', return_value=date(2026, 10, 3)) def test_week_is_monday_sunday_user_scoped_and_snapshot_based(self, today): self.entry(self.user, date(2026, 9, 28), 'Monday food') self.entry(self.user, date(2026, 10, 3), '=Snapshot food') self.entry(self.user, date(2026, 10, 4), 'Sunday food') self.entry(self.user, date(2026, 9, 27), 'Outside week') self.entry(self.other, date(2026, 10, 3), 'Private food') response, rows = self.rows('week') self.assertIn('food-2026-W40.csv', response['Content-Disposition']) self.assertEqual([row['date'] for row in rows], ['2026-09-28', '2026-10-03', '2026-10-04']) self.assertEqual(rows[1]['product_name'], "'=Snapshot food") self.assertEqual(rows[1]['portion_label'], 'My bowl') self.assertEqual(rows[1]['calories_kcal'], '174') self.assertEqual(rows[1]['fibre_g'], '') self.assertEqual(rows[1]['time'], '08:42:00') @patch('food.views.localdate_for_user', return_value=date(2026, 10, 3)) def test_month_includes_only_calendar_month(self, today): self.entry(self.user, date(2026, 9, 30), 'September') self.entry(self.user, date(2026, 10, 1), 'October first') self.entry(self.user, date(2026, 10, 31), 'October last') response, rows = self.rows('month') self.assertIn('food-2026-10.csv', response['Content-Disposition']) self.assertEqual([row['product_name'] for row in rows], ['October first', 'October last']) def test_export_requires_login_and_rejects_other_periods(self): self.assertEqual(self.client.get(reverse('food:export', args=['year'])).status_code, 404) self.client.logout() self.assertEqual(self.client.get(reverse('food:export', args=['week'])).status_code, 302) def test_diary_uses_bounded_queries_for_multiple_rows(self): UserSettings.objects.create(user=self.user) for index in range(8): FoodLogEntry.objects.create(user=self.user, date=date(2026, 10, 3), meal=MealType.LUNCH, product=self.product, product_name_snapshot=f'Food {index}', amount=100, unit=Unit.GRAM) with self.assertNumQueries(3): context = diary_context(self.user, date(2026, 10, 3)) self.assertEqual(len(context['meals'][1]['entries']), 8)