public async Task Delete_Bar_Correctly() { //Arrange var options = TestUtilities.GetOptions(nameof(Delete_Bar_Correctly)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockBarDtoMapper = new Mock <IDtoMapper <Bar, BarDTO> >(); var newBar = new Bar { Id = 1, Name = "TestBar", }; using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Bars.AddAsync(newBar); await arrangeContext.SaveChangesAsync(); var barService = new BarService(arrangeContext, mockBarDtoMapper.Object, mockDateTimeProvider.Object); var result = await barService.DeleteBarAsync(1); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var deletedBar = await assertContext.Bars.FirstAsync(); Assert.IsTrue(deletedBar.IsDeleted); } }
public async Task Delete_Cocktail_Correctly() { //Arrange var options = TestUtilities.GetOptions(nameof(Delete_Cocktail_Correctly)); var mockCocktailDtoMapper = new Mock <IDtoMapper <Cocktail, CocktailDto> >(); var mockCocktailIngretientService = new Mock <ICocktailIngredientService>(); var mockIngredientsService = new Mock <IIngredientService>(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var cocktail = new Cocktail { Id = 1, Name = "TestCocktail", }; using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Cocktails.AddAsync(cocktail); await arrangeContext.SaveChangesAsync(); var cocktailService = new CocktailService(arrangeContext, mockCocktailDtoMapper.Object, mockDateTimeProvider.Object, mockCocktailIngretientService.Object, mockIngredientsService.Object); var result = await cocktailService.DeleteCocktailAsync(1); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var deletedCocktail = await assertContext.Cocktails.FirstAsync(); Assert.IsTrue(deletedCocktail.IsDeleted); } }
public async Task Create_Cocktail_Correctly() { //Arrange var options = TestUtilities.GetOptions(nameof(Create_Cocktail_Correctly)); var mockCocktailDtoMapper = new Mock <IDtoMapper <Cocktail, CocktailDto> >(); var mockCocktailIngretientService = new Mock <ICocktailIngredientService>(); var mockIngredientsService = new Mock <IIngredientService>(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var ingredient = new Ingredient { Id = 1, Name = "TestIngredient" }; var ingredientDto = new IngredientDto { Id = 1, Name = "TestIngredient" }; string[] ingredients = new string[] { "TestIngredient" }; var cocktailDto = new CocktailDto { Id = 1, Name = "TestCocktail", Ingredients = ingredients, }; var cocktailIngredient = new CocktailIngredient { CocktailId = cocktailDto.Id, IngredientId = ingredient.Id }; mockCocktailDtoMapper.Setup(x => x.MapDto(It.IsAny <Cocktail>())).Returns(cocktailDto); mockIngredientsService.Setup(x => x.GetIngredientByNameAsync(It.IsAny <string>())) .Returns(Task.FromResult(ingredient)); mockCocktailIngretientService.Setup(x => x.CreateCocktailIngredientAsync(It.IsAny <int>(), It.IsAny <int>())).Returns(Task.FromResult(cocktailIngredient)); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Ingredients.AddAsync(ingredient); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new CocktailService(assertContext, mockCocktailDtoMapper.Object, mockDateTimeProvider.Object, mockCocktailIngretientService.Object, mockIngredientsService.Object); var result = await sut.CreateCocktailAsync(cocktailDto); Assert.IsInstanceOfType(result, typeof(CocktailDto)); Assert.AreEqual(1, result.Id); Assert.AreEqual("TestCocktail", result.Name); Assert.AreEqual("TestIngredient", result.Ingredients.First()); } }
public async Task Return_Correct_Model_When_ParamsAreValid() { //Arrange var options = TestUtilities.GetOptions(nameof(Return_Correct_Model_When_ParamsAreValid)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockBarRatingDtoMapper = new Mock <IDtoMapper <BarRating, BarRatingDto> >(); var user = new User { Id = 1 }; var bar = new Bar { Id = 1 }; var rating = new BarRating { Id = 1, UserId = 1, BarId = 1, Value = 5, }; var ratingDto = new BarRatingDto { Id = 1, UserId = 1, BarId = 1, Value = 5, }; mockBarRatingDtoMapper.Setup(x => x.MapDto(It.IsAny <BarRating>())).Returns(ratingDto); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Bars.AddAsync(bar); await arrangeContext.Users.AddAsync(user); await arrangeContext.BarRatings.AddAsync(rating); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new BarRatingService(assertContext, mockBarRatingDtoMapper.Object, mockDateTimeProvider.Object); var result = await sut.GetRatingAsync(1, 1); Assert.IsInstanceOfType(result, typeof(BarRatingDto)); Assert.AreEqual(1, result.Id); Assert.AreEqual(1, result.UserId); Assert.AreEqual(1, result.BarId); Assert.AreEqual(5, result.Value); } }
public async Task Get_Correct_Models_WhenParamsAreValid() { //Arrange var options = TestUtilities.GetOptions(nameof(Get_Correct_Models_WhenParamsAreValid)); var mockCocktailDtoMapper = new Mock <IDtoMapper <Cocktail, CocktailDto> >(); var mockCocktailIngretientService = new Mock <ICocktailIngredientService>(); var mockIngredientsService = new Mock <IIngredientService>(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var cocktail1 = new Cocktail { Id = 1, Name = "TestCocktail1", }; var cocktail2 = new Cocktail { Id = 2, Name = "TestCocktail2", }; var list = new List <CocktailDto> { new CocktailDto { Id = 1, Name = "TestCocktail1", }, new CocktailDto { Id = 2, Name = "TestCocktail2", } }; mockCocktailDtoMapper.Setup(x => x.MapDto(It.IsAny <ICollection <Cocktail> >())).Returns(list); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Cocktails.AddAsync(cocktail1); await arrangeContext.Cocktails.AddAsync(cocktail2); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new CocktailService(assertContext, mockCocktailDtoMapper.Object, mockDateTimeProvider.Object, mockCocktailIngretientService.Object, mockIngredientsService.Object); var result = await sut.GetAllCocktailsAsync(); Assert.IsInstanceOfType(result, typeof(ICollection <CocktailDto>)); Assert.AreEqual(1, result.First().Id); Assert.AreEqual("TestCocktail1", result.First().Name); Assert.AreEqual(2, result.Last().Id); Assert.AreEqual("TestCocktail2", result.Last().Name); } }
public async Task ReturnCorrectCollection_WhenParamsAreValid() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnCorrectCollection_WhenParamsAreValid)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockBarDtoMapper = new Mock <IDtoMapper <Bar, BarDTO> >(); var newBar1 = new Bar { Id = 1, Name = "TestBar1", }; var newBar2 = new Bar { Id = 2, Name = "TestBar2", }; var newBar3 = new Bar { Id = 3, Name = "TestBar3", }; var list = new List <BarDTO> { new BarDTO { Id = 1, Name = "TestBar1" }, new BarDTO { Id = 2, Name = "TestBar2" } }; mockBarDtoMapper.Setup(x => x.MapDto(It.IsAny <ICollection <Bar> >())).Returns(list); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Bars.AddAsync(newBar1); await arrangeContext.Bars.AddAsync(newBar2); await arrangeContext.Bars.AddAsync(newBar3); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new BarService(assertContext, mockBarDtoMapper.Object, mockDateTimeProvider.Object); var result = await sut.GetBarsForPeginationAsync(2, 1); Assert.IsInstanceOfType(result, typeof(ICollection <BarDTO>)); Assert.AreEqual(2, result.Count); Assert.AreEqual(1, result.First().Id); Assert.AreEqual("TestBar1", result.First().Name); Assert.AreEqual(2, result.Last().Id); Assert.AreEqual("TestBar2", result.Last().Name); } }
public async Task GetCorrectIngredients_When_ParamsAreValid() { //Arrange var options = TestUtilities.GetOptions(nameof(GetCorrectIngredients_When_ParamsAreValid)); var mapper = new Mock <IDtoMapper <Ingredient, IngredientDto> >(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var ingredient = new Ingredient { Id = 1, Name = "Cola" }; var ingredient1 = new Ingredient { Id = 2, Name = "Coca-cola" }; var ingredientsDto = new List <IngredientDto> { new IngredientDto { Id = 1, Name = "Cola" }, new IngredientDto { Id = 2, Name = "Coca-cola" } }; mapper.Setup(x => x.MapDto(It.IsAny <ICollection <Ingredient> >())).Returns(ingredientsDto); using (var arrangeContext = new CocktailMagicianContext(options)) { //Act await arrangeContext.Ingredients.AddAsync(ingredient); await arrangeContext.Ingredients.AddAsync(ingredient1); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new CocktailMagicianContext(options)) { //Assert var sut = new IngredientService(assertContext, mapper.Object, mockDateTimeProvider.Object); var result = await sut.GetAllIngredientsAsync(); Assert.IsInstanceOfType(result, typeof(ICollection <IngredientDto>)); Assert.AreEqual(2, result.Count()); Assert.AreEqual(ingredient.Id, result.First().Id); Assert.AreEqual(ingredient.Name, result.First().Name); Assert.AreEqual(ingredient1.Id, result.Last().Id); Assert.AreEqual(ingredient1.Name, result.Last().Name); } }
public async Task EditCocktailRating_When_It_Exists() { //Arrange var options = TestUtilities.GetOptions(nameof(EditCocktailRating_When_It_Exists)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mapper = new Mock <IDtoMapper <CocktailRating, CocktailRatingDto> >(); var user = new User { Id = 1 }; var cocktail = new Cocktail { Id = 1 }; var rating = new CocktailRating { Id = 1, UserId = 1, CocktailId = 1, Value = 4, }; var ratingDto = new CocktailRatingDto { Id = 1, UserId = 1, CocktailId = 1, Value = 5, }; mapper.Setup(x => x.MapDto(It.IsAny <CocktailRating>())).Returns(ratingDto); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.CocktailRatings.AddAsync(rating); await arrangeContext.Cocktails.AddAsync(cocktail); await arrangeContext.Users.AddAsync(user); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new CocktailRatingService(assertContext, mockDateTimeProvider.Object, mapper.Object); var result = await sut.CreateRatingAsync(ratingDto); Assert.IsInstanceOfType(result, typeof(CocktailRatingDto)); Assert.AreEqual(1, result.Id); Assert.AreEqual(1, result.CocktailId); Assert.AreEqual(1, result.UserId); Assert.AreEqual(5, result.Value); } }
public async Task ReturnNull_When_NoIngredientsFound() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnNull_When_NoIngredientsFound)); var mockCocktailDtoMapper = new Mock <IDtoMapper <Cocktail, CocktailDto> >(); var mockCocktailIngretientService = new Mock <ICocktailIngredientService>(); var mockIngredientsService = new Mock <IIngredientService>(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var ingredient = new Ingredient { Id = 1, Name = "TestIngredient" }; var ingredientDto = new IngredientDto { Id = 1, Name = "TestIngredient" }; string[] ingredients = new string[] { "TestIngredient" }; var cocktailDto = new CocktailDto { Id = 1, Name = "TestCocktail", Ingredients = ingredients, }; var cocktailIngredient = new CocktailIngredient { CocktailId = cocktailDto.Id, IngredientId = ingredient.Id }; mockCocktailDtoMapper.Setup(x => x.MapDto(It.IsAny <Cocktail>())).Returns(cocktailDto); mockCocktailIngretientService.Setup(x => x.CreateCocktailIngredientAsync(It.IsAny <int>(), It.IsAny <int>())).Returns(Task.FromResult(cocktailIngredient)); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Ingredients.AddAsync(ingredient); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new CocktailService(assertContext, mockCocktailDtoMapper.Object, mockDateTimeProvider.Object, mockCocktailIngretientService.Object, mockIngredientsService.Object); var result = await sut.CreateCocktailAsync(cocktailDto); Assert.IsNull(result); } }
public async Task ReturnCorrectCollectionOfIngredients_WhenParamsAreValid() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnCorrectCollectionOfIngredients_WhenParamsAreValid)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mapper = new Mock <IDtoMapper <Ingredient, IngredientDto> >(); var ingredient1 = new Ingredient { Id = 1, Name = "Cola", IsDeleted = false }; var ingredient2 = new Ingredient { Id = 2, Name = "Water", IsDeleted = false }; var list = new List <IngredientDto> { new IngredientDto { Id = 1, Name = "Cola" }, new IngredientDto { Id = 2, Name = "Water" } }; mapper.Setup(x => x.MapDto(It.IsAny <ICollection <Ingredient> >())).Returns(list); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Ingredients.AddAsync(ingredient1); await arrangeContext.Ingredients.AddAsync(ingredient2); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new IngredientService(assertContext, mapper.Object, mockDateTimeProvider.Object); var result = await sut.GetIngredientsForPeginationAsync(2, 1); Assert.IsInstanceOfType(result, typeof(ICollection <IngredientDto>)); Assert.AreEqual(2, result.Count); Assert.AreEqual(1, result.First().Id); Assert.AreEqual("Cola", result.First().Name); Assert.AreEqual(2, result.Last().Id); Assert.AreEqual("Water", result.Last().Name); } }
public async Task ReturnCorrectCocktails_When_Searching_By_Name() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnCorrectCocktails_When_Searching_By_Name)); var mockCocktailDtoMapper = new Mock <IDtoMapper <Cocktail, CocktailDto> >(); var mockCocktailIngretientService = new Mock <ICocktailIngredientService>(); var mockIngredientsService = new Mock <IIngredientService>(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); string[] ingredients = new string[] { "TestIngredient" }; var newCocktail1 = new Cocktail { Id = 1, Name = "TestCocktail1", }; var newCocktail2 = new Cocktail { Id = 2, Name = "TestCocktail2", }; var list = new List <CocktailDto>() { new CocktailDto { Id = 1, Name = "TestCocktail1", Ingredients = ingredients }, new CocktailDto { Id = 2, Name = "TestCocktail2", Ingredients = ingredients } }; mockCocktailDtoMapper.Setup(x => x.MapDto(It.IsAny <ICollection <Cocktail> >())).Returns(list); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Cocktails.AddAsync(newCocktail1); await arrangeContext.Cocktails.AddAsync(newCocktail2); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new CocktailService(assertContext, mockCocktailDtoMapper.Object, mockDateTimeProvider.Object, mockCocktailIngretientService.Object, mockIngredientsService.Object); var result = await sut.SearchCocktailsAsync("test"); Assert.IsInstanceOfType(result, typeof(ICollection <CocktailDto>)); Assert.AreEqual(2, result.Count); Assert.AreEqual("TestCocktail1", result.First().Name); Assert.AreEqual("TestCocktail2", result.Last().Name); } }
public void ReturnCorrectValue_When_ParamsAreValidInMethod() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnCorrectValue_When_ParamsAreValidInMethod)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mapper = new Mock <IDtoMapper <CocktailRating, CocktailRatingDto> >(); var user1 = new User { Id = 1 }; var user2 = new User { Id = 2 }; var cocktail = new Cocktail { Id = 1 }; var rating1 = new CocktailRating { Id = 1, UserId = 1, CocktailId = 1, Value = 5, }; var rating2 = new CocktailRating { Id = 2, UserId = 2, CocktailId = 1, Value = 3, }; using (var arrangeContext = new CocktailMagicianContext(options)) { arrangeContext.Cocktails.AddAsync(cocktail); arrangeContext.Users.AddAsync(user1); arrangeContext.Users.AddAsync(user2); arrangeContext.CocktailRatings.AddAsync(rating1); arrangeContext.CocktailRatings.AddAsync(rating2); arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new CocktailRatingService(assertContext, mockDateTimeProvider.Object, mapper.Object); var result = sut.GetAverageCocktailRating(1); Assert.AreEqual(4, result); } }
public async Task ReturnCorrectBars_When_Searching_By_Rating() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnCorrectBars_When_Searching_By_Rating)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockBarDtoMapper = new Mock <IDtoMapper <Bar, BarDTO> >(); var newBar1 = new Bar { Id = 1, Name = "TestBar1", }; var newBar2 = new Bar { Id = 2, Name = "TestBar2", }; var list = new List <BarDTO>() { new BarDTO { Id = 1, Name = "TestBar1", AverageRating = 5 }, new BarDTO { Id = 2, Name = "TestBar2", AverageRating = 5 } }; mockBarDtoMapper.Setup(x => x.MapDto(It.IsAny <ICollection <Bar> >())).Returns(list); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Bars.AddAsync(newBar1); await arrangeContext.Bars.AddAsync(newBar2); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new BarService(assertContext, mockBarDtoMapper.Object, mockDateTimeProvider.Object); var result = await sut.SearchBarsAsync("5"); Assert.IsInstanceOfType(result, typeof(ICollection <BarDTO>)); Assert.AreEqual(2, result.Count); Assert.AreEqual(5, result.First().AverageRating); Assert.AreEqual(5, result.Last().AverageRating); } }
public async Task Return_Correct_Cocktail_WhenParamsAreValid() { //Arrange var options = TestUtilities.GetOptions(nameof(Return_Correct_Cocktail_WhenParamsAreValid)); var mockCocktailDtoMapper = new Mock <IDtoMapper <Cocktail, CocktailDto> >(); var mockCocktailIngretientService = new Mock <ICocktailIngredientService>(); var mockIngredientsService = new Mock <IIngredientService>(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var bar = new Bar { Id = 1 }; var cocktail = new Cocktail { Id = 1, Name = "TestCocktail" }; var barCocktail = new BarCocktail { BarId = 1, CocktailId = 1 }; var cocktailDto = new CocktailDto { Id = 1, Name = "TestCocktail", }; mockCocktailDtoMapper.Setup(x => x.MapDto(It.IsAny <Cocktail>())).Returns(cocktailDto); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.BarCocktails.AddAsync(barCocktail); await arrangeContext.Cocktails.AddAsync(cocktail); await arrangeContext.Bars.AddAsync(bar); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new CocktailService(assertContext, mockCocktailDtoMapper.Object, mockDateTimeProvider.Object, mockCocktailIngretientService.Object, mockIngredientsService.Object); var result = await sut.GetBarCocktailsAsync(1); Assert.IsInstanceOfType(result, typeof(ICollection <CocktailDto>)); Assert.AreEqual(1, result.First().Id); Assert.AreEqual("TestCocktail", result.First().Name); } }
public async Task ThrowWhen_IngredientIsContainsInCocktail() { //Arrange var options = TestUtilities.GetOptions(nameof(ThrowWhen_IngredientIsContainsInCocktail)); var mapper = new Mock <IDtoMapper <Ingredient, IngredientDto> >(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var ingredient = new Ingredient { Id = 1, Name = "Cola", IsDeleted = false }; var cocktail = new Cocktail { Id = 1, Name = "ColaCokctail" }; var cocktailIngr = new CocktailIngredient { CocktailId = cocktail.Id, IngredientId = ingredient.Id, }; using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Ingredients.AddAsync(ingredient); await arrangeContext.Cocktails.AddAsync(cocktail); await arrangeContext.CocktailIngredients.AddAsync(cocktailIngr); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new CocktailMagicianContext(options)) { //Act & Assert var sut = new IngredientService(assertContext, mapper.Object, mockDateTimeProvider.Object); await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.DeleteIngredientAsync(ingredient.Id)); } }
public async Task Update_Cocktail_Correctly() { //Arrange var options = TestUtilities.GetOptions(nameof(Update_Cocktail_Correctly)); var mockCocktailDtoMapper = new Mock <IDtoMapper <Cocktail, CocktailDto> >(); var mockCocktailIngretientService = new Mock <ICocktailIngredientService>(); var mockIngredientsService = new Mock <IIngredientService>(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var cocktail = new Cocktail { Id = 1, Name = "TestCocktail", ShortDescription = "TestShortDescription", LongDescription = "TestLongDescription", }; var cocktailDto = new CocktailDto { Id = 1, Name = "NewTestCocktail", ShortDescription = "NewTestShortDescription", LongDescription = "NewTestLongDescription", }; mockCocktailDtoMapper.Setup(x => x.MapDto(It.IsAny <Cocktail>())).Returns(cocktailDto); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Cocktails.AddAsync(cocktail); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new CocktailService(assertContext, mockCocktailDtoMapper.Object, mockDateTimeProvider.Object, mockCocktailIngretientService.Object, mockIngredientsService.Object); var result = await sut.UpdateCocktailAsync(cocktailDto); var editedCocktail = await assertContext.Cocktails.FirstAsync(); Assert.IsInstanceOfType(result, typeof(CocktailDto)); Assert.AreEqual("NewTestCocktail", editedCocktail.Name); Assert.AreEqual("NewTestShortDescription", editedCocktail.ShortDescription); Assert.AreEqual("NewTestLongDescription", editedCocktail.LongDescription); } }
public async Task <bool> DeleteUserAsync(int id) { var user = await this.context.Users .FirstOrDefaultAsync(user => !user.IsDeleted && user.Id == id); if (user == null) { return(false); } user.IsDeleted = true; context.Users.Update(user); await context.SaveChangesAsync(); return(true); }
public async Task Update_Bar_Correctly() { //Arrange var options = TestUtilities.GetOptions(nameof(Update_Bar_Correctly)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockBarDtoMapper = new Mock <IDtoMapper <Bar, BarDTO> >(); var newBar = new Bar { Id = 1, Name = "TestBar", Info = "TestInfo", Address = "TestAddress", }; var newBarDto = new BarDTO { Id = 1, Name = "NewTestBar", Info = "NewTestInfo", Address = "NewTestAddress", }; mockBarDtoMapper.Setup(x => x.MapDto(It.IsAny <Bar>())).Returns(newBarDto); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Bars.AddAsync(newBar); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new BarService(assertContext, mockBarDtoMapper.Object, mockDateTimeProvider.Object); var result = await sut.EditBarAsync(newBarDto); var editedBar = await assertContext.Bars.FirstAsync(); Assert.IsInstanceOfType(result, typeof(BarDTO)); Assert.AreEqual("NewTestBar", editedBar.Name); Assert.AreEqual("NewTestInfo", editedBar.Info); Assert.AreEqual("NewTestAddress", editedBar.Address); } }
public async Task <CocktailDto> CreateCocktailAsync(CocktailDto tempCocktailDto) { if (tempCocktailDto == null) { throw new ArgumentNullException("No entity found"); } if (tempCocktailDto.Ingredients.Count == 0) { throw new ArgumentNullException("The ingredients are missing"); } var cocktail = new Cocktail { Name = tempCocktailDto.Name, ShortDescription = tempCocktailDto.ShortDescription, LongDescription = tempCocktailDto.LongDescription, ImageUrl = tempCocktailDto.ImageUrl, ImageThumbnailUrl = tempCocktailDto.ImageThumbnailUrl, }; await _context.Cocktails.AddAsync(cocktail); await _context.SaveChangesAsync(); foreach (var item in tempCocktailDto.Ingredients) { var ingredient = await this._ingredientService.GetIngredientByNameAsync(item); if (ingredient == null) { return(null); } var cocktailIngredient = await this._cocktailIngredientService.CreateCocktailIngredientAsync(cocktail.Id, ingredient.Id); cocktail.CocktailIngredients.Add(cocktailIngredient); await this._context.SaveChangesAsync(); } var cocktailDto = this._cocktailDtoMapper.MapDto(cocktail); return(cocktailDto); }
public async Task Create_Comment_Correctly() { //Arrange var options = TestUtilities.GetOptions(nameof(Create_Comment_Correctly)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockBarCommentDtoMapper = new Mock <IDtoMapper <BarComment, BarCommentDto> >(); var user = new User { Id = 1 }; var bar = new Bar { Id = 1 }; var commentDto = new BarCommentDto { Id = 1, UserId = 1, BarId = 1, Text = "TestComment", }; mockBarCommentDtoMapper.Setup(x => x.MapDto(It.IsAny <BarComment>())).Returns(commentDto); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Bars.AddAsync(bar); await arrangeContext.Users.AddAsync(user); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new BarCommentsService(assertContext, mockBarCommentDtoMapper.Object, mockDateTimeProvider.Object); var result = await sut.CreateCommentAsync(commentDto); Assert.IsInstanceOfType(result, typeof(BarCommentDto)); Assert.AreEqual(1, result.Id); Assert.AreEqual(1, result.BarId); Assert.AreEqual(1, result.UserId); Assert.AreEqual("TestComment", result.Text); } }
public async Task SetCorrectParam_WhenValueIsValid() { //Arrange var options = TestUtilities.GetOptions(nameof(SetCorrectParam_WhenValueIsValid)); var mapperMock = new Mock <IDtoMapper <Ingredient, IngredientDto> >(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var ingredient = new Ingredient { Id = 1, Name = "Cola", IsDeleted = false }; var ingredientDto = new IngredientDto { Id = 1, Name = "Coca-cola", }; mapperMock.Setup(x => x.MapDto(It.IsAny <Ingredient>())).Returns(ingredientDto); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Ingredients.AddAsync(ingredient); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new CocktailMagicianContext(options)) { //Act & Assert var sut = new IngredientService(assertContext, mapperMock.Object, mockDateTimeProvider.Object); var result = await sut.EditIngredientAsync(1, "Coca-cola"); var selectIngredient = await assertContext.Ingredients.FirstAsync(); Assert.AreEqual("Coca-cola", selectIngredient.Name); } }
public async Task ReturnCorrectInstance_IngredientDtoByName() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnCorrectInstance_IngredientDtoByName)); var mapper = new Mock <IDtoMapper <Ingredient, IngredientDto> >(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var ingredient = new Ingredient { Id = 1, Name = "Cola", IsDeleted = false }; var ingredientDto = new IngredientDto { Id = 1, Name = "Cola", }; mapper.Setup(x => x.MapDto(It.IsAny <Ingredient>())).Returns(ingredientDto); using (var arrangeContext = new CocktailMagicianContext(options)) { //Act await arrangeContext.Ingredients.AddAsync(ingredient); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new CocktailMagicianContext(options)) { //Assert var sut = new IngredientService(assertContext, mapper.Object, mockDateTimeProvider.Object); var result = await sut.GetIngredientByNameAsync("Cola"); Assert.AreEqual(ingredient.Id, result.Id); Assert.AreEqual(ingredient.Name, result.Name); Assert.IsInstanceOfType(result, typeof(Ingredient)); } }
public async Task ReturnNull_When_DtoParamsAreInvalid() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnNull_When_DtoParamsAreInvalid)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockBarCommentDtoMapper = new Mock <IDtoMapper <BarComment, BarCommentDto> >(); var user = new User { Id = 1 }; var bar = new Bar { Id = 1 }; var commentDto = new BarCommentDto { Id = 1, UserId = 0, BarId = 0, Text = null, }; using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Bars.AddAsync(bar); await arrangeContext.Users.AddAsync(user); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new BarCommentsService(assertContext, mockBarCommentDtoMapper.Object, mockDateTimeProvider.Object); var result = await sut.CreateCommentAsync(null); Assert.IsNull(result); } }
public async Task DeleteIngredient_Correctly() { //Arrange var options = TestUtilities.GetOptions(nameof(DeleteIngredient_Correctly)); var mapper = new Mock <IDtoMapper <Ingredient, IngredientDto> >(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var ingredient = new Ingredient { Id = 1, Name = "Cola", IsDeleted = false }; var ingredientDto = new IngredientDto { Id = 1, Name = "Cola", }; mapper.Setup(x => x.MapDto(It.IsAny <Ingredient>())).Returns(ingredientDto); using (var arrangeContext = new CocktailMagicianContext(options)) { //Act await arrangeContext.Ingredients.AddAsync(ingredient); await arrangeContext.SaveChangesAsync(); var barService = new IngredientService(arrangeContext, mapper.Object, mockDateTimeProvider.Object); var result = await barService.DeleteIngredientAsync(1); } using (var assertContext = new CocktailMagicianContext(options)) { //Assert Assert.IsTrue(ingredient.IsDeleted); } }
public async Task Return_CorrectModel_WhenParamsAreValid() { //Arrange var options = TestUtilities.GetOptions(nameof(Return_CorrectModel_WhenParamsAreValid)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockBarDtoMapper = new Mock <IDtoMapper <Bar, BarDTO> >(); var newBar = new Bar { Id = 1, Name = "TestBar", }; var barDto = new BarDTO { Id = 1, Name = "TestBar", }; mockBarDtoMapper.Setup(x => x.MapDto(It.IsAny <Bar>())).Returns(barDto); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Bars.AddAsync(newBar); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new BarService(assertContext, mockBarDtoMapper.Object, mockDateTimeProvider.Object); var result = await sut.GetBarAsync(1); Assert.IsInstanceOfType(result, typeof(BarDTO)); Assert.AreEqual(1, result.Id); Assert.AreEqual("TestBar", result.Name); } }
public async Task ReturnInstanceOfCollectionIngredientDto() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnInstanceOfCollectionIngredientDto)); var mapper = new Mock <IDtoMapper <Ingredient, IngredientDto> >(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var ingredient1 = new Ingredient { Id = 1, Name = "Cola", IsDeleted = false }; var ingredient2 = new Ingredient { Id = 2, Name = "Ice", IsDeleted = false }; var cocktail = new Cocktail { Id = 1, Name = "IceCola", ShortDescription = "iceiceBaby" }; var cocktailIngr1 = new CocktailIngredient { CocktailId = cocktail.Id, IngredientId = ingredient1.Id, }; var cocktailIngr2 = new CocktailIngredient { CocktailId = cocktail.Id, IngredientId = ingredient2.Id, }; var ingredientsDto = new List <IngredientDto> { new IngredientDto { Id = 1, Name = "Cola" }, new IngredientDto { Id = 2, Name = "Ice" } }; mapper.Setup(x => x.MapDto(It.IsAny <ICollection <Ingredient> >())).Returns(ingredientsDto); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Ingredients.AddAsync(ingredient1); await arrangeContext.Ingredients.AddAsync(ingredient2); await arrangeContext.Cocktails.AddAsync(cocktail); await arrangeContext.CocktailIngredients.AddAsync(cocktailIngr1); await arrangeContext.CocktailIngredients.AddAsync(cocktailIngr2); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new CocktailMagicianContext(options)) { //Act & Assert var sut = new IngredientService(assertContext, mapper.Object, mockDateTimeProvider.Object); var result = await sut.GetCocktailIngredientsAsync(1); Assert.IsInstanceOfType(result, typeof(ICollection <IngredientDto>)); } }
public async Task ReturnCorrectModels_When_ParamsAreValid() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnCorrectModels_When_ParamsAreValid)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockBarRatingDtoMapper = new Mock <IDtoMapper <BarRating, BarRatingDto> >(); var user1 = new User { Id = 1 }; var user2 = new User { Id = 2 }; var bar = new Bar { Id = 1 }; var rating1 = new BarRating { Id = 1, UserId = 1, BarId = 1, Value = 5, }; var rating2 = new BarRating { Id = 2, UserId = 2, BarId = 1, Value = 3, }; var list = new List <BarRatingDto>() { new BarRatingDto { Id = 1, UserId = 1, BarId = 1, Value = 5, }, new BarRatingDto { Id = 2, UserId = 2, BarId = 1, Value = 3, } }; mockBarRatingDtoMapper.Setup(x => x.MapDto(It.IsAny <ICollection <BarRating> >())).Returns(list); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Bars.AddAsync(bar); await arrangeContext.Users.AddAsync(user1); await arrangeContext.Users.AddAsync(user2); await arrangeContext.BarRatings.AddAsync(rating1); await arrangeContext.BarRatings.AddAsync(rating2); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new BarRatingService(assertContext, mockBarRatingDtoMapper.Object, mockDateTimeProvider.Object); var result = await sut.GetAllBarRatingAsync(1); Assert.AreEqual(2, result.Count); Assert.AreEqual(1, result.First().Id); Assert.AreEqual(1, result.First().UserId); Assert.AreEqual(1, result.First().BarId); Assert.AreEqual(5, result.First().Value); Assert.AreEqual(2, result.Last().Id); Assert.AreEqual(2, result.Last().UserId); Assert.AreEqual(1, result.Last().BarId); Assert.AreEqual(3, result.Last().Value); } }
public async Task ReturnCorrectCountOfIngredientsInCocktail() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnCorrectCountOfIngredientsInCocktail)); var mapper = new Mock <IDtoMapper <Ingredient, IngredientDto> >(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var ingredient1 = new Ingredient { Id = 1, Name = "Cola", IsDeleted = false }; var ingredient2 = new Ingredient { Id = 2, Name = "Ice", IsDeleted = false }; var cocktail = new Cocktail { Id = 1, Name = "IceCola", ShortDescription = "iceiceBaby" }; var cocktailIngr1 = new CocktailIngredient { CocktailId = cocktail.Id, IngredientId = ingredient1.Id, }; var cocktailIngr2 = new CocktailIngredient { CocktailId = cocktail.Id, IngredientId = ingredient2.Id, }; using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Ingredients.AddAsync(ingredient1); await arrangeContext.Ingredients.AddAsync(ingredient2); await arrangeContext.Cocktails.AddAsync(cocktail); await arrangeContext.CocktailIngredients.AddAsync(cocktailIngr1); await arrangeContext.CocktailIngredients.AddAsync(cocktailIngr2); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new CocktailMagicianContext(options)) { //Act & Assert var sut = new IngredientService(assertContext, mapper.Object, mockDateTimeProvider.Object); var result = await sut.GetCocktailIngredientsAsync(1); Assert.AreEqual(2, result.Count()); } }
public async Task Return_CorrectModel_When_ParamsAreValid() { //Arrange var options = TestUtilities.GetOptions(nameof(Return_CorrectModel_When_ParamsAreValid)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockBarCommentDtoMapper = new Mock <IDtoMapper <BarComment, BarCommentDto> >(); var user = new User { Id = 1 }; var bar = new Bar { Id = 1 }; var comment1 = new BarComment { Id = 1, UserId = 1, BarId = 1, Text = "TestComment1", }; var comment2 = new BarComment { Id = 2, UserId = 1, BarId = 1, Text = "TestComment2", }; var list = new List <BarCommentDto> { new BarCommentDto { Id = 1, UserId = 1, BarId = 1, Text = "TestComment1", }, new BarCommentDto { Id = 2, UserId = 1, BarId = 1, Text = "TestComment2", } }; mockBarCommentDtoMapper.Setup(x => x.MapDto(It.IsAny <ICollection <BarComment> >())).Returns(list); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Bars.AddAsync(bar); await arrangeContext.Users.AddAsync(user); await arrangeContext.BarComments.AddAsync(comment1); await arrangeContext.BarComments.AddAsync(comment2); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new BarCommentsService(assertContext, mockBarCommentDtoMapper.Object, mockDateTimeProvider.Object); var result = await sut.GetBarCommentsAsync(1); Assert.IsInstanceOfType(result, typeof(ICollection <BarCommentDto>)); Assert.AreEqual(1, result.First().Id); Assert.AreEqual(1, result.First().UserId); Assert.AreEqual(1, result.First().BarId); Assert.AreEqual("TestComment1", result.First().Text); Assert.AreEqual(2, result.Last().Id); Assert.AreEqual(1, result.Last().UserId); Assert.AreEqual(1, result.Last().BarId); Assert.AreEqual("TestComment2", result.Last().Text); } }