diff --git a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Ratings/RatingPublicAppService_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Ratings/RatingPublicAppService_Tests.cs new file mode 100644 index 0000000000..1a21da8425 --- /dev/null +++ b/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(); + _cmsKitTestData = GetRequiredService(); + } + + protected override void AfterAddApplication(IServiceCollection services) + { + _currentUser = Substitute.For(); + 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().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().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().FirstOrDefault(x => x.Id == rating.Id); + + deletedComment.ShouldBeNull(); + }); + } + } +} \ No newline at end of file diff --git a/modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Ratings/RatingRepository_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Ratings/RatingRepository_Tests.cs new file mode 100644 index 0000000000..390ddd1cb3 --- /dev/null +++ b/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 + { + + } +} \ No newline at end of file diff --git a/modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Ratings/RatingRepository_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Ratings/RatingRepository_Tests.cs new file mode 100644 index 0000000000..c0814253e6 --- /dev/null +++ b/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 + { + + } +} \ No newline at end of file diff --git a/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs b/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs index 12c6255623..f91e79fb84 100644 --- a/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs +++ b/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 + )); + } } -} +} \ No newline at end of file diff --git a/modules/cms-kit/test/Volo.CmsKit.TestBase/Ratings/RatingRepository_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.TestBase/Ratings/RatingRepository_Tests.cs new file mode 100644 index 0000000000..b09b380f8e --- /dev/null +++ b/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 : CmsKitTestBase + where TStartupModule : IAbpModule + { + private readonly CmsKitTestData _cmsKitTestData; + private readonly IRatingRepository _ratingRepository; + + public RatingRepository_Tests() + { + _cmsKitTestData = GetRequiredService(); + _ratingRepository = GetRequiredService(); + } + + [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(); + } + } +} \ No newline at end of file