From 1d321a3856b628e8e7caa370b5e48df48bbb2217 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 17 Sep 2018 10:02:16 +0300 Subject: [PATCH] Blogging module: Added basic unit tests --- .../Volo/Blogging/BlogAppService_Tests.cs | 88 +++++++++++++++++++ .../Volo/Blogging/CommentAppService_Tests.cs | 49 ++++++++++- .../Volo/Blogging/PostAppService_Tests.cs | 28 +++++- .../Volo/Blogging/TagAppService_Tests.cs | 85 ++++++++++++++++++ .../Volo/Blogging/BloggingTestData.cs | 2 + .../Volo/Blogging/BloggingTestDataBuilder.cs | 12 ++- .../Blogging/Blogs/BlogRepository_Tests.cs | 8 +- 7 files changed, 262 insertions(+), 10 deletions(-) create mode 100644 modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/BlogAppService_Tests.cs create mode 100644 modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/TagAppService_Tests.cs diff --git a/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/BlogAppService_Tests.cs b/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/BlogAppService_Tests.cs new file mode 100644 index 0000000000..b127f8683d --- /dev/null +++ b/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/BlogAppService_Tests.cs @@ -0,0 +1,88 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Shouldly; +using Volo.Blogging.Blogs; +using Volo.Blogging.Blogs.Dtos; +using Volo.Blogging.Comments; +using Volo.Blogging.Comments.Dtos; +using Volo.Blogging.Posts; +using Xunit; + +namespace Volo.Blogging +{ + public class BlogAppService_Tests : BloggingApplicationTestBase + { + private readonly IBlogAppService _blogAppService; + private readonly IBlogRepository _blogRepository; + + public BlogAppService_Tests() + { + _blogAppService = GetRequiredService(); + _blogRepository = GetRequiredService(); + } + + [Fact] + public async Task Should_Get_List_Of_Blogs() + { + var blogs = await _blogAppService.GetListAsync(); + + blogs.Items.Count.ShouldBeGreaterThan(0); + } + + [Fact] + public async Task Should_Get_Blog_By_Shortname() + { + var targetBlog = (await _blogAppService.GetListAsync()).Items.First(); + + var blog = await _blogAppService.GetByShortNameAsync(targetBlog.ShortName); + + blog.ShouldNotBeNull(); + blog.Name.ShouldBe(targetBlog.Name); + } + + [Fact] + public async Task Should_Create_A_Blog() + { + var name = "test name"; + var shortName = "test shortName"; + var description = "test description"; + + var blogDto = await _blogAppService.Create(new CreateBlogDto() { Name = name, ShortName = name, Description = description }); + + UsingDbContext(context => + { + var blog = context.Blogs.FirstOrDefault(q => q.Id == blogDto.Id); + blog.ShouldNotBeNull(); + blog.Name.ShouldBe(blogDto.Name); + blog.ShortName.ShouldBe(blogDto.ShortName); + blog.Description.ShouldBe(blogDto.Description); + }); + } + + [Fact] + public async Task Should_Update_A_Blog() + { + var newDescription = "new description"; + + var oldBlog = (await _blogRepository.GetListAsync()).FirstOrDefault(); ; + + await _blogAppService.Update(oldBlog.Id, new UpdateBlogDto() + { Description = newDescription, Name = oldBlog.Name, ShortName = oldBlog.ShortName }); + + UsingDbContext(context => + { + var blog = context.Blogs.FirstOrDefault(q => q.Id == oldBlog.Id); + blog.Description.ShouldBe(newDescription); + }); + } + + [Fact] + public async Task Should_Delete_A_Blog() + { + var blog = (await _blogRepository.GetListAsync()).First(); + + await _blogAppService.Delete(blog.Id); + } + } +} diff --git a/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/CommentAppService_Tests.cs b/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/CommentAppService_Tests.cs index 7c1eee6bc2..d9dcdf41c5 100644 --- a/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/CommentAppService_Tests.cs +++ b/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/CommentAppService_Tests.cs @@ -28,16 +28,57 @@ namespace Volo.Blogging [Fact] public async Task Should_Get_List_Of_Comments() { - var blog = (await _blogRepository.GetListAsync()).FirstOrDefault(); var post = (await _postRepository.GetListAsync()).FirstOrDefault(); - var comment1 = await _commentRepository.InsertAsync(new Comment(Guid.NewGuid(), post.Id, null, "qweasd")); - var comment2 = await _commentRepository.InsertAsync(new Comment(Guid.NewGuid(), post.Id, null, "qweasd")); + await _commentRepository.InsertAsync(new Comment(Guid.NewGuid(), post.Id, null, "qweasd")); + await _commentRepository.InsertAsync(new Comment(Guid.NewGuid(), post.Id, null, "qweasd")); var comments = await _commentAppService.GetHierarchicalListOfPostAsync( new GetCommentListOfPostAsync() { PostId = post.Id }); - comments.Count.ShouldBe(2); + comments.Count.ShouldBeGreaterThan(2); + } + + [Fact] + public async Task Should_Create_A_Comment() + { + var postId = (await _postRepository.GetListAsync()).First().Id; + var content = "test content"; + + var commentWithDetailsDto = await _commentAppService.CreateAsync(new CreateCommentDto() + {PostId = postId,Text = content}); + + UsingDbContext(context => + { + var comment = context.Comments.FirstOrDefault(q => q.Id == commentWithDetailsDto.Id); + comment.ShouldNotBeNull(); + comment.Text.ShouldBe(commentWithDetailsDto.Text); + }); + } + + [Fact] + public async Task Should_Update_A_Comment() + { + var newContent = "new content"; + + var oldComment = (await _commentRepository.GetListAsync()).FirstOrDefault(); ; + + await _commentAppService.UpdateAsync(oldComment.Id, new UpdateCommentDto() + {Text = newContent}); + + UsingDbContext(context => + { + var comment = context.Comments.FirstOrDefault(q => q.Id == oldComment.Id); + comment.Text.ShouldBe(newContent); + }); + } + + [Fact] + public async Task Should_Delete_A_Comment() + { + var comment = (await _commentRepository.GetListAsync()).First(); + + await _commentAppService.DeleteAsync(comment.Id); } } } diff --git a/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/PostAppService_Tests.cs b/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/PostAppService_Tests.cs index 7e611ef1b1..ec4210446c 100644 --- a/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/PostAppService_Tests.cs +++ b/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/PostAppService_Tests.cs @@ -12,16 +12,32 @@ namespace Volo.Blogging private readonly IPostAppService _postAppService; private readonly IBlogRepository _blogRepository; private readonly IPostRepository _postRepository; - private readonly BloggingTestData _testData; public PostAppService_Tests() { - _testData = GetRequiredService(); _postAppService = GetRequiredService(); _blogRepository = GetRequiredService(); _postRepository = GetRequiredService(); } + [Fact] + public async Task Should_Get_List_Of_Posts() + { + var blogId = (await _blogRepository.GetListAsync()).First().Id; + var posts = await _postAppService.GetListByBlogIdAndTagName(blogId, null); + posts.Items.Count.ShouldBeGreaterThan(0); + } + + [Fact] + public async Task Should_Get_Fore_Reading() + { + var blogId = (await _blogRepository.GetListAsync()).First().Id; + var post = (await _postRepository.GetListAsync()).First(p=>p.BlogId == blogId); + var postToRead = await _postAppService.GetForReadingAsync(new GetPostInput() {BlogId = blogId, Url = post.Url}); + + postToRead.ShouldNotBeNull(); + } + [Fact] public async Task Should_Create_A_Post() { @@ -79,5 +95,13 @@ namespace Volo.Blogging post.Content.ShouldBe(newContent); }); } + + [Fact] + public async Task Should_Delete_A_Post() + { + var post = (await _postRepository.GetListAsync()).First(); + + await _postAppService.DeleteAsync(post.Id); + } } } diff --git a/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/TagAppService_Tests.cs b/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/TagAppService_Tests.cs new file mode 100644 index 0000000000..e2bca13c9a --- /dev/null +++ b/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/TagAppService_Tests.cs @@ -0,0 +1,85 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Shouldly; +using Volo.Blogging.Blogs; +using Volo.Blogging.Blogs.Dtos; +using Volo.Blogging.Comments; +using Volo.Blogging.Comments.Dtos; +using Volo.Blogging.Posts; +using Volo.Blogging.Tagging; +using Volo.Blogging.Tagging.Dtos; +using Xunit; + +namespace Volo.Blogging +{ + public class TagAppService_Tests : BloggingApplicationTestBase + { + private readonly ITagAppService _tagAppService; + private readonly ITagRepository _tagRepository; + + public TagAppService_Tests() + { + _tagAppService = GetRequiredService(); + _tagRepository = GetRequiredService(); + } + + [Fact] + public async Task Should_Get_List_Of_Tags() + { + var tags = await _tagAppService.GetListAsync(); + + tags.Count.ShouldBeGreaterThan(0); + } + + [Fact] + public async Task Should_Get_Popular_Tags() + { + var tags = await _tagAppService.GetPopularTags(new GetPopularTagsInput() { ResultCount = 5, MinimumPostCount = 0 }); + + tags.Count.ShouldBeGreaterThan(0); + } + + [Fact] + public async Task Should_Create_A_Tag() + { + var name = "test name"; + var description = "test description"; + + var tagDto = await _tagAppService.CreateAsync(new CreateTagDto() { Name = name, Description = description }); + + UsingDbContext(context => + { + var tag = context.Tags.FirstOrDefault(q => q.Id == tagDto.Id); + tag.ShouldNotBeNull(); + tag.Name.ShouldBe(tagDto.Name); + tag.Description.ShouldBe(tagDto.Description); + }); + } + + [Fact] + public async Task Should_Update_A_Tag() + { + var newDescription = "new description"; + + var oldTag = (await _tagRepository.GetListAsync()).FirstOrDefault(); ; + + await _tagAppService.UpdateAsync(oldTag.Id, new UpdateTagDto() + { Description = newDescription, Name = oldTag.Name}); + + UsingDbContext(context => + { + var tag = context.Tags.FirstOrDefault(q => q.Id == oldTag.Id); + tag.Description.ShouldBe(newDescription); + }); + } + + [Fact] + public async Task Should_Delete_A_Tag() + { + var tag = (await _tagRepository.GetListAsync()).First(); + + await _tagAppService.DeleteAsync(tag.Id); + } + } +} diff --git a/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestData.cs b/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestData.cs index 323e29d45f..72910fb6ca 100644 --- a/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestData.cs +++ b/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestData.cs @@ -8,5 +8,7 @@ namespace Volo.Blogging public Guid Blog1Id { get; } = Guid.NewGuid(); public Guid Blog1Post1Id { get; } = Guid.NewGuid(); public Guid Blog1Post2Id { get; } = Guid.NewGuid(); + public Guid Blog1Post1Comment1Id { get; } = Guid.NewGuid(); + public string Tag1Name { get; } = "Tag1Name"; } } \ No newline at end of file diff --git a/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestDataBuilder.cs b/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestDataBuilder.cs index 2d1c3df7b9..799fca03e8 100644 --- a/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestDataBuilder.cs +++ b/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestDataBuilder.cs @@ -3,7 +3,9 @@ using System.Threading.Tasks; using Volo.Abp.DependencyInjection; using Volo.Abp.Threading; using Volo.Blogging.Blogs; +using Volo.Blogging.Comments; using Volo.Blogging.Posts; +using Volo.Blogging.Tagging; namespace Volo.Blogging { @@ -12,15 +14,21 @@ namespace Volo.Blogging private readonly BloggingTestData _testData; private readonly IBlogRepository _blogRepository; private readonly IPostRepository _postRepository; + private readonly ICommentRepository _commentRepository; + private readonly ITagRepository _tagRepository; public BloggingTestDataBuilder( BloggingTestData testData, IBlogRepository blogRepository, - IPostRepository postRepository) + IPostRepository postRepository, + ICommentRepository commentRepository, + ITagRepository tagRepository) { _testData = testData; _blogRepository = blogRepository; _postRepository = postRepository; + _commentRepository = commentRepository; + _tagRepository = tagRepository; } public void Build() @@ -33,6 +41,8 @@ namespace Volo.Blogging await _blogRepository.InsertAsync(new Blog(_testData.Blog1Id, "The First Blog", "blog-1")); await _postRepository.InsertAsync(new Post(_testData.Blog1Post1Id, _testData.Blog1Id, Guid.Empty, "title", "coverImage", "url")); await _postRepository.InsertAsync(new Post(_testData.Blog1Post2Id, _testData.Blog1Id, Guid.Empty, "title", "coverImage", "url")); + await _commentRepository.InsertAsync(new Comment(_testData.Blog1Post1Comment1Id,_testData.Blog1Post1Id,null,"text")); + await _tagRepository.InsertAsync(new Tag(_testData.Tag1Name)); } } } diff --git a/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/Blogs/BlogRepository_Tests.cs b/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/Blogs/BlogRepository_Tests.cs index a6976fcc4d..f1dfa58ab4 100644 --- a/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/Blogs/BlogRepository_Tests.cs +++ b/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/Blogs/BlogRepository_Tests.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Linq; +using System.Threading.Tasks; using Shouldly; using Volo.Abp.Modularity; using Xunit; @@ -16,9 +17,10 @@ namespace Volo.Blogging.Blogs } [Fact] - public async Task FindByShortNameAsync() + public async Task Should_Find_By_ShortName() { - var blog = await BlogRepository.FindByShortNameAsync("blog-1"); + var blogFromRepository = (await BlogRepository.GetListAsync()).First(); + var blog = await BlogRepository.FindByShortNameAsync(blogFromRepository.ShortName); blog.ShouldNotBeNull(); } }