def test_retrieve_ingredient_list(self): """Test retrieving a list of ingredients""" sample_ingredient(user=self.user, name='Sugar') sample_ingredient(user=self.user, name='Salt') resp = self.client.get(INGREDIENTS_URL) ingredients = Ingredient.objects.all() serializer = IngredientSerializer(ingredients, many=True) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(resp.data, serializer.data)
def test_ingredients_limited_to_user(self): """Test that only ingredients for the authenticated user are returned""" user2 = sample_user(email='*****@*****.**') sample_ingredient(user=user2, name='Vinegar') ingredient = sample_ingredient(user=self.user, name='Tumeric') resp = self.client.get(INGREDIENTS_URL) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(len(resp.data), 1) self.assertEqual(resp.data[0]['name'], ingredient.name)
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) resp = self.client.get(url) serializer = RecipeDetailSerializer(recipe) self.assertEqual(resp.data, serializer.data)
def test_create_recipe_with_ingredients(self): """Test creating recipe with ingredients""" ingredient1 = sample_ingredient(user=self.user, name='Prawns') ingredient2 = sample_ingredient(user=self.user, name='Ginger') payload = { 'title': 'thai prawn red carry', "ingredients": [ingredient1.id, ingredient2.id], "time_minutes": 20, 'price': 7.00 } resp = self.client.post(RECIPES_URL, payload) self.assertEqual(resp.status_code, status.HTTP_201_CREATED) recipe = Recipe.objects.get(id=resp.data['id']) ingredients = recipe.ingredients.all() self.assertEqual(ingredients.count(), 2) self.assertIn(ingredient1, ingredients) self.assertIn(ingredient2, ingredients)
def test_ingredient_str(self): """Test that ingredient string representation""" ingredient = sample_ingredient(user=sample_user(), name='Cucumber') self.assertEqual(str(ingredient), ingredient.name)