Esempio n. 1
0
    def test_list_recipes(self):
        """
        Test retrieving a list of recipes
        """
        sample_recipe(user=self.user)
        sample_recipe(user=self.user)

        res = self.client.get(RECIPES_URL)

        recipes = Recipe.objects.all().order_by('-id')
        serializer = RecipeSerializer(recipes, many=True)
        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(res.data, serializer.data)
Esempio n. 2
0
    def test_list_recipes_only_for_current_user(self):
        """
        Recipes returned must be for the current user
        """
        user2 = sample_user(email="*****@*****.**")
        sample_recipe(user=user2)
        recipe = sample_recipe(user=self.user)

        res = self.client.get(RECIPES_URL)

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(len(res.data), 1)
        self.assertEqual(res.data[0]['title'], recipe.title)
    def test_retrieve_ingredients_assigned_unique(self):
        """
        Test that resulting ingredient list does not contain repeated values
        """
        ingredient = sample_ingredient(user=self.user, name='Eggs')
        sample_ingredient(user=self.user, name='Cheese')
        recipe1 = sample_recipe(
            user=self.user,
            title='Eggs Benedict',
        )
        recipe2 = sample_recipe(
            user=self.user,
            title='Coriander Eggs on Toast',
        )
        recipe1.ingredients.add(ingredient)
        recipe2.ingredients.add(ingredient)

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

        self.assertEqual(len(res.data), 1)
Esempio n. 4
0
    def test_retrieve_tags_assigned_unique(self):
        """
        Test that resulting tag list does not contain repeated values
        """
        tag = sample_tag(user=self.user, name='Breakfast')
        sample_tag(user=self.user, name='Lunch')
        recipe1 = sample_recipe(
            user=self.user,
            title='Pancakes',
        )
        recipe2 = sample_recipe(
            user=self.user,
            title='Porridge',
        )
        recipe1.tags.add(tag)
        recipe2.tags.add(tag)

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

        self.assertEqual(len(res.data), 1)
Esempio n. 5
0
    def test_filter_recipes_by_ingredients(self):
        """
        Test returning recipes with specific ingredients
        """
        recipe1 = sample_recipe(user=self.user, title='Posh Beans on Toast')
        recipe2 = sample_recipe(user=self.user, title='Chicken Cacciatore')
        ing1 = sample_ingredient(user=self.user, name='Feta Cheese')
        ing2 = sample_ingredient(user=self.user, name='Chicken')
        recipe1.ingredients.add(ing1)
        recipe2.ingredients.add(ing2)
        recipe3 = sample_recipe(user=self.user, title='Steak and Mushrooms')

        res = self.client.get(
            RECIPES_URL,
            {'ingredients': f'{ing1.id},{ing2.id}'},
        )
        serializer1 = RecipeSerializer(recipe1)
        serializer2 = RecipeSerializer(recipe2)
        serializer3 = RecipeSerializer(recipe3)
        self.assertIn(serializer1.data, res.data)
        self.assertIn(serializer2.data, res.data)
        self.assertNotIn(serializer3.data, res.data)
Esempio n. 6
0
    def test_filter_recipes_by_tags(self):
        """
        Test returning recipes with specific tags
        """
        recipe1 = sample_recipe(user=self.user, title='That vegetable curry')
        recipe2 = sample_recipe(user=self.user, title='Aubergine with tahini')
        tag1 = sample_tag(user=self.user, name='Vegan')
        tag2 = sample_tag(user=self.user, name='Vegetarian')
        recipe1.tags.add(tag1)
        recipe2.tags.add(tag2)
        recipe3 = sample_recipe(user=self.user, title='Fish and Chips')

        res = self.client.get(
            RECIPES_URL,
            {'tags': f'{tag1.id},{tag2.id}'},
        )
        serializer1 = RecipeSerializer(recipe1)
        serializer2 = RecipeSerializer(recipe2)
        serializer3 = RecipeSerializer(recipe3)
        self.assertIn(serializer1.data, res.data)
        self.assertIn(serializer2.data, res.data)
        self.assertNotIn(serializer3.data, res.data)
Esempio n. 7
0
    def test_view_recipe_detail(self):
        """
        Test viewing a recipe detail
        """
        recipe = sample_recipe(user=self.user)
        recipe.tags.add(sample_tag(user=self.user))
        recipe.ingredients.add(sample_ingredient(user=self.user))

        url = DETAIL_URL(recipe.id)
        res = self.client.get(url)

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        serializer = RecipeDetailSerializer(recipe)
        self.assertEqual(res.data, serializer.data)
    def test_retrieve_ingredients_assigned_to_recipes(self):
        """
        Test filtering ingredients by those assigned to recipes
        """
        ingredient1 = sample_ingredient(user=self.user, name='Apples')
        ingredient2 = sample_ingredient(user=self.user, name='Turkey')
        recipe = sample_recipe(
            user=self.user,
            title='Apple Crumble',
        )
        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)
Esempio n. 9
0
    def test_retrieve_tags_assigned_to_recipes(self):
        """
        Test filtering tags by those assigned to recipes
        """
        tag1 = sample_tag(user=self.user, name='Breakfast')
        tag2 = sample_tag(user=self.user, name='Lunch')
        recipe = sample_recipe(
            user=self.user,
            title='Coriander Eggs on Toast',
        )
        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)
Esempio n. 10
0
    def test_partial_update_recipe(self):
        """
        Test updating recipe with PATCH
        """
        recipe = sample_recipe(user=self.user)
        recipe.tags.add(sample_tag(user=self.user))
        new_tag = sample_tag(user=self.user, name='Curry')

        payload = {
            'title': 'Spaghetti Carbonara',
            'tags': [new_tag.id],
        }
        res = self.client.patch(DETAIL_URL(recipe.id), payload)

        recipe.refresh_from_db()
        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(recipe.title, payload['title'])
        tags = recipe.tags.all()
        self.assertEqual(len(tags), 1)
        self.assertIn(new_tag, tags)
Esempio n. 11
0
    def test_full_update_recipe(self):
        """
        Test updating recipe with PUT
        """
        recipe = sample_recipe(user=self.user)
        recipe.ingredients.add(sample_ingredient(user=self.user))
        new_ing = sample_ingredient(user=self.user, name='Spaghetti')

        payload = {
            'title': 'Spaghetti with Mushroom Sauce',
            'ingredients': [new_ing.id],
            'time_minutes': 30,
            'price': '20.00'
        }
        res = self.client.put(DETAIL_URL(recipe.id), payload)

        recipe.refresh_from_db()
        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(recipe.title, payload['title'])
        ingredients = recipe.ingredients.all()
        self.assertEqual(len(ingredients), 1)
        self.assertIn(new_ing, ingredients)
Esempio n. 12
0
 def setUp(self):
     self.user = sample_user()
     self.client = APIClient()
     self.client.force_authenticate(self.user)
     self.recipe = sample_recipe(user=self.user)