mirror of https://github.com/abpframework/abp.git
5 changed files with 215 additions and 4 deletions
@ -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(); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.CmsKit.Ratings; |
|||
|
|||
namespace Volo.CmsKit.EntityFrameworkCore.Ratings |
|||
{ |
|||
public class RatingRepository_Tests : RatingRepository_Tests<CmsKitEntityFrameworkCoreTestModule> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -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> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -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…
Reference in new issue