コード例 #1
0
    def test_retrive_recipes(self):
        """Test retrieving a list of recipes"""
        utils.create_recipe(self.user)
        utils.create_recipe(self.user)

        res = self.client.get(utils.RECIPES_URL)

        serializer = RecipeSerializer(utils.all_recipes(), many=True)

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(res.data, serializer.data)
コード例 #2
0
    def test_recipes_limited_to_user(self):
        """Test retrieving recipes for user"""
        utils.create_recipe(self.user)
        utils.create_recipe(self.user2)

        res = self.client.get(utils.RECIPES_URL)

        recipes = Recipe.objects.filter(user=self.user)
        serializer = RecipeSerializer(recipes, many=True)

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(len(res.data), 1)
        self.assertEqual(res.data, serializer.data)
コード例 #3
0
    def test_retrieve_ingredients_assigned_unique(self):
        """Test filtering ingredients by assigned returns unique items"""
        ingredient = utils.create_ingredent(self.user, "Appels")
        utils.create_ingredent(self.user, "Turkey")

        recipe1 = utils.create_recipe(self.user, **utils.RECIPE_PAYLOAD)
        recipe2 = utils.create_recipe(self.user, **utils.RECIPE_PAYLOAD_UPDATE)

        recipe1.ingredients.add(ingredient)
        recipe2.ingredients.add(ingredient)

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

        self.assertEqual(len(res.data), 1)
コード例 #4
0
    def test_retrieve_tags_assigned_unique(self):
        """Test filtering tags by assigned returns unique items"""
        tag = utils.create_tag(self.user, "Vegan")
        utils.create_tag(self.user, "Lunch")

        recipe1 = utils.create_recipe(self.user, **utils.RECIPE_PAYLOAD)
        recipe2 = utils.create_recipe(self.user, **utils.RECIPE_PAYLOAD_UPDATE)

        recipe1.tags.add(tag)
        recipe2.tags.add(tag)

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

        self.assertEqual(len(res.data), 1)
コード例 #5
0
    def test_view_recipe_detail(self):
        """Test viewing a recipe detial"""
        recipe = utils.create_recipe(self.user)
        recipe.tags.add(utils.create_tag(self.user, "Vegan"))
        recipe.ingredients.add(utils.create_ingredent(self.user, "Salt"))

        res = self.client.get(utils.recipe_detail_url(recipe.id))

        serializer = RecipeDetailSerializer(recipe)

        self.assertEqual(res.data, serializer.data)
コード例 #6
0
    def test_filter_recipes_by_ingredients(self):
        """Test returning recipes with specific ingredients"""
        recipe1 = utils.create_recipe(self.user)
        recipe2 = utils.create_recipe(self.user)
        recipe3 = utils.create_recipe(self.user)

        ingredient1 = utils.create_ingredent(self.user, "Salt")
        ingredient2 = utils.create_ingredent(self.user, "Pepper")

        recipe1.ingredients.add(ingredient1)
        recipe2.ingredients.add(ingredient2)

        res = self.client.get(
            utils.RECIPES_URL,
            {"ingredients": f"{ingredient1.id}, {ingredient2.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)
コード例 #7
0
    def test_filter_recipes_by_tags(self):
        """Test returning recipes with specific tags"""
        recipe1 = utils.create_recipe(self.user)
        recipe2 = utils.create_recipe(self.user)
        recipe3 = utils.create_recipe(self.user)

        tag1 = utils.create_tag(self.user, "Vegan")
        tag2 = utils.create_tag(self.user, "Vegetatian")

        recipe1.tags.add(tag1)
        recipe2.tags.add(tag2)

        res = self.client.get(
            utils.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)
コード例 #8
0
    def test_retrieve_ingredients_assigned_to_recipes(self):
        """Test filtering ingredients by those assigned to recipes"""
        ingredient1 = utils.create_ingredent(self.user, "Appels")
        ingredient2 = utils.create_ingredent(self.user, "Turkey")

        recipe = utils.create_recipe(self.user, **utils.RECIPE_PAYLOAD)
        recipe.ingredients.add(ingredient1)

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

        serializer1 = IngredientSerializer(ingredient1)
        serializer2 = IngredientSerializer(ingredient2)

        self.assertIn(serializer1.data, res.data)
        self.assertNotIn(serializer2.data, res.data)
コード例 #9
0
    def test_retrieve_tags_assigned_to_recipes(self):
        """Test filtering tags by those assigned to recipes"""
        tag1 = utils.create_tag(self.user, "Vegan")
        tag2 = utils.create_tag(self.user, "Lunch")

        recipe = utils.create_recipe(self.user)
        recipe.tags.add(tag1)

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

        serializer1 = TagSerializer(tag1)
        serializer2 = TagSerializer(tag2)

        self.assertIn(serializer1.data, res.data)
        self.assertNotIn(serializer2.data, res.data)
コード例 #10
0
    def test_full_update_recipe(self):
        """Test updating a recipe with put"""
        recipe = utils.create_recipe(self.user)
        recipe.tags.add(utils.create_tag(self.user, "Spicy"))
        url = utils.recipe_detail_url(recipe.id)
        payload = deepcopy(utils.RECIPE_PAYLOAD)
        res = self.client.put(url, payload)

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

        recipe.refresh_from_db()
        self.assertEqual(recipe.title, payload["title"])
        self.assertEqual(recipe.time_minutes, payload["time_minutes"])
        self.assertEqual(recipe.price, payload["price"])
        tags = recipe.tags.all()
        self.assertEqual(len(tags), 0)
コード例 #11
0
    def test_partial_update_recipe(self):
        """Test updating a recipe with patch"""
        recipe = utils.create_recipe(self.user)
        recipe.tags.add(utils.create_tag(self.user, "Spicy"))
        new_tag = utils.create_tag(self.user, "Curry")

        payload = deepcopy(utils.RECIPE_PAYLOAD)
        payload.update({"tags": [new_tag.id]})

        url = utils.recipe_detail_url(recipe.id)
        res = self.client.patch(url, payload)

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

        recipe.refresh_from_db()
        self.assertEqual(recipe.title, payload["title"])
        tags = recipe.tags.all()
        self.assertEqual(len(tags), 1)
        self.assertIn(new_tag, tags)
コード例 #12
0
 def setUp(self):
     self.client = APIClient()
     self.user = utils.create_user(**utils.USER_PAYLOAD)
     self.client.force_authenticate(self.user)
     self.recipe = utils.create_recipe(self.user)
コード例 #13
0
 def test_recipe_str(self):
     """Test the recipe string represention"""
     recipe = utils.create_recipe(self.user)
     self.assertEqual(str(recipe), recipe.title)