public async Task Throw_When_DtoIngredientsIsNull() { //Arrange var options = TestUtilities.GetOptions(nameof(Throw_When_DtoIngredientsIsNull)); 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[] { }; var cocktailDto = new CocktailDto { Id = 1, Name = "TestCocktail", Ingredients = ingredients, }; //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new CocktailService(assertContext, mockCocktailDtoMapper.Object, mockDateTimeProvider.Object, mockCocktailIngretientService.Object, mockIngredientsService.Object); await Assert.ThrowsExceptionAsync <ArgumentNullException>(() => sut.CreateCocktailAsync(cocktailDto)); } }
public async Task CreateCocktailCorrectly() { //Arrange var options = TestUtilities.GetOptions(nameof(CreateCocktailCorrectly)); var ingredientServiceMock = new Mock <ICocktailIngredientService>(); var cocktailMapperToDTOMock = new Mock <IDTOServiceMapper <CocktailDTO, Cocktail> >(); var cocktailMapperMock = new Mock <IDTOServiceMapper <Cocktail, CocktailDTO> >(); var commentMapperToDTOMock = new Mock <IDTOServiceMapper <CommentDTO, CocktailComment> >(); var commentMapperMock = new Mock <IDTOServiceMapper <CocktailComment, CommentDTO> >(); var addCocktailMapperMock = new Mock <IDTOServiceMapper <Cocktail, AddCocktailDTO> >(); var cocktailRatingToDTOMock = new Mock <IDTOServiceMapper <RatingDTO, CocktailRating> >(); var cocktailDTOMock = new Mock <CocktailDTO>(); var cocktailIngredientDTOMock = new Mock <List <CocktailIngredientDTO> >(); var cocktailMock = new Mock <Cocktail>(); cocktailDTOMock.Object.Ingredients = cocktailIngredientDTOMock.Object; //Act cocktailMapperToDTOMock.Setup(m => m.MapFrom(It.IsAny <CocktailDTO>())).Returns(cocktailMock.Object); using (var actContext = new IriOnCocktailServiceDbContext(options)) { var sut = new CocktailService(actContext, ingredientServiceMock.Object, cocktailMapperMock.Object, cocktailMapperToDTOMock.Object, commentMapperToDTOMock.Object, commentMapperMock.Object, addCocktailMapperMock.Object, cocktailRatingToDTOMock.Object); await sut.CreateCocktail(cocktailDTOMock.Object); } using (var assertContext = new IriOnCocktailServiceDbContext(options)) { Assert.AreEqual(1, assertContext.Cocktails.Count()); } }
public async Task ReturnTrue_When_CocktailIsDeleted() { var options = Utils.GetOptions(nameof(ReturnTrue_When_CocktailIsDeleted)); var cocktail = new Cocktail() { Id = Guid.Parse("9ef97551-87f6-40ce-a88b-6c0e876ccb51"), Name = "Margarita", Description = "The Margarita is one of the most " + "popular cocktails in North America—for good reason. " + "Combining the tang of lime and the sweetness of o" + "range liqueur with the distinctive strength of " + "tequila, our classic Margarita strikes all of the right keys." }; using (var arrangeContext = new CMContext(options)) { arrangeContext.Cocktails.Add(cocktail); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new CMContext(options)) { var sut = new CocktailService(assertContext); Guid resultGuid = new Guid("9ef97551-87f6-40ce-a88b-6c0e876ccb51"); var result = await sut.DeleteCocktail(resultGuid); Assert.IsTrue(result); } }
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 Throw_When_NoCocktailsFound() { //Arrange var options = TestUtilities.GetOptions(nameof(Throw_When_NoCocktailsFound)); var mockCocktailDtoMapper = new Mock <IDtoMapper <Cocktail, CocktailDto> >(); var mockCocktailIngretientService = new Mock <ICocktailIngredientService>(); var mockIngredientsService = new Mock <IIngredientService>(); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var cocktailDto = new CocktailDto { Id = 1, Name = "NewTestCocktail", ShortDescription = "NewTestShortDescription", LongDescription = "NewTestLongDescription", }; //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new CocktailService(assertContext, mockCocktailDtoMapper.Object, mockDateTimeProvider.Object, mockCocktailIngretientService.Object, mockIngredientsService.Object); await Assert.ThrowsExceptionAsync <ArgumentNullException>(() => sut.UpdateCocktailAsync(cocktailDto)); } }
public async Task ReturnCorrectCocktails() { //Arrange var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockCocktailMapper = new Mock <ICocktailMapper>(); mockCocktailMapper.Setup(c => c.MapToCocktailDTO(It.IsAny <Cocktail>())) .Returns <Cocktail>(c => new CocktailDTO { Id = c.Id, Name = c.Name, AverageRating = c.AverageRating }); var mockIngMapper = new Mock <IIngredientMapper>(); var mockBarMapper = new Mock <IBarMapper>(); var mockCocktailReviewService = new Mock <ICocktailReviewService>(); var options = Utils.GetOptions(nameof(ReturnCorrectCocktails)); Utils.GetInMemoryDataBase(options); //Act & Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new CocktailService(mockDateTimeProvider.Object, mockCocktailMapper.Object, mockIngMapper.Object, mockBarMapper.Object, assertContext, mockCocktailReviewService.Object); var result = (await sut.GetAllCocktailssAsync()).ToList(); var cocktailsCount = assertContext.Cocktails.Count(); Assert.AreEqual(cocktailsCount, result.Count); } }
public async Task CorrectlyHideCocktail() { string cocktailName = "testName"; int cocktailId = 14; double oldRating = 2.5; byte[] coverPhoto = new byte[0]; int cocktailCommentsCount = 2; string[] primaryIngredients = new string[1] { "test1" }; var mockIngredientService = new Mock <IIngredientService>().Object; var options = TestUtilities.GetOptions(nameof(CorrectlyHideCocktail)); using (var arrangeContext = new CocktailDatabaseContext(options)) { arrangeContext.Cocktails.Add(new Cocktail() { Name = cocktailName, Id = cocktailId, Hidden = 0 }); arrangeContext.SaveChanges(); } using (var assertContext = new CocktailDatabaseContext(options)) { var sut = new CocktailService(assertContext, mockIngredientService); await sut.HideCocktailAsync(cocktailId); Assert.AreEqual(1, assertContext.Cocktails.First().Hidden); } }
public async Task ThrowArgumentException_When_CocktailAlreadyExists() { var testCocktailName = "TestCocktailName"; var options = TestUtilities.GetOptions(nameof(ThrowArgumentException_When_CocktailAlreadyExists)); var cocktailCreateRequest = new CocktailCreateRequest() { Name = testCocktailName }; using (var arrangeContext = new AppDBContext(options)) { await arrangeContext.Cocktails.AddAsync(new CocktailEntity() { Name = testCocktailName }); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new AppDBContext(options)) { var sut = new CocktailService(assertContext); await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.Create(cocktailCreateRequest)); } }
public async Task CocktailCommentAsync_ThrowsWhenNull() { //Arrange var options = TestUtilities.GetOptions(nameof(CocktailCommentAsync_ThrowsWhenNull)); var ingredientServiceMock = new Mock <ICocktailIngredientService>(); var cocktailMapperToDTOMock = new Mock <IDTOServiceMapper <CocktailDTO, Cocktail> >(); var cocktailMapperMock = new Mock <IDTOServiceMapper <Cocktail, CocktailDTO> >(); var commentMapperToDTOMock = new Mock <IDTOServiceMapper <CommentDTO, CocktailComment> >(); var commentMapperMock = new Mock <IDTOServiceMapper <CocktailComment, CommentDTO> >(); var addCocktailMapperMock = new Mock <IDTOServiceMapper <Cocktail, AddCocktailDTO> >(); var cocktailRatingToDTOMock = new Mock <IDTOServiceMapper <RatingDTO, CocktailRating> >(); //Act var CommentDTOMock = new Mock <CommentDTO>(); //commentMapperToDTOMock.Setup(m => m.MapFrom(It.IsAny<CommentDTO>())).Returns(null); using (var actContext = new IriOnCocktailServiceDbContext(options)) { var sut = new CocktailService(actContext, ingredientServiceMock.Object, cocktailMapperMock.Object, cocktailMapperToDTOMock.Object, commentMapperToDTOMock.Object, commentMapperMock.Object, addCocktailMapperMock.Object, cocktailRatingToDTOMock.Object); await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.CocktailCommentAsync(CommentDTOMock.Object)); } }
public async Task Should_ReturnCocktailCorrectlyFromGivenName() { //arrange string cocktailName = "testName"; int cocktailId = 14; byte[] coverPhoto = new byte[0]; string[] primaryIngredients = new string[1] { "test1" }; var mockIngredientService = new Mock <IIngredientService>().Object; var options = TestUtilities.GetOptions(nameof(Should_ReturnCocktailCorrectlyFromGivenName)); using (var arrangeContext = new CocktailDatabaseContext(options)) { arrangeContext.Cocktails.Add(new Cocktail() { Name = cocktailName });; arrangeContext.SaveChanges(); } using (var assertContext = new CocktailDatabaseContext(options)) { var sut = new CocktailService(assertContext, mockIngredientService); var cocktail = await sut.FindCocktailByNameAsync(cocktailName); Assert.IsNotNull(cocktail); Assert.AreEqual(cocktailName, cocktail.Name); } }
public async Task GetAllCocktails_ForHM_Correct() { var options = Utils.GetOptions(nameof(GetAllCocktails_ForHM_Correct)); var cocktail = new Cocktail() { Id = Guid.Parse("9ef97551-87f6-40ce-a88b-6c0e876ccb51"), Name = "Margarita", Description = "The Margarita is one of the most " + "popular cocktails in North America—for good reason. " + "Combining the tang of lime and the sweetness of o" + "range liqueur with the distinctive strength of " + "tequila, our classic Margarita strikes all of the right keys.", AvgRating = 5 }; var cocktail2 = new Cocktail() { Id = Guid.Parse("8a15e590-0b66-4fae-abfa-d75812b76da6"), Name = "White Russian", Description = "The White Russian is decadent and sophisticated. " + "Combining vodka, Kahlúa and cream and serving it on the " + "rocks create a delicious alternative to adult milkshakes. The Dude's favourite one.", AvgRating = 4, }; var cocktail3 = new Cocktail() { Id = Guid.Parse("96a5f42a-e14d-4cf6-834c-368f4e77076c"), Name = "Dry Martini", Description = "You could stumble down a very deep, very dark rabbit hole trying to determine who mixed the world’s first Martini. Was it a California prospector during the 1849 Gold Rush or the barman at a flossy New York City hotel 50 years later? Both stories hold water. Neither will leave you feeling as blissful and content as a well-made Dry Martini.", AvgRating = 4 }; var cocktail4 = new Cocktail() { Id = Guid.Parse("74d3f564-5811-4eda-97d6-e39d6bbd35a9"), Name = "Cosmopolitan", Description = "The legendary Cosmopolitan is a simple cocktail with a big history. It reached its height of popularity in the 1990s, when the HBO show “Sex and the City” was at its peak. The pink-hued Martini was a favorite of the characters on the show.", AvgRating = 3 }; using (var arrangeContext = new CMContext(options)) { arrangeContext.Cocktails.Add(cocktail); arrangeContext.Cocktails.Add(cocktail2); arrangeContext.Cocktails.Add(cocktail3); arrangeContext.Cocktails.Add(cocktail4); await arrangeContext.SaveChangesAsync(); } //Act,Assert using (var assertContext = new CMContext(options)) { var sut = new CocktailService(assertContext); var result = await sut.GetAllCocktailsForHomePage(); Assert.AreEqual(cocktail.Id, result[0].Id); Assert.AreEqual(cocktail.Name, result[0].Name); Assert.AreEqual(3, result.Count); } }
public async Task GetAllCocktailsDTOReturnsCorrectDTOs() { var options = TestUtilities.GetOptions(nameof(GetAllCocktailsDTOReturnsCorrectDTOs)); var ingredientServiceMock = new Mock <ICocktailIngredientService>(); var cocktailMapperToDTOMock = new Mock <IDTOServiceMapper <CocktailDTO, Cocktail> >(); var cocktailMapperMock = new Mock <IDTOServiceMapper <Cocktail, CocktailDTO> >(); var commentMapperToDTOMock = new Mock <IDTOServiceMapper <CommentDTO, CocktailComment> >(); var commentMapperMock = new Mock <IDTOServiceMapper <CocktailComment, CommentDTO> >(); var addCocktailMapperMock = new Mock <IDTOServiceMapper <Cocktail, AddCocktailDTO> >(); var cocktailRatingToDTOMock = new Mock <IDTOServiceMapper <RatingDTO, CocktailRating> >(); var ingrMapper = new Mock <IDTOServiceMapper <CocktailIngredient, CocktailIngredientDTO> >(); ingrMapper.Setup(x => x.MapFrom(It.IsAny <CocktailIngredient>())).Returns(It.IsAny <CocktailIngredientDTO>()); var cocktail = new Cocktail() { Name = "Name", Id = "1", Motto = "Motto", PicUrl = "Pic", }; var cocktail2 = new Cocktail() { Name = "Name2", Id = "12", Motto = "Motto2", PicUrl = "Pic2", }; var cocktailDTO = new CocktailDTO() { Name = "Name", Id = "1", Motto = "Motto", PicUrl = "Pic", }; //Act cocktailMapperMock.Setup(m => m.MapFrom(It.IsAny <Cocktail>())).Returns(cocktailDTO); using (var actContext = new IriOnCocktailServiceDbContext(options)) { await actContext.Cocktails.AddAsync(cocktail); await actContext.Cocktails.AddAsync(cocktail2); await actContext.SaveChangesAsync(); } using (var assertContext = new IriOnCocktailServiceDbContext(options)) { var sut = new CocktailService(assertContext, ingredientServiceMock.Object, cocktailMapperMock.Object, cocktailMapperToDTOMock.Object, commentMapperToDTOMock.Object, commentMapperMock.Object, addCocktailMapperMock.Object, cocktailRatingToDTOMock.Object); var result = await sut.GetAllCocktailsDTO(); Assert.AreEqual(2, result.Count); } }
public async Task OrderCocktailsByRatingAscending() { //Arrange var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockCocktailMapper = new Mock <ICocktailMapper>(); mockCocktailMapper .Setup(c => c.MapToCocktailDTO(It.IsAny <Cocktail>())) .Returns <Cocktail>(c => new CocktailDTO { Name = c.Name, AverageRating = c.AverageRating }); var mockIngMapper = new Mock <IIngredientMapper>(); mockIngMapper .Setup(i => i.MapToIngredientDTO(It.IsAny <Ingredient>())) .Returns <Ingredient>(i => new IngredientDTO { Name = i.Name }); var mockBarMapper = new Mock <IBarMapper>(); var mockCocktailReviewService = new Mock <ICocktailReviewService>(); var options = Utils.GetOptions(nameof(OrderCocktailsByRatingAscending)); var orderBy = "AverageRating"; var direction = "asc"; var expected = new List <CocktailDTO> { new CocktailDTO { Name = "Bozdugan", AverageRating = 3.8, }, new CocktailDTO { Name = "Mojito", AverageRating = 4.5, }, new CocktailDTO { Name = "Gin Fizz", AverageRating = 4.9 } }; Utils.GetInMemoryDataBase(options); //Act & Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new CocktailService(mockDateTimeProvider.Object, mockCocktailMapper.Object, mockIngMapper.Object, mockBarMapper.Object, assertContext, mockCocktailReviewService.Object); var result = await sut.ListAllCocktailsAsync(0, 10, null, orderBy, direction); Assert.AreEqual(expected.Count, result.Count); for (int i = 0; i < expected.Count; i++) { Assert.AreEqual(expected[i].Name, result[i].Name); } } }
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 UpdateCorrectlyCocktailAverageRating() { //arrange string cocktailName = "testName"; int cocktailId = 14; int barId = 12; double oldRating = 2.5; byte[] coverPhoto = new byte[0]; int cocktailCommentsCount = 2; string[] primaryIngredients = new string[1] { "test1" }; var mockIngredientService = new Mock <IIngredientService>().Object; var options = TestUtilities.GetOptions(nameof(UpdateCorrectlyCocktailAverageRating)); using (var arrangeContext = new CocktailDatabaseContext(options)) { arrangeContext.Cocktails.Add(new Cocktail() { Name = cocktailName, Id = cocktailId, AverageRating = oldRating });; arrangeContext.SaveChanges(); } using (var arrangeContext = new CocktailDatabaseContext(options)) { arrangeContext.Users.Add(new User { UserName = "******" }); arrangeContext.Users.Add(new User { UserName = "******" }); arrangeContext.SaveChanges(); } using (var actContext = new CocktailDatabaseContext(options)) { actContext.CocktailRating.Add(new CocktailRating() { Cocktail = actContext.Cocktails.First(), User = actContext.Users.First(), Rating = 6 }); actContext.CocktailRating.Add(new CocktailRating() { Cocktail = actContext.Cocktails.First(), User = actContext.Users.Skip(1).First(), Rating = 4 }); actContext.SaveChanges(); } using (var assertContext = new CocktailDatabaseContext(options)) { var sut = new CocktailService(assertContext, mockIngredientService); await sut.UpdateAverageRatingAsync(cocktailId); Assert.AreNotEqual(oldRating, assertContext.Cocktails.First().AverageRating); } }
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 Shoud_ReturnCocktailCommentsCorrectly() { //arrange string cocktailName = "testName"; int cocktailId = 14; byte[] coverPhoto = new byte[0]; int cocktailCommentsCount = 2; string[] primaryIngredients = new string[1] { "test1" }; var mockIngredientService = new Mock <IIngredientService>().Object; var options = TestUtilities.GetOptions(nameof(Shoud_ReturnCocktailCommentsCorrectly)); using (var arrangeContext = new CocktailDatabaseContext(options)) { arrangeContext.Cocktails.Add(new Cocktail() { Name = cocktailName, Id = cocktailId });; arrangeContext.SaveChanges(); } using (var arrangeContext = new CocktailDatabaseContext(options)) { arrangeContext.Users.Add(new User { UserName = "******" }); arrangeContext.Users.Add(new User { UserName = "******" }); arrangeContext.SaveChanges(); } using (var actContext = new CocktailDatabaseContext(options)) { actContext.CocktailComment.Add(new CocktailComment() { Cocktail = actContext.Cocktails.First(), User = actContext.Users.First() }); actContext.CocktailComment.Add(new CocktailComment() { Cocktail = actContext.Cocktails.First(), User = actContext.Users.Skip(1).First() }); actContext.SaveChanges(); } using (var assertContext = new CocktailDatabaseContext(options)) { var sut = new CocktailService(assertContext, mockIngredientService); var comments = await sut.GetCocktailCommentsAsync(cocktailId, 6); Assert.AreEqual(cocktailCommentsCount, comments.Count()); } }
public async Task ReturnCorrectTypeOfInstance() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnCorrectTypeOfInstance)); var mapperMock = new Mock <IDtoMapper <Cocktail, CocktailDto> >(); var barMapperMock = new Mock <IDtoMapper <Bar, BarDto> >(); var cocktailDetailsMapperMock = new Mock <IDtoMapper <Cocktail, DetailsCocktailDto> >(); var ingredientMapperMock = new Mock <IDtoMapper <Ingredient, IngredientDto> >(); var ingredientServiceMock = new Mock <IIngredientService>(); var cocktailIngredientServiceMock = new Mock <ICocktailIngredientService>(); var testGuid = Guid.NewGuid(); var entity = new Cocktail { Id = testGuid, Name = "TestOneName", ImagePath = "ImagePathOne", Info = "TestInfo" }; var dtoEntity = new CocktailDto { Id = testGuid, Name = "TestOneName", ImagePath = "ImagePathOne", Info = "TestInfo" }; mapperMock.Setup(x => x.MapFrom(It.IsAny <Cocktail>())).Returns(dtoEntity); using (var actContext = new CWContext(options)) { //Act await actContext.Cocktails.AddAsync(entity); await actContext.SaveChangesAsync(); } using (var assertContext = new CWContext(options)) { //Assert var sut = new CocktailService(assertContext, mapperMock.Object, barMapperMock.Object, cocktailDetailsMapperMock.Object, ingredientServiceMock.Object, cocktailIngredientServiceMock.Object); var result = await sut.DeleteAsync(testGuid); Assert.IsInstanceOfType(result, typeof(CocktailDto)); Assert.AreEqual(dtoEntity.Name, result.Name); Assert.AreEqual(dtoEntity.Info, result.Info); Assert.AreEqual(dtoEntity.ImagePath, result.ImagePath); Assert.AreEqual(dtoEntity.Id, result.Id); } }
public async Task ThrowArgumentException_WhenCocktailIsMissing() { var testCocktailID = 1; var options = TestUtilities.GetOptions(nameof(ThrowArgumentException_WhenCocktailIsMissing)); using (var assertContext = new AppDBContext(options)) { var sut = new CocktailService(assertContext); await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.GetCocktail(testCocktailID)); } }
public async Task Throw_Exception_When_Cocktail_IsNull() { var options = Utils.GetOptions(nameof(Throw_Exception_When_Cocktail_IsNull)); using (var assertContext = new CMContext(options)) { var sut = new CocktailService(assertContext); await Assert.ThrowsExceptionAsync <ArgumentNullException> (async() => await sut.CreateCocktail(null)); } }
public async Task UpdateCocktail_WhenParamsAreValid() { //Arrange var mockDateTimeprovider = new Mock <IDateTimeProvider>(); var mockCocktailMapper = new Mock <ICocktailMapper>(); mockCocktailMapper.Setup(c => c.MapToCocktail(It.IsAny <CocktailDTO>())) .Returns <CocktailDTO>(c => new Cocktail { Id = c.Id, Name = c.Name, IsDeleted = c.IsDeleted }); var mockIngMapper = new Mock <IIngredientMapper>(); mockIngMapper.Setup(i => i.MapToIngredient(It.IsAny <IngredientDTO>())) .Returns <IngredientDTO>(i => new Ingredient { Id = i.Id, Name = i.Name }); var mockBarMapper = new Mock <IBarMapper>(); var mockCocktailReviewService = new Mock <ICocktailReviewService>(); var options = Utils.GetOptions(nameof(UpdateCocktail_WhenParamsAreValid)); Utils.GetInMemoryDataBase(options); var newDTO = new CocktailDTO { Name = "Gin sTonik", Ingredients = new List <IngredientDTO> { new IngredientDTO { Id = 3 }, new IngredientDTO { Id = 4 } } }; //Act & Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new CocktailService(mockDateTimeprovider.Object, mockCocktailMapper.Object, mockIngMapper.Object, mockBarMapper.Object, assertContext, mockCocktailReviewService.Object); var result = await sut.UpdateCocktailAsync(2, newDTO); Assert.AreEqual(newDTO.Id, result.Id); Assert.AreEqual(newDTO.Name, result.Name); Assert.AreEqual(newDTO.IsDeleted, result.IsDeleted); Assert.AreEqual(newDTO.Ingredients.ToList().Count, result.Ingredients.ToList().Count); CollectionAssert.AreEqual(newDTO.Ingredients.ToList(), result.Ingredients.ToList()); } }
public async Task <IHttpActionResult> GetCocktail(int id) { CocktailService cocktailService = new CocktailService(_db); var cocktail = await cocktailService.GetCocktailAsync(id); if (cocktail == null) { return(NotFound()); } return(Ok(cocktail)); }
public async Task Throw_When_CINotFound() { var options = Utils.GetOptions(nameof(Throw_When_CINotFound)); using (var assertContext = new CMContext(options)) { var sut = new CocktailService(assertContext); await Assert.ThrowsExceptionAsync <ArgumentNullException> (async() => await sut.AddIngredientToCocktail(null)); } }
public async Task ThrowArgumentException_WhenCocktailDoesntExist() { var testCocktailId = 2; var options = TestUtilities.GetOptions(nameof(ThrowArgumentException_WhenCocktailDoesntExist)); using (var assertContext = new AppDBContext(options)) { var sut = new CocktailService(assertContext); await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.Toggle(testCocktailId)); } }
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 GetAllCocktailDTOs_ThrowsCorrectMsgWhenNotAvailable() { var options = TestUtilities.GetOptions(nameof(GetAllCocktailDTOs_ThrowsCorrectMsgWhenNotAvailable)); var ingredientServiceMock = new Mock <ICocktailIngredientService>(); var cocktailMapperToDTOMock = new Mock <IDTOServiceMapper <CocktailDTO, Cocktail> >(); var cocktailMapperMock = new Mock <IDTOServiceMapper <Cocktail, CocktailDTO> >(); var commentMapperToDTOMock = new Mock <IDTOServiceMapper <CommentDTO, CocktailComment> >(); var commentMapperMock = new Mock <IDTOServiceMapper <CocktailComment, CommentDTO> >(); var addCocktailMapperMock = new Mock <IDTOServiceMapper <Cocktail, AddCocktailDTO> >(); var cocktailRatingToDTOMock = new Mock <IDTOServiceMapper <RatingDTO, CocktailRating> >(); var ingrMapper = new Mock <IDTOServiceMapper <CocktailIngredient, CocktailIngredientDTO> >(); ingrMapper.Setup(x => x.MapFrom(It.IsAny <CocktailIngredient>())).Returns(It.IsAny <CocktailIngredientDTO>()); var cocktail = new Cocktail() { Name = "Name", Id = "1", Motto = "Motto", PicUrl = "Pic", NotAvailable = true, }; var cocktailDTO = new CocktailDTO() { Name = "Name", Id = "1", Motto = "Motto", PicUrl = "Pic", NotAvailable = true, }; var cocktailDTOMock = new Mock <CocktailDTO>(); var cocktailMock = new Mock <Cocktail>(); //Act cocktailMapperMock.Setup(m => m.MapFrom(It.IsAny <Cocktail>())).Returns(cocktailDTO); using (var actContext = new IriOnCocktailServiceDbContext(options)) { await actContext.Cocktails.AddAsync(cocktail); await actContext.SaveChangesAsync(); } using (var assertContext = new IriOnCocktailServiceDbContext(options)) { var sut = new CocktailService(assertContext, ingredientServiceMock.Object, cocktailMapperMock.Object, cocktailMapperToDTOMock.Object, commentMapperToDTOMock.Object, commentMapperMock.Object, addCocktailMapperMock.Object, cocktailRatingToDTOMock.Object); await Assert.ThrowsExceptionAsync <ArgumentNullException>(() => sut.GetAllCocktailsDTO(), GlobalConstants.NoCocktailsFound); } }
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 async Task Throw_When_DCocktailNotFound() { var options = Utils.GetOptions(nameof(Throw_When_DCocktailNotFound)); using (var assertContext = new CMContext(options)) { var sut = new CocktailService(assertContext); Guid resultGuid = new Guid("4039e0f3-8d2d-43a5-a82b-477e42371cd6"); await Assert.ThrowsExceptionAsync <ArgumentNullException> (async() => await sut.DeleteCocktail(resultGuid)); } }
public async Task GetAllCommentsForCocktailCorrectly() { var options = TestUtilities.GetOptions(nameof(GetAllCommentsForCocktailCorrectly)); var ingredientServiceMock = new Mock<ICocktailIngredientService>(); var cocktailMapperToDTOMock = new Mock<IDTOServiceMapper<CocktailDTO, Cocktail>>(); var cocktailMapperMock = new Mock<IDTOServiceMapper<Cocktail, CocktailDTO>>(); var commentMapperToDTOMock = new Mock<IDTOServiceMapper<CommentDTO, CocktailComment>>(); var commentMapperMock = new Mock<IDTOServiceMapper<CocktailComment, CommentDTO>>(); var addCocktailMapperMock = new Mock<IDTOServiceMapper<Cocktail, AddCocktailDTO>>(); var cocktailRatingToDTOMock = new Mock<IDTOServiceMapper<RatingDTO, CocktailRating>>(); var cocktail = new Cocktail() { Name = "Name", Id = "1", Motto = "Motto", PicUrl = "Pic", }; var cocktailComment = new CocktailComment { CocktailId = "1", Description = "description", CreatedOn = DateTime.Now, }; var cocktailComment2 = new CocktailComment { CocktailId = "1", Description = "description", CreatedOn = DateTime.Now, }; //Act //cocktailMapperMock.Setup(m => m.MapFrom(It.IsAny<Cocktail>())).Returns(cocktailDTO); using (var actContext = new IriOnCocktailServiceDbContext(options)) { await actContext.Cocktails.AddAsync(cocktail); await actContext.SaveChangesAsync(); await actContext.CocktailComments.AddAsync(cocktailComment); await actContext.CocktailComments.AddAsync(cocktailComment2); await actContext.SaveChangesAsync(); } using (var assertContext = new IriOnCocktailServiceDbContext(options)) { var sut = new CocktailService(assertContext, ingredientServiceMock.Object, cocktailMapperMock.Object, cocktailMapperToDTOMock.Object, commentMapperToDTOMock.Object, commentMapperMock.Object, addCocktailMapperMock.Object, cocktailRatingToDTOMock.Object); var result = await sut.GetAllCommentsForCocktail("1"); Assert.AreEqual(2, result.Count); } }
public async Task ReturnCocktailsSortedByNameAscending() { //Arrange var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockCocktailMapper = new Mock <ICocktailMapper>(); mockCocktailMapper.Setup(c => c.MapToCocktailDTO(It.IsAny <Cocktail>())) .Returns <Cocktail>(c => new CocktailDTO { Name = c.Name }); var mockIngMapper = new Mock <IIngredientMapper>(); var mockBarMapper = new Mock <IBarMapper>(); var mockCocktailReviewService = new Mock <ICocktailReviewService>(); var options = Utils.GetOptions(nameof(ReturnCocktailsSortedByNameAscending)); var sort = "Name"; var direction = "asc"; var expected = new List <CocktailDTO> { new CocktailDTO { Name = "Bozdugan" }, new CocktailDTO { Name = "Gin Fizz" }, new CocktailDTO { Name = "Mojito" } }; Utils.GetInMemoryDataBase(options); //Act & Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new CocktailService(mockDateTimeProvider.Object, mockCocktailMapper.Object, mockIngMapper.Object, mockBarMapper.Object, assertContext, mockCocktailReviewService.Object); var result = await sut.ListAllCocktailsAsync(0, 10, null, sort, direction); var resultAsList = result.ToList(); Assert.AreEqual(expected.Count, resultAsList.ToList().Count); for (int i = 0; i < expected.Count; i++) { Assert.AreEqual(expected[i].Name, resultAsList[i].Name); } } }