public async void DeleteCommentByIdAsync_PositiveAndNegative_TestAsync() { var options = new DbContextOptionsBuilder <ApartmentContext>() .UseInMemoryDatabase(databaseName: "DeleteCommentByIdAsync_PositiveAndNegative_TestAsync") .Options; using (var context = new ApartmentContext(options)) { context.AddRange(_comments); await context.SaveChangesAsync(); } using (var context = new ApartmentContext(options)) { var comment = await context.Comments.AsNoTracking().FirstOrDefaultAsync(); var service = new CommentAdministrationService(context, _mapper); var resultPositive = await service.DeleteCommentByIdAsync(comment.Id.ToString()); var resultNegative = await service.DeleteCommentByIdAsync(new Guid().ToString()); resultPositive.IsSuccess.Should().BeTrue(); resultPositive.Message.Should().BeNull(); resultNegative.IsSuccess.Should().BeFalse(); resultNegative.Message.Should().Contain("not exist"); } }
public async void GetAllCommentsByApartmentIdAsync_PositiveAndNegative_TestAsync() { var options = new DbContextOptionsBuilder <ApartmentContext>() .UseInMemoryDatabase(databaseName: "GetAllCommentsByApartmentIdAsync_PositiveAndNegative_TestAsync") .Options; Apartment apartmentWithComments; using (var context = new ApartmentContext(options)) { context.AddRange(_apartments); await context.SaveChangesAsync(); apartmentWithComments = context.Apartments.AsNoTracking().FirstOrDefault(); foreach (var item in _comments) { item.ApartmentId = apartmentWithComments.Id; } context.AddRange(_comments); await context.SaveChangesAsync(); } using (var context = new ApartmentContext(options)) { var service = new CommentAdministrationService(context, _mapper); var commentsInBase = await context.Comments.AsNoTracking().ToListAsync(); var apartmentWithoutComments = context.Apartments.Where(_ => _.Id != apartmentWithComments.Id).FirstOrDefault(); var resultPositive = await service.GetAllCommentsByApartmentIdAsync(new PagedRequest <string>(apartmentWithComments.Id.ToString())); var resultNegative = await service.GetAllCommentsByApartmentIdAsync(new PagedRequest <string>(apartmentWithoutComments.Id.ToString())); foreach (var item in commentsInBase) { resultPositive.Data.Data .Where(_ => _.Id == item.Id.ToString()) .FirstOrDefault() .Should().NotBeNull(); } resultNegative.Data.Data.Should().BeEmpty(); } }
public async void UpdateCommentAsync_PositiveAndNegative_TestAsync() { var options = new DbContextOptionsBuilder <ApartmentContext>() .UseInMemoryDatabase(databaseName: "UpdateCommentAsync_PositiveAndNegative_TestAsync") .Options; using (var context = new ApartmentContext(options)) { context.AddRange(_comments); await context.SaveChangesAsync(); } using (var context = new ApartmentContext(options)) { var comment = await context.Comments.AsNoTracking().FirstOrDefaultAsync(); var service = new CommentAdministrationService(context, _mapper); CommentDTOAdministration updateComment = new CommentDTOAdministration() { Id = comment.Id.ToString(), Title = "newTitle", Text = "newText" }; CommentDTOAdministration failComment = new CommentDTOAdministration() { Id = new Guid().ToString(), Title = "newTitle", Text = "newText" }; var resultPositive = await service.UpdateCommentAsync(updateComment); var resultNegative = await service.UpdateCommentAsync(failComment); resultPositive.IsSuccess.Should().BeTrue(); resultPositive.Data.Title.Should().BeEquivalentTo(updateComment.Title); resultPositive.Data.Title.Should().NotBeEquivalentTo(comment.Title); resultNegative.IsSuccess.Should().BeFalse(); } }