mirror of https://github.com/abpframework/abp.git
10 changed files with 349 additions and 14 deletions
@ -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(); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -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); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -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(); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
using Volo.CmsKit.Reactions; |
|||
|
|||
namespace Volo.CmsKit.EntityFrameworkCore.Reactions |
|||
{ |
|||
public class UserReactionRepository_Tests : UserReactionRepository_Tests<CmsKitEntityFrameworkCoreTestModule> |
|||
{ |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
using Volo.CmsKit.Reactions; |
|||
|
|||
namespace Volo.CmsKit.MongoDB.Reactions |
|||
{ |
|||
public class UserReactionRepository_Tests : UserReactionRepository_Tests<CmsKitMongoDbTestModule> |
|||
{ |
|||
} |
|||
} |
|||
@ -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…
Reference in new issue