コード例 #1
0
        public async Task ReturnPostInputModelById()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "ReturnPostInputModelById_Database")
                          .Options;
            var dbContext   = new ApplicationDbContext(options);
            var postService = new PostService(dbContext);

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

            await postService.CreatePostAsync("newPost2", "Hello i am tweet2", "u2", 1);

            await postService.CreatePostAsync("newPost3", "Hello i am tweet3", "u3", 2);

            await postService.EditPostAsync(1, "Hello i am new tweet2 new", "new title", "u1", false);

            await postService.DeletePostAsync(1, "u1", false);

            var postsCount = await dbContext.Posts.CountAsync();

            var post = await postService.ReturnPostInputModelByIdAsync(2);

            Assert.Equal(2, post.Id);
            Assert.Equal("u2", post.UserId);
            Assert.Equal("Hello i am tweet2", post.Content);
            Assert.Equal("newPost2", post.Title);
            Assert.Equal(2, postsCount);
        }
コード例 #2
0
        public async Task DeletePostShouldDeletePost()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "DeletePost_Database")
                          .Options;
            var dbContext   = new ApplicationDbContext(options);
            var postService = new PostService(dbContext);
            var voteService = new VoteService(dbContext);

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

            await postService.CreatePostAsync("newPost2", "Hello i am tweet2", "u2", 1);

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

            await postService.EditPostAsync(1, "Hello i am new tweet2 new", "new title", "u1", false);

            await postService.DeletePostAsync(1, "u1", false);

            var postsCount = await dbContext.Posts.CountAsync();

            var post = await dbContext.Posts.FirstOrDefaultAsync();

            Assert.Equal(2, post.Id);
            Assert.Equal("u2", post.UserId);
            Assert.Equal("Hello i am tweet2", post.Content);
            Assert.Equal("newPost2", post.Title);
            Assert.Equal(1, postsCount);
            Assert.Equal(0, await dbContext.Votes.CountAsync());
        }
コード例 #3
0
        public async Task PostsCount()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "PostsCount_Database")
                          .Options;
            var dbContext   = new ApplicationDbContext(options);
            var postService = new PostService(dbContext);

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

            await postService.CreatePostAsync("Tweets2", "Hello i am tweet2", "u2", 1);

            Assert.Equal(2, await postService.PostsCountAsync(null));
        }
コード例 #4
0
        public async void Throws_Given_PostDto_isNull()
        {
            IPostService postService = new PostService(_mockPostRepository.Object);

            //DTO is null
            await Assert.ThrowsAsync <ArgumentNullException>(async() => await postService.CreatePostAsync(null));
        }
コード例 #5
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());
        }
コード例 #6
0
        public async Task CanAddPostToDB()
        {
            // add this nuget package to your project:
            // Install - Package Microsoft.EntityFrameworkCore.InMemory - Version 3.1.3
            // Arrange
            DbContextOptions <CMSDbContext> options = new DbContextOptionsBuilder <CMSDbContext>()
                                                      .UseInMemoryDatabase("CanAddPostToDB")
                                                      .Options;

            // open the connection to the database
            using (CMSDbContext context = new CMSDbContext(options))
            {
                PostService ps = new PostService(context);

                Post post = new Post()
                {
                    Title   = "This is our Test Post",
                    Content = "Let's see if this works! YAYY!"
                };

                var result = await ps.CreatePostAsync(post);

                // Check if the post exists through the context directly
                var data = context.Posts.Find(post.ID);
                Assert.Equal(result, data);

                // change our service to have a return and check the data that came back
                Assert.Equal("This is our Test Post", post.Title);
            }
        }
コード例 #7
0
        public async void Create_New_Post_Success()
        {
            IPostService postService = new PostService(_mockPostRepository.Object);


            CreatePostViewModel createPostViewModel = new CreatePostViewModel
            {
                Text   = PostText,
                Title  = PostTitle,
                FromId = Guid.NewGuid()
            };

            Post post = new Post(new Content(createPostViewModel.Text), new Title(createPostViewModel.Title), createPostViewModel.FromId);

            _mockPostRepository.Setup(repo => repo.AddAsync(It.Is <Post>(p => p.FromId == post.FromId))).Returns(Task.CompletedTask);
            _mockPostRepository.Setup(repo => repo.AddAsync(It.Is <Post>(p => p.FromId != post.FromId)));
            _mockPostRepository.Setup(repo => repo.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(post);


            var returnedPost = await postService.CreatePostAsync(createPostViewModel);

            Assert.NotNull(returnedPost);
            Assert.Equal(post.Title.Text, createPostViewModel.Title);
            Assert.Equal(post.Content.Text, createPostViewModel.Text);
            Assert.Equal(post.FromId, createPostViewModel.FromId);
        }
コード例 #8
0
        public async void Throws_Given_PostDto_text_isInvalid()
        {
            IPostService postService = new PostService(_mockPostRepository.Object);

            CreatePostViewModel createPostViewModel = new CreatePostViewModel();

            //Text is invalid
            await Assert.ThrowsAsync <ArgumentNullException>(async() => await postService.CreatePostAsync(createPostViewModel));
        }
コード例 #9
0
        public async Task PostServiceTests_CreatePostAsync_ShouldCreatePostSuccessfully()
        {
            //Arrange
            var postRequest = new PostModel
            {
                Title   = "Test Post",
                Content = "Test Post Content"
            };
            //Act
            var result = await _postService.CreatePostAsync(postRequest);

            _postRepository.Verify(x => x.Add(It.IsAny <Post>()), Times.Once);
            _unitOfWork.Verify(x => x.SaveChangesAsync(), Times.Once);


            //Assert
            Assert.NotNull(result);
            Assert.Equal(postRequest.Title, result.Title);
        }
コード例 #10
0
        public async Task AllPosts()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "AllPosts_Database")
                          .Options;
            var dbContext   = new ApplicationDbContext(options);
            var postService = new PostService(dbContext);

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

            await postService.CreatePostAsync("Tweets2", "Hello i am tweet2", "u1", 1);

            var allPosts  = postService.AllPosts(null, null);
            var firstPost = await allPosts.FirstAsync();

            Assert.Equal("Tweets", firstPost.Title);
            Assert.Equal("Hello i am tweet", firstPost.Content);
            Assert.Equal("u1", firstPost.UserId);
            Assert.Equal(2, await allPosts.CountAsync());
        }
コード例 #11
0
        public async void Throws_Given_PostDto_from_isInvalid()
        {
            IPostService postService = new PostService(_mockPostRepository.Object);

            CreatePostViewModel createPostViewModel = new CreatePostViewModel
            {
                Text  = PostText,
                Title = PostTitle
            };

            //From is invalid
            await Assert.ThrowsAsync <ArgumentException>(async() => await postService.CreatePostAsync(createPostViewModel));
        }
コード例 #12
0
        public async Task CreatePost()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Posts_Database")
                          .Options;
            var dbContext   = new ApplicationDbContext(options);
            var postService = new PostService(dbContext);

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

            await postService.CreatePostAsync("Tweets2", "Hello i am tweet2", "u2", 1);

            var postCount = await dbContext.Posts.CountAsync();

            var post = await dbContext.Posts.FirstAsync();

            Assert.Equal(1, post.Id);
            Assert.Equal("Tweets", post.Title);
            Assert.Equal("Hello i am tweet", post.Content);
            Assert.Equal("u1", post.UserId);
            Assert.Equal(2, postCount);
        }
コード例 #13
0
        public async Task <IActionResult> Create(CreatePostModel model)
        {
            var validationResult = _service.ValidateCreatePost(User, model);

            if (!validationResult.Valid)
            {
                return(BadRequest(validationResult.Result));
            }
            var metadata = GetFileDestinationMetadata();
            var entity   = await _service.CreatePostAsync(model, metadata);

            context.SaveChanges();
            return(Created($"/{ApiEndpoint.POST_API}?id={entity.Id}",
                           new AppResultBuilder().Success(entity.Id)));
        }
コード例 #14
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());
        }
コード例 #15
0
        public async Task EditPost()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "EditPost_Database")
                          .Options;
            var dbContext   = new ApplicationDbContext(options);
            var postService = new PostService(dbContext);

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

            await postService.EditPostAsync(1, "Hello i am new content", "Hello i am new title", "u1", false);

            var post = await dbContext.Posts.FirstAsync();

            Assert.Equal(1, post.Id);
            Assert.Equal("Hello i am new content", post.Content);
            Assert.Equal("Hello i am new title", post.Title);
            Assert.Equal("u1", post.UserId);
            Assert.Equal(1, await postService.PostsCountAsync(null));
        }
コード例 #16
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);
        }
コード例 #17
0
        public async void AllVotesForPost()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "AllVotesForPost_Database")
                          .Options;
            var dbContext   = new ApplicationDbContext(options);
            var voteService = new VoteService(dbContext);
            var postService = new PostService(dbContext);

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

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

            await voteService.VoteAsync("u2", 1, true);

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

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

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

            Assert.Equal(1, vote.Id);
            Assert.Equal(VoteType.Up, vote.VoteType);
            Assert.Equal(1, vote.PostId);
            Assert.Equal("u1", vote.UserId);
            Assert.Equal(3, votesSum);
            Assert.Equal(3, votesCount);
        }