Skip to content

Commit

Permalink
Add tests for filtering recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlo-myskov committed Nov 13, 2023
1 parent df53d52 commit cca0bb9
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions app/recipe/tests/test_recipe_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,47 @@ def test_clear_recipe_ingredients(self):
self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertEqual(recipe.ingredients.count(), 0)

def test_filter_by_tags(self):
"""Test filtering recipes by tags."""
r1 = create_recipe(user=self.user, title='Thai Vegetable Curry')
r2 = create_recipe(user=self.user, title='Aubergine with Tahini')
tag1 = Tag.objects.create(user=self.user, name='Vegan')
tag2 = Tag.objects.create(user=self.user, name='Vegeterian')
r1.tags.add(tag1)
r2.tags.add(tag2)
r3 = create_recipe(user=self.user, title='Fish and chips')

params = {'tags': f'{tag1.id},{tag2.id}'}
res = self.client.get(RECIPES_URL, params)

s1 = RecipeSerializer(r1)
s2 = RecipeSerializer(r2)
s3 = RecipeSerializer(r3)
self.assertIn(s1.data, res.data)
self.assertIn(s2.data, res.data)
self.assertNotIn(s3.data, res.data)

def test_filter_by_ingredients(self):
"""Test filtering recipes by ingredients."""
r1 = create_recipe(user=self.user, title='Posh Beans on Toast')
r2 = create_recipe(user=self.user, title='Chicken Cacciatore')
in1 = Ingredient.objects.create(user=self.user, name='Feta Cheese')
in2 = Ingredient.objects.create(user=self.user, name='Chicken')
r1.ingredients.add(in1)
r2.ingredients.add(in2)
r3 = create_recipe(user=self.user, title='Red Lentil Dahl')

params = {'ingredients': f'{in1.id},{in2.id}'}
res = self.client.get(RECIPES_URL, params)

s1 = RecipeSerializer(r1)
s2 = RecipeSerializer(r2)
s3 = RecipeSerializer(r3)

self.assertIn(s1.data, res.data)
self.assertIn(s2.data, res.data)
self.assertNotIn(s3.data, res.data)


class ImageUploadTest(TestCase):
"""Tests for the image upload API."""
Expand Down

0 comments on commit cca0bb9

Please sign in to comment.