def testGetFollowerView(self): # Favorite pasta recipe pastaRecipe = RecipeModel.objects.get(name="Pasta", author=self.user) Favorite.objects.create(recipe=pastaRecipe, user=self.user) data = {"recipe_id": pastaRecipe.id} request = self.factory.get(path="/ajax/toggle_favorite/", data=data) request.user = self.user response = favorite(request) # Favorite pancakes recipe pancakesRecipe = RecipeModel.objects.get(name="Pancakes", author=self.user) Favorite.objects.create(recipe=pancakesRecipe, user=self.user) data = {"recipe_id": pancakesRecipe.id} request = self.factory.get(path="/ajax/toggle_favorite/", data=data) request.user = self.user response = favorite(request) self.client.login(username="******", password="******") response = self.client.get(path='/following/') self.assertEqual(response.context["favorite_count"], 2)
def testGetFavoriteCount(self): pastaRecipe = RecipeModel.objects.get(name="Pasta", author=self.user) Favorite.objects.create(recipe=pastaRecipe, user=self.user) data = {"recipe_id": pastaRecipe.id} request = self.factory.get(path="/ajax/toggle_favorite/", data=data) request.user = self.user response = favorite(request) self.assertEqual(response.status_code, 200) pizzaRecipe = RecipeModel.objects.get(name="Pizza", author=self.user) Favorite.objects.create(recipe=pizzaRecipe, user=self.user) data = {"recipe_id": pizzaRecipe.id} request = self.factory.get(path="/ajax/toggle_favorite/", data=data) request.user = self.user response = favorite(request) self.assertEqual(response.status_code, 200) pieRecipe = RecipeModel.objects.get(name="Pie", author=self.user) Favorite.objects.create(recipe=pieRecipe, user=self.user) data = {"recipe_id": pieRecipe.id} request = self.factory.get(path="/ajax/toggle_favorite/", data=data) request.user = self.user response = favorite(request) self.assertEqual(response.status_code, 200) favorite_count = getFavoriteCount(self.user) self.assertEqual(favorite_count, 3)
def testGetFavoriteView(self): # Favorite pasta recipe pastaRecipe = RecipeModel.objects.get(name="Pasta", author=self.user) Favorite.objects.create(recipe=pastaRecipe, user=self.user) data = {"recipe_id": pastaRecipe.id} request = self.factory.get(path="/ajax/toggle_favorite/", data=data) request.user = self.user response = favorite(request) # Favorite pie recipe pieRecipe = RecipeModel.objects.get(name="Pie", author=self.user) Favorite.objects.create(recipe=pieRecipe, user=self.user) data = {"recipe_id": pieRecipe.id} request = self.factory.get(path="/ajax/toggle_favorite/", data=data) request.user = self.user response = favorite(request) # At this point, three recipes have been favorited for user testuser self.client.login(username="******", password="******") response = self.client.get(path='/favorite/') recipes = response.context["recipes"] expected_recipes = ["Pasta", "Pie"] i = 0 for recipe in recipes: self.assertEqual(recipe["name"], expected_recipes[i]) i += 1
def testGetFavoriteView(self): # Favorite pasta recipe pastaRecipe = RecipeModel.objects.get(name="Pasta", author=self.user) Favorite.objects.create(recipe=pastaRecipe, user=self.user) data = {"recipe_id": pastaRecipe.id} request = self.factory.get(path="/ajax/toggle_favorite/", data=data) request.user = self.user response = favorite(request) self.client.login(username="******", password="******") response = self.client.get(path='/favorite/') follow_count = response.context["followers"] following_count = response.context["following"] recipes = response.context["recipes"] expected_recipes = ["Pasta"] i = 0 for recipe in recipes: self.assertEqual(recipe["name"], expected_recipes[i]) i += 1 self.assertEqual(follow_count, 1) self.assertEqual(following_count, 1)
def testToggleFavorite(self): pastaRecipe = RecipeModel.objects.get(name="Pasta", author=self.user) Favorite.objects.create(recipe=pastaRecipe, user=self.user) data = {"recipe_id": pastaRecipe.id} request = self.factory.get(path="/ajax/toggle_favorite/", data=data) request.user = self.user response = favorite(request) fav = Favorite.objects.get(recipe=pastaRecipe, user=self.user) self.assertEqual(response.status_code, 200) self.assertEqual(fav.favorite, 1) response = favorite(request) fav = Favorite.objects.get(recipe=pastaRecipe, user=self.user) self.assertEqual(response.status_code, 200) self.assertEqual(fav.favorite, 0)