From f56b866f36a7252d89c4f1950477d5d6b108a65f Mon Sep 17 00:00:00 2001 From: GameBelial <243387971@qq.com> Date: Tue, 1 Sep 2020 22:44:39 +0800 Subject: [PATCH 1/6] Complete the test method for creating an order. --- .../Orders/OrderAppServiceTests.cs | 66 ++++++++++++++++++- .../OrdersApplicationTestBase.cs | 16 ++++- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs index 7f9a0540..32c44211 100644 --- a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs @@ -1,5 +1,13 @@ -using Shouldly; +using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; +using EasyAbp.EShop.Orders.Orders.Dtos; +using EasyAbp.EShop.Products.Products; +using EasyAbp.EShop.Products.Products.Dtos; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; +using Shouldly; using Xunit; namespace EasyAbp.EShop.Orders.Orders @@ -8,19 +16,71 @@ namespace EasyAbp.EShop.Orders.Orders { private readonly IOrderAppService _orderAppService; + private readonly Guid _storeId = Guid.Parse("982C6439-AAC4-CD86-21E0-D89DFE066305"); + private readonly Guid _productId = Guid.Parse("309A5529-A42A-9E0F-85FA-879B17B70EA1"); + private readonly Guid _productSkuId = Guid.Parse("309A5529-A42A-9E0F-85FA-879B17B70EA2"); + public OrderAppServiceTests() { _orderAppService = GetRequiredService(); } + protected override void AfterAddApplication(IServiceCollection services) + { + var productAppService = Substitute.For(); + productAppService.GetAsync(_productId, _storeId).Returns(Task.FromResult(new ProductDto + { + CreationTime = DateTime.Now, + IsPublished = true, + Id = _productId, + ProductSkus = new List + { + new ProductSkuDto + { + Id = _productSkuId, + OrderMinQuantity = 0, + OrderMaxQuantity = 100, + AttributeOptionIds = new List() + } + }, + InventoryStrategy = InventoryStrategy.NoNeed + })); + + services.AddTransient(_ => productAppService); + } + [Fact] - public async Task Test1() + public async Task Should_Create_A_Order() { // Arrange + var createOrderDto = new CreateOrderDto + { + CustomerRemark = "customer remark", + StoreId = _storeId, + OrderLines = new List + { + new CreateOrderLineDto + { + ProductId = _productId, + ProductSkuId = _productSkuId, + Quantity = 10 + } + } + }; // Act + var response = await _orderAppService.CreateAsync(createOrderDto); // Assert + response.ShouldNotBeNull(); + response.CustomerRemark.ShouldBe("customer remark"); + + UsingDbContext(context => + { + context.Orders.Count().ShouldBe(1); + var order = context.Orders.First(); + order.CustomerRemark.ShouldBe("customer remark"); + }); } } -} +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/OrdersApplicationTestBase.cs b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/OrdersApplicationTestBase.cs index e6fad2fe..b0c32162 100644 --- a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/OrdersApplicationTestBase.cs +++ b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/OrdersApplicationTestBase.cs @@ -1,10 +1,22 @@ -namespace EasyAbp.EShop.Orders +using System; +using EasyAbp.EShop.Orders.EntityFrameworkCore; +using EasyAbp.EShop.Products.Products; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; + +namespace EasyAbp.EShop.Orders { /* Inherit from this class for your application layer tests. * See SampleAppService_Tests for example. */ public abstract class OrdersApplicationTestBase : OrdersTestBase { - + protected void UsingDbContext(Action action) + { + using (var dbContext = GetRequiredService()) + { + action.Invoke(dbContext); + } + } } } \ No newline at end of file From 51f4244a3bd3db77db73f8f893ae403362731ef0 Mon Sep 17 00:00:00 2001 From: GameBelial <243387971@qq.com> Date: Tue, 8 Sep 2020 15:09:39 +0800 Subject: [PATCH 2/6] Complete the basic unit test of the order module. --- .../Orders/OrderAppServiceTests.cs | 74 ++++++++++++++++--- .../OrderTestData.cs | 14 ++++ 2 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.TestBase/OrderTestData.cs diff --git a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs index 32c44211..eba07a59 100644 --- a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs @@ -16,10 +16,6 @@ namespace EasyAbp.EShop.Orders.Orders { private readonly IOrderAppService _orderAppService; - private readonly Guid _storeId = Guid.Parse("982C6439-AAC4-CD86-21E0-D89DFE066305"); - private readonly Guid _productId = Guid.Parse("309A5529-A42A-9E0F-85FA-879B17B70EA1"); - private readonly Guid _productSkuId = Guid.Parse("309A5529-A42A-9E0F-85FA-879B17B70EA2"); - public OrderAppServiceTests() { _orderAppService = GetRequiredService(); @@ -28,16 +24,16 @@ namespace EasyAbp.EShop.Orders.Orders protected override void AfterAddApplication(IServiceCollection services) { var productAppService = Substitute.For(); - productAppService.GetAsync(_productId, _storeId).Returns(Task.FromResult(new ProductDto + productAppService.GetAsync(OrderTestData.Product1Id, OrderTestData.Store1Id).Returns(Task.FromResult(new ProductDto { CreationTime = DateTime.Now, IsPublished = true, - Id = _productId, + Id = OrderTestData.Product1Id, ProductSkus = new List { new ProductSkuDto { - Id = _productSkuId, + Id = OrderTestData.ProductSku1Id, OrderMinQuantity = 0, OrderMaxQuantity = 100, AttributeOptionIds = new List() @@ -56,13 +52,13 @@ namespace EasyAbp.EShop.Orders.Orders var createOrderDto = new CreateOrderDto { CustomerRemark = "customer remark", - StoreId = _storeId, + StoreId = OrderTestData.Store1Id, OrderLines = new List { new CreateOrderLineDto { - ProductId = _productId, - ProductSkuId = _productSkuId, + ProductId = OrderTestData.Product1Id, + ProductSkuId = OrderTestData.ProductSku1Id, Quantity = 10 } } @@ -82,5 +78,63 @@ namespace EasyAbp.EShop.Orders.Orders order.CustomerRemark.ShouldBe("customer remark"); }); } + + [Fact] + public async Task Order_Should_Complete() + { + // Arrange + await Should_Create_A_Order(); + Guid orderId = Guid.Empty; + UsingDbContext(db => + { + var order = db.Orders.First(); + orderId = order.Id; + order.SetPaidTime(DateTime.Now); + order.SetReducedInventoryAfterPaymentTime(DateTime.Now); + db.SaveChanges(); + }); + + // Act + var response = await _orderAppService.CompleteAsync(orderId); + + // Assert + response.ShouldNotBeNull(); + response.Id.ShouldBe(orderId); + response.OrderStatus.ShouldBe(OrderStatus.Completed); + } + + [Fact] + public async Task Order_Should_Cancel() + { + // Arrange + await Should_Create_A_Order(); + Guid orderId = Guid.Empty; + UsingDbContext(db => + { + var order = db.Orders.First(); + orderId = order.Id; + }); + + var cancelRequestDto = new CancelOrderInput + { + CancellationReason = "Repeat orders." + }; + + // Act + var response = await _orderAppService.CancelAsync(orderId, cancelRequestDto); + + // Assert + response.ShouldNotBeNull(); + response.OrderStatus.ShouldBe(OrderStatus.Canceled); + response.CancellationReason.ShouldBe("Repeat orders."); + + UsingDbContext(db => + { + var order = db.Orders.FirstOrDefault(o => o.Id == orderId); + order.ShouldNotBeNull(); + order.OrderStatus.ShouldBe(OrderStatus.Canceled); + order.CancellationReason.ShouldBe("Repeat orders."); + }); + } } } \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.TestBase/OrderTestData.cs b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.TestBase/OrderTestData.cs new file mode 100644 index 00000000..2097343b --- /dev/null +++ b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.TestBase/OrderTestData.cs @@ -0,0 +1,14 @@ +using System; +using Volo.Abp.DependencyInjection; + +namespace EasyAbp.EShop.Orders +{ + public class OrderTestData + { + public static Guid Store1Id { get; } = Guid.NewGuid(); + + public static Guid Product1Id { get; } = Guid.NewGuid(); + + public static Guid ProductSku1Id { get; } = Guid.NewGuid(); + } +} \ No newline at end of file From 9d6ad6a3d3b05f361222c6ce68d81b873dce38c0 Mon Sep 17 00:00:00 2001 From: GameBelial <243387971@qq.com> Date: Tue, 8 Sep 2020 16:32:16 +0800 Subject: [PATCH 3/6] Complete the basic unit test of the stores module. --- .../Stores/StoreAppServiceTests.cs | 29 ++++++++++++++++++- .../StoresApplicationTestBase.cs | 13 +++++++-- .../StoresTestData.cs | 7 +++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.TestBase/StoresTestData.cs diff --git a/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/Stores/StoreAppServiceTests.cs b/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/Stores/StoreAppServiceTests.cs index 4a2b3947..3b9f8799 100644 --- a/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/Stores/StoreAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/Stores/StoreAppServiceTests.cs @@ -1,5 +1,7 @@ +using System.Linq; using Shouldly; using System.Threading.Tasks; +using EasyAbp.EShop.Stores.Stores.Dtos; using Xunit; namespace EasyAbp.EShop.Stores.Stores @@ -14,13 +16,38 @@ namespace EasyAbp.EShop.Stores.Stores } [Fact] - public async Task Test1() + public async Task Should_Create_A_Store() { // Arrange + var request = new CreateUpdateStoreDto + { + Name = "New Store 2" + }; // Act + var response = await _storeAppService.CreateAsync(request); // Assert + response.ShouldNotBeNull(); + response.Name.ShouldBe("New Store 2"); + + UsingDbContext(db => + { + var store = db.Stores.FirstOrDefault(x=>x.Id == response.Id); + store.ShouldNotBeNull(); + store.Name.ShouldBe("New Store 2"); + }); + } + + [Fact] + public async Task Should_Return_Default_Store() + { + // Arrange & Act + var response = await _storeAppService.GetDefaultAsync(); + + // Assert + response.ShouldNotBeNull(); + response.Name.ShouldBe("My Store"); } } } diff --git a/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/StoresApplicationTestBase.cs b/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/StoresApplicationTestBase.cs index 29b3751d..14ead32c 100644 --- a/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/StoresApplicationTestBase.cs +++ b/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/StoresApplicationTestBase.cs @@ -1,10 +1,19 @@ -namespace EasyAbp.EShop.Stores +using System; +using EasyAbp.EShop.Stores.EntityFrameworkCore; + +namespace EasyAbp.EShop.Stores { /* Inherit from this class for your application layer tests. * See SampleAppService_Tests for example. */ public abstract class StoresApplicationTestBase : StoresTestBase { - + protected void UsingDbContext(Action action) + { + using (var dbContext = GetRequiredService()) + { + action.Invoke(dbContext); + } + } } } \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.TestBase/StoresTestData.cs b/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.TestBase/StoresTestData.cs new file mode 100644 index 00000000..60278156 --- /dev/null +++ b/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.TestBase/StoresTestData.cs @@ -0,0 +1,7 @@ +namespace EasyAbp.EShop.Stores +{ + public static class StoresTestData + { + + } +} \ No newline at end of file From 587607e813ae84d3298a9f26124b8afeae1ea8c9 Mon Sep 17 00:00:00 2001 From: GameBelial <243387971@qq.com> Date: Tue, 8 Sep 2020 17:02:58 +0800 Subject: [PATCH 4/6] Complete the basic unit test of the products module. --- .../Products/ProductAppServiceTests.cs | 55 ++++++++++++++++++- .../ProductsApplicationTestBase.cs | 13 ++++- .../EShopProductsTestBaseModule.cs | 4 ++ .../ProductsTestData.cs | 11 ++++ .../ProductsTestDataBuilder.cs | 27 +++++++++ 5 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestData.cs create mode 100644 modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestDataBuilder.cs diff --git a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs index 034e71e2..d553c885 100644 --- a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs @@ -1,5 +1,10 @@ +using System.Collections.Generic; +using System.Linq; using Shouldly; using System.Threading.Tasks; +using EasyAbp.EShop.Products.Options; +using EasyAbp.EShop.Products.Products.Dtos; +using Microsoft.Extensions.Options; using Xunit; namespace EasyAbp.EShop.Products.Products @@ -7,20 +12,66 @@ namespace EasyAbp.EShop.Products.Products public class ProductAppServiceTests : ProductsApplicationTestBase { private readonly IProductAppService _productAppService; + private readonly EShopProductsOptions _eShopProductsOptions; public ProductAppServiceTests() { _productAppService = GetRequiredService(); + _eShopProductsOptions = GetRequiredService>().Value; } [Fact] - public async Task Test1() + public async Task Should_Create_A_Product_With_SKU() { // Arrange + _eShopProductsOptions.Groups.Configure("Default Group Name", x => + { + x.DisplayName = "Default Group Name"; + x.Description = "Default Description"; + }); + + var requestDto = new CreateUpdateProductDto + { + ProductGroupName = "Default Group Name", + ProductDetailId = ProductsTestData.ProductDetails1Id, + StoreId = ProductsTestData.Store1Id, + UniqueName = "Unique Pencil", + DisplayName = "Pencil", + InventoryStrategy = InventoryStrategy.NoNeed, + DisplayOrder = 0, + IsPublished = true, + ProductAttributes = new List + { + new CreateUpdateProductAttributeDto + { + DisplayName = "Default Attribute 1", + Description = "Default Description 1", + DisplayOrder = 1, + ProductAttributeOptions = new List + { + new CreateUpdateProductAttributeOptionDto + { + DisplayName = "Option?" + } + } + } + } + }; // Act + var response = await _productAppService.CreateAsync(requestDto); // Assert + response.ShouldNotBeNull(); + response.DisplayName.ShouldBe("Pencil"); + response.UniqueName.ShouldBe("Unique Pencil"); + + UsingDbContext(db => + { + var product = db.Products.FirstOrDefault(x => x.Id == response.Id); + product.ShouldNotBeNull(); + product.DisplayName.ShouldBe("Pencil"); + }); } } -} +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/ProductsApplicationTestBase.cs b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/ProductsApplicationTestBase.cs index 2c773c37..d61aa13d 100644 --- a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/ProductsApplicationTestBase.cs +++ b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/ProductsApplicationTestBase.cs @@ -1,10 +1,19 @@ -namespace EasyAbp.EShop.Products +using System; +using EasyAbp.EShop.Products.EntityFrameworkCore; + +namespace EasyAbp.EShop.Products { /* Inherit from this class for your application layer tests. * See SampleAppService_Tests for example. */ public abstract class ProductsApplicationTestBase : ProductsTestBase { - + protected void UsingDbContext(Action action) + { + using (var dbContext = GetRequiredService()) + { + action.Invoke(dbContext); + } + } } } \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/EShopProductsTestBaseModule.cs b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/EShopProductsTestBaseModule.cs index 7bfbba49..330bbe21 100644 --- a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/EShopProductsTestBaseModule.cs +++ b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/EShopProductsTestBaseModule.cs @@ -35,6 +35,10 @@ namespace EasyAbp.EShop.Products await scope.ServiceProvider .GetRequiredService() .SeedAsync(); + + await scope.ServiceProvider + .GetRequiredService() + .BuildAsync(); } }); } diff --git a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestData.cs b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestData.cs new file mode 100644 index 00000000..6dfaba4b --- /dev/null +++ b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestData.cs @@ -0,0 +1,11 @@ +using System; + +namespace EasyAbp.EShop.Products +{ + public class ProductsTestData + { + public static Guid Store1Id { get; } = Guid.NewGuid(); + + public static Guid ProductDetails1Id { get; } = Guid.NewGuid(); + } +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestDataBuilder.cs b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestDataBuilder.cs new file mode 100644 index 00000000..f315c55b --- /dev/null +++ b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestDataBuilder.cs @@ -0,0 +1,27 @@ +using System.Threading.Tasks; +using EasyAbp.EShop.Products.ProductDetails; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Threading; + +namespace EasyAbp.EShop.Products +{ + public class ProductsTestDataBuilder : ITransientDependency + { + private readonly IProductDetailRepository _productDetailRepository; + + public ProductsTestDataBuilder(IProductDetailRepository productDetailRepository) + { + _productDetailRepository = productDetailRepository; + } + + public void Build() + { + AsyncHelper.RunSync(BuildAsync); + } + + public async Task BuildAsync() + { + await _productDetailRepository.InsertAsync(new ProductDetail(ProductsTestData.ProductDetails1Id, "Product Details")); + } + } +} \ No newline at end of file From b9b58505540aa3bb98b086b9767b1c7738418e8f Mon Sep 17 00:00:00 2001 From: GameBelial <243387971@qq.com> Date: Tue, 8 Sep 2020 19:10:22 +0800 Subject: [PATCH 5/6] Complete the basic unit test of the payments module. --- .../Payments/PaymentAppServiceTests.cs | 43 ++++++++++++++++--- .../Refunds/RefundAppServiceTests.cs | 27 +++++++++--- .../PaymentsTestData.cs | 15 +++++++ .../PaymentsTestDataBuilder.cs | 28 ++++++++++++ 4 files changed, 103 insertions(+), 10 deletions(-) create mode 100644 modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs create mode 100644 modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestDataBuilder.cs diff --git a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/PaymentAppServiceTests.cs b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/PaymentAppServiceTests.cs index d8c7dd39..8272e796 100644 --- a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/PaymentAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/PaymentAppServiceTests.cs @@ -1,5 +1,11 @@ -using Shouldly; +using System; +using System.Collections.Generic; using System.Threading.Tasks; +using EasyAbp.EShop.Orders.Orders; +using EasyAbp.EShop.Orders.Orders.Dtos; +using EasyAbp.EShop.Payments.Payments.Dtos; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; using Xunit; namespace EasyAbp.EShop.Payments.Payments @@ -8,19 +14,46 @@ namespace EasyAbp.EShop.Payments.Payments { private readonly IPaymentAppService _paymentAppService; + protected override void AfterAddApplication(IServiceCollection services) + { + MockOrderService(services); + } + + private void MockOrderService(IServiceCollection services) + { + var orderService = Substitute.For(); + orderService.GetAsync(PaymentsTestData.Order1).Returns(Task.FromResult(new OrderDto + { + Id = PaymentsTestData.Order1, + Currency = "$", + TotalDiscount = 10, + TotalPrice = 100, + StoreId = PaymentsTestData.Store1 + })); + + services.AddTransient(_ => orderService); + } + public PaymentAppServiceTests() { _paymentAppService = GetRequiredService(); } [Fact] - public async Task Test1() + public async Task Should_Success_Create_Payment() { // Arrange + var request = new CreatePaymentDto + { + OrderIds = new List + { + PaymentsTestData.Order1 + }, + PaymentMethod = "Alipay" + }; - // Act - - // Assert + // Act & Assert + await _paymentAppService.CreateAsync(request); } } } diff --git a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/RefundAppServiceTests.cs b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/RefundAppServiceTests.cs index 626e117c..23a1ad86 100644 --- a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/RefundAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/RefundAppServiceTests.cs @@ -1,5 +1,6 @@ -using Shouldly; +using System.Collections.Generic; using System.Threading.Tasks; +using EasyAbp.EShop.Payments.Refunds.Dtos; using Xunit; namespace EasyAbp.EShop.Payments.Refunds @@ -13,14 +14,30 @@ namespace EasyAbp.EShop.Payments.Refunds _refundAppService = GetRequiredService(); } + // TODO: There may be BUG. [Fact] - public async Task Test1() + public async Task Should_Refund_Success() { // Arrange + var request = new CreateEShopRefundInput + { + DisplayReason = "Reason", + CustomerRemark = "Customer Remark", + PaymentId = PaymentsTestData.Payment1, + StaffRemark = "StaffRemark", + RefundItems = new List + { + new CreateEShopRefundItemInput + { + CustomerRemark = "CustomerRemark", + OrderId = PaymentsTestData.Order1, + StaffRemark = "StaffRemark" + } + } + }; - // Act - - // Assert + // Act & Assert + await _refundAppService.CreateAsync(request); } } } diff --git a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs new file mode 100644 index 00000000..da43ca82 --- /dev/null +++ b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs @@ -0,0 +1,15 @@ +using System; + +namespace EasyAbp.EShop.Payments +{ + public static class PaymentsTestData + { + public static Guid Order1 { get; } = Guid.NewGuid(); + + public static Guid Order2 { get; } = Guid.NewGuid(); + + public static Guid Store1 { get; } = Guid.NewGuid(); + + public static Guid Payment1 { get; } = Guid.NewGuid(); + } +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestDataBuilder.cs b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestDataBuilder.cs new file mode 100644 index 00000000..a29cb9a2 --- /dev/null +++ b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestDataBuilder.cs @@ -0,0 +1,28 @@ +using System.Threading.Tasks; +using EasyAbp.EShop.Payments.Payments; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Threading; + +namespace EasyAbp.EShop.Payments +{ + public class PaymentsTestDataBuilder : ITransientDependency + { + private readonly IPaymentRepository _paymentRepository; + + public PaymentsTestDataBuilder(IPaymentRepository paymentRepository) + { + _paymentRepository = paymentRepository; + } + + public void Build() + { + AsyncHelper.RunSync(BuildAsync); + } + + public async Task BuildAsync() + { + // TODO: How to create payment? + // await _paymentRepository.InsertAsync(new Payment()); + } + } +} \ No newline at end of file From 2877da8077c406a48d5912b7fa888d85faa3476f Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Wed, 9 Sep 2020 17:01:32 +0800 Subject: [PATCH 6/6] Improve tests --- .../Orders/OrderAppServiceTests.cs | 108 ++++++++++++++++-- .../Orders/OrderCreatedEventHandler.cs | 35 ++++++ .../OrderTestData.cs | 2 + .../Payments/PaymentAppServiceTests.cs | 26 ++++- .../Payments/TestCreatePaymentEventHandler.cs | 19 +++ .../Refunds/RefundAppServiceTests.cs | 84 +++++++++++++- .../Refunds/TestRefundPaymentEventHandler.cs | 19 +++ .../PaymentsTestData.cs | 4 + .../Products/ProductAppServiceTests.cs | 52 ++++++++- .../Stores/StoreAppServiceTests.cs | 2 +- 10 files changed, 331 insertions(+), 20 deletions(-) create mode 100644 modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderCreatedEventHandler.cs create mode 100644 modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/TestCreatePaymentEventHandler.cs create mode 100644 modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/TestRefundPaymentEventHandler.cs diff --git a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs index eba07a59..1ca12872 100644 --- a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using EasyAbp.EShop.Orders.Orders.Dtos; using EasyAbp.EShop.Products.Products; using EasyAbp.EShop.Products.Products.Dtos; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using NSubstitute; using Shouldly; @@ -29,24 +30,32 @@ namespace EasyAbp.EShop.Orders.Orders CreationTime = DateTime.Now, IsPublished = true, Id = OrderTestData.Product1Id, + ProductGroupName = "Default", + ProductGroupDisplayName = "Default", + UniqueName = "Pencil", + DisplayName = "Hello pencil", ProductSkus = new List { new ProductSkuDto { Id = OrderTestData.ProductSku1Id, + Name = "My SKU", OrderMinQuantity = 0, OrderMaxQuantity = 100, - AttributeOptionIds = new List() + AttributeOptionIds = new List(), + Price = 1m, + Currency = "CNY", } }, - InventoryStrategy = InventoryStrategy.NoNeed + InventoryStrategy = InventoryStrategy.NoNeed, + LastModificationTime = OrderTestData.ProductLastModificationTime })); services.AddTransient(_ => productAppService); } [Fact] - public async Task Should_Create_A_Order() + public async Task Order_Should_Be_Created() { // Arrange var createOrderDto = new CreateOrderDto @@ -65,29 +74,107 @@ namespace EasyAbp.EShop.Orders.Orders }; // Act - var response = await _orderAppService.CreateAsync(createOrderDto); + var createResponse = await _orderAppService.CreateAsync(createOrderDto); + var response = await _orderAppService.GetAsync(createResponse.Id); // Assert response.ShouldNotBeNull(); + response.Currency.ShouldBe("CNY"); + response.CanceledTime.ShouldBeNull(); + response.CancellationReason.ShouldBeNullOrEmpty(); + response.CompletionTime.ShouldBeNull(); response.CustomerRemark.ShouldBe("customer remark"); + response.OrderNumber.ShouldNotBeNull(); + response.OrderStatus.ShouldBe(OrderStatus.Pending); + response.PaidTime.ShouldBeNull(); + response.PaymentId.ShouldBeNull(); + response.RefundAmount.ShouldBe(0m); + response.StaffRemark.ShouldBeNullOrEmpty(); + response.StoreId.ShouldBe(OrderTestData.Store1Id); + response.TotalDiscount.ShouldBe(0m); + response.TotalPrice.ShouldBe(10m); + response.ActualTotalPrice.ShouldBe(10m); + response.CustomerUserId.ShouldBe(Guid.Parse("2e701e62-0953-4dd3-910b-dc6cc93ccb0d")); + response.ProductTotalPrice.ShouldBe(10m); + response.ReducedInventoryAfterPaymentTime.ShouldBeNull(); + response.ReducedInventoryAfterPlacingTime.ShouldNotBeNull(); + response.OrderLines.Count.ShouldBe(1); + + var responseOrderLine = response.OrderLines.First(); + responseOrderLine.ProductId.ShouldBe(OrderTestData.Product1Id); + responseOrderLine.ProductSkuId.ShouldBe(OrderTestData.ProductSku1Id); + responseOrderLine.ProductDisplayName.ShouldBe("Hello pencil"); + responseOrderLine.ProductUniqueName.ShouldBe("Pencil"); + responseOrderLine.ProductGroupName.ShouldBe("Default"); + responseOrderLine.ProductGroupDisplayName.ShouldBe("Default"); + responseOrderLine.SkuName.ShouldBe("My SKU"); + responseOrderLine.UnitPrice.ShouldBe(1m); + responseOrderLine.TotalPrice.ShouldBe(10m); + responseOrderLine.TotalDiscount.ShouldBe(0m); + responseOrderLine.ActualTotalPrice.ShouldBe(10m); + responseOrderLine.Currency.ShouldBe("CNY"); + responseOrderLine.Quantity.ShouldBe(10); + responseOrderLine.ProductModificationTime.ShouldBe(OrderTestData.ProductLastModificationTime); + responseOrderLine.RefundAmount.ShouldBe(0m); + responseOrderLine.RefundedQuantity.ShouldBe(0); UsingDbContext(context => { context.Orders.Count().ShouldBe(1); - var order = context.Orders.First(); + var order = context.Orders.Include(x => x.OrderLines).First(); + order.ShouldNotBeNull(); + order.Currency.ShouldBe("CNY"); + order.CanceledTime.ShouldBeNull(); + order.CancellationReason.ShouldBeNullOrEmpty(); + order.CompletionTime.ShouldBeNull(); order.CustomerRemark.ShouldBe("customer remark"); + order.OrderNumber.ShouldNotBeNull(); + order.OrderStatus.ShouldBe(OrderStatus.Pending); + order.PaidTime.ShouldBeNull(); + order.PaymentId.ShouldBeNull(); + order.RefundAmount.ShouldBe(0m); + order.StaffRemark.ShouldBeNullOrEmpty(); + order.StoreId.ShouldBe(OrderTestData.Store1Id); + order.TotalDiscount.ShouldBe(0m); + order.TotalPrice.ShouldBe(10m); + order.ActualTotalPrice.ShouldBe(10m); + order.CustomerUserId.ShouldBe(Guid.Parse("2e701e62-0953-4dd3-910b-dc6cc93ccb0d")); + order.ProductTotalPrice.ShouldBe(10m); + order.ReducedInventoryAfterPaymentTime.ShouldBeNull(); + order.ReducedInventoryAfterPlacingTime.ShouldNotBeNull(); + order.OrderLines.Count.ShouldBe(1); + + var orderLine = order.OrderLines.First(); + orderLine.ProductId.ShouldBe(OrderTestData.Product1Id); + orderLine.ProductSkuId.ShouldBe(OrderTestData.ProductSku1Id); + orderLine.ProductDisplayName.ShouldBe("Hello pencil"); + orderLine.ProductUniqueName.ShouldBe("Pencil"); + orderLine.ProductGroupName.ShouldBe("Default"); + orderLine.ProductGroupDisplayName.ShouldBe("Default"); + orderLine.SkuName.ShouldBe("My SKU"); + orderLine.UnitPrice.ShouldBe(1m); + orderLine.TotalPrice.ShouldBe(10m); + orderLine.TotalDiscount.ShouldBe(0m); + orderLine.ActualTotalPrice.ShouldBe(10m); + orderLine.Currency.ShouldBe("CNY"); + orderLine.Quantity.ShouldBe(10); + orderLine.ProductModificationTime.ShouldBe(OrderTestData.ProductLastModificationTime); + orderLine.RefundAmount.ShouldBe(0m); + orderLine.RefundedQuantity.ShouldBe(0); }); } [Fact] - public async Task Order_Should_Complete() + public async Task Order_Should_Be_Completed() { // Arrange - await Should_Create_A_Order(); + await Order_Should_Be_Created(); Guid orderId = Guid.Empty; UsingDbContext(db => { var order = db.Orders.First(); + order.OrderStatus.ShouldNotBe(OrderStatus.Completed); + order.CompletionTime.ShouldBeNull(); orderId = order.Id; order.SetPaidTime(DateTime.Now); order.SetReducedInventoryAfterPaymentTime(DateTime.Now); @@ -104,14 +191,16 @@ namespace EasyAbp.EShop.Orders.Orders } [Fact] - public async Task Order_Should_Cancel() + public async Task Order_Should_Be_Canceled() { // Arrange - await Should_Create_A_Order(); + await Order_Should_Be_Created(); Guid orderId = Guid.Empty; UsingDbContext(db => { var order = db.Orders.First(); + order.OrderStatus.ShouldNotBe(OrderStatus.Canceled); + order.CanceledTime.ShouldNotBeNull(); orderId = order.Id; }); @@ -126,6 +215,7 @@ namespace EasyAbp.EShop.Orders.Orders // Assert response.ShouldNotBeNull(); response.OrderStatus.ShouldBe(OrderStatus.Canceled); + response.CompletionTime.ShouldNotBeNull(); response.CancellationReason.ShouldBe("Repeat orders."); UsingDbContext(db => diff --git a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderCreatedEventHandler.cs b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderCreatedEventHandler.cs new file mode 100644 index 00000000..ea8e7ae3 --- /dev/null +++ b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderCreatedEventHandler.cs @@ -0,0 +1,35 @@ +using System.Threading.Tasks; +using EasyAbp.EShop.Products.Products; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Domain.Entities.Events.Distributed; +using Volo.Abp.EventBus.Distributed; +using Volo.Abp.MultiTenancy; + +namespace EasyAbp.EShop.Orders.Orders +{ + public class OrderCreatedEventHandler : IDistributedEventHandler>, ITransientDependency + { + private readonly ICurrentTenant _currentTenant; + private readonly IDistributedEventBus _distributedEventBus; + + public OrderCreatedEventHandler( + ICurrentTenant currentTenant, + IDistributedEventBus distributedEventBus) + { + _currentTenant = currentTenant; + _distributedEventBus = distributedEventBus; + } + + public virtual async Task HandleEventAsync(EntityCreatedEto eventData) + { + using var changeTenant = _currentTenant.Change(eventData.Entity.TenantId); + + await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto + { + TenantId = eventData.Entity.TenantId, + OrderId = eventData.Entity.Id, + IsSuccess = true + }); + } + } +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.TestBase/OrderTestData.cs b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.TestBase/OrderTestData.cs index 2097343b..05851986 100644 --- a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.TestBase/OrderTestData.cs +++ b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.TestBase/OrderTestData.cs @@ -10,5 +10,7 @@ namespace EasyAbp.EShop.Orders public static Guid Product1Id { get; } = Guid.NewGuid(); public static Guid ProductSku1Id { get; } = Guid.NewGuid(); + + public static DateTime ProductLastModificationTime { get; } = DateTime.Today; } } \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/PaymentAppServiceTests.cs b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/PaymentAppServiceTests.cs index 8272e796..a792fdc5 100644 --- a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/PaymentAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/PaymentAppServiceTests.cs @@ -6,6 +6,7 @@ using EasyAbp.EShop.Orders.Orders.Dtos; using EasyAbp.EShop.Payments.Payments.Dtos; using Microsoft.Extensions.DependencyInjection; using NSubstitute; +using Shouldly; using Xunit; namespace EasyAbp.EShop.Payments.Payments @@ -13,6 +14,7 @@ namespace EasyAbp.EShop.Payments.Payments public class PaymentAppServiceTests : PaymentsApplicationTestBase { private readonly IPaymentAppService _paymentAppService; + private readonly TestCreatePaymentEventHandler _testCreatePaymentEventHandler; protected override void AfterAddApplication(IServiceCollection services) { @@ -25,10 +27,19 @@ namespace EasyAbp.EShop.Payments.Payments orderService.GetAsync(PaymentsTestData.Order1).Returns(Task.FromResult(new OrderDto { Id = PaymentsTestData.Order1, - Currency = "$", - TotalDiscount = 10, - TotalPrice = 100, - StoreId = PaymentsTestData.Store1 + Currency = "CNY", + ActualTotalPrice = 0, + StoreId = PaymentsTestData.Store1, + OrderLines = new List + { + new OrderLineDto + { + Id = PaymentsTestData.OrderLine1, + Currency = "CNY", + ActualTotalPrice = 0, + Quantity = 1 + } + } })); services.AddTransient(_ => orderService); @@ -37,10 +48,11 @@ namespace EasyAbp.EShop.Payments.Payments public PaymentAppServiceTests() { _paymentAppService = GetRequiredService(); + _testCreatePaymentEventHandler = GetRequiredService(); } [Fact] - public async Task Should_Success_Create_Payment() + public async Task Should_Publish_Create_Payment_Event() { // Arrange var request = new CreatePaymentDto @@ -49,11 +61,13 @@ namespace EasyAbp.EShop.Payments.Payments { PaymentsTestData.Order1 }, - PaymentMethod = "Alipay" + PaymentMethod = "Free" }; // Act & Assert await _paymentAppService.CreateAsync(request); + + _testCreatePaymentEventHandler.IsEventPublished.ShouldBe(true ); } } } diff --git a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/TestCreatePaymentEventHandler.cs b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/TestCreatePaymentEventHandler.cs new file mode 100644 index 00000000..46d6104f --- /dev/null +++ b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/TestCreatePaymentEventHandler.cs @@ -0,0 +1,19 @@ +using System.Threading.Tasks; +using EasyAbp.PaymentService.Payments; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus.Distributed; + +namespace EasyAbp.EShop.Payments.Payments +{ + public class TestCreatePaymentEventHandler : IDistributedEventHandler, ISingletonDependency + { + public bool IsEventPublished { get; protected set; } + + public Task HandleEventAsync(CreatePaymentEto eventData) + { + IsEventPublished = true; + + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/RefundAppServiceTests.cs b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/RefundAppServiceTests.cs index 23a1ad86..02063e80 100644 --- a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/RefundAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/RefundAppServiceTests.cs @@ -1,6 +1,13 @@ +using System; using System.Collections.Generic; using System.Threading.Tasks; +using EasyAbp.EShop.Orders.Orders; +using EasyAbp.EShop.Orders.Orders.Dtos; +using EasyAbp.EShop.Payments.Payments; using EasyAbp.EShop.Payments.Refunds.Dtos; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; +using Shouldly; using Xunit; namespace EasyAbp.EShop.Payments.Refunds @@ -8,15 +15,77 @@ namespace EasyAbp.EShop.Payments.Refunds public class RefundAppServiceTests : PaymentsApplicationTestBase { private readonly IRefundAppService _refundAppService; + private readonly TestRefundPaymentEventHandler _testRefundPaymentEventHandler; + protected override void AfterAddApplication(IServiceCollection services) + { + MockPaymentRepository(services); + MockOrderService(services); + } + + private void MockPaymentRepository(IServiceCollection services) + { + var paymentRepository = Substitute.For(); + paymentRepository.GetAsync(PaymentsTestData.Payment1).Returns(x => + { + var paymentType = typeof(Payment); + var paymentItemType = typeof(PaymentItem); + + var paymentItem = Activator.CreateInstance(paymentItemType, true) as PaymentItem; + paymentItem.ShouldNotBeNull(); + paymentItemType.GetProperty(nameof(PaymentItem.Id))?.SetValue(paymentItem, PaymentsTestData.PaymentItem1); + paymentItemType.GetProperty(nameof(PaymentItem.ActualPaymentAmount))?.SetValue(paymentItem, 0m); + paymentItemType.GetProperty(nameof(PaymentItem.ItemType))?.SetValue(paymentItem, PaymentsConsts.PaymentItemType); + paymentItemType.GetProperty(nameof(PaymentItem.ItemKey))?.SetValue(paymentItem, PaymentsTestData.Order1.ToString()); + paymentItem.ExtraProperties.Add("StoreId", PaymentsTestData.Store1.ToString()); + + var payment = Activator.CreateInstance(paymentType, true) as Payment; + payment.ShouldNotBeNull(); + paymentType.GetProperty(nameof(Payment.Id))?.SetValue(payment, PaymentsTestData.Payment1); + paymentType.GetProperty(nameof(Payment.Currency))?.SetValue(payment, "CNY"); + paymentType.GetProperty(nameof(Payment.ActualPaymentAmount))?.SetValue(payment, 0m); + paymentType.GetProperty(nameof(Payment.PaymentItems))?.SetValue(payment, new List {paymentItem}); + + return payment; + }); + + services.AddTransient(_ => paymentRepository); + } + + private void MockOrderService(IServiceCollection services) + { + var orderService = Substitute.For(); + orderService.GetAsync(PaymentsTestData.Order1).Returns(Task.FromResult(new OrderDto + { + Id = PaymentsTestData.Order1, + Currency = "CNY", + ActualTotalPrice = 0, + StoreId = PaymentsTestData.Store1, + OrderLines = new List + { + new OrderLineDto + { + Id = PaymentsTestData.OrderLine1, + Currency = "CNY", + ActualTotalPrice = 0, + Quantity = 1 + } + }, + PaymentId = PaymentsTestData.Payment1 + })); + + services.AddTransient(_ => orderService); + } + public RefundAppServiceTests() { _refundAppService = GetRequiredService(); + _testRefundPaymentEventHandler = GetRequiredService(); } // TODO: There may be BUG. [Fact] - public async Task Should_Refund_Success() + public async Task Should_Publish_Refund_Payment_Event() { // Arrange var request = new CreateEShopRefundInput @@ -31,13 +100,24 @@ namespace EasyAbp.EShop.Payments.Refunds { CustomerRemark = "CustomerRemark", OrderId = PaymentsTestData.Order1, - StaffRemark = "StaffRemark" + StaffRemark = "StaffRemark", + OrderLines = new List + { + new OrderLineRefundInfoModel + { + OrderLineId = PaymentsTestData.OrderLine1, + Quantity = 1, + TotalAmount = 0 + } + } } } }; // Act & Assert await _refundAppService.CreateAsync(request); + + _testRefundPaymentEventHandler.IsEventPublished.ShouldBe(true); } } } diff --git a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/TestRefundPaymentEventHandler.cs b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/TestRefundPaymentEventHandler.cs new file mode 100644 index 00000000..6451d7c9 --- /dev/null +++ b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/TestRefundPaymentEventHandler.cs @@ -0,0 +1,19 @@ +using System.Threading.Tasks; +using EasyAbp.PaymentService.Payments; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus.Distributed; + +namespace EasyAbp.EShop.Payments.Payments +{ + public class TestRefundPaymentEventHandler : IDistributedEventHandler, ISingletonDependency + { + public bool IsEventPublished { get; protected set; } + + public Task HandleEventAsync(RefundPaymentEto eventData) + { + IsEventPublished = true; + + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs index da43ca82..b564f4c0 100644 --- a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs +++ b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs @@ -7,9 +7,13 @@ namespace EasyAbp.EShop.Payments public static Guid Order1 { get; } = Guid.NewGuid(); public static Guid Order2 { get; } = Guid.NewGuid(); + + public static Guid OrderLine1 { get; } = Guid.NewGuid(); public static Guid Store1 { get; } = Guid.NewGuid(); public static Guid Payment1 { get; } = Guid.NewGuid(); + + public static Guid PaymentItem1 { get; } = Guid.NewGuid(); } } \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs index d553c885..995a55ea 100644 --- a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs @@ -1,9 +1,11 @@ +using System; using System.Collections.Generic; using System.Linq; using Shouldly; using System.Threading.Tasks; using EasyAbp.EShop.Products.Options; using EasyAbp.EShop.Products.Products.Dtos; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using Xunit; @@ -21,7 +23,7 @@ namespace EasyAbp.EShop.Products.Products } [Fact] - public async Task Should_Create_A_Product_With_SKU() + public async Task Should_Create_A_Product() { // Arrange _eShopProductsOptions.Groups.Configure("Default Group Name", x => @@ -51,7 +53,7 @@ namespace EasyAbp.EShop.Products.Products { new CreateUpdateProductAttributeOptionDto { - DisplayName = "Option?" + DisplayName = "Option 1" } } } @@ -63,6 +65,7 @@ namespace EasyAbp.EShop.Products.Products // Assert response.ShouldNotBeNull(); + response.IsPublished.ShouldBe(true); response.DisplayName.ShouldBe("Pencil"); response.UniqueName.ShouldBe("Unique Pencil"); @@ -73,5 +76,50 @@ namespace EasyAbp.EShop.Products.Products product.DisplayName.ShouldBe("Pencil"); }); } + + [Fact] + public async Task Should_Create_A_Sku() + { + await Should_Create_A_Product(); + Guid productId = default; + Guid productAttributeOptionId = default; + UsingDbContext(db => + { + var product = db.Products + .Include(x => x.ProductAttributes) + .ThenInclude(x => x.ProductAttributeOptions) + .FirstOrDefault(x => x.UniqueName == "Unique Pencil"); + product.ShouldNotBeNull(); + product.ProductAttributes.ShouldNotBeNull(); + product.ProductAttributes.Count.ShouldBe(1); + var productAttribute = product.ProductAttributes.First(); + productAttribute.ProductAttributeOptions.ShouldNotBeNull(); + productAttribute.ProductAttributeOptions.Count.ShouldBe(1); + productId = product.Id; + productAttributeOptionId = productAttribute.ProductAttributeOptions.First().Id; + }); + + var response = await _productAppService.CreateSkuAsync(productId, ProductsTestData.Store1Id, new CreateProductSkuDto + { + AttributeOptionIds = new List {productAttributeOptionId}, + Currency = "CNY", + Price = 1m, + OrderMinQuantity = 1, + OrderMaxQuantity = 10 + }); + + response.ShouldNotBeNull(); + response.MinimumPrice.ShouldBe(1m); + response.ProductSkus.Count.ShouldBe(1); + + var responseSku = response.ProductSkus.First(); + responseSku.Currency.ShouldBe("CNY"); + responseSku.Price.ShouldBe(1m); + responseSku.AttributeOptionIds.Count.ShouldBe(1); + responseSku.AttributeOptionIds.First().ShouldBe(productAttributeOptionId); + responseSku.OrderMinQuantity.ShouldBe(1); + responseSku.OrderMaxQuantity.ShouldBe(10); + + } } } \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/Stores/StoreAppServiceTests.cs b/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/Stores/StoreAppServiceTests.cs index 3b9f8799..f7bd8d97 100644 --- a/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/Stores/StoreAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/Stores/StoreAppServiceTests.cs @@ -47,7 +47,7 @@ namespace EasyAbp.EShop.Stores.Stores // Assert response.ShouldNotBeNull(); - response.Name.ShouldBe("My Store"); + response.Name.ShouldBe("My store"); } } }