def test_ingredients_limited_to_user(self): """Test that ingredients for the authenticated user are returned""" utils.create_ingredent(self.user2, "Vinegar") ingredient = utils.create_ingredent(self.user, "Tumeric") res = self.client.get(utils.INGREDIENTS_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data[0]["name"], ingredient.name)
def test_retrieve_ingredient_list(self): """Test retrieving a list of ingredients""" utils.create_ingredent(self.user, "Kale") utils.create_ingredent(self.user, "Salt") res = self.client.get(utils.INGREDIENTS_URL) serializer = IngredientSerializer(utils.all_ingredients(), many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
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)
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)
def test_create_recipe_with_ingredients(self): """Test creating a recipe with ingredients""" ingredient1 = utils.create_ingredent(self.user, "Prawns") ingredient2 = utils.create_ingredent(self.user, "Ginger") payload = deepcopy(utils.RECIPE_PAYLOAD) payload.update({"ingredients": [ingredient1.id, ingredient2.id]}) res = self.client.post(utils.RECIPES_URL, payload) self.assertEqual(res.status_code, status.HTTP_201_CREATED) recipe = Recipe.objects.get(id=res.data["id"]) ingredients = recipe.ingredients.all() self.assertEqual(ingredients.count(), 2) self.assertIn(ingredient1, ingredients) self.assertIn(ingredient2, ingredients)
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)
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)
def test_ingredient_str(self): """Test the ingredient string representation""" ingredient = utils.create_ingredent(self.user, "Cabbage") self.assertEqual(str(ingredient), ingredient.name)