diff --git a/EShop.sln b/EShop.sln index 3542d607..569a8985 100644 --- a/EShop.sln +++ b/EShop.sln @@ -355,6 +355,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F468A386-566 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{485204B1-7603-4EA0-B3A4-73CB89B0D5BC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests", "plugins\Inventories\DaprActors\test\EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests\EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests.csproj", "{733C51A3-19C8-45C4-8B22-3FD40CAF4EFB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -921,6 +923,10 @@ Global {B6F3ACD5-463E-4455-A094-057A82941A94}.Debug|Any CPU.Build.0 = Debug|Any CPU {B6F3ACD5-463E-4455-A094-057A82941A94}.Release|Any CPU.ActiveCfg = Release|Any CPU {B6F3ACD5-463E-4455-A094-057A82941A94}.Release|Any CPU.Build.0 = Release|Any CPU + {733C51A3-19C8-45C4-8B22-3FD40CAF4EFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {733C51A3-19C8-45C4-8B22-3FD40CAF4EFB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {733C51A3-19C8-45C4-8B22-3FD40CAF4EFB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {733C51A3-19C8-45C4-8B22-3FD40CAF4EFB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1098,6 +1104,7 @@ Global {CF4DE32D-9629-4C48-9BE8-5B83A1C27291} = {F468A386-5660-4888-981A-6ECF15182D32} {6CD1A8B5-8AB7-4A31-8333-024A7FB602D1} = {F468A386-5660-4888-981A-6ECF15182D32} {485204B1-7603-4EA0-B3A4-73CB89B0D5BC} = {6E6FE4B9-4117-4F57-B219-EE47E4046096} + {733C51A3-19C8-45C4-8B22-3FD40CAF4EFB} = {485204B1-7603-4EA0-B3A4-73CB89B0D5BC} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {28315BFD-90E7-4E14-A2EA-F3D23AF4126F} diff --git a/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/DaprActorsProductInventoryProviderTests.cs b/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/DaprActorsProductInventoryProviderTests.cs new file mode 100644 index 00000000..ce701107 --- /dev/null +++ b/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/DaprActorsProductInventoryProviderTests.cs @@ -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(); + + 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(); + + 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); + } +} \ No newline at end of file diff --git a/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/EShopProductsDaprActorsInventoryDomainTestModule.cs b/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/EShopProductsDaprActorsInventoryDomainTestModule.cs new file mode 100644 index 00000000..8b3c22fb --- /dev/null +++ b/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/EShopProductsDaprActorsInventoryDomainTestModule.cs @@ -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 +{ +} \ No newline at end of file diff --git a/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests.csproj b/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests.csproj new file mode 100644 index 00000000..5fc3de69 --- /dev/null +++ b/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + EasyAbp.EShop.Products.DaprActorsInventory.Domain + + + + + + + + + + + + + + + + + + + diff --git a/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/FakeInventoryActor.cs b/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/FakeInventoryActor.cs new file mode 100644 index 00000000..58ae0129 --- /dev/null +++ b/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/FakeInventoryActor.cs @@ -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 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; + } +} \ No newline at end of file diff --git a/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/ProductsDaprActorsInventoryTestBase.cs b/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/ProductsDaprActorsInventoryTestBase.cs new file mode 100644 index 00000000..55c5d3fc --- /dev/null +++ b/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/ProductsDaprActorsInventoryTestBase.cs @@ -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 + { + protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) + { + options.UseAutofac(); + } + + protected virtual Task WithUnitOfWorkAsync(Func func) + { + return WithUnitOfWorkAsync(new AbpUnitOfWorkOptions(), func); + } + + protected virtual async Task WithUnitOfWorkAsync(AbpUnitOfWorkOptions options, Func action) + { + using (var scope = ServiceProvider.CreateScope()) + { + var uowManager = scope.ServiceProvider.GetRequiredService(); + + using (var uow = uowManager.Begin(options)) + { + await action(); + + await uow.CompleteAsync(); + } + } + } + + protected virtual Task WithUnitOfWorkAsync(Func> func) + { + return WithUnitOfWorkAsync(new AbpUnitOfWorkOptions(), func); + } + + protected virtual async Task WithUnitOfWorkAsync(AbpUnitOfWorkOptions options, + Func> func) + { + using (var scope = ServiceProvider.CreateScope()) + { + var uowManager = scope.ServiceProvider.GetRequiredService(); + + using (var uow = uowManager.Begin(options)) + { + var result = await func(); + await uow.CompleteAsync(); + return result; + } + } + } + } +} \ No newline at end of file diff --git a/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/TestInventoryActorProvider.cs b/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/TestInventoryActorProvider.cs new file mode 100644 index 00000000..db26bdad --- /dev/null +++ b/plugins/Inventories/DaprActors/test/EasyAbp.EShop.Products.DaprActorsInventory.Domain.Tests/TestInventoryActorProvider.cs @@ -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 GetAsync(ActorId actorId, string actorType) + { + return Task.FromResult(Actor ??= _serviceProvider.GetRequiredService()); + } +} \ No newline at end of file