Browse Source

CmsKit: Rating missing tests

pull/5216/head
EngincanV 6 years ago
parent
commit
a57c1b975f
  1. 103
      modules/cms-kit/test/Volo.CmsKit.Application.Tests/Ratings/RatingPublicAppService_Tests.cs
  2. 9
      modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Ratings/RatingRepository_Tests.cs
  3. 11
      modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Ratings/RatingRepository_Tests.cs
  4. 47
      modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs
  5. 49
      modules/cms-kit/test/Volo.CmsKit.TestBase/Ratings/RatingRepository_Tests.cs

103
modules/cms-kit/test/Volo.CmsKit.Application.Tests/Ratings/RatingPublicAppService_Tests.cs

@ -0,0 +1,103 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
using Volo.Abp.Users;
using Volo.CmsKit.Public.Ratings;
using Xunit;
namespace Volo.CmsKit.Ratings
{
public class RatingPublicAppService_Tests : CmsKitApplicationTestBase
{
private readonly IRatingPublicAppService _ratingAppService;
private ICurrentUser _currentUser;
private readonly CmsKitTestData _cmsKitTestData;
public RatingPublicAppService_Tests()
{
_ratingAppService = GetRequiredService<IRatingPublicAppService>();
_cmsKitTestData = GetRequiredService<CmsKitTestData>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
_currentUser = Substitute.For<ICurrentUser>();
services.AddSingleton(_currentUser);
}
[Fact]
public async Task CreateAsync()
{
_currentUser.Id.Returns(_cmsKitTestData.User1Id);
var newRating = await _ratingAppService.CreateAsync(
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
new CreateRatingInput
{
StarCount = 4
});
UsingDbContext(context =>
{
var ratings = context.Set<Rating>().Where(x =>
x.EntityId == _cmsKitTestData.EntityId1 && x.EntityType == _cmsKitTestData.EntityType1).ToList();
ratings
.Any(c => c.Id == newRating.Id && c.CreatorId == newRating.CreatorId && c.StarCount == newRating.StarCount)
.ShouldBeTrue();
});
}
[Fact]
public async Task UpdateAsync()
{
_currentUser.Id.Returns(_cmsKitTestData.User1Id);
var rating = await _ratingAppService.CreateAsync(
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
new CreateRatingInput
{
StarCount = 4
});
await _ratingAppService.UpdateAsync(rating.Id, new UpdateRatingInput
{
StarCount = 5
});
UsingDbContext(context =>
{
var updatedRating = context.Set<Rating>().Single(x => x.Id == rating.Id);
updatedRating.StarCount.ShouldBe((short)5);
});
}
[Fact]
public async Task DeleteAsync()
{
_currentUser.Id.Returns(_cmsKitTestData.User1Id);
var rating = await _ratingAppService.CreateAsync(
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
new CreateRatingInput
{
StarCount = 4
});
await _ratingAppService.DeleteAsync(rating.Id);
UsingDbContext(context =>
{
var deletedComment = context.Set<Rating>().FirstOrDefault(x => x.Id == rating.Id);
deletedComment.ShouldBeNull();
});
}
}
}

9
modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Ratings/RatingRepository_Tests.cs

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

11
modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Ratings/RatingRepository_Tests.cs

@ -0,0 +1,11 @@
using Volo.CmsKit.Ratings;
using Xunit;
namespace Volo.CmsKit.MongoDB.Ratings
{
[Collection(MongoTestCollection.Name)]
public class RatingRepository_Tests : RatingRepository_Tests<CmsKitMongoDbTestModule>
{
}
}

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

@ -4,6 +4,7 @@ using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.Users;
using Volo.CmsKit.Comments;
using Volo.CmsKit.Ratings;
using Volo.CmsKit.Reactions;
using Volo.CmsKit.Users;
@ -16,19 +17,22 @@ namespace Volo.CmsKit
private readonly CmsKitTestData _cmsKitTestData;
private readonly ICommentRepository _commentRepository;
private readonly ReactionManager _reactionManager;
private readonly IRatingRepository _ratingRepository;
public CmsKitDataSeedContributor(
IGuidGenerator guidGenerator,
ICmsUserRepository cmsUserRepository,
CmsKitTestData cmsKitTestData,
ICommentRepository commentRepository,
ReactionManager reactionManager)
ReactionManager reactionManager,
IRatingRepository ratingRepository)
{
_guidGenerator = guidGenerator;
_cmsUserRepository = cmsUserRepository;
_cmsKitTestData = cmsKitTestData;
_commentRepository = commentRepository;
_reactionManager = reactionManager;
_ratingRepository = ratingRepository;
}
public async Task SeedAsync(DataSeedContext context)
@ -38,13 +42,17 @@ namespace Volo.CmsKit
await SeedCommentsAsync();
await SeedReactionsAsync();
await SeedRatingsAsync();
}
private async Task SeedUsersAsync()
{
await _cmsUserRepository.InsertAsync(new CmsUser(new UserData(_cmsKitTestData.User1Id, "user1", "user1@volo.com",
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",
await _cmsUserRepository.InsertAsync(new CmsUser(new UserData(_cmsKitTestData.User2Id, "user2",
"user2@volo.com",
"user", "2")));
}
@ -131,5 +139,36 @@ namespace Volo.CmsKit
_cmsKitTestData.EntityId1,
StandardReactions.ThumbsUp);
}
private async Task SeedRatingsAsync()
{
await _ratingRepository.InsertAsync(new Rating(_guidGenerator.Create(),
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
4,
_cmsKitTestData.User1Id
));
await _ratingRepository.InsertAsync(new Rating(_guidGenerator.Create(),
_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1,
5,
_cmsKitTestData.User1Id
));
await _ratingRepository.InsertAsync(new Rating(_guidGenerator.Create(),
_cmsKitTestData.EntityType2,
_cmsKitTestData.EntityId2,
5,
_cmsKitTestData.User2Id
));
await _ratingRepository.InsertAsync(new Rating(_guidGenerator.Create(),
_cmsKitTestData.EntityType2,
_cmsKitTestData.EntityId2,
1,
_cmsKitTestData.User2Id
));
}
}
}
}

49
modules/cms-kit/test/Volo.CmsKit.TestBase/Ratings/RatingRepository_Tests.cs

@ -0,0 +1,49 @@
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Modularity;
using Xunit;
namespace Volo.CmsKit.Ratings
{
public abstract class RatingRepository_Tests<TStartupModule> : CmsKitTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
private readonly CmsKitTestData _cmsKitTestData;
private readonly IRatingRepository _ratingRepository;
public RatingRepository_Tests()
{
_cmsKitTestData = GetRequiredService<CmsKitTestData>();
_ratingRepository = GetRequiredService<IRatingRepository>();
}
[Fact]
public async Task GetListAsync()
{
var list = await _ratingRepository.GetListAsync(_cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1);
list.Count.ShouldBeGreaterThan(0);
}
[Fact]
public async Task GetCurrentUserRatingAsync()
{
var userRating = await _ratingRepository.GetCurrentUserRatingAsync(_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1, _cmsKitTestData.User1Id);
userRating.ShouldNotBeNull();
userRating.EntityId.ShouldBe(_cmsKitTestData.EntityId1);
userRating.EntityType.ShouldBe(_cmsKitTestData.EntityType1);
userRating.CreatorId.ShouldBe(_cmsKitTestData.User1Id);
}
[Fact]
public async Task GetGroupedStarCountsAsync()
{
var list = await _ratingRepository.GetGroupedStarCountsAsync(_cmsKitTestData.EntityType1,
_cmsKitTestData.EntityId1);
list.ShouldNotBeNull();
}
}
}
Loading…
Cancel
Save