mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-27 01:32:03 +08:00
mall + trade:增加 TradeDiscountActivityPriceCalculator 的单元测试
This commit is contained in:
parent
1452f6b231
commit
0d08c814ec
@ -47,8 +47,6 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
|
|||||||
@InjectMocks
|
@InjectMocks
|
||||||
private PriceServiceImpl priceService;
|
private PriceServiceImpl priceService;
|
||||||
|
|
||||||
@Mock
|
|
||||||
private DiscountActivityService discountService;
|
|
||||||
@Mock
|
@Mock
|
||||||
private RewardActivityService rewardActivityService;
|
private RewardActivityService rewardActivityService;
|
||||||
@Mock
|
@Mock
|
||||||
@ -56,91 +54,6 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private ProductSkuApi productSkuApi;
|
private ProductSkuApi productSkuApi;
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCalculatePrice_discountActivity() {
|
|
||||||
// 准备参数
|
|
||||||
PriceCalculateReqDTO calculateReqDTO = new PriceCalculateReqDTO().setUserId(randomLongId())
|
|
||||||
.setItems(asList(new PriceCalculateReqDTO.Item().setSkuId(10L).setCount(2),
|
|
||||||
new PriceCalculateReqDTO.Item().setSkuId(20L).setCount(3)));
|
|
||||||
// mock 方法(商品 SKU 信息)
|
|
||||||
ProductSkuRespDTO productSku01 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(10L).setPrice(100));
|
|
||||||
ProductSkuRespDTO productSku02 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(20L).setPrice(50));
|
|
||||||
when(productSkuApi.getSkuList(eq(asSet(10L, 20L)))).thenReturn(asList(productSku01, productSku02));
|
|
||||||
// mock 方法(限时折扣 DiscountActivity 信息)
|
|
||||||
DiscountProductDetailBO discountProduct01 = randomPojo(DiscountProductDetailBO.class, o -> o.setActivityId(1000L)
|
|
||||||
.setActivityName("活动 1000 号").setSkuId(10L)
|
|
||||||
.setDiscountType(PromotionDiscountTypeEnum.PRICE.getType()).setDiscountPrice(40));
|
|
||||||
DiscountProductDetailBO discountProduct02 = randomPojo(DiscountProductDetailBO.class, o -> o.setActivityId(2000L)
|
|
||||||
.setActivityName("活动 2000 号").setSkuId(20L)
|
|
||||||
.setDiscountType(PromotionDiscountTypeEnum.PERCENT.getType()).setDiscountPercent(60));
|
|
||||||
when(discountService.getMatchDiscountProductList(eq(asSet(10L, 20L)))).thenReturn(
|
|
||||||
MapUtil.builder(10L, discountProduct01).put(20L, discountProduct02).map());
|
|
||||||
|
|
||||||
// 10L: 100 * 2 - 40 * 2 = 120
|
|
||||||
// 20L:50 * 3 - 50 * 3 * 0.4 = 90
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO);
|
|
||||||
// 断言 Order 部分
|
|
||||||
PriceCalculateRespDTO.Order order = priceCalculate.getOrder();
|
|
||||||
assertEquals(order.getTotalPrice(), 350);
|
|
||||||
assertEquals(order.getDiscountPrice(), 0);
|
|
||||||
assertEquals(order.getPointPrice(), 0);
|
|
||||||
assertEquals(order.getDeliveryPrice(), 0);
|
|
||||||
assertEquals(order.getPayPrice(), 210);
|
|
||||||
assertNull(order.getCouponId());
|
|
||||||
// 断言 OrderItem 部分
|
|
||||||
assertEquals(order.getItems().size(), 2);
|
|
||||||
PriceCalculateRespDTO.OrderItem orderItem01 = order.getItems().get(0);
|
|
||||||
assertEquals(orderItem01.getSkuId(), 10L);
|
|
||||||
assertEquals(orderItem01.getCount(), 2);
|
|
||||||
assertEquals(orderItem01.getOriginalPrice(), 200);
|
|
||||||
assertEquals(orderItem01.getOriginalUnitPrice(), 100);
|
|
||||||
assertEquals(orderItem01.getDiscountPrice(), 80);
|
|
||||||
assertEquals(orderItem01.getPayPrice(), 120);
|
|
||||||
assertEquals(orderItem01.getOrderPartPrice(), 0);
|
|
||||||
assertEquals(orderItem01.getOrderDividePrice(), 120);
|
|
||||||
PriceCalculateRespDTO.OrderItem orderItem02 = order.getItems().get(1);
|
|
||||||
assertEquals(orderItem02.getSkuId(), 20L);
|
|
||||||
assertEquals(orderItem02.getCount(), 3);
|
|
||||||
assertEquals(orderItem02.getOriginalPrice(), 150);
|
|
||||||
assertEquals(orderItem02.getOriginalUnitPrice(), 50);
|
|
||||||
assertEquals(orderItem02.getDiscountPrice(), 60);
|
|
||||||
assertEquals(orderItem02.getPayPrice(), 90);
|
|
||||||
assertEquals(orderItem02.getOrderPartPrice(), 0);
|
|
||||||
assertEquals(orderItem02.getOrderDividePrice(), 90);
|
|
||||||
// 断言 Promotion 部分
|
|
||||||
assertEquals(priceCalculate.getPromotions().size(), 2);
|
|
||||||
PriceCalculateRespDTO.Promotion promotion01 = priceCalculate.getPromotions().get(0);
|
|
||||||
assertEquals(promotion01.getId(), 1000L);
|
|
||||||
assertEquals(promotion01.getName(), "活动 1000 号");
|
|
||||||
assertEquals(promotion01.getType(), PromotionTypeEnum.DISCOUNT_ACTIVITY.getType());
|
|
||||||
assertEquals(promotion01.getLevel(), PromotionLevelEnum.SKU.getLevel());
|
|
||||||
assertEquals(promotion01.getTotalPrice(), 200);
|
|
||||||
assertEquals(promotion01.getDiscountPrice(), 80);
|
|
||||||
assertTrue(promotion01.getMatch());
|
|
||||||
assertEquals(promotion01.getDescription(), "限时折扣:省 0.80 元");
|
|
||||||
PriceCalculateRespDTO.PromotionItem promotionItem01 = promotion01.getItems().get(0);
|
|
||||||
assertEquals(promotion01.getItems().size(), 1);
|
|
||||||
assertEquals(promotionItem01.getSkuId(), 10L);
|
|
||||||
assertEquals(promotionItem01.getOriginalPrice(), 200);
|
|
||||||
assertEquals(promotionItem01.getDiscountPrice(), 80);
|
|
||||||
PriceCalculateRespDTO.Promotion promotion02 = priceCalculate.getPromotions().get(1);
|
|
||||||
assertEquals(promotion02.getId(), 2000L);
|
|
||||||
assertEquals(promotion02.getName(), "活动 2000 号");
|
|
||||||
assertEquals(promotion02.getType(), PromotionTypeEnum.DISCOUNT_ACTIVITY.getType());
|
|
||||||
assertEquals(promotion02.getLevel(), PromotionLevelEnum.SKU.getLevel());
|
|
||||||
assertEquals(promotion02.getTotalPrice(), 150);
|
|
||||||
assertEquals(promotion02.getDiscountPrice(), 60);
|
|
||||||
assertTrue(promotion02.getMatch());
|
|
||||||
assertEquals(promotion02.getDescription(), "限时折扣:省 0.60 元");
|
|
||||||
PriceCalculateRespDTO.PromotionItem promotionItem02 = promotion02.getItems().get(0);
|
|
||||||
assertEquals(promotion02.getItems().size(), 1);
|
|
||||||
assertEquals(promotionItem02.getSkuId(), 20L);
|
|
||||||
assertEquals(promotionItem02.getOriginalPrice(), 150);
|
|
||||||
assertEquals(promotionItem02.getDiscountPrice(), 60);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试满减送活动,匹配的情况
|
* 测试满减送活动,匹配的情况
|
||||||
*/
|
*/
|
||||||
|
@ -53,10 +53,13 @@ public class TradeDiscountActivityPriceCalculator implements TradePriceCalculato
|
|||||||
Integer newDiscountPrice = orderItem.getPayPrice() - newPayPrice;
|
Integer newDiscountPrice = orderItem.getPayPrice() - newPayPrice;
|
||||||
|
|
||||||
// 3.1 记录优惠明细
|
// 3.1 记录优惠明细
|
||||||
|
if (orderItem.getSelected()) {
|
||||||
|
// 注意,只有在选中的情况下,才会记录到优惠明细。否则仅仅是更新 SKU 优惠金额,用于展示
|
||||||
TradePriceCalculatorHelper.addPromotion(result, orderItem,
|
TradePriceCalculatorHelper.addPromotion(result, orderItem,
|
||||||
discountProduct.getActivityId(), discountProduct.getActivityName(), PromotionTypeEnum.DISCOUNT_ACTIVITY.getType(),
|
discountProduct.getActivityId(), discountProduct.getActivityName(), PromotionTypeEnum.DISCOUNT_ACTIVITY.getType(),
|
||||||
StrUtil.format("限时折扣:省 {} 元", formatPrice(newDiscountPrice)),
|
StrUtil.format("限时折扣:省 {} 元", formatPrice(newDiscountPrice)),
|
||||||
newDiscountPrice);
|
newDiscountPrice);
|
||||||
|
}
|
||||||
// 3.2 更新 SKU 优惠金额
|
// 3.2 更新 SKU 优惠金额
|
||||||
orderItem.setDiscountPrice(orderItem.getDiscountPrice() + newDiscountPrice);
|
orderItem.setDiscountPrice(orderItem.getDiscountPrice() + newDiscountPrice);
|
||||||
TradePriceCalculatorHelper.recountPayPrice(orderItem);
|
TradePriceCalculatorHelper.recountPayPrice(orderItem);
|
||||||
|
@ -36,7 +36,7 @@ class TradeCouponPriceCalculatorTest extends BaseMockitoUnitTest {
|
|||||||
private CouponApi couponApi;
|
private CouponApi couponApi;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void calculate() {
|
public void testCalculate() {
|
||||||
// 准备参数
|
// 准备参数
|
||||||
TradePriceCalculateReqBO param = new TradePriceCalculateReqBO()
|
TradePriceCalculateReqBO param = new TradePriceCalculateReqBO()
|
||||||
.setUserId(233L).setCouponId(1024L)
|
.setUserId(233L).setCouponId(1024L)
|
||||||
@ -120,7 +120,7 @@ class TradeCouponPriceCalculatorTest extends BaseMockitoUnitTest {
|
|||||||
assertEquals(orderItem04.getCouponPrice(), 0);
|
assertEquals(orderItem04.getCouponPrice(), 0);
|
||||||
assertEquals(orderItem04.getPointPrice(), 0);
|
assertEquals(orderItem04.getPointPrice(), 0);
|
||||||
assertEquals(orderItem04.getPayPrice(), 300);
|
assertEquals(orderItem04.getPayPrice(), 300);
|
||||||
// 断言 Promotion 部分
|
// 断言:Promotion 部分
|
||||||
assertEquals(result.getPromotions().size(), 1);
|
assertEquals(result.getPromotions().size(), 1);
|
||||||
TradePriceCalculateRespBO.Promotion promotion01 = result.getPromotions().get(0);
|
TradePriceCalculateRespBO.Promotion promotion01 = result.getPromotions().get(0);
|
||||||
assertEquals(promotion01.getId(), 1024L);
|
assertEquals(promotion01.getId(), 1024L);
|
||||||
@ -140,4 +140,5 @@ class TradeCouponPriceCalculatorTest extends BaseMockitoUnitTest {
|
|||||||
assertEquals(promotionItem012.getTotalPrice(), 150);
|
assertEquals(promotionItem012.getTotalPrice(), 150);
|
||||||
assertEquals(promotionItem012.getDiscountPrice(), 30);
|
assertEquals(promotionItem012.getDiscountPrice(), 30);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,118 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.service.price.calculator;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
||||||
|
import cn.iocoder.yudao.module.promotion.api.discount.DiscountActivityApi;
|
||||||
|
import cn.iocoder.yudao.module.promotion.api.discount.dto.DiscountProductRespDTO;
|
||||||
|
import cn.iocoder.yudao.module.promotion.enums.common.PromotionDiscountTypeEnum;
|
||||||
|
import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum;
|
||||||
|
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
|
||||||
|
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link TradeDiscountActivityPriceCalculator} 的单元测试类
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
public class TradeDiscountActivityPriceCalculatorTest extends BaseMockitoUnitTest {
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private TradeDiscountActivityPriceCalculator tradeDiscountActivityPriceCalculator;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private DiscountActivityApi discountActivityApi;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCalculate() {
|
||||||
|
// 准备参数
|
||||||
|
TradePriceCalculateReqBO param = new TradePriceCalculateReqBO()
|
||||||
|
.setItems(asList(
|
||||||
|
new TradePriceCalculateReqBO.Item().setSkuId(10L).setCount(2).setSelected(true), // 匹配活动,且已选中
|
||||||
|
new TradePriceCalculateReqBO.Item().setSkuId(20L).setCount(3).setSelected(false) // 匹配活动,但未选中
|
||||||
|
));
|
||||||
|
TradePriceCalculateRespBO result = new TradePriceCalculateRespBO()
|
||||||
|
.setPrice(new TradePriceCalculateRespBO.Price())
|
||||||
|
.setPromotions(new ArrayList<>())
|
||||||
|
.setItems(asList(
|
||||||
|
new TradePriceCalculateRespBO.OrderItem().setSkuId(10L).setCount(2).setSelected(true)
|
||||||
|
.setPrice(100),
|
||||||
|
new TradePriceCalculateRespBO.OrderItem().setSkuId(20L).setCount(3).setSelected(false)
|
||||||
|
.setPrice(50)
|
||||||
|
));
|
||||||
|
// 保证价格被初始化上
|
||||||
|
TradePriceCalculatorHelper.recountPayPrice(result.getItems());
|
||||||
|
TradePriceCalculatorHelper.recountAllPrice(result);
|
||||||
|
|
||||||
|
// mock 方法(限时折扣活动)
|
||||||
|
when(discountActivityApi.getMatchDiscountProductList(eq(asSet(10L, 20L)))).thenReturn(asList(
|
||||||
|
randomPojo(DiscountProductRespDTO.class, o -> o.setActivityId(1000L)
|
||||||
|
.setActivityName("活动 1000 号").setSkuId(10L)
|
||||||
|
.setDiscountType(PromotionDiscountTypeEnum.PRICE.getType()).setDiscountPrice(40)),
|
||||||
|
randomPojo(DiscountProductRespDTO.class, o -> o.setActivityId(2000L)
|
||||||
|
.setActivityName("活动 2000 号").setSkuId(20L)
|
||||||
|
.setDiscountType(PromotionDiscountTypeEnum.PERCENT.getType()).setDiscountPercent(60))
|
||||||
|
));
|
||||||
|
// 10L: 100 * 2 - 40 * 2 = 120
|
||||||
|
// 20L:50 * 3 - 50 * 3 * 0.4 = 90
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
tradeDiscountActivityPriceCalculator.calculate(param, result);
|
||||||
|
// 断言:Price 部分
|
||||||
|
TradePriceCalculateRespBO.Price price = result.getPrice();
|
||||||
|
assertEquals(price.getTotalPrice(), 200);
|
||||||
|
assertEquals(price.getDiscountPrice(), 80);
|
||||||
|
assertEquals(price.getPointPrice(), 0);
|
||||||
|
assertEquals(price.getDeliveryPrice(), 0);
|
||||||
|
assertEquals(price.getCouponPrice(), 0);
|
||||||
|
assertEquals(price.getPayPrice(), 120);
|
||||||
|
assertNull(result.getCouponId());
|
||||||
|
// 断言:SKU 1
|
||||||
|
assertEquals(result.getItems().size(), 2);
|
||||||
|
TradePriceCalculateRespBO.OrderItem orderItem01 = result.getItems().get(0);
|
||||||
|
assertEquals(orderItem01.getSkuId(), 10L);
|
||||||
|
assertEquals(orderItem01.getCount(), 2);
|
||||||
|
assertEquals(orderItem01.getPrice(), 100);
|
||||||
|
assertEquals(orderItem01.getDiscountPrice(), 80);
|
||||||
|
assertEquals(orderItem01.getDeliveryPrice(), 0);
|
||||||
|
assertEquals(orderItem01.getCouponPrice(), 0);
|
||||||
|
assertEquals(orderItem01.getPointPrice(), 0);
|
||||||
|
assertEquals(orderItem01.getPayPrice(), 120);
|
||||||
|
// 断言:SKU 2
|
||||||
|
TradePriceCalculateRespBO.OrderItem orderItem02 = result.getItems().get(1);
|
||||||
|
assertEquals(orderItem02.getSkuId(), 20L);
|
||||||
|
assertEquals(orderItem02.getCount(), 3);
|
||||||
|
assertEquals(orderItem02.getPrice(), 50);
|
||||||
|
assertEquals(orderItem02.getDiscountPrice(), 60);
|
||||||
|
assertEquals(orderItem02.getDeliveryPrice(), 0);
|
||||||
|
assertEquals(orderItem02.getCouponPrice(), 0);
|
||||||
|
assertEquals(orderItem02.getPointPrice(), 0);
|
||||||
|
assertEquals(orderItem02.getPayPrice(), 90);
|
||||||
|
// 断言:Promotion 部分
|
||||||
|
assertEquals(result.getPromotions().size(), 1);
|
||||||
|
TradePriceCalculateRespBO.Promotion promotion01 = result.getPromotions().get(0);
|
||||||
|
assertEquals(promotion01.getId(), 1000L);
|
||||||
|
assertEquals(promotion01.getName(), "活动 1000 号");
|
||||||
|
assertEquals(promotion01.getType(), PromotionTypeEnum.DISCOUNT_ACTIVITY.getType());
|
||||||
|
assertEquals(promotion01.getTotalPrice(), 200);
|
||||||
|
assertEquals(promotion01.getDiscountPrice(), 80);
|
||||||
|
assertTrue(promotion01.getMatch());
|
||||||
|
assertEquals(promotion01.getDescription(), "限时折扣:省 0.80 元");
|
||||||
|
TradePriceCalculateRespBO.PromotionItem promotionItem01 = promotion01.getItems().get(0);
|
||||||
|
assertEquals(promotion01.getItems().size(), 1);
|
||||||
|
assertEquals(promotionItem01.getSkuId(), 10L);
|
||||||
|
assertEquals(promotionItem01.getTotalPrice(), 200);
|
||||||
|
assertEquals(promotionItem01.getDiscountPrice(), 80);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user