Browse Source

Improve tests

pull/99/head
gdlcf88 6 years ago
parent
commit
2877da8077
  1. 108
      modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs
  2. 35
      modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderCreatedEventHandler.cs
  3. 2
      modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.TestBase/OrderTestData.cs
  4. 26
      modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/PaymentAppServiceTests.cs
  5. 19
      modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/TestCreatePaymentEventHandler.cs
  6. 84
      modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/RefundAppServiceTests.cs
  7. 19
      modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/TestRefundPaymentEventHandler.cs
  8. 4
      modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs
  9. 52
      modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs
  10. 2
      modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/Stores/StoreAppServiceTests.cs

108
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<ProductSkuDto>
{
new ProductSkuDto
{
Id = OrderTestData.ProductSku1Id,
Name = "My SKU",
OrderMinQuantity = 0,
OrderMaxQuantity = 100,
AttributeOptionIds = new List<Guid>()
AttributeOptionIds = new List<Guid>(),
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 =>

35
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<EntityCreatedEto<OrderEto>>, 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<OrderEto> eventData)
{
using var changeTenant = _currentTenant.Change(eventData.Entity.TenantId);
await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto
{
TenantId = eventData.Entity.TenantId,
OrderId = eventData.Entity.Id,
IsSuccess = true
});
}
}
}

2
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;
}
}

26
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<OrderLineDto>
{
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<IPaymentAppService>();
_testCreatePaymentEventHandler = GetRequiredService<TestCreatePaymentEventHandler>();
}
[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 );
}
}
}

19
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<CreatePaymentEto>, ISingletonDependency
{
public bool IsEventPublished { get; protected set; }
public Task HandleEventAsync(CreatePaymentEto eventData)
{
IsEventPublished = true;
return Task.CompletedTask;
}
}
}

84
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<IPaymentRepository>();
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> {paymentItem});
return payment;
});
services.AddTransient(_ => paymentRepository);
}
private void MockOrderService(IServiceCollection services)
{
var orderService = Substitute.For<IOrderAppService>();
orderService.GetAsync(PaymentsTestData.Order1).Returns(Task.FromResult(new OrderDto
{
Id = PaymentsTestData.Order1,
Currency = "CNY",
ActualTotalPrice = 0,
StoreId = PaymentsTestData.Store1,
OrderLines = new List<OrderLineDto>
{
new OrderLineDto
{
Id = PaymentsTestData.OrderLine1,
Currency = "CNY",
ActualTotalPrice = 0,
Quantity = 1
}
},
PaymentId = PaymentsTestData.Payment1
}));
services.AddTransient(_ => orderService);
}
public RefundAppServiceTests()
{
_refundAppService = GetRequiredService<IRefundAppService>();
_testRefundPaymentEventHandler = GetRequiredService<TestRefundPaymentEventHandler>();
}
// 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<OrderLineRefundInfoModel>
{
new OrderLineRefundInfoModel
{
OrderLineId = PaymentsTestData.OrderLine1,
Quantity = 1,
TotalAmount = 0
}
}
}
}
};
// Act & Assert
await _refundAppService.CreateAsync(request);
_testRefundPaymentEventHandler.IsEventPublished.ShouldBe(true);
}
}
}

19
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<RefundPaymentEto>, ISingletonDependency
{
public bool IsEventPublished { get; protected set; }
public Task HandleEventAsync(RefundPaymentEto eventData)
{
IsEventPublished = true;
return Task.CompletedTask;
}
}
}

4
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();
}
}

52
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<Guid> {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);
}
}
}

2
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");
}
}
}

Loading…
Cancel
Save