Add cook later

This commit is contained in:
Joren de Graaf
2026-10-15 19:58:33 +02:00
parent b72283818c
commit f36f05b9bc
27 changed files with 523 additions and 129 deletions
+16 -34
View File
@@ -1,11 +1,10 @@
from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.core.management.base import CommandError
from django.db import IntegrityError, transaction
from django.test import TestCase, override_settings
from django.urls import reverse
from .models import Recipe, RecipeSelection
from .models import CookLater, Recipe, RecipeSelection
@override_settings(RECIPE_MATCH_DEMO_USERNAME='match-demo')
@@ -25,49 +24,38 @@ class RecipeMatchTests(TestCase):
self.own = Recipe.objects.create(owner=self.user, title='My pasta')
self.client.force_login(self.user)
def test_discovery_filters_and_existing_permissions(self):
def test_discovery_filters_and_permissions(self):
page = self.client.get(reverse('recipes:match'))
self.assertContains(page, 'Lemon salmon')
self.assertContains(page, 'My pasta')
self.assertNotContains(page, 'Private cake')
self.assertNotContains(page, 'Foreign meal')
self.assertNotContains(self.client.get(reverse('recipes:match'), {'time': '30', 'tag': 'Fish'}),
'My pasta')
self.assertNotContains(self.client.get(reverse('recipes:match'), {'time': '30', 'tag': 'Fish'}), 'My pasta')
self.assertContains(self.client.get(reverse('recipes:match'), {'q': 'Salmon'}), 'Lemon salmon')
self.assertEqual(self.client.get(reverse('recipes:detail', args=[self.recipe.api_id])).status_code, 404)
self.assertEqual(self.client.get(reverse('recipes:photo', args=[self.recipe.api_id])).status_code, 404)
self.assertEqual(self.client.get(reverse('recipes:match-photo', args=[self.private.api_id])).status_code, 404)
self.assertEqual(self.client.get(reverse('recipes:discover-detail', args=[self.recipe.api_id])).status_code, 200)
self.assertEqual(self.client.get(reverse('recipes:discover-detail', args=[self.private.api_id])).status_code, 404)
def test_select_is_idempotent_and_matches_server_side(self):
def test_save_is_idempotent_without_match_semantics(self):
RecipeSelection.objects.create(user=self.demo, recipe=self.recipe)
url = reverse('recipes:match-select', args=[self.recipe.api_id])
self.assertEqual(self.client.get(url).status_code, 405)
first = self.client.post(url)
self.assertTrue(first.json()['matched'])
self.assertTrue(self.client.post(url).json()['matched'])
self.assertEqual(RecipeSelection.objects.filter(user=self.user, recipe=self.recipe).count(), 1)
self.assertNotContains(self.client.get(reverse('recipes:match')), 'Lemon salmon')
self.assertFalse(self.client.post(reverse('recipes:match-select', args=[self.own.api_id])).json()['matched'])
self.assertTrue(self.client.post(url).json()['is_cook_later'])
self.assertFalse(self.client.post(url).json()['created'])
self.assertEqual(CookLater.objects.filter(user=self.user, recipe=self.recipe).count(), 1)
self.assertContains(self.client.get(reverse('recipes:match')), 'Lemon salmon')
self.assertContains(self.client.get(reverse('recipes:cook-later')), 'Lemon salmon')
self.assertEqual(self.client.post(reverse('recipes:match-select', args=[self.private.api_id])).status_code, 404)
self.assertEqual(self.client.post(reverse('recipes:match-select', args=[self.foreign.api_id])).status_code, 404)
with transaction.atomic(), self.assertRaises(IntegrityError):
RecipeSelection.objects.create(user=self.user, recipe=self.recipe)
def test_reset_restores_own_cards_and_preserves_other_picks(self):
RecipeSelection.objects.create(user=self.demo, recipe=self.recipe)
RecipeSelection.objects.create(user=self.other, recipe=self.recipe)
def test_old_reset_does_not_remove_cook_later(self):
RecipeSelection.objects.create(user=self.user, recipe=self.recipe)
url = reverse('recipes:match-reset')
self.assertEqual(self.client.get(url).status_code, 405)
self.assertNotContains(self.client.get(reverse('recipes:match')), 'Lemon salmon')
response = self.client.post(url)
self.assertRedirects(response, reverse('recipes:match'))
self.client.post(reverse('recipes:match-select', args=[self.recipe.api_id]))
self.client.post(reverse('recipes:match-reset'))
self.assertFalse(RecipeSelection.objects.filter(user=self.user).exists())
self.assertTrue(RecipeSelection.objects.filter(user=self.demo, recipe=self.recipe).exists())
self.assertTrue(RecipeSelection.objects.filter(user=self.other, recipe=self.recipe).exists())
self.assertContains(self.client.get(reverse('recipes:match')), 'Lemon salmon')
self.assertTrue(CookLater.objects.filter(user=self.user, recipe=self.recipe).exists())
def test_seed_and_reset_uses_existing_demo_recipes(self):
def test_seed_still_uses_existing_demo_recipes(self):
self.recipe.status = Recipe.Status.DRAFT
self.recipe.save(update_fields=('status',))
with self.assertRaises(CommandError):
@@ -76,9 +64,3 @@ class RecipeMatchTests(TestCase):
self.recipe.save(update_fields=('status',))
call_command('seed_recipe_match')
self.assertTrue(RecipeSelection.objects.filter(user=self.demo, recipe=self.recipe).exists())
RecipeSelection.objects.create(user=self.user, recipe=self.recipe)
call_command('seed_recipe_match', reset_user='visitor')
self.assertFalse(RecipeSelection.objects.filter(user=self.user).exists())
self.assertTrue(RecipeSelection.objects.filter(user=self.demo).exists())
call_command('seed_recipe_match', source_username='visitor', reset_demo=True)
self.assertTrue(RecipeSelection.objects.filter(user=self.demo, recipe=self.own).exists())