def test_partial_recipe_update_success(self): """Test patching Recipe is successful""" recipe = sample_recipe(user=self.user) recipe.tags.add(sample_tag(user=self.user)) tag = sample_tag(user=self.user, name="Local") payload = {"title": "Updated Recipe", "tags": [tag.id]} res = self.client.patch(recipe_detail_url(recipe.id), payload) recipe.refresh_from_db() tags = recipe.tags.all() self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(tags), 1) self.assertEqual(recipe.title, payload["title"])
def test_retrieve_tags_assigned_to_recipes(self): """Returns a list of tags that are assigned to recipes""" tag1 = sample_tag(user=self.user, name="Spicy") tag2 = sample_tag(user=self.user, name="Dinner") recipe = sample_recipe(user=self.user, title="Koshary") recipe.tags.add(tag1) res = self.client.get(TAGS_URL, {"assigned_only": 1}) serializer1 = TagSerializer(tag1) serializer2 = TagSerializer(tag2) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertIn(serializer1.data, res.data) self.assertNotIn(serializer2.data, res.data)
def test_create_recipe_with_tags_success(self): """Test creating a Recipe with some tags is successful""" tag1 = sample_tag(user=self.user, name="Local") tag2 = sample_tag(user=self.user, name="Fast") payload = { "title": "Koshary", "tags": [tag1.id, tag2.id], "time_minutes": 40, "price": 10, } res = self.client.post(RECIPE_URL, payload) recipe = Recipe.objects.get(id=res.data["id"]) tags = recipe.tags.all() self.assertEqual(res.status_code, status.HTTP_201_CREATED) self.assertIn(tag1, tags) self.assertIn(tag2, tags)
def test_filter_recipes_by_tag(self): """Returns recipes list with a specific tag""" recipe1 = sample_recipe(user=self.user, title="Koshary") recipe2 = sample_recipe(user=self.user, title="Bashamel") recipe3 = sample_recipe(user=self.user, title="Ta3meya") tag1 = sample_tag(user=self.user, name="Egyptian") tag2 = sample_tag(user=self.user, name="Well Done") recipe1.tags.add(tag1) recipe2.tags.add(tag2) res = self.client.get(RECIPE_URL, {"tags": f"{tag1.name},{tag2.name}"}) 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_retrieve_recipe_detail_success(self): """Test retrieving a Recipe detail is correct and successful""" recipe = sample_recipe(user=self.user) recipe.tags.add(sample_tag(user=self.user)) recipe.ingredients.add(sample_ingredient(user=self.user)) res = self.client.get(recipe_detail_url(recipe.id)) serializer = RecipeDetailSerializer(recipe) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def test_full_recipe_update_successful(self): """Test fully updating a Recipe is successful""" recipe = sample_recipe(user=self.user) recipe.tags.add(sample_tag(user=self.user)) payload = { "title": "Updated Recipe", "time_minutes": 10, "price": 20, } res = self.client.put(recipe_detail_url(recipe.id), payload) recipe.refresh_from_db() for key in payload.keys(): self.assertEqual(getattr(recipe, key), payload[key]) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(recipe.tags.all()), 0)