mirror of https://github.com/EasyAbp/EShop.git
7 changed files with 225 additions and 0 deletions
@ -0,0 +1,48 @@ |
|||
using System.Threading.Tasks; |
|||
using EasyAbp.EShop.Products.ProductInventories; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace EasyAbp.EShop.Products.DaprActorsInventory.Domain; |
|||
|
|||
public class DaprActorsProductInventoryProviderTests : ProductsDaprActorsInventoryTestBase |
|||
{ |
|||
[Fact] |
|||
public async Task Should_Get_Inventory() |
|||
{ |
|||
var inventoryProvider = ServiceProvider.GetRequiredService<DaprActorsProductInventoryProvider>(); |
|||
|
|||
var inventoryDataModel = await inventoryProvider.GetInventoryDataAsync(new InventoryQueryModel()); |
|||
|
|||
inventoryDataModel.ShouldNotBeNull(); |
|||
inventoryDataModel.Inventory.ShouldBe(100); |
|||
inventoryDataModel.Sold.ShouldBe(0); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Change_Inventory() |
|||
{ |
|||
var inventoryProvider = ServiceProvider.GetRequiredService<DaprActorsProductInventoryProvider>(); |
|||
|
|||
var result = await inventoryProvider.TryReduceInventoryAsync(new InventoryQueryModel(), 2, true); |
|||
|
|||
result.ShouldBeTrue(); |
|||
|
|||
var inventoryDataModel = await inventoryProvider.GetInventoryDataAsync(new InventoryQueryModel()); |
|||
|
|||
inventoryDataModel.ShouldNotBeNull(); |
|||
inventoryDataModel.Inventory.ShouldBe(98); |
|||
inventoryDataModel.Sold.ShouldBe(2); |
|||
|
|||
result = await inventoryProvider.TryIncreaseInventoryAsync(new InventoryQueryModel(), 1, true); |
|||
|
|||
result.ShouldBeTrue(); |
|||
|
|||
inventoryDataModel = await inventoryProvider.GetInventoryDataAsync(new InventoryQueryModel()); |
|||
|
|||
inventoryDataModel.ShouldNotBeNull(); |
|||
inventoryDataModel.Inventory.ShouldBe(99); |
|||
inventoryDataModel.Sold.ShouldBe(1); |
|||
} |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
using Volo.Abp; |
|||
using Volo.Abp.Authorization; |
|||
using Volo.Abp.Autofac; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace EasyAbp.EShop.Products.DaprActorsInventory.Domain; |
|||
|
|||
[DependsOn( |
|||
typeof(AbpAutofacModule), |
|||
typeof(AbpTestBaseModule), |
|||
typeof(AbpAuthorizationModule), |
|||
typeof(EShopProductsDaprActorsInventoryDomainModule) |
|||
)] |
|||
public class EShopProductsDaprActorsInventoryDomainTestModule : AbpModule |
|||
{ |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>net6.0</TargetFramework> |
|||
<RootNamespace>EasyAbp.EShop.Products.DaprActorsInventory.Domain</RootNamespace> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> |
|||
<PackageReference Include="NSubstitute" Version="4.2.2" /> |
|||
<PackageReference Include="Shouldly" Version="4.0.1" /> |
|||
<PackageReference Include="xunit" Version="2.4.1" /> |
|||
<PackageReference Include="xunit.extensibility.execution" Version="2.4.1" /> |
|||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" /> |
|||
<PackageReference Include="Volo.Abp.Autofac" Version="$(AbpVersion)" /> |
|||
<PackageReference Include="Volo.Abp.Authorization" Version="$(AbpVersion)" /> |
|||
<PackageReference Include="Volo.Abp.TestBase" Version="$(AbpVersion)" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\EasyAbp.EShop.Products.DaprActorsInventory.Domain\EasyAbp.EShop.Products.DaprActorsInventory.Domain.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,43 @@ |
|||
using System.Threading.Tasks; |
|||
using EasyAbp.EShop.Plugins.Inventories.DaprActors; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace EasyAbp.EShop.Products.DaprActorsInventory.Domain; |
|||
|
|||
public class FakeInventoryActor : IInventoryActor, ITransientDependency |
|||
{ |
|||
private InventoryStateModel StateModel { get; } = new() |
|||
{ |
|||
Inventory = 100, |
|||
Sold = 0 |
|||
}; |
|||
|
|||
public Task<InventoryStateModel> GetInventoryStateAsync() |
|||
{ |
|||
return Task.FromResult(StateModel); |
|||
} |
|||
|
|||
public Task IncreaseInventoryAsync(int quantity, bool decreaseSold) |
|||
{ |
|||
StateModel.Inventory += quantity; |
|||
|
|||
if (decreaseSold) |
|||
{ |
|||
StateModel.Sold -= quantity; |
|||
} |
|||
|
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
public Task ReduceInventoryAsync(int quantity, bool increaseSold) |
|||
{ |
|||
StateModel.Inventory -= quantity; |
|||
|
|||
if (increaseSold) |
|||
{ |
|||
StateModel.Sold += quantity; |
|||
} |
|||
|
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
@ -0,0 +1,61 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.Testing; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace EasyAbp.EShop.Products.DaprActorsInventory.Domain |
|||
{ |
|||
/* All test classes are derived from this class, directly or indirectly. */ |
|||
public abstract class |
|||
ProductsDaprActorsInventoryTestBase : AbpIntegratedTest<EShopProductsDaprActorsInventoryDomainTestModule> |
|||
{ |
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
{ |
|||
options.UseAutofac(); |
|||
} |
|||
|
|||
protected virtual Task WithUnitOfWorkAsync(Func<Task> func) |
|||
{ |
|||
return WithUnitOfWorkAsync(new AbpUnitOfWorkOptions(), func); |
|||
} |
|||
|
|||
protected virtual async Task WithUnitOfWorkAsync(AbpUnitOfWorkOptions options, Func<Task> action) |
|||
{ |
|||
using (var scope = ServiceProvider.CreateScope()) |
|||
{ |
|||
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>(); |
|||
|
|||
using (var uow = uowManager.Begin(options)) |
|||
{ |
|||
await action(); |
|||
|
|||
await uow.CompleteAsync(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
protected virtual Task<TResult> WithUnitOfWorkAsync<TResult>(Func<Task<TResult>> func) |
|||
{ |
|||
return WithUnitOfWorkAsync(new AbpUnitOfWorkOptions(), func); |
|||
} |
|||
|
|||
protected virtual async Task<TResult> WithUnitOfWorkAsync<TResult>(AbpUnitOfWorkOptions options, |
|||
Func<Task<TResult>> func) |
|||
{ |
|||
using (var scope = ServiceProvider.CreateScope()) |
|||
{ |
|||
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>(); |
|||
|
|||
using (var uow = uowManager.Begin(options)) |
|||
{ |
|||
var result = await func(); |
|||
await uow.CompleteAsync(); |
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Dapr.Actors; |
|||
using EasyAbp.EShop.Plugins.Inventories.DaprActors; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace EasyAbp.EShop.Products.DaprActorsInventory.Domain; |
|||
|
|||
[Dependency(ReplaceServices = true)] |
|||
public class TestInventoryActorProvider : IInventoryActorProvider, ITransientDependency |
|||
{ |
|||
private IInventoryActor Actor { get; set; } |
|||
|
|||
private readonly IServiceProvider _serviceProvider; |
|||
|
|||
public TestInventoryActorProvider(IServiceProvider serviceProvider) |
|||
{ |
|||
_serviceProvider = serviceProvider; |
|||
} |
|||
|
|||
public Task<IInventoryActor> GetAsync(ActorId actorId, string actorType) |
|||
{ |
|||
return Task.FromResult(Actor ??= _serviceProvider.GetRequiredService<FakeInventoryActor>()); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue