From d700687780bc5d3bacc1e5d631487f9459d54715 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 21 Jan 2019 09:43:54 +0300 Subject: [PATCH] microservice demo: application tests currently failing --- .../ProductAppService_Tests.cs | 93 +++++++++++++++++++ .../ProductManagementApplicationTestBase.cs | 26 ++++++ .../ProductManagementApplicationTestModule.cs | 8 +- .../ProductManagementTestData.cs | 22 ++++- .../ProductManagementTestDataBuilder.cs | 20 ++-- 5 files changed, 160 insertions(+), 9 deletions(-) create mode 100644 samples/MicroserviceDemo/modules/product/test/ProductManagement.Application.Tests/ProductManagement/ProductAppService_Tests.cs create mode 100644 samples/MicroserviceDemo/modules/product/test/ProductManagement.Application.Tests/ProductManagement/ProductManagementApplicationTestBase.cs diff --git a/samples/MicroserviceDemo/modules/product/test/ProductManagement.Application.Tests/ProductManagement/ProductAppService_Tests.cs b/samples/MicroserviceDemo/modules/product/test/ProductManagement.Application.Tests/ProductManagement/ProductAppService_Tests.cs new file mode 100644 index 0000000000..a3320d200f --- /dev/null +++ b/samples/MicroserviceDemo/modules/product/test/ProductManagement.Application.Tests/ProductManagement/ProductAppService_Tests.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.Application.Dtos; +using Xunit; + +namespace ProductManagement +{ + public class ProductAppService_Tests : ProductManagementApplicationTestBase + { + private readonly IProductAppService _productAppService; + private readonly IProductRepository _productRepository; + private readonly ProductManagementTestData _testData; + + public ProductAppService_Tests() + { + _productAppService = GetRequiredService(); + _productRepository = GetRequiredService(); + _testData = GetRequiredService(); + } + + [Fact] + public async Task GetListPagedAsync() + { + var result = await _productAppService.GetListPagedAsync(new PagedAndSortedResultRequestDto()); + + result.Items.Count.ShouldBeGreaterThan(0); + } + + [Fact] + public async Task GetListAsync() + { + var result = await _productAppService.GetListAsync(); + + result.Items.Count.ShouldBeGreaterThan(0); + } + + [Fact] + public async Task GetAsync() + { + var result = await _productAppService.GetAsync(_testData.ProductId1); + + result.ShouldNotBeNull(); + result.Name.ShouldBe(_testData.ProductName1); + result.Code.ShouldBe(_testData.ProductCode1); + result.Price.ShouldBe(_testData.ProductPrice1); + result.StockCount.ShouldBe(_testData.ProductStockCount1); + } + + [Fact] + public async Task CreateAsync() + { + var result = await _productAppService.CreateAsync(new CreateProductDto() + { + Code = "Code", + Name = "Name", + Price = 15, + StockCount = 14 + }); + + result.ShouldNotBeNull(); + } + + [Fact] + public async Task UpdateAsync() + { + var result = await _productAppService.UpdateAsync(_testData.ProductId1, new UpdateProductDto() + { + Code = nameof(Product.Code), + Name = nameof(Product.Name), + Price = 15, + StockCount = 14 + }); + + result.ShouldNotBeNull(); + result.Code.ShouldBe(nameof(Product.Code)); + result.Name.ShouldBe(nameof(Product.Name)); + } + + [Fact] + public async Task DeleteAsync() + { + await _productAppService.DeleteAsync(_testData.ProductId2); + + var result = await _productRepository.GetAsync(_testData.ProductId2); + + result.ShouldBeNull(); + } + + } +} diff --git a/samples/MicroserviceDemo/modules/product/test/ProductManagement.Application.Tests/ProductManagement/ProductManagementApplicationTestBase.cs b/samples/MicroserviceDemo/modules/product/test/ProductManagement.Application.Tests/ProductManagement/ProductManagementApplicationTestBase.cs new file mode 100644 index 0000000000..1e9582ee93 --- /dev/null +++ b/samples/MicroserviceDemo/modules/product/test/ProductManagement.Application.Tests/ProductManagement/ProductManagementApplicationTestBase.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; +using ProductManagement.EntityFrameworkCore; + +namespace ProductManagement +{ + public abstract class ProductManagementApplicationTestBase : ProductManagementTestBase + { + protected virtual void UsingDbContext(Action action) + { + using (var dbContext = GetRequiredService()) + { + action.Invoke(dbContext); + } + } + + protected virtual T UsingDbContext(Func action) + { + using (var dbContext = GetRequiredService()) + { + return action.Invoke(dbContext); + } + } + } +} diff --git a/samples/MicroserviceDemo/modules/product/test/ProductManagement.Application.Tests/ProductManagement/ProductManagementApplicationTestModule.cs b/samples/MicroserviceDemo/modules/product/test/ProductManagement.Application.Tests/ProductManagement/ProductManagementApplicationTestModule.cs index 44d801edf7..a1d713dc1f 100644 --- a/samples/MicroserviceDemo/modules/product/test/ProductManagement.Application.Tests/ProductManagement/ProductManagementApplicationTestModule.cs +++ b/samples/MicroserviceDemo/modules/product/test/ProductManagement.Application.Tests/ProductManagement/ProductManagementApplicationTestModule.cs @@ -1,4 +1,5 @@ -using Volo.Abp.Modularity; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; namespace ProductManagement { @@ -8,6 +9,9 @@ namespace ProductManagement )] public class ProductManagementApplicationTestModule : AbpModule { - + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.AddAlwaysAllowAuthorization(); + } } } diff --git a/samples/MicroserviceDemo/modules/product/test/ProductManagement.TestBase/ProductManagement/ProductManagementTestData.cs b/samples/MicroserviceDemo/modules/product/test/ProductManagement.TestBase/ProductManagement/ProductManagementTestData.cs index 31fe9f75bd..14ad09731f 100644 --- a/samples/MicroserviceDemo/modules/product/test/ProductManagement.TestBase/ProductManagement/ProductManagementTestData.cs +++ b/samples/MicroserviceDemo/modules/product/test/ProductManagement.TestBase/ProductManagement/ProductManagementTestData.cs @@ -1,8 +1,28 @@ -using Volo.Abp.DependencyInjection; +using System; +using Volo.Abp.DependencyInjection; namespace ProductManagement { public class ProductManagementTestData : ISingletonDependency { + public Guid ProductId1 { get; } = Guid.NewGuid(); + + public string ProductCode1 { get; } = "ProductCode1"; + + public string ProductName1 { get; } = "ProductName1"; + + public float ProductPrice1 { get; } = 20; + + public int ProductStockCount1 { get; } = 100; + + public Guid ProductId2 { get; } = Guid.NewGuid(); + + public string ProductCode2 { get; } = "ProductCode2"; + + public string ProductName2 { get; } = "ProductName2"; + + public float ProductPrice2 { get; } = 30; + + public int ProductStockCount2 { get; } = 110; } } diff --git a/samples/MicroserviceDemo/modules/product/test/ProductManagement.TestBase/ProductManagement/ProductManagementTestDataBuilder.cs b/samples/MicroserviceDemo/modules/product/test/ProductManagement.TestBase/ProductManagement/ProductManagementTestDataBuilder.cs index 151be4308b..68c858a271 100644 --- a/samples/MicroserviceDemo/modules/product/test/ProductManagement.TestBase/ProductManagement/ProductManagementTestDataBuilder.cs +++ b/samples/MicroserviceDemo/modules/product/test/ProductManagement.TestBase/ProductManagement/ProductManagementTestDataBuilder.cs @@ -1,24 +1,32 @@ -using Volo.Abp.DependencyInjection; +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; using Volo.Abp.Guids; +using Volo.Abp.Threading; namespace ProductManagement { public class ProductManagementTestDataBuilder : ITransientDependency { - private readonly IGuidGenerator _guidGenerator; private ProductManagementTestData _testData; + private readonly ProductManager _productManager; public ProductManagementTestDataBuilder( - IGuidGenerator guidGenerator, - ProductManagementTestData testData) + ProductManagementTestData testData, + ProductManager productManager) { - _guidGenerator = guidGenerator; _testData = testData; + _productManager = productManager; } public void Build() { - + AsyncHelper.RunSync(BuildAsync); + } + + public async Task BuildAsync() + { + await _productManager.CreateAsync(_testData.ProductCode1, _testData.ProductName1, _testData.ProductPrice1, _testData.ProductStockCount1); + await _productManager.CreateAsync(_testData.ProductCode2, _testData.ProductName2, _testData.ProductPrice2, _testData.ProductStockCount2); } } } \ No newline at end of file