Browse Source

Blogging module: Added basic unit tests

pull/441/head
Yunus Emre Kalkan 8 years ago
parent
commit
1d321a3856
  1. 88
      modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/BlogAppService_Tests.cs
  2. 49
      modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/CommentAppService_Tests.cs
  3. 28
      modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/PostAppService_Tests.cs
  4. 85
      modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/TagAppService_Tests.cs
  5. 2
      modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestData.cs
  6. 12
      modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestDataBuilder.cs
  7. 8
      modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/Blogs/BlogRepository_Tests.cs

88
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<IBlogAppService>();
_blogRepository = GetRequiredService<IBlogRepository>();
}
[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);
}
}
}

49
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);
}
}
}

28
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<BloggingTestData>();
_postAppService = GetRequiredService<IPostAppService>();
_blogRepository = GetRequiredService<IBlogRepository>();
_postRepository = GetRequiredService<IPostRepository>();
}
[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);
}
}
}

85
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<ITagAppService>();
_tagRepository = GetRequiredService<ITagRepository>();
}
[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);
}
}
}

2
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";
}
}

12
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));
}
}
}

8
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();
}
}

Loading…
Cancel
Save