public async Task <CocktailRatingDto> CreateAsync(CocktailRatingDto tempCocktailRating) { if (tempCocktailRating == null) { throw new BusinessLogicException(ExceptionMessages.CocktailRatingNull); } var cocktailRating = new CocktailRating { Value = tempCocktailRating.Value, UserId = tempCocktailRating.UserId, CocktailId = tempCocktailRating.CocktailId, CreatedOn = tempCocktailRating.CreatedOn, ModifiedOn = tempCocktailRating.ModifiedOn, DeletedOn = tempCocktailRating.DeletedOn, IsDeleted = tempCocktailRating.IsDeleted }; await this.context.CocktailRatings.AddAsync(cocktailRating); await this.context.SaveChangesAsync(); var cocktailRatingDto = this.dtoMapper.MapFrom(cocktailRating); return(cocktailRatingDto); }
public void MapFrom_Should_CorrectlyMapFrom_Cocktail_To_CocktailRatingDto() { //Arrange var sut = new CocktailRatingDtoMapper(); var cocktailRating = new CocktailRating { Cocktail = new Cocktail { Id = Guid.NewGuid(), Name = "testCocktail", Info = "testInfo", ImagePath = "testPath", }, User = new User { Id = Guid.NewGuid(), UserName = "******", Email = "*****@*****.**" }, Value = 5, CreatedOn = DateTime.MinValue, }; //Act var result = sut.MapFrom(cocktailRating); //Assert Assert.AreEqual(result.CocktailId, cocktailRating.CocktailId); Assert.AreEqual(result.UserId, cocktailRating.UserId); Assert.AreEqual(result.Value, cocktailRating.Value); Assert.AreEqual(result.CreatedOn, cocktailRating.CreatedOn); }
public async Task RateCocktailAsync(int userId, int rating, int cocktailId) { if (await dbContext.CocktailRating.AnyAsync(p => (p.UserId == userId && p.CocktailId == cocktailId))) { var givenRating = await dbContext.CocktailRating.FirstOrDefaultAsync(p => (p.UserId == userId && p.CocktailId == cocktailId)); givenRating.Rating = rating; await dbContext.SaveChangesAsync(); await cService.UpdateAverageRatingAsync(cocktailId); } else { var user = await FindUserByIdAsync(userId); var cocktail = await cService.FindCocktailByIdAsync(cocktailId); var cocktailRating = new CocktailRating() { User = user, Cocktail = cocktail, Rating = rating, CocktailName = cocktail.Name, UserUserName = user.UserName }; await dbContext.CocktailRating.AddAsync(cocktailRating); await dbContext.SaveChangesAsync(); await cService.UpdateAverageRatingAsync(cocktailId); } }
public void MapFrom_Should_ReturnCorrectInstanceOf_CocktailDto() { //Arrange var sut = new CocktailRatingDtoMapper(); var cocktailRating = new CocktailRating { Cocktail = new Cocktail { Id = Guid.NewGuid(), Name = "testCocktail", Info = "testInfo", ImagePath = "testPath", }, User = new User { Id = Guid.NewGuid(), UserName = "******", Email = "*****@*****.**" }, Value = 5, CreatedOn = DateTime.MinValue, }; //Act var result = sut.MapFrom(cocktailRating); //Assert Assert.IsInstanceOfType(result, typeof(CocktailRatingDto)); }
public async Task CocktailRatingAsyncCreatedCorrectly() { //Arrange var options = TestUtilities.GetOptions(nameof(CocktailRatingAsyncCreatedCorrectly)); 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 cocktailRatingDTO = new CocktailRating(); var RatingDTOMock = new Mock <RatingDTO>(); cocktailRatingToDTOMock.Setup(m => m.MapFrom(It.IsAny <RatingDTO>())).Returns(cocktailRatingDTO); 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.CocktailRatingAsync(RatingDTOMock.Object); } using (var assertContext = new IriOnCocktailServiceDbContext(options)) { Assert.AreEqual(1, assertContext.CocktailRatings.Count()); } }
public void ReturnCorrectInstance_OfType_CocktailRatingDto() { //Arrange var sut = new CocktailRatingDtoMapper(); var cocktail = new Cocktail { Id = 1, Name = "TestCocktail", }; var user = new User { Id = 1, UserName = "******", Email = "*****@*****.**", }; var cocktailRating = new CocktailRating { CocktailId = cocktail.Id, UserId = user.Id, Value = 5, }; //Act var result = sut.MapDto(cocktailRating); //Assert Assert.IsInstanceOfType(result, typeof(CocktailRatingDto)); Assert.AreEqual(result.CocktailId, cocktailRating.CocktailId); Assert.AreEqual(result.UserId, cocktailRating.UserId); Assert.AreEqual(result.Value, cocktailRating.Value); }
public async Task Return_Correct_Model_When_ParamsAreValidCocktailRating() { //Arrange var options = TestUtilities.GetOptions(nameof(Return_Correct_Model_When_ParamsAreValidCocktailRating)); 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 = 5, }; 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.Cocktails.AddAsync(cocktail); await arrangeContext.Users.AddAsync(user); await arrangeContext.CocktailRatings.AddAsync(rating); 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.GetRatingAsync(1, 1); Assert.IsInstanceOfType(result, typeof(CocktailRatingDto)); Assert.AreEqual(1, result.Id); Assert.AreEqual(1, result.UserId); Assert.AreEqual(1, result.CocktailId); Assert.AreEqual(5, result.Value); } }
public async Task CorrectlyCreateCocktailRating() { //Arrange var options = TestUtilities.GetOptions(nameof(CorrectlyCreateCocktailRating)); var mapperMock = new Mock <IDtoMapper <CocktailRating, CocktailRatingDto> >(); var cocktailId = Guid.NewGuid(); var cocktailIdTwo = Guid.NewGuid(); var userId = Guid.NewGuid(); var userIdTwo = Guid.NewGuid(); var createdOn = DateTime.UtcNow; var entityDto = new CocktailRatingDto { CocktailId = cocktailId, UserId = userId, UserName = "******", Value = 2, CreatedOn = createdOn }; var entity = new CocktailRating { CocktailId = cocktailId, UserId = userIdTwo, Value = 2, CreatedOn = createdOn }; mapperMock.Setup(x => x.MapFrom(It.IsAny <CocktailRating>())).Returns(entityDto); using (var assertContext = new CWContext(options)) { //Assert var sut = new CocktailRatingService(assertContext, mapperMock.Object); var result = await sut.CreateAsync(entityDto); Assert.IsInstanceOfType(result, typeof(CocktailRatingDto)); Assert.AreEqual(cocktailId, result.CocktailId); Assert.AreEqual(userId, result.UserId); Assert.AreEqual(userId, result.UserId); Assert.AreEqual(2, result.Value); Assert.AreEqual("testusername", result.UserName); Assert.AreEqual(entityDto.CocktailId, result.CocktailId); Assert.AreEqual(entityDto.UserId, result.UserId); Assert.AreEqual(entityDto.UserId, result.UserId); Assert.AreEqual(entityDto.Value, result.Value); Assert.AreEqual(entityDto.UserName, result.UserName); } }
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 static CocktailRatingDTO GetDTO(this CocktailRating rating) { if (rating == null) { throw new ArgumentNullException(); } return(new CocktailRatingDTO { Cocktail = rating.Cocktail, CocktailId = rating.CocktailId, User = rating.User, UserId = rating.UserId, Rating = rating.Rating, }); }
public async Task ReturnInstanceOfCollectionCocktailRatingDtos() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnInstanceOfCollectionCocktailRatingDtos)); var mapperMock = new Mock <IDtoMapper <CocktailRating, CocktailRatingDto> >(); var cocktailId = Guid.NewGuid(); var userId = Guid.NewGuid(); var userIdTwo = Guid.NewGuid(); var date1 = new DateTime(2017, 1, 18); var date2 = new DateTime(2017, 1, 20); var cocktail = new Cocktail { Id = cocktailId, Name = "testName" }; var user = new User { Id = userId, UserName = "******" }; var entity = new CocktailRating { CocktailId = cocktailId, UserId = userId, Value = 2, CreatedOn = date1, }; var entityTwo = new CocktailRating { CocktailId = cocktailId, UserId = userIdTwo, Value = 2, CreatedOn = date2, }; var list = new List <CocktailRatingDto>() { new CocktailRatingDto { CocktailId = cocktailId, UserId = userId, Value = 2 }, new CocktailRatingDto { CocktailId = cocktailId, UserId = userIdTwo, Value = 2 }, }; mapperMock.Setup(x => x.MapFrom(It.IsAny <ICollection <CocktailRating> >())).Returns(list); using (var arrangeContext = new CWContext(options)) { //await arrangeContext.Cocktails.AddAsync(cocktail); //await arrangeContext.Users.AddAsync(user); await arrangeContext.CocktailRatings.AddAsync(entity); await arrangeContext.CocktailRatings.AddAsync(entityTwo); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new CWContext(options)) { //Act & Assert var sut = new CocktailRatingService(assertContext, mapperMock.Object); var result = await sut.GetAllRatingsAsync(cocktailId); Assert.IsInstanceOfType(result, typeof(ICollection <CocktailRatingDto>)); Assert.AreEqual(2, result.Count()); Assert.AreEqual(entity.Value, result.First().Value); Assert.AreEqual(entity.UserId, result.First().UserId); Assert.AreEqual(entityTwo.Value, result.Last().Value); Assert.AreEqual(entityTwo.UserId, result.Last().UserId); } }
public async Task ReturnCorrectEntity_When_ParamsAreValid() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnCorrectEntity_When_ParamsAreValid)); 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, }; var list = new List <CocktailRatingDto>() { new CocktailRatingDto { Id = 1, UserId = 1, CocktailId = 1, Value = 5, }, new CocktailRatingDto { Id = 2, UserId = 2, CocktailId = 1, Value = 3, } }; mapper.Setup(x => x.MapDto(It.IsAny <ICollection <CocktailRating> >())).Returns(list); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Cocktails.AddAsync(cocktail); await arrangeContext.Users.AddAsync(user1); await arrangeContext.Users.AddAsync(user2); await arrangeContext.CocktailRatings.AddAsync(rating1); await arrangeContext.CocktailRatings.AddAsync(rating2); 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.GetAllRatingsAsync(1); Assert.AreEqual(2, result.Count); Assert.AreEqual(1, result.First().Id); Assert.AreEqual(1, result.First().UserId); Assert.AreEqual(1, result.First().CocktailId); Assert.AreEqual(5, result.First().Value); Assert.AreEqual(2, result.Last().Id); Assert.AreEqual(2, result.Last().UserId); Assert.AreEqual(1, result.Last().CocktailId); Assert.AreEqual(3, result.Last().Value); } }