Browse Source

CmsKit: comment tests

pull/5016/head
Yunus Emre Kalkan 6 years ago
parent
commit
c3f472fdc6
  1. 24
      modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs
  2. 2
      modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs
  3. 23
      modules/cms-kit/test/Volo.CmsKit.Application.Tests/CmsKitApplicationTestBase.cs
  4. 101
      modules/cms-kit/test/Volo.CmsKit.Application.Tests/Comments/CommentPublicAppService_Tests.cs
  5. 21
      modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Samples/SampleManager_Tests.cs
  6. 9
      modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Comments/CommentRepository_Tests.cs
  7. 12
      modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Samples/SampleRepository_Tests.cs
  8. 9
      modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Comments/CommentRepository_Tests.cs
  9. 14
      modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Samples/SampleRepository_Tests.cs
  10. 77
      modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs
  11. 22
      modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitTestData.cs
  12. 44
      modules/cms-kit/test/Volo.CmsKit.TestBase/Comments/CommentRepository_Tests.cs
  13. 27
      modules/cms-kit/test/Volo.CmsKit.TestBase/Samples/SampleRepository_Tests.cs

24
modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MongoDB.Driver.Linq; using MongoDB.Driver.Linq;
@ -24,17 +25,26 @@ namespace Volo.CmsKit.MongoDB.Comments
Check.NotNullOrWhiteSpace(entityType, nameof(entityType)); Check.NotNullOrWhiteSpace(entityType, nameof(entityType));
Check.NotNullOrWhiteSpace(entityId, nameof(entityId)); Check.NotNullOrWhiteSpace(entityId, nameof(entityId));
var query = from comment in GetMongoQueryable() var authorsQuery = from comment in GetMongoQueryable()
join user in DbContext.CmsUsers on comment.CreatorId equals user.Id join user in DbContext.CmsUsers on comment.CreatorId equals user.Id
where entityType == comment.EntityType && entityId == comment.EntityId where entityType == comment.EntityType && entityId == comment.EntityId
orderby comment.CreationTime orderby comment.CreationTime
select new CommentWithAuthor select user;
{
Comment = comment,
Author = user
};
return await query.ToListAsync(); var authors = await authorsQuery.ToListAsync();
var comments = await GetMongoQueryable()
.Where(c => c.EntityId == entityId && c.EntityType == entityType)
.OrderBy(c => c.CreationTime).ToListAsync();
return comments
.Select(
comment =>
new CommentWithAuthor
{
Comment = comment,
Author = authors.FirstOrDefault(a => a.Id == comment.CreatorId)
}).ToList();
} }
public override async Task DeleteAsync(Guid id, bool autoSave = false, CancellationToken cancellationToken = default) public override async Task DeleteAsync(Guid id, bool autoSave = false, CancellationToken cancellationToken = default)

2
modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs

@ -59,7 +59,7 @@ namespace Volo.CmsKit.Public.Comments
input.Text, input.Text,
input.RepliedCommentId, input.RepliedCommentId,
user.Id, user.Id,
CurrentTenant.GetId() CurrentTenant.Id
)); ));
return ObjectMapper.Map<Comment, CommentDto>(comment); return ObjectMapper.Map<Comment, CommentDto>(comment);

23
modules/cms-kit/test/Volo.CmsKit.Application.Tests/CmsKitApplicationTestBase.cs

@ -1,4 +1,7 @@
namespace Volo.CmsKit using System;
using Volo.CmsKit.EntityFrameworkCore;
namespace Volo.CmsKit
{ {
/* Inherit from this class for your application layer tests. /* Inherit from this class for your application layer tests.
* See SampleAppService_Tests for example. * See SampleAppService_Tests for example.
@ -6,5 +9,21 @@
public abstract class CmsKitApplicationTestBase : CmsKitTestBase<CmsKitApplicationTestModule> public abstract class CmsKitApplicationTestBase : CmsKitTestBase<CmsKitApplicationTestModule>
{ {
protected virtual void UsingDbContext(Action<ICmsKitDbContext> action)
{
using (var dbContext = GetRequiredService<ICmsKitDbContext>())
{
action.Invoke(dbContext);
}
}
protected virtual T UsingDbContext<T>(Func<ICmsKitDbContext, T> action)
{
using (var dbContext = GetRequiredService<ICmsKitDbContext>())
{
return action.Invoke(dbContext);
}
}
} }
} }

101
modules/cms-kit/test/Volo.CmsKit.Application.Tests/Comments/CommentPublicAppService_Tests.cs

@ -0,0 +1,101 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Users;
using Volo.CmsKit.Public.Comments;
using Xunit;
namespace Volo.CmsKit.Comments
{
public class CommentPublicAppService_Tests : CmsKitApplicationTestBase
{
private readonly ICommentPublicAppService _commentAppService;
private ICurrentUser _currentUser;
private readonly CmsKitTestData _cmsKitTestData;
public CommentPublicAppService_Tests()
{
_commentAppService = GetRequiredService<ICommentPublicAppService>();
_cmsKitTestData = GetRequiredService<CmsKitTestData>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
_currentUser = Substitute.For<ICurrentUser>();
services.AddSingleton(_currentUser);
}
[Fact]
public async Task GetAllForEntityAsync()
{
var list = await _commentAppService.GetAllForEntityAsync(_cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1);
list.Items.Count.ShouldBe(2);
list.Items.First().Replies.Count.ShouldBe(2);
}
[Fact]
public async Task CreateAsync()
{
_currentUser.Id.Returns(_cmsKitTestData.User2Id);
var newComment = await _commentAppService.CreateAsync(new CreateCommentInput
{
EntityId = _cmsKitTestData.EntityId1,
EntityType = _cmsKitTestData.EntityType1,
RepliedCommentId = null,
Text = "newComment"
});
UsingDbContext(context =>
{
var comments = context.Comments.Where(x =>
x.EntityId == _cmsKitTestData.EntityId1 && x.EntityType == _cmsKitTestData.EntityType1).ToList();
comments
.Any(c=>c.Id == newComment.Id && c.CreatorId == newComment.CreatorId && c.Text == "newComment")
.ShouldBeTrue();
});
}
[Fact]
public async Task UpdateAsync()
{
_currentUser.Id.Returns(_cmsKitTestData.User1Id);
await _commentAppService.UpdateAsync(_cmsKitTestData.CommentWithChildId, new UpdateCommentInput
{
Text = "I'm Updated"
});
UsingDbContext(context =>
{
var comment = context.Comments.Single(x =>
x.Id == _cmsKitTestData.CommentWithChildId);
comment.Text.ShouldBe("I'm Updated");
});
}
[Fact]
public async Task DeleteAsync()
{
_currentUser.Id.Returns(_cmsKitTestData.User1Id);
await _commentAppService.DeleteAsync(_cmsKitTestData.CommentWithChildId);
UsingDbContext(context =>
{
var comment = context.Comments.FirstOrDefault(x =>
x.Id == _cmsKitTestData.CommentWithChildId);
comment.ShouldBeNull();
});
}
}
}

21
modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Samples/SampleManager_Tests.cs

@ -1,21 +0,0 @@
using System.Threading.Tasks;
using Xunit;
namespace Volo.CmsKit.Samples
{
public class SampleManager_Tests : CmsKitDomainTestBase
{
//private readonly SampleManager _sampleManager;
public SampleManager_Tests()
{
//_sampleManager = GetRequiredService<SampleManager>();
}
[Fact]
public async Task Method1Async()
{
}
}
}

9
modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Comments/CommentRepository_Tests.cs

@ -0,0 +1,9 @@
using Volo.CmsKit.Comments;
namespace Volo.CmsKit.EntityFrameworkCore.Comments
{
public class CommentRepository_Tests : CommentRepository_Tests<CmsKitEntityFrameworkCoreTestModule>
{
}
}

12
modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Samples/SampleRepository_Tests.cs

@ -1,12 +0,0 @@
using Volo.CmsKit.Samples;
namespace Volo.CmsKit.EntityFrameworkCore.Samples
{
public class SampleRepository_Tests : SampleRepository_Tests<CmsKitEntityFrameworkCoreTestModule>
{
/* Don't write custom repository tests here, instead write to
* the base class.
* One exception can be some specific tests related to EF core.
*/
}
}

9
modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Comments/CommentRepository_Tests.cs

@ -0,0 +1,9 @@
using Volo.CmsKit.Comments;
namespace Volo.CmsKit.MongoDB.Comments
{
public class CommentRepository_Tests : CommentRepository_Tests<CmsKitMongoDbTestModule>
{
}
}

14
modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Samples/SampleRepository_Tests.cs

@ -1,14 +0,0 @@
using Volo.CmsKit.Samples;
using Xunit;
namespace Volo.CmsKit.MongoDB.Samples
{
[Collection(MongoTestCollection.Name)]
public class SampleRepository_Tests : SampleRepository_Tests<CmsKitMongoDbTestModule>
{
/* Don't write custom repository tests here, instead write to
* the base class.
* One exception can be some specific tests related to MongoDB.
*/
}
}

77
modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs

@ -2,26 +2,87 @@
using Volo.Abp.Data; using Volo.Abp.Data;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids; using Volo.Abp.Guids;
using Volo.Abp.Users;
using Volo.CmsKit.Comments;
using Volo.CmsKit.Reactions;
using Volo.CmsKit.Users;
namespace Volo.CmsKit namespace Volo.CmsKit
{ {
public class CmsKitDataSeedContributor : IDataSeedContributor, ITransientDependency public class CmsKitDataSeedContributor : IDataSeedContributor, ITransientDependency
{ {
private readonly IGuidGenerator _guidGenerator; private readonly IGuidGenerator _guidGenerator;
private readonly ICmsUserRepository _cmsUserRepository;
private readonly CmsKitTestData _cmsKitTestData;
private readonly IUserReactionRepository _userReactionRepository;
private readonly ICommentRepository _commentRepository;
public CmsKitDataSeedContributor( public CmsKitDataSeedContributor(
IGuidGenerator guidGenerator) IGuidGenerator guidGenerator,
ICmsUserRepository cmsUserRepository,
CmsKitTestData cmsKitTestData,
IUserReactionRepository userReactionRepository,
ICommentRepository commentRepository)
{ {
_guidGenerator = guidGenerator; _guidGenerator = guidGenerator;
_cmsUserRepository = cmsUserRepository;
_cmsKitTestData = cmsKitTestData;
_userReactionRepository = userReactionRepository;
_commentRepository = commentRepository;
} }
public Task SeedAsync(DataSeedContext context) public async Task SeedAsync(DataSeedContext context)
{ {
/* Instead of returning the Task.CompletedTask, you can insert your test data await _cmsUserRepository.InsertAsync(new CmsUser(new UserData(_cmsKitTestData.User1Id, "user1","user1@volo.com", "user","1")));
* at this point! await _cmsUserRepository.InsertAsync(new CmsUser(new UserData(_cmsKitTestData.User2Id, "user2","user2@volo.com", "user","2")));
*/
var comment1 = await _commentRepository.InsertAsync(new Comment(_cmsKitTestData.CommentWithChildId,
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
"comment",
null,
_cmsKitTestData.User1Id
));
await _commentRepository.InsertAsync(new Comment(_guidGenerator.Create(),
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
"reply",
comment1.Id,
_cmsKitTestData.User2Id
));
await _commentRepository.InsertAsync(new Comment(_guidGenerator.Create(),
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
"reply",
comment1.Id,
_cmsKitTestData.User1Id
));
await _commentRepository.InsertAsync(new Comment(_guidGenerator.Create(),
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
"comment",
null,
_cmsKitTestData.User2Id
));
await _commentRepository.InsertAsync(new Comment(_guidGenerator.Create(),
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId2,
"comment",
null,
_cmsKitTestData.User2Id
));
return Task.CompletedTask; await _commentRepository.InsertAsync(new Comment(_guidGenerator.Create(),
_cmsKitTestData.EntityType2,
_cmsKitTestData.EntityId1,
"comment",
null,
_cmsKitTestData.User2Id
));
} }
} }
} }

22
modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitTestData.cs

@ -0,0 +1,22 @@
using System;
using Volo.Abp.DependencyInjection;
namespace Volo.CmsKit
{
public class CmsKitTestData : ISingletonDependency
{
public Guid User1Id { get; } = Guid.NewGuid();
public Guid User2Id { get; } = Guid.NewGuid();
public Guid CommentWithChildId { get; } = Guid.NewGuid();
public string EntityType1 { get; } = "EntityName1";
public string EntityType2 { get; } = "EntityName2";
public string EntityId1 { get; } = "1";
public string EntityId2 { get; } = "2";
}
}

44
modules/cms-kit/test/Volo.CmsKit.TestBase/Comments/CommentRepository_Tests.cs

@ -0,0 +1,44 @@
using System.Linq;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Modularity;
using Xunit;
namespace Volo.CmsKit.Comments
{
public abstract class CommentRepository_Tests<TStartupModule> : CmsKitTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
private readonly CmsKitTestData _cmsKitTestData;
private readonly ICommentRepository _commentRepository;
public CommentRepository_Tests()
{
_cmsKitTestData = GetRequiredService<CmsKitTestData>();
_commentRepository = GetRequiredService<ICommentRepository>();
}
[Fact]
public async Task GetListWithAuthorsAsync()
{
var list = await _commentRepository.GetListWithAuthorsAsync(_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1);
list.Count.ShouldBe(4);
list.Any(x=>x.Comment == null).ShouldBeFalse();
list.Any(x=>x.Author == null).ShouldBeFalse();
}
[Fact]
public async Task DeleteAsync()
{
await _commentRepository.DeleteAsync(_cmsKitTestData.CommentWithChildId);
var list = await _commentRepository.GetListAsync();
list.Any(x=>
x.Id == _cmsKitTestData.CommentWithChildId || x.RepliedCommentId == _cmsKitTestData.CommentWithChildId)
.ShouldBeFalse();
}
}
}

27
modules/cms-kit/test/Volo.CmsKit.TestBase/Samples/SampleRepository_Tests.cs

@ -1,27 +0,0 @@
using System.Threading.Tasks;
using Volo.Abp.Modularity;
using Xunit;
namespace Volo.CmsKit.Samples
{
/* Write your custom repository tests like that, in this project, as abstract classes.
* Then inherit these abstract classes from EF Core & MongoDB test projects.
* In this way, both database providers are tests with the same set tests.
*/
public abstract class SampleRepository_Tests<TStartupModule> : CmsKitTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
//private readonly ISampleRepository _sampleRepository;
protected SampleRepository_Tests()
{
//_sampleRepository = GetRequiredService<ISampleRepository>();
}
[Fact]
public async Task Method1Async()
{
}
}
}
Loading…
Cancel
Save