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 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 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 void InvokeDataCommitOnce_WhenThePassedArgumentsAreValid() { //Arrange var dataMock = new Mock <IHomeMadeFoodData>(); var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>(); IngredientsService ingredientsService = new IngredientsService(dataMock.Object, foodCategoriesServiceMock.Object); string name = "NameOfTheIngredient"; decimal pricePerMeasuringUnit = 1.19m; Guid foodCategoryId = Guid.NewGuid(); Guid recipeId = Guid.NewGuid(); double quantityPerMeasuringUnit = 0.250; Ingredient ingredient = new Ingredient() { Name = name, RecipeId = recipeId, FoodCategoryId = foodCategoryId, QuantityInMeasuringUnit = quantityPerMeasuringUnit, PricePerMeasuringUnit = pricePerMeasuringUnit }; dataMock.Setup(x => x.Ingredients.Update(ingredient)); //Act ingredientsService.EditIngredient(ingredient); //Assert dataMock.Verify(x => x.SaveChanges(), Times.Once); }
public async Task ReturnFalse_when_NotValidIngredientIDAsync() { //Arrange var options = Utils.GetOptions("ReturnFalse_when_NotValidIngredientIDAsync"); var entity = new Ingredient { Name = "Lime", IsAlcoholic = false }; var mockMapper = new Mock <IIngredientMapper>(); using (var arrangeContext = new BCcontext(options)) { arrangeContext.Ingredients.Add(entity); arrangeContext.SaveChanges(); } //Act & Assert using (var context = new BCcontext(options)) { var sut = new IngredientsService(context, mockMapper.Object); var result = await sut.DeleteAsync(Utils.MySampleGuid()); Assert.IsFalse(result); } }
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 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)); }
static void Main(string[] args) { var fileService = new FileService("./Resources/ingredients.txt"); var ingredients = fileService.ReadLinesFromFile(); var units = new List <string>() { "ml", "cup", "cups", "g", "l" }; var products = new List <string>() { "milk", "water", "coffeebeans", "whippedcream" }; var features = new List <string>() { "boiled", "steamed", "roasted", "foamed" }; var ingredientsValidator = new IngredientsService(ingredients, units, products, features); var coffeeIngredients = new List <Ingredient>(); foreach (var i in ingredients) { try { var ingredient = ingredientsValidator.IsIngredientValid(i); coffeeIngredients.Add(ingredient); } catch (Exception ex) { Console.WriteLine(ex); } } var recipe = new Recipe() { Ingredients = coffeeIngredients }; var coffeeService = new CoffeeService(); var coffeeBuilder = coffeeService.MakeCoffee(recipe); if (coffeeBuilder == null) { Console.WriteLine("We don't know to prepare that coffee"); } else { var preparedCoffee = coffeeBuilder.GetPreparedCoffee(); Console.WriteLine($"Here is your coffee {preparedCoffee.GetCoffeeType()}"); } Console.ReadKey(); }
public async Task CreateIngredient_When_IngredientDoesNotExist() { var options = Utils.GetOptions(nameof(CreateIngredient_When_IngredientDoesNotExist)); var testIngredientName = "IngredientName"; var entity = new Ingredient() { Name = "SampleIngredientName", IsAlcoholic = true, }; var mockMapper = new Mock <IIngredientMapper>(); mockMapper.Setup(x => x.MapDTOToEntity(It.IsAny <IngredientDTO>())) .Returns((IngredientDTO i) => new Ingredient() { Name = testIngredientName, IsAlcoholic = true }); mockMapper.Setup(x => x.MapEntityToDTO(It.IsAny <Ingredient>())) .Returns((Ingredient i) => new IngredientDTO() { Name = testIngredientName, IsAlcoholic = true }); using (var arrangeContext = new BCcontext(options)) { await arrangeContext.Ingredients .AddAsync(entity); await arrangeContext.SaveChangesAsync(); } using (var actContext = new BCcontext(options)) { var sut = new IngredientsService(actContext, mockMapper.Object); var ingredient = new IngredientDTO() { Name = testIngredientName, IsAlcoholic = true, }; var testIngredient = await sut.CreateAsync(ingredient); await actContext.SaveChangesAsync(); } using (var assertContext = new BCcontext(options)) { Assert.AreEqual(2, assertContext.Ingredients.Count()); var ingredient = await assertContext.Ingredients.FirstOrDefaultAsync(x => x.Name == testIngredientName); Assert.IsNotNull(ingredient); Assert.IsTrue(ingredient.IsAlcoholic); } }
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); }
public RecipesService( DbContextProvider dbContextProvider, RecipesDtoMapper recipesDtoMapper, RecipesRepository recipesRepository, IngredientsService ingredientsService) : base(dbContextProvider, recipesRepository) { _recipesDtoMapper = recipesDtoMapper; _ingredientsService = ingredientsService; }
public void ShouldThrow_WhenGuidIdParameterIsEmpty() { //Arrange var dataMock = new Mock <IHomeMadeFoodData>(); var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>(); IngredientsService ingredientsService = new IngredientsService(dataMock.Object, foodCategoriesServiceMock.Object); //Act&Assert Assert.Throws <ArgumentException>(() => ingredientsService.GetIngredientById(Guid.Empty)); }
private IIngredientsService CreateMockAndConfigureService(IList <Ingredient> list, IList <Nutrition> nutritionList, IList <Recipe> recipeList) { var mockIngredientRepo = new Mock <IDeletableEntityRepository <Ingredient> >(); mockIngredientRepo.Setup(x => x.All()) .Returns(list.AsQueryable()); mockIngredientRepo.Setup(x => x.AllAsNoTracking()) .Returns(new TestAsyncEnumerable <Ingredient>(list).AsQueryable()); mockIngredientRepo.Setup(x => x.AddAsync(It.IsAny <Ingredient>())) .Callback((Ingredient ingredient) => list.Add(ingredient)); mockIngredientRepo.Setup(x => x.Delete(It.IsAny <Ingredient>())) .Callback((Ingredient ingredient) => ingredient.IsDeleted = true); mockIngredientRepo.Setup(x => x.Update(It.IsAny <Ingredient>())) .Callback((Ingredient ingredient) => { list.FirstOrDefault(x => x.Id == ingredient.Id).Name = ingredient.Name; list.FirstOrDefault(x => x.Id == ingredient.Id).VolumeInMlPer100Grams = ingredient.VolumeInMlPer100Grams; }); var mockNutritionsRepo = new Mock <IDeletableEntityRepository <Nutrition> >(); mockNutritionsRepo.Setup(x => x.All()) .Returns(nutritionList.AsQueryable()); mockNutritionsRepo.Setup(x => x.Delete(It.IsAny <Nutrition>())) .Callback((Nutrition nutrition) => nutrition.IsDeleted = true); mockNutritionsRepo.Setup(x => x.Update(It.IsAny <Nutrition>())) .Callback((Nutrition nutrition) => { nutritionList.FirstOrDefault(x => x.Id == nutrition.Id).Calories = nutrition.Calories; nutritionList.FirstOrDefault(x => x.Id == nutrition.Id).Carbohydrates = nutrition.Carbohydrates; nutritionList.FirstOrDefault(x => x.Id == nutrition.Id).Fats = nutrition.Fats; nutritionList.FirstOrDefault(x => x.Id == nutrition.Id).Proteins = nutrition.Proteins; nutritionList.FirstOrDefault(x => x.Id == nutrition.Id).Fibres = nutrition.Fibres; }); var mockRecipeRepo = new Mock <IDeletableEntityRepository <Recipe> >(); mockRecipeRepo.Setup(x => x.All()) .Returns(new TestAsyncEnumerable <Recipe>(recipeList).AsQueryable()); var mockRecipeIngredientRepo = new Mock <IDeletableEntityRepository <RecipeIngredient> >(); var mockNutritionsService = new Mock <NutritionsService>( mockNutritionsRepo.Object, mockIngredientRepo.Object, mockRecipeIngredientRepo.Object, mockRecipeRepo.Object); var service = new IngredientsService( mockIngredientRepo.Object, mockNutritionsRepo.Object, mockRecipeRepo.Object, mockNutritionsService.Object); return(service); }
public RecipesSeeder( IRecipersService recipersService, UserManager <ApplicationUser> userManager, IngredientsService ingredientsService, IRepository <Ingredient> ingredientsRepository) { this.recipersService = recipersService; this.userManager = userManager; this.ingredientsService = ingredientsService; this.ingredientsRepository = ingredientsRepository; }
public void Throw_WhenThePassedIngredientIsNull() { //Arrange var dataMock = new Mock <IHomeMadeFoodData>(); var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>(); IngredientsService ingredientsService = new IngredientsService(dataMock.Object, foodCategoriesServiceMock.Object); Ingredient ingredient = null; //Act & Assert Assert.Throws <ArgumentNullException>(() => ingredientsService.AddIngredient(ingredient)); }
public void Throw_WhenThePassedIngredientIsNull() { //Arrange var dataMock = new Mock <IHomeMadeFoodData>(); var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>(); IngredientsService ingredientsService = new IngredientsService(dataMock.Object, foodCategoriesServiceMock.Object); dataMock.Setup(x => x.Ingredients.Update(It.IsAny <Ingredient>())); //Act & Assert Assert.Throws <ArgumentNullException>(() => ingredientsService.EditIngredient(null)); }
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); }
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 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 void Invoke_TheDataIngredientsRepositoryMethodGetAll_Once() { //Arrange var dataMock = new Mock <IHomeMadeFoodData>(); var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>(); IngredientsService ingredientsService = new IngredientsService(dataMock.Object, foodCategoriesServiceMock.Object); dataMock.Setup(x => x.Ingredients.All); //Act IEnumerable <Ingredient> ingredients = ingredientsService.GetAllIngredients(); //Assert dataMock.Verify(x => x.Ingredients.All, Times.Once); }
public IngredientsServiceTests() { var options = new DbContextOptionsBuilder <PizzaLabDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var dbContext = new PizzaLabDbContext(options); var mapperProfile = new MappingConfiguration(); var conf = new MapperConfiguration(cfg => cfg.AddProfile(mapperProfile)); var mapper = new Mapper(conf); this._ingredientsRepository = new EfRepository <Ingredient>(dbContext); this._ingredientsService = new IngredientsService(_ingredientsRepository, mapper); }
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); }
public void Throw_WhenThePassedNameIsNull() { //Arrange var dataMock = new Mock <IHomeMadeFoodData>(); var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>(); IngredientsService ingredientsService = new IngredientsService(dataMock.Object, foodCategoriesServiceMock.Object); string ingredientName = null; decimal pricePerMeasuringUnit = 1.19m; Guid foodCategoryId = Guid.NewGuid(); Guid recipeId = Guid.NewGuid(); double quantityPerMeasuringUnit = 0.250; //Act & Assert Assert.Throws <ArgumentNullException>(() => ingredientsService.AddIngredient(ingredientName, foodCategoryId, pricePerMeasuringUnit, quantityPerMeasuringUnit, recipeId)); }
public void ReturnsNull_WhenIdIsValidButThereIsNoSuchIngredientInDatabase() { //Arrange var dataMock = new Mock <IHomeMadeFoodData>(); var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>(); IngredientsService ingredientsService = new IngredientsService(dataMock.Object, foodCategoriesServiceMock.Object); Guid ingredientId = Guid.NewGuid(); dataMock.Setup(c => c.Ingredients.GetById(ingredientId)).Returns <Ingredient>(null); //Act Ingredient ingredientResult = ingredientsService.GetIngredientById(ingredientId); //Assert Assert.IsNull(ingredientResult); }
public void ReturnIngredient() { //Arrange var dataMock = new Mock <IHomeMadeFoodData>(); var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>(); IngredientsService ingredientsService = new IngredientsService(dataMock.Object, foodCategoriesServiceMock.Object); string ingredientName = "Name of the ingredient"; decimal pricePerMeasuringUnit = 1.19m; Guid foodCategoryId = Guid.NewGuid(); Guid recipeId = Guid.NewGuid(); double quantityPerMeasuringUnit = 0.250; //Act var ingredient = ingredientsService.CreateIngredient(ingredientName, foodCategoryId, pricePerMeasuringUnit, quantityPerMeasuringUnit); //Assert Assert.IsInstanceOf <Ingredient>(ingredient); }
public void CallFoodCategoriesServiceMethod_AddIngredientQuantityToFoodCategory() { //Arrange var dataMock = new Mock <IHomeMadeFoodData>(); var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>(); IngredientsService ingredientsService = new IngredientsService(dataMock.Object, foodCategoriesServiceMock.Object); string ingredientName = "Name of the ingredient"; decimal pricePerMeasuringUnit = 1.19m; Guid foodCategoryId = Guid.NewGuid(); Guid recipeId = Guid.NewGuid(); double quantityPerMeasuringUnit = 0.250; //Act var ingredient = ingredientsService.CreateIngredient(ingredientName, foodCategoryId, pricePerMeasuringUnit, quantityPerMeasuringUnit); //Assert foodCategoriesServiceMock.Verify(x => x.AddIngredientQuantityToFoodCategory(ingredient), Times.Once); }
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); }