public async Task TestUpdatesRecipeWithNewIngredients() { var options = _fixture.Options; var recipesContext = new RecipesContext(options); var controller = CreateRecipesController(recipesContext); // create existing recipe without ingredients var emptyIngredientList = new List <Ingredient>(); var existingRecipe = new Recipe { Name = "existing title", Description = "existing description", Ingredients = emptyIngredientList, Id = 8 }; await recipesContext.Recipes.AddAsync(existingRecipe); await recipesContext.SaveChangesAsync(); var ingredientList = new List <UpdatedIngredientDto>(); var ingredient1 = new UpdatedIngredientDto("Ingredient1", 100, "gr"); var ingredient2 = new UpdatedIngredientDto("Ingredient2", 200, "kg"); ingredientList.Add(ingredient1); ingredientList.Add(ingredient2); var newRecipe = new UpdatedRecipeDto("Updated recipe", "Updated description", ingredientList, 8); var result = await controller.PatchRecipe(8, newRecipe); Assert.IsType <ActionResult <Recipe> >(result); Assert.Equal("Updated recipe", result.Value.Name); Assert.Equal("Updated description", result.Value.Description); Assert.NotNull(result.Value.Ingredients); Assert.Equal(2, result.Value.Ingredients.Count); Assert.Equal("Ingredient1", result.Value.Ingredients.ToList()[0].Name); Assert.Equal(100, result.Value.Ingredients.ToList()[0].Amount); Assert.Equal("gr", result.Value.Ingredients.ToList()[0].Unit); Assert.Equal("Ingredient2", result.Value.Ingredients.ToList()[1].Name); Assert.Equal(200, result.Value.Ingredients.ToList()[1].Amount); Assert.Equal("kg", result.Value.Ingredients.ToList()[1].Unit); }
public async Task <ActionResult <Ingredient> > UpdateIngredient(UpdatedIngredientDto updatedIngredient, Ingredient currentIngredient) { if (updatedIngredient.Amount != null) { currentIngredient.Amount = (int)updatedIngredient.Amount; } if (updatedIngredient.Name != null) { currentIngredient.Name = updatedIngredient.Name; } if (updatedIngredient.Unit != null) { currentIngredient.Unit = updatedIngredient.Unit; } await _databaseActions.UpdateIngredient(currentIngredient); return(currentIngredient); }
public async Task TestUpdatesRecipeWithExistingIngredients() { var options = _fixture.Options; var recipesContext = new RecipesContext(options); var controller = CreateRecipesController(recipesContext); // add existing ingredients to database var ingredientList = new List <Ingredient>(); var existingIngredient1 = new Ingredient("Ingredient1", 100, "gr") { Id = 6 }; var existingIngredient2 = new Ingredient("Ingredient2", 200, "kg") { Id = 7 }; ingredientList.Add(existingIngredient1); ingredientList.Add(existingIngredient2); await recipesContext.Ingredients.AddAsync(existingIngredient1); await recipesContext.Ingredients.AddAsync(existingIngredient2); // have existing recipe with ingredients var existingRecipe = new Recipe { Name = "existing title", Description = "existing description", Ingredients = ingredientList, Id = 12 }; await recipesContext.Recipes.AddAsync(existingRecipe); await recipesContext.SaveChangesAsync(); var updatedIngredientList = new List <UpdatedIngredientDto>(); var ingredient1 = new UpdatedIngredientDto("Ingredient1 updated", 333, "gr") { Id = 6 }; var ingredient2 = new UpdatedIngredientDto("Ingredient2 updated", 555, "kg") { Id = 7 }; updatedIngredientList.Add(ingredient1); updatedIngredientList.Add(ingredient2); var newRecipe = new UpdatedRecipeDto("Updated recipe", "Updated description", updatedIngredientList, 12); var result = await controller.PatchRecipe(12, newRecipe); Assert.IsType <ActionResult <Recipe> >(result); Assert.Equal("Updated recipe", result.Value.Name); Assert.Equal("Updated description", result.Value.Description); Assert.Equal(2, result.Value.Ingredients.Count); Assert.Equal("Ingredient1 updated", result.Value.Ingredients.ToList()[0].Name); Assert.Equal(333, result.Value.Ingredients.ToList()[0].Amount); Assert.Equal("gr", result.Value.Ingredients.ToList()[0].Unit); Assert.Equal("Ingredient2 updated", result.Value.Ingredients.ToList()[1].Name); Assert.Equal(555, result.Value.Ingredients.ToList()[1].Amount); Assert.Equal("kg", result.Value.Ingredients.ToList()[1].Unit); }