def test_create_recipe_with_other_users_tags(self): """Test creating recipe with other's user tags""" user2 = create_user('*****@*****.**', 'OtherPassword') tag1 = sample_tag(user=self.user, name='Beef') tag2 = sample_tag(user=user2, name='Mutton') payload = { 'title': 'Meat stew', 'tags': [tag1.id, tag2.id], 'time_minutes': 60, 'price': 100, } res = self.client.post(RECIPES_URL, payload) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn(str(tag2.id), res.content.decode())
def test_partial_update_recipe(self): """Test updating a recipe with patch""" recipe = sample_recipe(user=self.user) recipe.tags.add(sample_tag(user=self.user)) new_tag = sample_tag(user=self.user) payload = {'title': 'Title', 'tags': [new_tag.id]} url = detail_url(recipe.id) self.client.patch(url, payload) recipe.refresh_from_db() self.assertEqual(recipe.title, payload['title']) tags = recipe.tags.all() self.assertEqual(len(tags), 1) self.assertIn(new_tag, tags)
def test_create_recipe_with_tags(self): """Test creating recipe with tags""" tag1 = sample_tag(user=self.user, name='Vegan') tag2 = sample_tag(user=self.user, name='Dessert') payload = { 'title': 'Avocado lime cheesecake', 'tags': [tag1.id, tag2.id], 'time_minutes': 60, 'price': 20.00, } res = self.client.post(RECIPES_URL, payload) self.assertEqual(res.status_code, status.HTTP_201_CREATED) recipe = Recipe.objects.get(id=res.data['id']) tags = recipe.tags.all() self.assertEqual(tags.count(), 2) self.assertIn(tag1, tags) self.assertIn(tag2, tags)
def test_view_recipe_detail(self): """Test viewing a recipe detail""" tag = sample_tag(user=self.user) ingredient = sample_ingredient(user=self.user) recipe = sample_recipe(user=self.user, tags=[tag], ingredients=[ingredient]) url = detail_url(recipe.id) res = self.client.get(url) serializer = RecipeDetailSerializer(recipe) self.assertEqual(res.data, serializer.data)
def test_full_update_recipe(self): """Test updating a recipe with put""" recipe = sample_recipe(user=self.user) recipe.tags.add(sample_tag(user=self.user)) payload = {'title': 'Title', 'time_minutes': 25, 'price': 5.00} url = detail_url(recipe.id) self.client.put(url, payload) 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)