import hashlib from django.contrib.auth import get_user_model from django.test import TestCase from .models import CanonicalIngredient, FoodLogEntry, FoodProduct, InventoryLocation, KitchenInventoryItem, NutritionSource, SuggestedIngredient from .nutrition import preview, commit, search class NutritionImportTests(TestCase): def setUp(self): self.user = get_user_model().objects.create_user(username='nutrition-owner') self.other = get_user_model().objects.create_user(username='nutrition-other') self.payload = {'source': {'programme': 'Body Support', 'document_name': 'shopping.pdf', 'sha256': hashlib.sha256(b'pdf').hexdigest()}, 'ingredients': [ {'row_key': 'p1-broccoli', 'original_label': 'Broccoli vers', 'canonical_name': 'broccoli', 'quantity': '250', 'unit': 'g', 'page': 1, 'preparation': 'raw', 'category': 'vegetable', 'subcategory': 'cruciferous', 'confidence': '0.9'}, {'row_key': 'p1-paprika', 'original_label': 'Paprikapoeder', 'canonical_name': 'paprika powder', 'quantity': '5', 'unit': 'g', 'page': 1, 'preparation': 'dry', 'category': 'spice', 'confidence': '0.9'}]} def test_preview_commit_and_repeat_are_read_only_to_stock_and_diary(self): result = preview(self.user, self.payload) self.assertEqual(SuggestedIngredient.objects.count(), 0) self.assertEqual(result['blocking'], []) committed = commit(self.user, self.payload, result['confirmation'], 'key-one') self.assertEqual(committed['created'], 2) self.assertTrue(commit(self.user, self.payload, result['confirmation'], 'key-one')['idempotent']) self.assertEqual(NutritionSource.objects.count(), 1) self.assertEqual(SuggestedIngredient.objects.count(), 2) self.assertEqual(KitchenInventoryItem.objects.count(), 0) self.assertEqual(FoodLogEntry.objects.count(), 0) self.assertEqual(search(self.user, category='vegetable', programme='Body Support')['count'], 1) self.assertEqual(search(self.other, category='vegetable')['count'], 0) def test_ambiguous_preparation_blocks_commit(self): self.payload['ingredients'][0]['preparation'] = 'unknown' result = preview(self.user, self.payload) self.assertTrue(result['blocking']) with self.assertRaises(ValueError): commit(self.user, self.payload, result['confirmation'], 'key-two') self.assertEqual(CanonicalIngredient.objects.count(), 0) def test_raw_and_cooked_are_distinct(self): result = preview(self.user, self.payload) commit(self.user, self.payload, result['confirmation'], 'key-one') self.payload['source']['sha256'] = hashlib.sha256(b'pdf2').hexdigest() self.payload['ingredients'] = [{**self.payload['ingredients'][0], 'preparation': 'cooked'}] result = preview(self.user, self.payload) commit(self.user, self.payload, result['confirmation'], 'key-two') self.assertEqual(CanonicalIngredient.objects.filter(name='broccoli').count(), 2) def test_attached_body_support_pdf_row_preview(self): # Page 1 of the card attachment boodschappenlijst-17923863104217.pdf. self.payload['source']['sha256'] = '7afcefff2e7661b324bbfcd717adfb75274e0ad8b76254b65f11a0d8982f7d0c' self.payload['source']['document_name'] = 'boodschappenlijst-17923863104217.pdf' self.payload['ingredients'] = [ {'row_key': 'page1-broccoli', 'original_label': 'Broccoli vers', 'canonical_name': 'broccoli', 'quantity': '250', 'unit': 'g', 'page': 1, 'preparation': 'raw', 'category': 'vegetable', 'subcategory': 'cruciferous'}, {'row_key': 'page1-pasta', 'original_label': 'Pasta volkoren gekookt', 'canonical_name': 'wholegrain pasta', 'quantity': '150', 'unit': 'g', 'page': 1, 'preparation': 'cooked', 'category': 'grain', 'subcategory': 'wholegrain pasta'}, ] result = preview(self.user, self.payload) self.assertEqual(result['blocking'], []) self.assertEqual([row['category'] for row in result['ingredients']], ['vegetable', 'grain']) self.assertEqual(result['ingredients'][1]['preparation'], 'cooked') self.assertEqual(SuggestedIngredient.objects.count(), 0) def test_inventory_filter_is_read_only_and_counted_before_pagination(self): result = preview(self.user, self.payload) commit(self.user, self.payload, result['confirmation'], 'stock-key') product = FoodProduct.objects.create(owner=self.user, product_name='broccoli') location = InventoryLocation.objects.create(user=self.user, name='Fridge') KitchenInventoryItem.objects.create(user=self.user, product=product, location=location) found = search(self.user, inventory_status='in_stock', limit=1) self.assertEqual(found['count'], 1) self.assertEqual(found['results'][0]['name'], 'broccoli') self.assertEqual(found['results'][0]['inventory']['status'], 'exact_name_candidate') self.assertEqual(search(self.user, inventory_status='not_found')['count'], 1) self.assertEqual(KitchenInventoryItem.objects.count(), 1)