From c9439e1021a6f3833b174bb24fd58c3d91579f3e Mon Sep 17 00:00:00 2001 From: irving Date: Fri, 31 Oct 2025 00:10:40 -0400 Subject: [PATCH] =?UTF-8?q?test:=20=E8=A1=A5=E5=85=85=E4=BC=98=E6=83=A0?= =?UTF-8?q?=E5=88=B8=E5=BA=93=E5=AD=98=E7=9B=B8=E5=85=B3=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shop/service/CouponWhitelistTest.java | 26 +++++ .../impl/PlayCouponInfoServiceImplTest.java | 106 ++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 play-admin/src/test/java/com/starry/admin/modules/shop/service/impl/PlayCouponInfoServiceImplTest.java diff --git a/play-admin/src/test/java/com/starry/admin/modules/shop/service/CouponWhitelistTest.java b/play-admin/src/test/java/com/starry/admin/modules/shop/service/CouponWhitelistTest.java index 1036938..28eef67 100644 --- a/play-admin/src/test/java/com/starry/admin/modules/shop/service/CouponWhitelistTest.java +++ b/play-admin/src/test/java/com/starry/admin/modules/shop/service/CouponWhitelistTest.java @@ -146,6 +146,32 @@ public class CouponWhitelistTest { assertEquals("active", list.get(0).getId()); } + @Test + @DisplayName("列表过滤-库存:无限库存的优惠券可见") + void testQueryAllShowsUnlimitedInventory() { + PlayCustomUserInfoEntity current = new PlayCustomUserInfoEntity(); + current.setId("uid-4"); + ThreadLocalRequestDetail.setRequestDetail(current); + + PlayCouponInfoEntity unlimited = new PlayCouponInfoEntity(); + unlimited.setId("coupon-unlimited"); + unlimited.setCouponOnLineState(CouponOnlineState.ONLINE.getCode()); + unlimited.setValidityPeriodType(CouponValidityPeriodType.PERMANENT.getCode()); + unlimited.setRemainingQuantity(-1); + unlimited.setCouponQuantity(-1); + + when(couponDetailsService.selectByCustomId("uid-4")).thenReturn(Collections.emptyList()); + when(couponInfoService.queryAll()).thenReturn(Collections.singletonList(unlimited)); + + R resp = wxCouponController.queryAll(); + @SuppressWarnings("unchecked") + List list = (List) resp.getData(); + + assertNotNull(list); + assertEquals(1, list.size(), "无限库存优惠券不应被过滤"); + assertEquals("coupon-unlimited", list.get(0).getId()); + } + @Test @DisplayName("下单查询:存在限制原因的优惠券被过滤") void testQueryByOrderFiltersRestrictedCoupons() { diff --git a/play-admin/src/test/java/com/starry/admin/modules/shop/service/impl/PlayCouponInfoServiceImplTest.java b/play-admin/src/test/java/com/starry/admin/modules/shop/service/impl/PlayCouponInfoServiceImplTest.java new file mode 100644 index 0000000..e56f40e --- /dev/null +++ b/play-admin/src/test/java/com/starry/admin/modules/shop/service/impl/PlayCouponInfoServiceImplTest.java @@ -0,0 +1,106 @@ +package com.starry.admin.modules.shop.service.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.starry.admin.common.exception.CustomException; +import com.starry.admin.modules.custom.module.entity.PlayCustomUserInfoEntity; +import com.starry.admin.modules.shop.mapper.PlayCouponInfoMapper; +import com.starry.admin.modules.shop.module.entity.PlayCouponInfoEntity; +import com.starry.admin.modules.shop.module.enums.CouponObtainChannel; +import com.starry.admin.modules.shop.module.enums.CouponOnlineState; +import com.starry.admin.modules.shop.service.IPlayCouponDetailsService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class PlayCouponInfoServiceImplTest { + + @Mock + private PlayCouponInfoMapper playCouponInfoMapper; + + @Mock + private IPlayCouponDetailsService playCouponDetailsService; + + @InjectMocks + private PlayCouponInfoServiceImpl service; + + private PlayCouponInfoEntity couponInfo; + private PlayCustomUserInfoEntity customer; + + @BeforeEach + void setUp() { + couponInfo = new PlayCouponInfoEntity(); + couponInfo.setId("coupon-1"); + couponInfo.setCouponOnLineState(CouponOnlineState.ONLINE.getCode()); + couponInfo.setCouponQuantity(5); + couponInfo.setRemainingQuantity(5); + + customer = new PlayCustomUserInfoEntity(); + customer.setId("custom-1"); + customer.setNickname("nick"); + customer.setLevelId("level-1"); + } + + @Test + void claimCouponForCustom_consumesInventoryAndCreatesDetail() { + couponInfo.setClerkObtainedMaxQuantity(2); + when(playCouponInfoMapper.selectByIdForUpdate("coupon-1")).thenReturn(couponInfo); + when(playCouponDetailsService.countActiveByCustomAndCoupon("coupon-1", "custom-1")) + .thenReturn(0L); + when(playCouponInfoMapper.increaseIssuedAndConsumeRemaining("coupon-1")).thenReturn(1); + + service.claimCouponForCustom("coupon-1", customer, CouponObtainChannel.SELF_SERVICE); + + verify(playCouponInfoMapper).increaseIssuedAndConsumeRemaining("coupon-1"); + verify(playCouponDetailsService) + .create(eq("custom-1"), eq(customer.getNickname()), eq(customer.getLevelId()), + eq("coupon-1"), eq(CouponObtainChannel.SELF_SERVICE.getCode()), anyString()); + } + + @Test + void claimCouponForCustom_throwsWhenLimitReached() { + couponInfo.setClerkObtainedMaxQuantity(1); + when(playCouponInfoMapper.selectByIdForUpdate("coupon-1")).thenReturn(couponInfo); + when(playCouponDetailsService.countActiveByCustomAndCoupon("coupon-1", "custom-1")) + .thenReturn(1L); + + CustomException ex = assertThrows(CustomException.class, () -> + service.claimCouponForCustom("coupon-1", customer, CouponObtainChannel.SELF_SERVICE)); + + assertEquals("优惠券已达到领取上限", ex.getMessage()); + } + + @Test + void claimCouponForCustom_throwsWhenDepleted() { + couponInfo.setRemainingQuantity(0); + when(playCouponInfoMapper.selectByIdForUpdate("coupon-1")).thenReturn(couponInfo); + + CustomException ex = assertThrows(CustomException.class, () -> + service.claimCouponForCustom("coupon-1", customer, CouponObtainChannel.SELF_SERVICE)); + + assertEquals("优惠券已领完", ex.getMessage()); + } + + @Test + void claimCouponForCustom_allowsUnlimitedInventory() { + couponInfo.setRemainingQuantity(-1); + couponInfo.setCouponQuantity(-1); + when(playCouponInfoMapper.selectByIdForUpdate("coupon-1")).thenReturn(couponInfo); + when(playCouponInfoMapper.increaseIssuedAndConsumeRemaining("coupon-1")).thenReturn(1); + + service.claimCouponForCustom("coupon-1", customer, CouponObtainChannel.BACKEND_GRANT); + + verify(playCouponInfoMapper).increaseIssuedAndConsumeRemaining("coupon-1"); + verify(playCouponDetailsService) + .create(eq("custom-1"), eq(customer.getNickname()), eq(customer.getLevelId()), + eq("coupon-1"), eq(CouponObtainChannel.BACKEND_GRANT.getCode()), anyString()); + } +}