public async Task Create_WithExistingAllergens_ShouldCreateAlsoRelationToAllergens()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();
            var service   = new IngredientsService(dbContext);

            var allergenGluten = new Allergen {
                Name = "Gluten"
            };
            var allergenLupin = new Allergen {
                Name = "Lupin"
            };

            dbContext.Add(allergenGluten);
            dbContext.Add(allergenLupin);
            await dbContext.SaveChangesAsync();

            var ingredientVM = new IngredientCreateInputModel
            {
                Name        = "Flour",
                AllergenIds = new List <int>
                {
                    allergenGluten.Id, allergenLupin.Id
                }
            };

            var actual = await service.CreateAsync(ingredientVM);

            Assert.True(actual.IngredientAllergens.Count == 2);
        }
        public async Task GetUnused_ShouldReturnCorrectCount()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var ingredient = dbContext.Ingredients.First();
            var recipe     = new Recipe
            {
                Name             = "Test",
                RecipeIngredient = new List <RecipeIngredient>
                {
                    new RecipeIngredient {
                        Ingredient = ingredient
                    }
                }
            };

            dbContext.Recipes.Add(recipe);
            await dbContext.SaveChangesAsync();

            var service = new IngredientsService(dbContext);

            var expected = GetIngredients().Count();
            var actual   = service.GetAllUnused(recipe.Id).Count();

            Assert.True(actual == (expected - 1));
        }
示例#3
0
        public async Task GetAllToSelectListItemShouldReturnCorrectListWithVM()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            dbContext.Allergens.Add(new Allergen {
                Name = FirstName, ImagePath = "www"
            });
            dbContext.Allergens.Add(new Allergen {
                Name = SecondName, ImagePath = "www"
            });
            await dbContext.SaveChangesAsync();

            List <SelectListItem> expected = new List <SelectListItem>
            {
                new SelectListItem {
                    Value = "1", Text = FirstName
                },
                new SelectListItem {
                    Value = "2", Text = SecondName
                },
            };

            var service = new AllergensService(dbContext);
            List <SelectListItem> actual = service.AllToSelectListItems().ToList();

            for (int i = 0; i < expected.Count; i++)
            {
                var expectedEntry = expected[i];
                var actualEntry   = actual[i];

                Assert.True(expectedEntry.Text == actualEntry.Text);
            }
        }
        public async Task Create_WithExistingAndNonExistingAllergens_ShouldReturnNull()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();
            var service   = new IngredientsService(dbContext);

            var allergenGluten = new Allergen {
                Name = "Gluten"
            };

            dbContext.Add(allergenGluten);
            await dbContext.SaveChangesAsync();

            var ingredientVM = new IngredientCreateInputModel
            {
                Name        = "Flour",
                AllergenIds = new List <int>
                {
                    allergenGluten.Id, 2
                }
            };

            var actual = await service.CreateAsync(ingredientVM);

            Assert.True(actual == null);
        }
        public async Task GetByMatchingIngredinets_ShouldReturnRecipesInRightOrder()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var firstRecipe = dbContext.Recipes.First();

            var ingredientIds = firstRecipe.RecipeIngredient.Select(x => x.IngredientId).ToArray();
            var testRecipe    = new Recipe {
                Name = "Test"
            };

            for (int i = 1; i < ingredientIds.Count(); i++)
            {
                testRecipe.RecipeIngredient.Add(new RecipeIngredient {
                    IngredientId = ingredientIds[i]
                });
            }
            dbContext.Recipes.Add(testRecipe);
            await dbContext.SaveChangesAsync();

            var expected = new List <Recipe> {
                firstRecipe, testRecipe
            };

            var service = new RecipeService(dbContext);
            var actual  = service.GetRecipesByMatchingIngredients(ingredientIds).ToList();

            Assert.True(expected.SequenceEqual(actual));
        }
        public async Task Edit_WithAddingNewAllergens_ShouldBeSuccessful()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var service = new IngredientsService(dbContext);

            var entityBeforeEdit = dbContext.Ingredients.First();
            var countBeforeEdit  = entityBeforeEdit.IngredientAllergens.Count();
            var newAllergen      = dbContext.Allergens.Last();

            var test = new IngredientEditInputModel
            {
                Id            = entityBeforeEdit.Id,
                AllergenNames = new List <string> {
                    newAllergen.Name
                }
            };

            var entityAfterEdit = await service.EditAsync(test);

            var countAfterEdit = entityAfterEdit.IngredientAllergens.Count();

            Assert.True(countAfterEdit == (countBeforeEdit + 1));
        }
        public async Task GetAllToViewModelShouldReturnCorrectListWithVM()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var expected = new List <IngredientSimpleViewModel>
            {
                new IngredientSimpleViewModel {
                    Name = "Flour"
                },
                new IngredientSimpleViewModel {
                    Name = "Tomatoes"
                },
                new IngredientSimpleViewModel {
                    Name = "Pepperoni"
                },
                new IngredientSimpleViewModel {
                    Name = "Parmesan"
                },
            };

            var service = new IngredientsService(dbContext);
            var actual  = service.GetAllToViewModel <IngredientSimpleViewModel>().ToList();

            for (int i = 0; i < expected.Count; i++)
            {
                var expectedEntry = expected[i];
                var actualEntry   = actual[i];

                Assert.True(expectedEntry.Name == actualEntry.Name);
            }
        }
        public async Task Edit_WithQuantityNull_ShouldDeleteTheIngredientFromRecipe()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var recipe = dbContext.Recipes.First();

            var viewModel = new RecipeEditInputModel
            {
                Id              = recipe.Id,
                Name            = recipe.Name,
                CookingTimeName = recipe.CookingTime.Name,
                CategoryName    = recipe.Category.Name,
                IngredientNames = new List <string> {
                    "Eggs", "Mayo", "Avocado"
                },
                Quantity = new List <string> {
                    "3 pcs.", null, "3 pcs."
                },
            };

            var recipeIngredient = recipe.RecipeIngredient.Where(x => x.Ingredient.Name == "Mayo").First();

            var service = new RecipeService(dbContext);
            var actual  = await service.EditAsync(viewModel);

            Assert.DoesNotContain(recipeIngredient, actual.RecipeIngredient);
        }
示例#9
0
        public async Task GetAllToViewModelShouldReturnCorrectListWithVM()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            dbContext.Allergens.Add(new Allergen {
                Name = FirstName, ImagePath = "www"
            });
            dbContext.Allergens.Add(new Allergen {
                Name = SecondName, ImagePath = "www"
            });
            await dbContext.SaveChangesAsync();

            List <AllergenSimpleViewModel> expected = new List <AllergenSimpleViewModel>
            {
                new AllergenSimpleViewModel {
                    Id = 1, Name = "First", ImagePath = "www"
                },
                new AllergenSimpleViewModel {
                    Id = 2, Name = "Second", ImagePath = "www"
                },
            };

            var service = new AllergensService(dbContext);
            List <AllergenSimpleViewModel> actual = service.GetAllToViewModel <AllergenSimpleViewModel>().ToList();

            for (int i = 0; i < expected.Count; i++)
            {
                var expectedEntry = expected[i];
                var actualEntry   = actual[i];

                Assert.True(expectedEntry.Name == actualEntry.Name);
                Assert.True(expectedEntry.ImagePath == actualEntry.ImagePath);
            }
        }
示例#10
0
        public async Task GetAllToSelectListItemShouldReturnNullWhenEmptydB()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            var service = new AllergensService(dbContext);
            var actual  = service.AllToSelectListItems().ToList();

            Assert.True(actual.Count() == 0);
        }
        public async Task DeleteById_WithNotExistingId_ShouldReturnFalse()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();
            var service   = new IngredientsService(dbContext);

            var result = await service.DeleteByIdAsync(10);

            Assert.False(result);
        }
        public async Task GetViewModelById_ShouldReturnNull_IfIdDoesntExists()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            var service = new IngredientsService(dbContext);
            IngredientEditInputModel actual = await service.GetViewModelByIdAsync <IngredientEditInputModel>(1);

            Assert.True(actual == null);
        }
示例#13
0
        public async Task GetViewModelByIdShouldReturnNullIfIdDoesntExists()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();
            await dbContext.SaveChangesAsync();

            var service = new AllergensService(dbContext);
            AllergenViewModel actual = await service.GetViewModelByIdAsync <AllergenViewModel>(1);

            Assert.True(actual == null);
        }
示例#14
0
        public async Task GetGroupsByCategories_ShouldReturnCorrectViewModelInList()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var service = new RecipeService(dbContext);

            var result = service.GetGroupsByCategories();

            Assert.IsType <RecipeSimpleWithCategoryViewModel>(result.First().Recipes.First());
        }
示例#15
0
        public async Task Create_WithIngredientWithAllergens_ShouldAssignAllergensToRecipe()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            var service = new RecipeService(dbContext);

            var category = new Category {
                Name = "Vegan"
            };
            var cookingTime = new CookingTime {
                Name = "15 min."
            };

            dbContext.Categories.Add(category);
            dbContext.CookingTimes.Add(cookingTime);

            var ingredient = new Ingredient
            {
                Name = "Test",
                IngredientAllergens = new List <IngredientAllergen>
                {
                    new IngredientAllergen {
                        Allergen = new Allergen {
                            Name = "TestAllergen"
                        }
                    }
                }
            };

            dbContext.Ingredients.Add(ingredient);
            await dbContext.SaveChangesAsync();

            var recipeVM = new RecipeCreateInputModel
            {
                Name                 = "Recipe",
                CategoryId           = category.Id,
                CookingTimeId        = cookingTime.Id,
                IngredientQuantities = new IngredientQuantities
                {
                    IngredientNames = new List <string> {
                        "Test"
                    },
                    RecipeIngredientQuantity = new List <string> {
                        "3"
                    }
                }
            };

            Recipe actual = await service.CreateAsync(recipeVM);

            Assert.NotEmpty(actual.RecipeAllergens);
        }
        public async Task GetAllToViewModelShouldReturnCorrectNumber()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var expected = dbContext.Ingredients.Count();

            var service = new IngredientsService(dbContext);
            var actual  = service.GetAllToViewModel <IngredientSimpleViewModel>().ToList();

            Assert.True(expected == actual.Count());
        }
        public async Task DeleteById_WithExistingId_ShouldReturnTrue()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var service = new IngredientsService(dbContext);

            var test   = dbContext.Ingredients.First();
            var result = await service.DeleteByIdAsync(test.Id);

            Assert.True(result);
        }
        public async Task Edit_WithNonExistingId_ShouldReturnNull()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();
            var service   = new IngredientsService(dbContext);

            var tested = new IngredientEditInputModel {
                Id = 1
            };

            var result = await service.EditAsync(tested);

            Assert.Null(result);
        }
        public async Task GetViewModelById_ShouldReturnCorrectId()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var expectedVM = dbContext.Ingredients.First().To <IngredientEditInputModel>();

            var service = new IngredientsService(dbContext);
            var actual  = await service.GetViewModelByIdAsync <IngredientEditInputModel>(expectedVM.Id);

            Assert.True(expectedVM.Id == actual.Id);
        }
示例#20
0
        public async Task GetByCategory_WithNullCategory_ShouldReturnAllRecipes()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var expected = dbContext.Recipes;

            var service = new RecipeService(dbContext);
            var actual  = service.GetByCategory(null);

            Assert.Equal(expected, actual);
        }
        public async Task Create_FromViewModel_ShouldReturnRightType()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();
            var service   = new IngredientsService(dbContext);

            var ingredientVM = new IngredientCreateInputModel
            {
                Name = "Flour"
            };

            Ingredient actual = await service.CreateAsync(ingredientVM);

            Assert.IsType <Ingredient>(actual);
        }
        public async Task Create_WithoutAllergens_ShouldBeSucessfull()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();
            var service   = new IngredientsService(dbContext);

            var ingredientVM = new IngredientCreateInputModel
            {
                Name = "Flour"
            };

            var craeted = await service.CreateAsync(ingredientVM);

            Assert.NotNull(craeted);
        }
示例#23
0
        public async Task GetAllNonContainingByAllergenId_WithZeroAllergenIds_ShouldReturnAllRecipes()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var service   = new RecipeService(dbContext);
            var exptected = dbContext.Recipes.ToList();

            var ids    = new int[2];
            var actual = service.GetAllNonContainingByAllergenId(ids).ToList();

            Assert.True(actual.Count == exptected.Count);
        }
示例#24
0
        public async Task GetAllNonContainingByAllergenId_WithExistingAndNonExistingAllergenIds_ShouldReturnCorrectResults()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var allergen = dbContext.Allergens.First();
            var recipe   = dbContext.Recipes.First();
            var ids      = new int[] { 20, allergen.Id, 10 };

            var service = new RecipeService(dbContext);
            var actual  = service.GetAllNonContainingByAllergenId(ids).ToList();

            Assert.True(recipe != actual.First());
        }
示例#25
0
        public async Task GetByCategory_ShouldReturnTheRightRecipes()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var category = dbContext.Categories.First().Name;
            var expected = dbContext.Recipes.Where(x => x.Category.Name == category).First();

            var service = new RecipeService(dbContext);
            var actual  = service.GetByCategory(category).ToList();

            Assert.True(actual.Count() == 1);
            Assert.True(expected == actual.First());
        }
示例#26
0
        public async Task GetByMatchingIngredinets_ShouldReturnTheRightRecipes()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var ingredient = dbContext.Ingredients.First();
            var ids        = new int[] { ingredient.Id };
            var recipe     = dbContext.Recipes.First();

            var service = new RecipeService(dbContext);
            var actual  = service.GetRecipesByMatchingIngredients(ids);

            Assert.True(actual.Count() == 1);
            Assert.True(recipe.Name == actual.First().Name);
        }
示例#27
0
        public async Task GetViewModelByIdShouldReturnCorrectId()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            dbContext.Allergens.Add(new Allergen {
                Name = FirstName
            });
            await dbContext.SaveChangesAsync();

            AllergenViewModel expectedVM = dbContext.Allergens.First().To <AllergenViewModel>();

            var service = new AllergensService(dbContext);
            AllergenViewModel actual = await service.GetViewModelByIdAsync <AllergenViewModel>(expectedVM.Id);

            Assert.True(expectedVM.Id == actual.Id);
        }
示例#28
0
        public async Task Create_WithIngredientsWithIncorrectQuantities_ShouldThrowException()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            var service = new RecipeService(dbContext);

            var category = new Category {
                Name = "Vegan"
            };
            var cookingTime = new CookingTime {
                Name = "15 min."
            };

            dbContext.Categories.Add(category);
            dbContext.CookingTimes.Add(cookingTime);

            var ingredients = new List <Ingredient>
            {
                new Ingredient {
                    Name = "Test"
                },
                new Ingredient {
                    Name = "Test2"
                }
            };

            dbContext.Ingredients.AddRange(ingredients);
            await dbContext.SaveChangesAsync();

            var recipeVM = new RecipeCreateInputModel
            {
                Name                 = "Recipe",
                CategoryId           = category.Id,
                CookingTimeId        = cookingTime.Id,
                IngredientQuantities = new IngredientQuantities
                {
                    IngredientNames = new List <string> {
                        "Test", "Test2"
                    },
                    RecipeIngredientQuantity = new List <string> {
                        "3"
                    }
                }
            };

            await Assert.ThrowsAsync <ArgumentNullException>(() => service.CreateAsync(recipeVM));
        }
示例#29
0
        public async Task GetGroupsByCategories_ShouldReturnCorrectGroupNames()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var service = new RecipeService(dbContext);

            var expected = dbContext.Categories.Select(x => x.Name).ToList();

            var result = service.GetGroupsByCategories();

            for (int i = 0; i < result.Count(); i++)
            {
                Assert.True(result[i].GroupName == expected[i]);
            }
        }
        public async Task Edit_WithExistingId_ShouldReturnTheRightResult()
        {
            var dbContext = WantoeatDbContextInMemoryFactory.InitializeContext();

            await SeedData(dbContext);

            var service = new IngredientsService(dbContext);

            var actual = dbContext.Ingredients.First();

            var tested = new IngredientEditInputModel {
                Id = actual.Id
            };

            var result = await service.EditAsync(tested);

            Assert.True(actual.Id == result.Id);
        }