コード例 #1
0
        public async Task DeleteComment()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "DeleteComment_Database")
                          .Options;
            var dbContext      = new ApplicationDbContext(options);
            var commentService = new CommentService(dbContext);
            var voteService    = new VoteService(dbContext);

            await commentService.CreateCommentAsync(1, "u1", "Hello i am tweet", null, false);

            await commentService.CreateCommentAsync(1, "u2", "Hello i am tweet2", 1, false);

            await voteService.VoteForCommentAsync("u1", 1, true);

            await commentService.EditCommentAsync(2, "Hello i am new tweet2", "u2");

            await commentService.DeleteCommentByIdAsync(1, 1, "u1");

            var comentsCount = await dbContext.Comments.CountAsync();

            var comment = await dbContext.Comments.LastOrDefaultAsync();

            Assert.Equal(2, comment.Id);
            Assert.Equal("u2", comment.UserId);
            Assert.Equal("Hello i am new tweet2", comment.Content);
            Assert.Null(comment.ParentId);
            Assert.Equal(1, comentsCount);
            Assert.Equal(0, await dbContext.Votes.CountAsync());
        }
コード例 #2
0
        public async Task Top3AnswersForPost()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "TopAnswers_Database")
                          .Options;
            var dbContext      = new ApplicationDbContext(options);
            var commentService = new CommentService(dbContext);
            var postService    = new PostService(dbContext);
            var voteService    = new VoteService(dbContext);

            await postService.CreatePostAsync("Hello", "I am Kris", "Kris", 1);

            await commentService.CreateCommentAsync(1, "u1", "Hello i am tweet", null, true);

            await commentService.CreateCommentAsync(1, "u2", "Hello i am tweet2", 1, true);

            await commentService.CreateCommentAsync(1, "u3", "Hello i am tweet3", 1, true);

            await commentService.CreateCommentAsync(1, "u4", "Hello i am tweet4", 1, true);

            await voteService.VoteForCommentAsync("Kris", 1, true);

            var commentsForPost      = commentService.Top3AnswersForPost(1);
            var commentWithMostLikes = commentsForPost.FirstOrDefault();

            Assert.Equal(1, commentWithMostLikes.Id);
            Assert.Equal("u1", commentWithMostLikes.UserId);
            Assert.Equal("Hello i am tweet", commentWithMostLikes.Content);
            Assert.Null(commentWithMostLikes.ParentId);
            Assert.Equal(3, commentsForPost.Count());
        }
コード例 #3
0
        public async Task AddComment(string comment)
        {
            var res = JsonConvert.DeserializeObject <Comment>(comment);

            res.Date = DateTime.Now;
            await _commentService.CreateCommentAsync(res);
        }
コード例 #4
0
        public async void Throws_Given_CommentDto_isNull()
        {
            ICommentService commentService = new CommentService(_mockCommentsRepository.Object, _mockPostRepository.Object);

            //DTO is null
            await Assert.ThrowsAsync <ArgumentNullException>(async() => await commentService.CreateCommentAsync(null));
        }
コード例 #5
0
        public async void Create_Post_Returns_Right_Comment()
        {
            CreateCommentViewModel createCommentViewModel = new CreateCommentViewModel()
            {
                Text   = COMMENT_TEXT_CONTENT + Guid.NewGuid().ToString(),
                FromId = Guid.NewGuid(),
                PostId = Guid.NewGuid()
            };

            Post post = new Post(new Content("Post text content"), new Title("Post title"), Guid.NewGuid());

            Guid fromId = Guid.NewGuid();

            Comment newComment = new Comment(createCommentViewModel.FromId, post.Id, new Content(COMMENT_TEXT_CONTENT));

            _mockPostRepository.Setup(repo => repo.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(post);
            _mockCommentsRepository.Setup(repo => repo.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(newComment);
            _mockCommentsRepository.Setup(repo =>
                                          repo.AddAsync(It.Is <Comment>(comment => comment.Content.Text == createCommentViewModel.Text)))
            .Returns(Task.CompletedTask);

            ICommentService commentService = new CommentService(_mockCommentsRepository.Object, _mockPostRepository.Object);

            var createdComment = await commentService.CreateCommentAsync(createCommentViewModel);

            Assert.Equal(createdComment.Id, newComment.Id);
            Assert.Equal(createdComment.FromId, newComment.FromId);
            Assert.Equal(createdComment.PostId, newComment.PostId);
            Assert.Equal(createdComment.Content.Text, newComment.Content.Text);
        }
コード例 #6
0
        public async Task AllCommentsForPost()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "AllComments_Database")
                          .Options;
            var dbContext      = new ApplicationDbContext(options);
            var commentService = new CommentService(dbContext);
            var postService    = new PostService(dbContext);

            await postService.CreatePostAsync("Hello", "I am Kris", "Kris", 1);

            await commentService.CreateCommentAsync(1, "u1", "Hello i am tweet", null, false);

            await commentService.CreateCommentAsync(1, "u2", "Hello i am tweet2", 1, false);

            var commentsForPost = commentService.AllCommentsForPost(1);

            Assert.Equal(2, commentsForPost.Count());
        }
コード例 #7
0
        public async void Throws_Given_CommentDto_Text_isInvalid()
        {
            ICommentService commentService = new CommentService(_mockCommentsRepository.Object, _mockPostRepository.Object);

            CreateCommentViewModel createCommentViewModel = new CreateCommentViewModel()
            {
                Text   = COMMENT_TEXT_CONTENT,
                FromId = Guid.NewGuid(),
                PostId = Guid.Empty
            };

            await Assert.ThrowsAsync <ArgumentException>(async() => await commentService.CreateCommentAsync(createCommentViewModel));
        }
コード例 #8
0
        public async Task CreateComment()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "CreateComment_Database")
                          .Options;
            var dbContext      = new ApplicationDbContext(options);
            var commentService = new CommentService(dbContext);

            await commentService.CreateCommentAsync(1, "Tweets", "Hello i am tweet", null, false);

            await commentService.CreateCommentAsync(1, "Tweets2", "Hello i am tweet2", 1, false);

            var comentsCount = await dbContext.Comments.CountAsync();

            var comment = await dbContext.Comments.FirstAsync();

            Assert.Equal(1, comment.Id);
            Assert.Equal("Tweets", comment.UserId);
            Assert.Equal("Hello i am tweet", comment.Content);
            Assert.Null(comment.ParentId);
            Assert.Equal(2, comentsCount);
        }
コード例 #9
0
        public async void Throws_Post_With_Given_Id_Not_Exists()
        {
            _mockPostRepository.Setup(repo => repo.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(() => null);
            ICommentService commentService = new CommentService(_mockCommentsRepository.Object, _mockPostRepository.Object);

            CreateCommentViewModel createCommentViewModel = new CreateCommentViewModel()
            {
                Text   = COMMENT_TEXT_CONTENT,
                FromId = Guid.NewGuid(),
                PostId = Guid.NewGuid()
            };

            await Assert.ThrowsAsync <PostNotExistsException>(async() => await commentService.CreateCommentAsync(createCommentViewModel));
        }
コード例 #10
0
        public async Task EditComment()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "EditComment_Database")
                          .Options;
            var dbContext      = new ApplicationDbContext(options);
            var commentService = new CommentService(dbContext);

            await commentService.CreateCommentAsync(1, "u1", "Hello i am tweet", null, false);

            await commentService.CreateCommentAsync(1, "u2", "Hello i am tweet2", 1, false);

            await commentService.EditCommentAsync(2, "Hell i am new tweet", "u2");

            var comentsCount = await dbContext.Comments.CountAsync();

            var comment = await commentService.GetCommentByIdAsync(2);

            Assert.Equal(2, comment.Id);
            Assert.Equal("u2", comment.UserId);
            Assert.Equal("Hell i am new tweet", comment.Content);
            Assert.Equal(1, comment.ParentId);
            Assert.Equal(2, comentsCount);
        }
コード例 #11
0
        public async void VoteForCommentAsync()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "VoteForCommentAsync_Database")
                          .Options;
            var dbContext      = new ApplicationDbContext(options);
            var voteService    = new VoteService(dbContext);
            var postService    = new PostService(dbContext);
            var commentService = new CommentService(dbContext);

            await postService.CreatePostAsync("Tweets", "Hello i am tweet", "u1", 1);

            await commentService.CreateCommentAsync(1, "u1", "TweetsComment", null, false);

            await voteService.VoteForCommentAsync("u1", 1, true);

            await voteService.VoteForCommentAsync("u1", 1, false);

            for (int i = 0; i < 10; i++)
            {
                await voteService.VoteForCommentAsync("u1", 1, false);
            }
            for (int i = 0; i < 10; i++)
            {
                await voteService.VoteForCommentAsync("u1", 1, true);
            }
            await voteService.VoteForCommentAsync("u1", 1, false);

            var votesSum = voteService.AllVotesForComment(1);
            var vote     = await dbContext.Votes.FirstAsync();

            var votesCount = await dbContext.Votes.CountAsync();

            Assert.Equal(1, vote.Id);
            Assert.Equal(VoteType.Neutral, vote.VoteType);
            Assert.Equal(1, vote.CommentId);
            Assert.Equal("u1", vote.UserId);
            Assert.Equal(0, votesSum);
            Assert.Equal(1, votesCount);
        }
コード例 #12
0
        public async Task <ActionResult <UpdateCommentRequest> > AddComment(UpdateCommentRequest createdComment)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            try
            {
                var comment = await commentService.CreateCommentAsync(createdComment);

                return(CreatedAtAction("GetComment", new { commentId = comment }, comment));
            }
            catch (ResourceHasConflictException ex)
            {
                return(Conflict(ex.Message));
            }
            catch (ResourceNotFoundException ex)
            {
                return(NotFound(ex.Message));
            }
        }
コード例 #13
0
        public async Task CreateCommentAsync_Should_Pass()
        {
            // Arrange
            CommentDto commentDto = new CommentDto
            {
                MemeId = "a0Q558q",
                Text   = "Haha, super funny meme!",
                UserId = 1
            };
            // NOTE: the repository is used here instead of CommentService.GetCommentsByMemeIdAsync because relational data will not work in memory.
            List <Comment> commentsBefore = (List <Comment>) await CommentRepository.FindManyByExpressionAsync(comment => comment.MemeId.Equals("a0Q558q"));

            Assert.IsTrue(commentsBefore.Count.Equals(0));

            // Act
            ServiceResponseDto responseDto = await CommentService.CreateCommentAsync(commentDto);

            // Assert
            Assert.IsTrue(responseDto.Success);
            List <Comment> commentsAfter = (List <Comment>) await CommentRepository.FindManyByExpressionAsync(comment => comment.MemeId.Equals("a0Q558q"));

            Assert.IsTrue(commentsAfter.Count.Equals(1));
        }
コード例 #14
0
 public async Task CreateComment([FromBody] CommentData commentData)
 {
     await CommentService.CreateCommentAsync(commentData);
 }
コード例 #15
0
        public async Task CreateCommentAsync_Should_Throw_EntityValidationException_Because_Text_Is_Too_Short()
        {
            // Arrange
            CommentDto commentDto = new CommentDto
            {
                MemeId = "a0Q558q",
                Text   = "Too short",
                UserId = 1
            };

            // Act & Assert
            await Assert.ThrowsExceptionAsync <EntityValidationException>(async() => await CommentService.CreateCommentAsync(commentDto));
        }