Browse Source

Cmskit: reaction tests

pull/5016/head
Yunus Emre Kalkan 6 years ago
parent
commit
af58bd5f45
  1. 2
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/ReactionManager.cs
  2. 8
      modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs
  3. 2
      modules/cms-kit/test/Volo.CmsKit.Application.Tests/CmsKitApplicationTestBase.cs
  4. 105
      modules/cms-kit/test/Volo.CmsKit.Application.Tests/Reactions/ReactionPublicAppService_Tests.cs
  5. 19
      modules/cms-kit/test/Volo.CmsKit.Domain.Tests/CmsKitDomainTestBase.cs
  6. 82
      modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Reactions/ReactionManager_Tests.cs
  7. 8
      modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Reactions/UserReactionRepository_Tests.cs
  8. 8
      modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Reactions/UserReactionRepository_Tests.cs
  9. 59
      modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs
  10. 70
      modules/cms-kit/test/Volo.CmsKit.TestBase/Reactions/UserReactionRepository_Tests.cs

2
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/ReactionManager.cs

@ -74,7 +74,7 @@ namespace Volo.CmsKit.Reactions
entityId,
reactionName,
creatorId,
CurrentTenant.GetId()
CurrentTenant.Id
)
);
}

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

@ -31,7 +31,7 @@ namespace Volo.CmsKit.Public.Comments
CmsUserLookupService = cmsUserLookupService;
}
public async Task<ListResultDto<CommentWithDetailsDto>> GetAllForEntityAsync(string entityType, string entityId)
public virtual async Task<ListResultDto<CommentWithDetailsDto>> GetAllForEntityAsync(string entityType, string entityId)
{
CheckAuthorizationAsync(entityType);
@ -43,7 +43,7 @@ namespace Volo.CmsKit.Public.Comments
}
[Authorize]
public async Task<CommentDto> CreateAsync(CreateCommentInput input)
public virtual async Task<CommentDto> CreateAsync(CreateCommentInput input)
{
var user = await CmsUserLookupService.FindByIdAsync(CurrentUser.GetId());
@ -66,7 +66,7 @@ namespace Volo.CmsKit.Public.Comments
}
[Authorize]
public async Task<CommentDto> UpdateAsync(Guid id, UpdateCommentInput input)
public virtual async Task<CommentDto> UpdateAsync(Guid id, UpdateCommentInput input)
{
var comment = await CommentRepository.GetAsync(id);
@ -83,7 +83,7 @@ namespace Volo.CmsKit.Public.Comments
}
[Authorize]
public async Task DeleteAsync(Guid id)
public virtual async Task DeleteAsync(Guid id)
{
var comment = await CommentRepository.GetAsync(id);

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

@ -8,7 +8,6 @@ namespace Volo.CmsKit
*/
public abstract class CmsKitApplicationTestBase : CmsKitTestBase<CmsKitApplicationTestModule>
{
protected virtual void UsingDbContext(Action<ICmsKitDbContext> action)
{
using (var dbContext = GetRequiredService<ICmsKitDbContext>())
@ -24,6 +23,5 @@ namespace Volo.CmsKit
return action.Invoke(dbContext);
}
}
}
}

105
modules/cms-kit/test/Volo.CmsKit.Application.Tests/Reactions/ReactionPublicAppService_Tests.cs

@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
using Volo.Abp.Users;
using Volo.CmsKit.Public.Reactions;
using Xunit;
namespace Volo.CmsKit.Reactions
{
public class ReactionPublicAppService_Tests : CmsKitApplicationTestBase
{
private readonly CmsKitTestData _cmsKitTestData;
private readonly ReactionPublicAppService _reactionPublicAppService;
private ICurrentUser _currentUser;
public ReactionPublicAppService_Tests()
{
_cmsKitTestData = GetRequiredService<CmsKitTestData>();
_reactionPublicAppService = GetRequiredService<ReactionPublicAppService>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
_currentUser = Substitute.For<ICurrentUser>();
services.AddSingleton(_currentUser);
}
[Fact]
public async Task GetForSelectionAsync()
{
_currentUser.Id.Returns(_cmsKitTestData.User1Id);
var reactions = await _reactionPublicAppService.GetForSelectionAsync(
_cmsKitTestData.EntityType2,
_cmsKitTestData.EntityId1
);
reactions.Items.
First(r=>r.Reaction.Name == StandardReactions.Rocket).IsSelectedByCurrentUser.ShouldBeTrue();
reactions.Items.
First(r=>r.Reaction.Name == StandardReactions.Rocket).Count.ShouldBe(1);
reactions.Items.
Where(r=>r.Reaction.Name != StandardReactions.Rocket).All(r=>!r.IsSelectedByCurrentUser)
.ShouldBeTrue();
reactions.Items.
Where(r=>r.Reaction.Name != StandardReactions.Rocket).All(r=> r.Count == 0)
.ShouldBeTrue();
}
[Fact]
public async Task CreateAsync()
{
_currentUser.Id.Returns(_cmsKitTestData.User1Id);
await _reactionPublicAppService.CreateAsync(new CreateReactionDto
{
EntityType = _cmsKitTestData.EntityType2,
EntityId = _cmsKitTestData.EntityId2,
ReactionName = StandardReactions.Hooray
});
UsingDbContext(context =>
{
var reaction = context.UserReactions.FirstOrDefault(x =>
x.CreatorId == _cmsKitTestData.User1Id &&
x.ReactionName == StandardReactions.Hooray &&
x.EntityId == _cmsKitTestData.EntityId2 &&
x.EntityType == _cmsKitTestData.EntityType2);
reaction.ShouldNotBeNull();
});
}
[Fact]
public async Task DeleteAsync()
{
_currentUser.Id.Returns(_cmsKitTestData.User1Id);
await _reactionPublicAppService.DeleteAsync(new DeleteReactionDto
{
EntityType = _cmsKitTestData.EntityType1,
EntityId = _cmsKitTestData.EntityId1,
ReactionName = StandardReactions.Confused
});
UsingDbContext(context =>
{
var reaction = context.UserReactions.FirstOrDefault(x =>
x.CreatorId == _cmsKitTestData.User1Id &&
x.ReactionName == StandardReactions.Confused &&
x.EntityId == _cmsKitTestData.EntityId1 &&
x.EntityType == _cmsKitTestData.EntityType1);
reaction.ShouldBeNull();
});
}
}
}

19
modules/cms-kit/test/Volo.CmsKit.Domain.Tests/CmsKitDomainTestBase.cs

@ -1,10 +1,27 @@
namespace Volo.CmsKit
using System;
using Volo.CmsKit.EntityFrameworkCore;
namespace Volo.CmsKit
{
/* Inherit from this class for your domain layer tests.
* See SampleManager_Tests for example.
*/
public abstract class CmsKitDomainTestBase : CmsKitTestBase<CmsKitDomainTestModule>
{
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);
}
}
}
}

82
modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Reactions/ReactionManager_Tests.cs

@ -0,0 +1,82 @@
using System.Linq;
using System.Threading.Tasks;
using Shouldly;
using Xunit;
namespace Volo.CmsKit.Reactions
{
public class ReactionManager_Tests : CmsKitDomainTestBase
{
private readonly CmsKitTestData _cmsKitTestData;
private readonly ReactionManager _reactionManager;
public ReactionManager_Tests()
{
_cmsKitTestData = GetRequiredService<CmsKitTestData>();
_reactionManager = GetRequiredService<ReactionManager>();
}
[Fact]
public async Task GetReactionsAsync()
{
var reactions = await _reactionManager.GetReactionsAsync();
reactions.Count.ShouldBe(8);
var reactionsByEntityType = await _reactionManager.GetReactionsAsync(_cmsKitTestData.EntityType1);
reactionsByEntityType.Count.ShouldBe(8);
}
[Fact]
public async Task GetSummariesAsync()
{
var summary = await _reactionManager.GetSummariesAsync(
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1
);
summary.Single(s=>s.Reaction.Name == StandardReactions.ThumbsUp).Count.ShouldBe(2);
summary.Single(s=>s.Reaction.Name == StandardReactions.Confused).Count.ShouldBe(1);
summary.Single(s=>s.Reaction.Name == StandardReactions.Rocket).Count.ShouldBe(0);
}
[Fact]
public async Task CreateAsync()
{
var reaction = await _reactionManager.CreateAsync(
_cmsKitTestData.User2Id,
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId2,
StandardReactions.Eyes
);
reaction.CreatorId.ShouldBe(_cmsKitTestData.User2Id);
reaction.EntityType.ShouldBe(_cmsKitTestData.EntityType1);
reaction.EntityId.ShouldBe(_cmsKitTestData.EntityId2);
reaction.ReactionName.ShouldBe(StandardReactions.Eyes);
}
[Fact]
public async Task DeleteAsync()
{
await _reactionManager.DeleteAsync(
_cmsKitTestData.User1Id,
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
StandardReactions.Confused
);
UsingDbContext(context =>
{
var reaction = context.UserReactions.FirstOrDefault(x =>
x.CreatorId == _cmsKitTestData.User1Id &&
x.ReactionName == StandardReactions.Confused &&
x.EntityId == _cmsKitTestData.EntityId1 &&
x.EntityType == _cmsKitTestData.EntityType1);
reaction.ShouldBeNull();
});
}
}
}

8
modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Reactions/UserReactionRepository_Tests.cs

@ -0,0 +1,8 @@
using Volo.CmsKit.Reactions;
namespace Volo.CmsKit.EntityFrameworkCore.Reactions
{
public class UserReactionRepository_Tests : UserReactionRepository_Tests<CmsKitEntityFrameworkCoreTestModule>
{
}
}

8
modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Reactions/UserReactionRepository_Tests.cs

@ -0,0 +1,8 @@
using Volo.CmsKit.Reactions;
namespace Volo.CmsKit.MongoDB.Reactions
{
public class UserReactionRepository_Tests : UserReactionRepository_Tests<CmsKitMongoDbTestModule>
{
}
}

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

@ -14,28 +14,42 @@ namespace Volo.CmsKit
private readonly IGuidGenerator _guidGenerator;
private readonly ICmsUserRepository _cmsUserRepository;
private readonly CmsKitTestData _cmsKitTestData;
private readonly IUserReactionRepository _userReactionRepository;
private readonly ICommentRepository _commentRepository;
private readonly ReactionManager _reactionManager;
public CmsKitDataSeedContributor(
IGuidGenerator guidGenerator,
ICmsUserRepository cmsUserRepository,
CmsKitTestData cmsKitTestData,
IUserReactionRepository userReactionRepository,
ICommentRepository commentRepository)
ICommentRepository commentRepository,
ReactionManager reactionManager)
{
_guidGenerator = guidGenerator;
_cmsUserRepository = cmsUserRepository;
_cmsKitTestData = cmsKitTestData;
_userReactionRepository = userReactionRepository;
_commentRepository = commentRepository;
_reactionManager = reactionManager;
}
public async Task SeedAsync(DataSeedContext context)
{
await _cmsUserRepository.InsertAsync(new CmsUser(new UserData(_cmsKitTestData.User1Id, "user1","user1@volo.com", "user","1")));
await _cmsUserRepository.InsertAsync(new CmsUser(new UserData(_cmsKitTestData.User2Id, "user2","user2@volo.com", "user","2")));
await SeedUsersAsync();
await SeedCommentsAsync();
await SeedReactionsAsync();
}
private async Task SeedUsersAsync()
{
await _cmsUserRepository.InsertAsync(new CmsUser(new UserData(_cmsKitTestData.User1Id, "user1", "user1@volo.com",
"user", "1")));
await _cmsUserRepository.InsertAsync(new CmsUser(new UserData(_cmsKitTestData.User2Id, "user2", "user2@volo.com",
"user", "2")));
}
private async Task SeedCommentsAsync()
{
var comment1 = await _commentRepository.InsertAsync(new Comment(_cmsKitTestData.CommentWithChildId,
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
@ -84,5 +98,38 @@ namespace Volo.CmsKit
_cmsKitTestData.User2Id
));
}
private async Task SeedReactionsAsync()
{
await _reactionManager.CreateAsync(
_cmsKitTestData.User1Id,
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
StandardReactions.Confused);
await _reactionManager.CreateAsync(
_cmsKitTestData.User1Id,
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
StandardReactions.ThumbsUp);
await _reactionManager.CreateAsync(
_cmsKitTestData.User1Id,
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId2,
StandardReactions.Heart);
await _reactionManager.CreateAsync(
_cmsKitTestData.User1Id,
_cmsKitTestData.EntityType2,
_cmsKitTestData.EntityId1,
StandardReactions.Rocket);
await _reactionManager.CreateAsync(
_cmsKitTestData.User2Id,
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
StandardReactions.ThumbsUp);
}
}
}

70
modules/cms-kit/test/Volo.CmsKit.TestBase/Reactions/UserReactionRepository_Tests.cs

@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Modularity;
using Volo.CmsKit.Comments;
using Xunit;
namespace Volo.CmsKit.Reactions
{
public abstract class UserReactionRepository_Tests<TStartupModule> : CmsKitTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
private readonly CmsKitTestData _cmsKitTestData;
private readonly IUserReactionRepository __userReactionRepository;
public UserReactionRepository_Tests()
{
_cmsKitTestData = GetRequiredService<CmsKitTestData>();
__userReactionRepository = GetRequiredService<IUserReactionRepository>();
}
[Fact]
public async Task FindAsync()
{
var reaction = await __userReactionRepository.FindAsync(
_cmsKitTestData.User1Id,
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
StandardReactions.Confused
);
reaction.ShouldNotBeNull();
reaction.CreatorId.ShouldBe(_cmsKitTestData.User1Id);
reaction.EntityId.ShouldBe(_cmsKitTestData.EntityId1);
reaction.EntityType.ShouldBe(_cmsKitTestData.EntityType1);
reaction.ReactionName.ShouldBe(StandardReactions.Confused);
}
[Fact]
public async Task GetListForUserAsync()
{
var reactions = await __userReactionRepository.GetListForUserAsync(
_cmsKitTestData.User1Id,
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1
);
reactions.Count.ShouldBe(2);
reactions.Count(x=>x.ReactionName == StandardReactions.Confused).ShouldBe(1);
reactions.Count(x=>x.ReactionName == StandardReactions.ThumbsUp).ShouldBe(1);
}
[Fact]
public async Task GetSummariesAsync()
{
var summaries = await __userReactionRepository.GetSummariesAsync(
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1
);
summaries.Count.ShouldBe(2);
summaries.First(x => x.ReactionName == StandardReactions.Confused).Count.ShouldBe(1);
summaries.First(x => x.ReactionName == StandardReactions.ThumbsUp).Count.ShouldBe(2);
}
}
}
Loading…
Cancel
Save