Skip to content

Commit

Permalink
Unit test: Order using coupons discount (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzw218 authored Aug 8, 2024
1 parent 7a50178 commit eeedf08
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 43 deletions.
90 changes: 48 additions & 42 deletions EShop.sln

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public enum CouponType
/// <summary>
/// Discount every time the condition is met.
/// e.g. arrange ConditionAmount = 300 and DiscountAmount = 20.
/// If the price of a product is 300, the actual order price will be discounted by 60.
/// If the price of a product is 900, the actual order price will be discounted by 60.
/// </summary>
PerMeet = 2,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace EasyAbp.EShop.Plugins.Coupons
{
/* Inherit from this class for your application layer tests.
* See SampleAppService_Tests for example.
*/
public abstract class EShopOrdersPluginsCouponsTestBase : CouponsTestBase<EShopOrdersPluginsCouponsTestModule>
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using EasyAbp.EShop.Orders.Plugins.Coupons;
using Volo.Abp.Modularity;

namespace EasyAbp.EShop.Plugins.Coupons
{
[DependsOn(
typeof(EShopOrdersPluginsCouponsModule),
typeof(CouponsApplicationTestModule),
typeof(CouponsDomainTestModule)
)]
public class EShopOrdersPluginsCouponsTestModule : AbpModule
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>EasyAbp.EShop.Orders.Plugins.Coupons</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\modules\EasyAbp.EShop.Orders\test\EasyAbp.EShop.Orders.TestBase\EasyAbp.EShop.Orders.TestBase.csproj" />
<ProjectReference Include="..\..\src\EasyAbp.EShop.Orders.Plugins.Coupons\EasyAbp.EShop.Orders.Plugins.Coupons.csproj" />
<ProjectReference Include="..\EasyAbp.EShop.Plugins.Coupons.Application.Tests\EasyAbp.EShop.Plugins.Coupons.Application.Tests.csproj" />
<ProjectReference Include="..\EasyAbp.EShop.Plugins.Coupons.Domain.Tests\EasyAbp.EShop.Plugins.Coupons.Domain.Tests.csproj" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<ConfigureAwait ContinueOnCapturedContext="false" />
</Weavers>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. -->
<xs:element name="Weavers">
<xs:complexType>
<xs:all>
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" />
</xs:complexType>
</xs:element>
</xs:all>
<xs:attribute name="VerifyAssembly" type="xs:boolean">
<xs:annotation>
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="VerifyIgnoreCodes" type="xs:string">
<xs:annotation>
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="GenerateXsd" type="xs:boolean">
<xs:annotation>
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyAbp.EShop.Orders;
using EasyAbp.EShop.Orders.Orders;
using EasyAbp.EShop.Plugins.Coupons.CouponTemplates;
using EasyAbp.EShop.Products.Products;
using Shouldly;
using Volo.Abp.Data;
using Xunit;
using static EasyAbp.EShop.Orders.Authorization.OrdersPermissions;

namespace EasyAbp.EShop.Plugins.Coupons.Coupons
{
public class OrdersAppServiceTests : EShopOrdersPluginsCouponsTestBase
{
private readonly ICouponRepository _couponRepository;
private readonly ICouponTemplateRepository _couponTemplateRepository;

public OrdersAppServiceTests()
{
_couponRepository = GetRequiredService<ICouponRepository>();
_couponTemplateRepository = GetRequiredService<ICouponTemplateRepository>();
}

[Theory]
//Meet the condition discount 1 time
[InlineData(5, 2.3, 11, 2.3, CouponType.Normal)]
[InlineData(5, 2.3, 10, 2.3, CouponType.Normal)]
[InlineData(5, 2.3, 9, 2.3, CouponType.Normal)]
//The discount condition is not met
[InlineData(5, 2.3, 4, 0, CouponType.Normal)]

//Meet the condition discount 2 time
[InlineData(5, 2.3, 11, 4.6, CouponType.PerMeet)]
[InlineData(5, 2.3, 10, 4.6, CouponType.PerMeet)]

//Meet the condition discount 1 time
[InlineData(5, 2.3, 9, 2.3, CouponType.PerMeet)]
//The discount condition is not met
[InlineData(5, 2.3, 4, 0, CouponType.PerMeet)]
public async Task Should_CouponType_DiscountAmount(decimal couponConditionAmount, decimal couponDiscountAmount, decimal productSkuPrice,decimal discountAmount, CouponType couponType)
{
var template = new CouponTemplate(Guid.NewGuid(), null, null, couponType, "UName", "DName", "Desc", null
, DateTime.Now.AddDays(-1), DateTime.Now.AddDays(1), couponConditionAmount, couponDiscountAmount, "USD", true, null);
var coupon = new Coupon(Guid.NewGuid(), null, template.Id, Guid.Parse("2e701e62-0953-4dd3-910b-dc6cc93ccb0d"), null, null);
await WithUnitOfWorkAsync(async () =>
{
// Arrange
await _couponTemplateRepository.InsertAsync(template);
await _couponRepository.InsertAsync(coupon);
});
Order order = null;
var prodoctSku = new ProductSkuEto
{
Id = OrderTestData.ProductSku2Id,
AttributeOptionIds = new List<Guid>(),
Price = productSkuPrice,
Currency = "USD",
OrderMinQuantity = 1,
OrderMaxQuantity = 100,
};
await WithUnitOfWorkAsync(async () =>
{
var orderGenerator = GetRequiredService<INewOrderGenerator>();

var createOrderLine = new CreateOrderLineInfoModel(OrderTestData.Product1Id, OrderTestData.ProductSku2Id, 1);
var createOrderInfoModel = new CreateOrderInfoModel(OrderTestData.Store1Id, null,
new List<CreateOrderLineInfoModel>
{
createOrderLine
}
);
createOrderInfoModel.SetProperty("CouponId", coupon.Id);

var dic = new Dictionary<Guid, IProduct>
{
{
OrderTestData.Product1Id,
new ProductEto
{
Id = OrderTestData.Product1Id,
ProductSkus = new List<ProductSkuEto>{ prodoctSku }
}
}
};

order = await orderGenerator.GenerateAsync(Guid.NewGuid(), createOrderInfoModel, dic, new Dictionary<Guid, DateTime>());

});

//assert

var orderLine = order.OrderLines[0];

var orderLine2ExpectedPrice = 1 * prodoctSku.Price - discountAmount;
order.ActualTotalPrice.ShouldBe(orderLine2ExpectedPrice);
order.TotalDiscount.ShouldBe(order.TotalPrice - order.ActualTotalPrice);
order.OrderDiscounts.Count.ShouldBe(1);
var orderDiscount = order.OrderDiscounts.SingleOrDefault(x => x.OrderId == order.Id && x.OrderLineId == orderLine.Id && x.EffectGroup == "Coupon");
orderDiscount.DiscountedAmount.ShouldBe(discountAmount);
}

}
}

0 comments on commit eeedf08

Please sign in to comment.