Пример #1
0
        public async Task Handler_Should_AddPost()
        {
            var published = DateTime.UtcNow;
            var result    = await Mediator.Send(new AddPostCommand
            {
                UserName     = "******",
                Categories   = "category",
                Title        = "title",
                Content      = "content",
                Description  = "description",
                CoverCaption = "cover caption",
                CoverLink    = "cover link",
                Published    = published
            });

            result.IsSuccess.Should().BeTrue();
            result.Value.AuthorId.Should().Be(_authorId);
            result.Value.Title.Should().Be("title");
            result.Value.Categories.Should().Be("category");
            result.Value.Content.Should().Be("content");
            result.Value.Description.Should().Be("description");
            result.Value.CoverCaption.Should().Be("cover caption");
            result.Value.CoverLink.Should().Be("cover link");
            result.Value.Published.Should().Be(published);

            PostRepositoryMock.Verify(m => m.Add(It.IsAny <Post>()), Times.Once);
        }
Пример #2
0
        public GetPostQueryTests()
        {
            _author = new Author {
                UserName = "******", DisplayName = "Author 1"
            };

            PostRepositoryMock.Setup(m => m.SingleOrDefaultAsync(It.IsAny <Expression <Func <Post, bool> > >(), It.IsAny <CancellationToken>())).ReturnsAsync(CreatePost(0, _author));
        }
Пример #3
0
        public async Task Handler_Should_GetPosts_ForPage2()
        {
            var result = await Mediator.Send(new SearchPostsQuery { Page = 2 });

            result.IsSuccess.Should().BeTrue();

            PostRepositoryMock.Verify(m => m.GetAsync(It.IsAny <IEnumerable <Expression <Func <Post, bool> > > >(), 3, It.IsAny <int>(), It.IsAny <CancellationToken>()), Times.Once);
        }
Пример #4
0
        public async Task Handler_Should_ReturnNotFoundException()
        {
            PostRepositoryMock.Setup(m => m.SingleOrDefaultAsync(It.IsAny <Expression <Func <Post, bool> > >(), It.IsAny <CancellationToken>())).ReturnsAsync(default(Post));

            var result = await Mediator.Send(new GetPostQuery { Slug = "post-title-invalid" });

            result.IsSuccess.Should().BeFalse();
            result.Exception.Should().BeOfType <NotFoundException <Post> >();
        }
Пример #5
0
        public async Task Handler_Should_ReturnNotFoundException()
        {
            PostRepositoryMock.Setup(m => m.SingleOrDefaultAsync(It.IsAny <Expression <Func <Post, bool> > >(), It.IsAny <CancellationToken>())).ReturnsAsync(default(Post));

            var result = await Mediator.Send(new UnpublishPostCommand { Id = Guid.NewGuid() });

            result.IsSuccess.Should().BeFalse();
            result.Exception.Should().BeOfType <NotFoundException <Post> >();
        }
Пример #6
0
        public async Task Handler_Should_Next_CoverUrl_ReplaceBaseUrlWithUrlFormat()
        {
            PostRepositoryMock.Setup(m => m.GetNextAsync(It.IsAny <DateTime>(), It.IsAny <CancellationToken>())).ReturnsAsync(CreatePost(2, _author));

            var result = await Mediator.Send(new GetPostQuery { Slug = "post-title-0" });

            result.IsSuccess.Should().BeTrue();

            result.Value.Should().NotBeNull();
            result.Value.Next.CoverUrl.Should().Be("http://127.0.0.1:10000/devstoreaccount1/pineblog-tests/pineblog-tests/blog-cover-url");
        }
Пример #7
0
        public async Task Handler_Should_ReturnPostModel_WithNoPreviousPost()
        {
            PostRepositoryMock.Setup(m => m.GetNextAsync(It.IsAny <DateTime>(), It.IsAny <CancellationToken>())).ReturnsAsync(CreatePost(2, _author));

            var result = await Mediator.Send(new GetPostQuery { Slug = "post-title-0" });

            result.IsSuccess.Should().BeTrue();
            result.Value.Post.Title.Should().Be("Post title 0");
            result.Value.Previous.Should().BeNull();
            result.Value.Next.Should().NotBeNull();
        }
Пример #8
0
        public async Task Handler_Should_Previous_UrlsInContent_ReplaceBaseUrlWithUrlFormat()
        {
            PostRepositoryMock.Setup(m => m.GetPreviousAsync(It.IsAny <DateTime>(), It.IsAny <CancellationToken>())).ReturnsAsync(CreatePost(2, _author));

            var result = await Mediator.Send(new GetPostQuery { Slug = "post-title-0" });

            result.IsSuccess.Should().BeTrue();

            result.Value.Should().NotBeNull();
            result.Value.Previous.Content.Should().Be("content with an url: http://127.0.0.1:10000/devstoreaccount1/pineblog-tests/pineblog-tests/content-url-1. nice isn't it?");
        }
Пример #9
0
        public async Task Handler_Should_DeletePost()
        {
            Post deletedPost = null;

            PostRepositoryMock.Setup(m => m.Remove(It.IsAny <Post>())).Callback((Post p) => deletedPost = p);

            var result = await Mediator.Send(new DeletePostCommand { Id = _postId });

            result.IsSuccess.Should().BeTrue();

            deletedPost.Should().NotBeNull();
            deletedPost.Published.Should().BeNull();
        }
Пример #10
0
        public GetSyndicationFeedQueryTests() : base()
        {
            var author = new Author {
                UserName = "******", DisplayName = "Author 1"
            };

            var posts = new List <Post>();

            posts.Add(CreatePost(0, author, false, "cat1"));
            posts.Add(CreatePost(1, author, true, "cat1"));
            posts.Add(CreatePost(2, author, true, "cat1,cat2"));
            posts.Add(CreatePost(3, author, true, "cat2"));
            posts.Add(CreatePost(4, author, true, "cat1,cat2,cat3"));

            PostRepositoryMock.Setup(m => m.GetAsync(It.IsAny <IEnumerable <Expression <Func <Post, bool> > > >(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>())).ReturnsAsync(posts);
        }
Пример #11
0
        public async Task Handler_Should_Predicates_CategoryExpression_ForCategoryCat2()
        {
            IEnumerable <Expression <Func <Post, bool> > > createdPredicates = new List <Expression <Func <Post, bool> > >();

            PostRepositoryMock
            .Setup(m => m.GetAsync(It.IsAny <IEnumerable <Expression <Func <Post, bool> > > >(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .Callback((IEnumerable <Expression <Func <Post, bool> > > predicates, int _, int __, CancellationToken ___) => createdPredicates = predicates);

            var result = await Mediator.Send(new GetPagedPostListQuery { Page = 1, Category = "cat2", ItemsPerPage = 100 });

            result.IsSuccess.Should().BeTrue();

            Expression <Func <Post, bool> > categoryExpression = p => p.Categories.Contains("cat2");

            createdPredicates.Should().ContainEquivalentOf(categoryExpression);
        }
Пример #12
0
        public async Task Handler_Should_ReturnNotFoundException()
        {
            PostRepositoryMock.Setup(m => m.SingleOrDefaultAsync(It.IsAny <Expression <Func <Post, bool> > >(), It.IsAny <CancellationToken>())).ReturnsAsync(default(Post));

            var result = await Mediator.Send(new UpdatePostCommand
            {
                Id          = Guid.NewGuid(),
                Categories  = "category",
                Title       = "title",
                Content     = "content",
                Description = "description"
            });

            result.IsSuccess.Should().BeFalse();
            result.Exception.Should().BeOfType <NotFoundException <Post> >();
        }
Пример #13
0
        public async Task Handler_Should_Predicates_Empty_WhenIncludingUnpublishedPosts()
        {
            IEnumerable <Expression <Func <Post, bool> > > createdPredicates = new List <Expression <Func <Post, bool> > >();

            PostRepositoryMock
            .Setup(m => m.GetAsync(It.IsAny <IEnumerable <Expression <Func <Post, bool> > > >(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .Callback((IEnumerable <Expression <Func <Post, bool> > > predicates, int _, int __, CancellationToken ___) => createdPredicates = predicates);

            var result = await Mediator.Send(new GetPagedPostListQuery { Page = 1, IncludeUnpublished = true, ItemsPerPage = 100 });

            result.IsSuccess.Should().BeTrue();

            Expression <Func <Post, bool> > publishedExpression = p => p.Published != null;

            createdPredicates.Should().BeEmpty();
        }
Пример #14
0
        public UnpublishPostCommandTests()
        {
            var posts = new List <Post>();

            posts.Add(new Post
            {
                Id          = _postId,
                AuthorId    = Guid.NewGuid(),
                Title       = "Post title 0",
                Slug        = "post-title-0",
                Description = "Description",
                Content     = "Content",
                Published   = DateTime.UtcNow
            });

            PostRepositoryMock.Setup(m => m.SingleOrDefaultAsync(It.IsAny <Expression <Func <Post, bool> > >(), It.IsAny <CancellationToken>())).ReturnsAsync(posts.SingleOrDefault());
        }
Пример #15
0
        public SearchPostsQueryTests()
        {
            var author = new Author {
                Id = _authorId, UserName = "******", DisplayName = "Author 1"
            };

            var posts = new List <Post>();

            posts.Add(CreatePost(0, author, false));
            posts.Add(CreatePost(1, author, true));
            posts.Add(CreatePost(2, author, true));
            posts.Add(CreatePost(3, author, true));
            posts.Add(CreatePost(4, author, true));

            PostRepositoryMock.Setup(m => m.GetAsync(It.IsAny <IEnumerable <Expression <Func <Post, bool> > > >(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>())).ReturnsAsync(posts);
            PostRepositoryMock.Setup(m => m.CountAsync(It.IsAny <IEnumerable <Expression <Func <Post, bool> > > >(), It.IsAny <CancellationToken>())).ReturnsAsync(posts.Count);
        }
Пример #16
0
        public async Task Handler_Should_Predicates_PublishedNotNullAndSlugEqualsExpression()
        {
            Expression <Func <Post, bool> > createdPredicate = null;

            PostRepositoryMock
            .Setup(m => m.SingleOrDefaultAsync(It.IsAny <Expression <Func <Post, bool> > >(), It.IsAny <CancellationToken>()))
            .Callback((Expression <Func <Post, bool> > predicate, CancellationToken _) => createdPredicate = predicate)
            .ReturnsAsync(CreatePost(0, _author));

            var result = await Mediator.Send(new GetPostQuery { Slug = "post-title-invalid" });

            result.IsSuccess.Should().BeTrue();

            Expression <Func <Post, bool> > expression = p => p.Published != null && p.Slug.Equals("post-title-invalid");

            createdPredicate.Should().BeEquivalentTo(expression);
        }
Пример #17
0
        public GetPostByIdQueryTests()
        {
            var posts = new List <Post>();

            posts.Add(new Post
            {
                Id          = _postId,
                AuthorId    = Guid.NewGuid(),
                Title       = "Post title 0",
                Slug        = "post-title-0",
                Description = "Description",
                Content     = "content with an url: %URL%/pineblog-tests/content-url-1. nice isn't it?",
                CoverUrl    = "%URL%/pineblog-tests/blog-cover-url",
                Published   = DateTime.UtcNow
            });

            PostRepositoryMock.Setup(m => m.SingleOrDefaultAsync(It.IsAny <Expression <Func <Post, bool> > >(), It.IsAny <CancellationToken>())).ReturnsAsync(posts.SingleOrDefault());
        }
Пример #18
0
        public async Task Handler_Should_Predicates_SearchExpression_WhenCountPosts()
        {
            IEnumerable <Expression <Func <Post, bool> > > createdPredicates = new List <Expression <Func <Post, bool> > >();

            PostRepositoryMock
            .Setup(m => m.CountAsync(It.IsAny <IEnumerable <Expression <Func <Post, bool> > > >(), It.IsAny <CancellationToken>()))
            .Callback((IEnumerable <Expression <Func <Post, bool> > > predicates, CancellationToken _) => createdPredicates = predicates);

            var result = await Mediator.Send(new SearchPostsQuery { Page = 1, SearchQuery = "text text2", ItemsPerPage = 100 });

            result.IsSuccess.Should().BeTrue();

            var predicate = createdPredicates.Where(p =>
                                                    p.Body.ToString().Contains("p.Title.Contains(\"text\")") &&
                                                    p.Body.ToString().Contains("p.Title.Contains(\"text2\")") &&
                                                    p.Body.ToString().Contains("OrElse p.Description.Contains(\"text\")") &&
                                                    p.Body.ToString().Contains("OrElse p.Description.Contains(\"text2\")") &&
                                                    p.Body.ToString().Contains("OrElse p.Categories.Contains(\"text\")") &&
                                                    p.Body.ToString().Contains("OrElse p.Categories.Contains(\"text2\")") &&
                                                    p.Body.ToString().Contains("OrElse p.Content.Contains(\"text\")") &&
                                                    p.Body.ToString().Contains("OrElse p.Content.Contains(\"text2\")"));

            predicate.Should().NotBeNull();
        }