Browse Source

Merge pull request #99 from EasyAbp/complete-tests

Complete tests
pull/103/head
Super 6 years ago
committed by GitHub
parent
commit
226a96b877
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 210
      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. 16
      modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/OrdersApplicationTestBase.cs
  4. 16
      modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.TestBase/OrderTestData.cs
  5. 57
      modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/PaymentAppServiceTests.cs
  6. 19
      modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/TestCreatePaymentEventHandler.cs
  7. 107
      modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/RefundAppServiceTests.cs
  8. 19
      modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/TestRefundPaymentEventHandler.cs
  9. 19
      modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs
  10. 28
      modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestDataBuilder.cs
  11. 103
      modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs
  12. 13
      modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/ProductsApplicationTestBase.cs
  13. 4
      modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/EShopProductsTestBaseModule.cs
  14. 11
      modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestData.cs
  15. 27
      modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestDataBuilder.cs
  16. 29
      modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/Stores/StoreAppServiceTests.cs
  17. 13
      modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/StoresApplicationTestBase.cs
  18. 7
      modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.TestBase/StoresTestData.cs

210
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<IOrderAppService>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
var productAppService = Substitute.For<IProductAppService>();
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<ProductSkuDto>
{
new ProductSkuDto
{
Id = OrderTestData.ProductSku1Id,
Name = "My SKU",
OrderMinQuantity = 0,
OrderMaxQuantity = 100,
AttributeOptionIds = new List<Guid>(),
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<CreateOrderLineDto>
{
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.");
});
}
}
}
}

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

16
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<EShopOrdersApplicationTestModule>
{
protected void UsingDbContext(Action<IOrdersDbContext> action)
{
using (var dbContext = GetRequiredService<IOrdersDbContext>())
{
action.Invoke(dbContext);
}
}
}
}

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

57
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<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
}
}
}));
services.AddTransient(_ => orderService);
}
public PaymentAppServiceTests()
{
_paymentAppService = GetRequiredService<IPaymentAppService>();
_testCreatePaymentEventHandler = GetRequiredService<TestCreatePaymentEventHandler>();
}
[Fact]
public async Task Test1()
public async Task Should_Publish_Create_Payment_Event()
{
// Arrange
var request = new CreatePaymentDto
{
OrderIds = new List<Guid>
{
PaymentsTestData.Order1
},
PaymentMethod = "Free"
};
// Act
// Assert
// 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;
}
}
}

107
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<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 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<CreateEShopRefundItemInput>
{
new CreateEShopRefundItemInput
{
CustomerRemark = "CustomerRemark",
OrderId = PaymentsTestData.Order1,
StaffRemark = "StaffRemark",
OrderLines = new List<OrderLineRefundInfoModel>
{
new OrderLineRefundInfoModel
{
OrderLineId = PaymentsTestData.OrderLine1,
Quantity = 1,
TotalAmount = 0
}
}
}
}
};
// Act
// Assert
// 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;
}
}
}

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

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

103
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<IProductAppService>();
_eShopProductsOptions = GetRequiredService<IOptions<EShopProductsOptions>>().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<CreateUpdateProductAttributeDto>
{
new CreateUpdateProductAttributeDto
{
DisplayName = "Default Attribute 1",
Description = "Default Description 1",
DisplayOrder = 1,
ProductAttributeOptions = new List<CreateUpdateProductAttributeOptionDto>
{
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<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);
}
}
}
}

13
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<EShopProductsApplicationTestModule>
{
protected void UsingDbContext(Action<IProductsDbContext> action)
{
using (var dbContext = GetRequiredService<IProductsDbContext>())
{
action.Invoke(dbContext);
}
}
}
}

4
modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/EShopProductsTestBaseModule.cs

@ -35,6 +35,10 @@ namespace EasyAbp.EShop.Products
await scope.ServiceProvider
.GetRequiredService<IDataSeeder>()
.SeedAsync();
await scope.ServiceProvider
.GetRequiredService<ProductsTestDataBuilder>()
.BuildAsync();
}
});
}

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

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

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

13
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<EShopStoresApplicationTestModule>
{
protected void UsingDbContext(Action<IStoresDbContext> action)
{
using (var dbContext = GetRequiredService<IStoresDbContext>())
{
action.Invoke(dbContext);
}
}
}
}

7
modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.TestBase/StoresTestData.cs

@ -0,0 +1,7 @@
namespace EasyAbp.EShop.Stores
{
public static class StoresTestData
{
}
}
Loading…
Cancel
Save