public void Verify_Comment_GetUpdated_When_Update_Called( CommentOnType commentOnType) { //arrange Comment comment = new Comment(); switch (commentOnType) { case CommentOnType.Posts: comment = Helper.CommentFake.GetPostCommentExistsInDb( _userService, _postService, _commentService, out var Post, out var postCommentCreater); break; case CommentOnType.Users: comment = Helper.CommentFake.GetUserCommentExistsInDb( _userService, _commentService, out var user, out var userCommentCreater); break; } //act comment.CommentText = "comment is modified"; var updatedComment = _commentService.Update(commentOnType, comment) .Result; //assert Assert.Equal(updatedComment.CommentText, comment.CommentText); Assert.Equal(updatedComment, comment); }
public void Verify_Comment_Get_Deleted_When_Get_Called_With_ExistingComment(CommentOnType commentOnType) { //arrange //arrange Comment comment = new Comment(); switch (commentOnType) { case CommentOnType.Posts: comment = Helper.CommentFake.GetPostCommentExistsInDb( _userService, _postService, _commentService, out var Post, out var postCommentCreater); break; case CommentOnType.Users: comment = Helper.CommentFake.GetUserCommentExistsInDb( _userService, _commentService, out var user, out var userCommentCreater); break; } //act var deleteStatus = _commentService.Delete(comment).Result; //assert Assert.True(deleteStatus); Assert.Null(_commentService.GetCommentById(comment.Id).Result); }
public void Verify_Throw_NotSupportedException_When_Add_Called_With_NonExistingEntity( CommentOnType commentOnType) { //arrange Comment comment = new Comment(); var user = Helper.UserFake.GetUserExistsInDb(_userService); switch (commentOnType) { case CommentOnType.Posts: comment = Helper.CommentFake.GetDefaultPostComment(40, user.Id); break; case CommentOnType.Users: comment = Helper.CommentFake.GetDefaultUserComment(40, user.Id); break; } //assert var exception = Assert.ThrowsAsync <NotSupportedException>( async() => { //act await _commentService.Add(commentOnType, comment); }); Assert.Contains("not exists in our system", exception.Result.Message, StringComparison.OrdinalIgnoreCase); }
public void Verify_Throw_NotSupportedException_When_Update_Called_With_NonExistingEntity( CommentOnType commentOnType) { //arrange Comment comment = new Comment(); switch (commentOnType) { case CommentOnType.Posts: comment = Helper.CommentFake.GetPostCommentExistsInDb( _userService, _postService, _commentService, out var Post, out var postCommentCreater); break; case CommentOnType.Users: comment = Helper.CommentFake.GetUserCommentExistsInDb( _userService, _commentService, out var user, out var userCommentCreater); break; } comment.CommentOnId = 30; //assert var exception = Assert.ThrowsAsync <NotSupportedException>( async() => { //act await _commentService.Update(commentOnType, comment); }); Assert.Contains("not exists in our system", exception.Result.Message , StringComparison.OrdinalIgnoreCase); }
public void Verify_Get_Comment_When_GetCommentById_Called_For_ExistingComment( CommentOnType commentOnType) { //arrange Comment comment = new Comment(); switch (commentOnType) { case CommentOnType.Posts: comment = Helper.CommentFake.GetPostCommentExistsInDb( _userService, _postService, _commentService, out var Post, out var postCommentCreater); break; case CommentOnType.Users: comment = Helper.CommentFake.GetUserCommentExistsInDb( _userService, _commentService, out var user, out var userCommentCreater); break; } //act var dbComment = _commentService.GetCommentById(comment.Id).Result; //assert Assert.NotNull(dbComment); Assert.Equal(comment.Id, dbComment.Id); }
public async Task <Comment> Update(CommentOnType commentOn, Comment comment) { if (comment != null) { if (!await IsCreatedByExistsByType(comment.CommentOnId, commentOn)) { throw new NotSupportedException( $"{commentOn.ToString()} does not exists in our system!!"); } var dbComment = await _dbContext.Comments.Where(x => x.Id == comment.Id) .FirstOrDefaultAsync(); if (dbComment != null) { _mapper.Map(comment, dbComment); await _dbContext.SaveChangesAsync(); return(dbComment); } throw new NotSupportedException($"{nameof(comment)} does not exists in our system"); } throw new ArgumentNullException(nameof(comment)); }
public void Verify_Throw_ArgumentNullException_When_Update_Called_With_Null(CommentOnType commentOnType) { //assert Assert.ThrowsAsync <ArgumentNullException>( async() => { //act await _commentService.Update(commentOnType, null); }); }
public void Verify_Throw_ArgumentNullException_When_Add_Called_With_Null( CommentOnType commentOnType) { //assert var exception = Assert.ThrowsAsync <ArgumentNullException>(nameof(Comment), async() => { //act await _commentService.Add(commentOnType, null); }); Assert.Contains(nameof(Comment), exception.Result.Message , StringComparison.OrdinalIgnoreCase); }
private async Task <bool> IsCreatedByExistsByType(int id, CommentOnType commentOn) { if (commentOn == CommentOnType.Posts) { var post = await _postService.GetPostById(id); return(post != null); } else { var user = await _userService.GetUserById(id); return(user != null); } }
public async Task <Comment> Add(CommentOnType commentOn, Comment comment) { if (comment == null) { throw new ArgumentNullException(nameof(Comment)); } if (!await IsCreatedByExistsByType(comment.CommentOnId, commentOn)) { throw new NotSupportedException( $"{commentOn.ToString()} does not exists in our system!!"); } _dbContext.Comments.Add(comment); await _dbContext.SaveChangesAsync(); return(comment); }
public void Verify_Throw_NotSupportedException_When_Update_Called_With_NonExistingComment( CommentOnType commentOnType) { //arrange Comment comment = new Comment(); switch (commentOnType) { case CommentOnType.Posts: { var creator = Helper.UserFake.GetUserExistsInDb(_userService); var profile = Helper.PostFake.GetPostExistsInDb(creator, _postService); comment = Helper.CommentFake .GetDefaultPostComment(profile.Id, creator.Id); } break; case CommentOnType.Users: { var profile = Helper.UserFake.GetUserExistsInDb(_userService); var creator = Helper.UserFake.GetUserExistsInDb(_userService); comment = Helper.CommentFake .GetDefaultPostComment(profile.Id, creator.Id); } break; } comment.Id = 20; //assert var exception = Assert.ThrowsAsync <NotSupportedException>( async() => { //act await _commentService.Update(commentOnType, comment); }); Assert.Contains("not exists in our system", exception.Result.Message, StringComparison.OrdinalIgnoreCase); }
public void Verify_NewComment_Added_When_Add_Called_With_ExitingUser( CommentOnType commentOnType) { //arrange Comment comment = new Comment(); switch (commentOnType) { case CommentOnType.Posts: { var creator = Helper.UserFake.GetUserExistsInDb(_userService); var profile = Helper.PostFake.GetPostExistsInDb(creator, _postService); comment = Helper.CommentFake .GetDefaultPostComment(profile.Id, creator.Id); } break; case CommentOnType.Users: { var profile = Helper.UserFake.GetUserExistsInDb(_userService); var creator = Helper.UserFake.GetUserExistsInDb(_userService); comment = Helper.CommentFake .GetDefaultPostComment(profile.Id, creator.Id); } break; } //act var result = _commentService.Add(commentOnType, comment).Result; //assert var dbComment = _commentService.GetCommentById(comment.Id).Result; Assert.Equal(result.Id, dbComment.Id); Assert.Equal(result.CommentText, dbComment.CommentText); }
private async Task <IEnumerable <Comment> > GetCommentsByType(int postId, CommentOnType type) { return(await _dbContext.Comments .Where(_ => _.CommentOn == type.ToString() && _.CommentOnId == postId) .AsNoTracking().ToListAsync()); }