Skip to content

Commit

Permalink
Added assigned_only filter feature
Browse files Browse the repository at this point in the history
  • Loading branch information
42zz committed Jul 2, 2021
1 parent c4365d5 commit d4e2efe
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
48 changes: 47 additions & 1 deletion app/recipe/tests/test_ingredients_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rest_framework import status
from rest_framework.test import APIClient

from core.models import Ingredient
from core.models import Ingredient, Recipe

from recipe.serializers import IngredientSerializer

Expand Down Expand Up @@ -82,3 +82,49 @@ def test_create_ingredient_invalid(self):
res = self.client.post(INGREDIENTS_URL, payload)

self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)

def test_retrieve_ingredients_assigned_to_recipes(self):
"""Test filtering ingredients by those assigned to recipes"""
ingredient1 = Ingredient.objects.create(
user=self.user, name='Apples'
)
ingredient2 = Ingredient.objects.create(
user=self.user, name='Turkey'
)
recipe = Recipe.objects.create(
title='Apple crumble',
time_minutes=5,
price=10,
user=self.user
)
recipe.ingredients.add(ingredient1)

res = self.client.get(INGREDIENTS_URL, {'assigned_only': 1})

serializer1 = IngredientSerializer(ingredient1)
serializer2 = IngredientSerializer(ingredient2)
self.assertIn(serializer1.data, res.data)
self.assertNotIn(serializer2.data, res.data)

def test_retrieve_ingredients_assigned_unique(self):
"""Test filtering ingredients by assigned returns unique items"""
ingredient = Ingredient.objects.create(user=self.user, name='Eggs')
Ingredient.objects.create(user=self.user, name='Cheese')
recipe1 = Recipe.objects.create(
title='Eggs benedict',
time_minutes=30,
price=12.00,
user=self.user
)
recipe1.ingredients.add(ingredient)
recipe2 = Recipe.objects.create(
title='Coriander eggs on toast',
time_minutes=20,
price=5.00,
user=self.user
)
recipe2.ingredients.add(ingredient)

res = self.client.get(INGREDIENTS_URL, {'assigned_only': 1})

self.assertEqual(len(res.data), 1)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rest_framework import status
from rest_framework.test import APIClient

from core.models import Tag
from core.models import Tag, Recipe

from recipe.serializers import TagSerializer

Expand Down Expand Up @@ -80,3 +80,45 @@ def test_create_tag_invalid(self):
res = self.client.post(TAGS_URL, payload)

self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)

def test_retrieve_tags_assigned_to_recipes(self):
"""Test filtering tags by those assigned to recipes"""
tag1 = Tag.objects.create(user=self.user, name='Breakfast')
tag2 = Tag.objects.create(user=self.user, name='Lunch')
recipe = Recipe.objects.create(
title='Coriander eggs on toast',
time_minutes=10,
price=5.00,
user=self.user
)
recipe.tags.add(tag1)

res = self.client.get(TAGS_URL, {'assigned_only': 1})

serializer1 = TagSerializer(tag1)
serializer2 = TagSerializer(tag2)
self.assertIn(serializer1.data, res.data)
self.assertNotIn(serializer2.data, res.data)

def test_retrieve_tags_assigned_unique(self):
"""Test filtering tags by assined returns unique items"""
tag = Tag.objects.create(user=self.user, name='Breakfast')
Tag.objects.create(user=self.user, name='Lunch')
recipe1 = Recipe.objects.create(
title='Pancakes',
time_minutes=5,
price=3.00,
user=self.user
)
recipe1.tags.add(tag)
recipe2 = Recipe.objects.create(
title='Porridge',
time_minutes=3,
price=2.00,
user=self.user
)
recipe2.tags.add(tag)

res = self.client.get(TAGS_URL, {'assigned_only': 1})

self.assertEqual(len(res.data), 1)
11 changes: 10 additions & 1 deletion app/recipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ class BaseRecipeAttrViewSet(viewsets.GenericViewSet,

def get_queryset(self):
"""Return objects for the current authenticated user only"""
return self.queryset.filter(user=self.request.user).order_by('-name')
assigned_only = bool(
int(self.request.query_params.get('assigned_only', 0))
)
queryset = self.queryset
if assigned_only:
queryset = queryset.filter(recipe__isnull=False)

return queryset.filter(
user=self.request.user
).order_by('-name').distinct()

def perform_create(self, serializer):
"""Create a new object"""
Expand Down

0 comments on commit d4e2efe

Please sign in to comment.