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..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 @@ -1,5 +1,14 @@ -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.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; +using Shouldly; using Xunit; namespace EasyAbp.EShop.Orders.Orders @@ -13,14 +22,209 @@ namespace EasyAbp.EShop.Orders.Orders _orderAppService = GetRequiredService(); } + protected override void AfterAddApplication(IServiceCollection services) + { + var productAppService = Substitute.For(); + productAppService.GetAsync(OrderTestData.Product1Id, OrderTestData.Store1Id).Returns(Task.FromResult(new ProductDto + { + 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(), + Price = 1m, + Currency = "CNY", + } + }, + InventoryStrategy = InventoryStrategy.NoNeed, + LastModificationTime = OrderTestData.ProductLastModificationTime + })); + + services.AddTransient(_ => productAppService); + } + + [Fact] + public async Task Order_Should_Be_Created() + { + // Arrange + var createOrderDto = new CreateOrderDto + { + CustomerRemark = "customer remark", + StoreId = OrderTestData.Store1Id, + OrderLines = new List + { + new CreateOrderLineDto + { + ProductId = OrderTestData.Product1Id, + ProductSkuId = OrderTestData.ProductSku1Id, + Quantity = 10 + } + } + }; + + // Act + 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.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_Be_Completed() + { + // Arrange + 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); + 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 Test1() + public async Task Order_Should_Be_Canceled() { // Arrange + 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; + }); + + var cancelRequestDto = new CancelOrderInput + { + CancellationReason = "Repeat orders." + }; // Act + var response = await _orderAppService.CancelAsync(orderId, cancelRequestDto); // Assert + response.ShouldNotBeNull(); + response.OrderStatus.ShouldBe(OrderStatus.Canceled); + response.CompletionTime.ShouldNotBeNull(); + 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.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.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 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..05851986 --- /dev/null +++ b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.TestBase/OrderTestData.cs @@ -0,0 +1,16 @@ +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(); + + 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 d8c7dd39..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 @@ -1,5 +1,12 @@ -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 Shouldly; using Xunit; namespace EasyAbp.EShop.Payments.Payments @@ -7,20 +14,60 @@ namespace EasyAbp.EShop.Payments.Payments public class PaymentAppServiceTests : PaymentsApplicationTestBase { private readonly IPaymentAppService _paymentAppService; + private readonly TestCreatePaymentEventHandler _testCreatePaymentEventHandler; + + 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 = "CNY", + ActualTotalPrice = 0, + StoreId = PaymentsTestData.Store1, + OrderLines = new List + { + new OrderLineDto + { + Id = PaymentsTestData.OrderLine1, + Currency = "CNY", + ActualTotalPrice = 0, + Quantity = 1 + } + } + })); + + services.AddTransient(_ => orderService); + } public PaymentAppServiceTests() { _paymentAppService = GetRequiredService(); + _testCreatePaymentEventHandler = GetRequiredService(); } [Fact] - public async Task Test1() + public async Task Should_Publish_Create_Payment_Event() { // Arrange + var request = new CreatePaymentDto + { + OrderIds = new List + { + PaymentsTestData.Order1 + }, + PaymentMethod = "Free" + }; - // Act - - // Assert + // 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 626e117c..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,5 +1,13 @@ -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; +using EasyAbp.EShop.Payments.Refunds.Dtos; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; +using Shouldly; using Xunit; namespace EasyAbp.EShop.Payments.Refunds @@ -7,20 +15,109 @@ 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 Test1() + public async Task Should_Publish_Refund_Payment_Event() { // 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", + OrderLines = new List + { + new OrderLineRefundInfoModel + { + OrderLineId = PaymentsTestData.OrderLine1, + Quantity = 1, + TotalAmount = 0 + } + } + } + } + }; - // Act - - // Assert + // 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 new file mode 100644 index 00000000..b564f4c0 --- /dev/null +++ b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs @@ -0,0 +1,19 @@ +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 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.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 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..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,5 +1,12 @@ +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; namespace EasyAbp.EShop.Products.Products @@ -7,20 +14,112 @@ 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() { // 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 1" + } + } + } + } + }; // Act + var response = await _productAppService.CreateAsync(requestDto); // Assert + response.ShouldNotBeNull(); + response.IsPublished.ShouldBe(true); + 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"); + }); + } + + [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.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 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..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 @@ -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