fix:完善商品管理, 完善 mall.sql, 完善 product 测试相关sql, 完善 ProductSpuServiceImplTest 测试

This commit is contained in:
puhui999
2023-05-23 11:01:40 +08:00
parent 96e2bf020f
commit cac5545762
42 changed files with 1318 additions and 653 deletions

View File

@@ -18,8 +18,9 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEq
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO.PARENT_ID_NULL;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.CATEGORY_NOT_EXISTS;
import static cn.iocoder.yudao.module.product.enums.ProductConstants.PARENT_ID_NULL;
import static org.junit.jupiter.api.Assertions.*;
/**

View File

@@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.product.service.sku;
import cn.hutool.core.util.RandomUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.framework.test.core.util.AssertUtils;
@@ -49,23 +50,26 @@ public class ProductSkuServiceTest extends BaseDbUnitTest {
@MockBean
private ProductPropertyValueService productPropertyValueService;
public Long generateId() {
return RandomUtil.randomLong(100000, 999999);
}
public int generaInt(){return RandomUtil.randomInt(1,9999999);}
@Test
public void testUpdateSkuList() {
// mock 数据
ProductSkuDO sku01 = randomPojo(ProductSkuDO.class, o -> { // 测试更新
o.setSpuId(1L);
//o.setProperties(singletonList(new ProductSkuDO.Property(
// 10L, "颜色", 20L, "红色"))); TODO 新增字段已注释
o.setProperties(singletonList(new ProductSkuDO.Property(
10L, 20L)));
10L, "颜色", 20L, "红色")));
});
productSkuMapper.insert(sku01);
ProductSkuDO sku02 = randomPojo(ProductSkuDO.class, o -> { // 测试删除
o.setSpuId(1L);
//o.setProperties(singletonList(new ProductSkuDO.Property(
// 10L, "颜色", 30L, "蓝色"))); TODO 新增字段已注释
o.setProperties(singletonList(new ProductSkuDO.Property(
10L, 30L)));
10L, "颜色", 30L, "蓝色")));
});
productSkuMapper.insert(sku02);
// 准备参数
@@ -73,10 +77,12 @@ public class ProductSkuServiceTest extends BaseDbUnitTest {
String spuName = "测试商品";
List<ProductSkuCreateOrUpdateReqVO> skus = Arrays.asList(
randomPojo(ProductSkuCreateOrUpdateReqVO.class, o -> { // 测试更新
o.setProperties(singletonList(new ProductSkuCreateOrUpdateReqVO.Property(10L, 20L)));
o.setProperties(singletonList(new ProductSkuCreateOrUpdateReqVO.Property(
10L, "颜色", 20L, "红色")));
}),
randomPojo(ProductSkuCreateOrUpdateReqVO.class, o -> { // 测试新增
o.setProperties(singletonList(new ProductSkuCreateOrUpdateReqVO.Property(10L, 40L)));
o.setProperties(singletonList(new ProductSkuCreateOrUpdateReqVO.Property(
10L, "颜色", 20L, "红色")));
})
);

View File

@@ -6,15 +6,16 @@ import cn.hutool.core.util.RandomUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.product.controller.admin.spu.vo.ProductSpuCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.spu.vo.ProductSpuPageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.spu.vo.ProductSpuPageRespVO;
import cn.iocoder.yudao.module.product.controller.admin.spu.vo.ProductSpuUpdateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuCreateOrUpdateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.spu.vo.*;
import cn.iocoder.yudao.module.product.controller.app.spu.vo.AppProductSpuPageReqVO;
import cn.iocoder.yudao.module.product.convert.spu.ProductSpuConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO;
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
import cn.iocoder.yudao.module.product.dal.mysql.spu.ProductSpuMapper;
import cn.iocoder.yudao.module.product.enums.spu.ProductSpuPageTabEnum;
import cn.iocoder.yudao.module.product.enums.spu.ProductSpuStatusEnum;
import cn.iocoder.yudao.module.product.service.brand.ProductBrandServiceImpl;
import cn.iocoder.yudao.module.product.service.category.ProductCategoryServiceImpl;
@@ -22,6 +23,7 @@ import cn.iocoder.yudao.module.product.service.property.ProductPropertyService;
import cn.iocoder.yudao.module.product.service.property.ProductPropertyValueService;
import cn.iocoder.yudao.module.product.service.sku.ProductSkuServiceImpl;
import com.google.common.collect.Lists;
import org.apache.poi.ss.formula.functions.T;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -30,14 +32,19 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static org.assertj.core.util.Lists.newArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
// TODO @芋艿review 下单元测试
@@ -47,7 +54,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* @author 芋道源码
*/
@Import(ProductSpuServiceImpl.class)
@Disabled // TODO 芋艿:临时去掉
public class ProductSpuServiceImplTest extends BaseDbUnitTest {
@Resource
@@ -75,10 +81,35 @@ public class ProductSpuServiceImplTest extends BaseDbUnitTest {
return RandomUtil.randomLong(100000, 999999);
}
public int generaInt(){return RandomUtil.randomInt(1,9999999);}
@Test
public void testCreateSpu_success() {
// 准备参数
ProductSpuCreateReqVO createReqVO = randomPojo(ProductSpuCreateReqVO.class);
ProductSkuCreateOrUpdateReqVO skuCreateOrUpdateReqVO = randomPojo(ProductSkuCreateOrUpdateReqVO.class,o->{
// 限制范围为正整数
o.setCostPrice(generaInt());
o.setPrice(generaInt());
o.setMarketPrice(generaInt());
o.setStock(generaInt());
o.setWarnStock(10);
o.setSubCommissionFirstPrice(generaInt());
o.setSubCommissionSecondPrice(generaInt());
// 限制分数为两位数
o.setWeight(RandomUtil.randomDouble(10,2, RoundingMode.HALF_UP));
o.setVolume(RandomUtil.randomDouble(10,2, RoundingMode.HALF_UP));
});
ProductSpuCreateReqVO createReqVO = randomPojo(ProductSpuCreateReqVO.class,o->{
o.setCategoryId(generateId());
o.setBrandId(generateId());
o.setUnit(RandomUtil.randomInt(1,20)); // 限制商品单位范围
o.setSort(RandomUtil.randomInt(1,100)); // 限制排序范围
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setVirtualSalesCount(generaInt()); // 限制范围为正整数
o.setActivityOrders(newArrayList(1,3,2,4,5)); // 活动排序
o.setSkus(newArrayList(skuCreateOrUpdateReqVO,skuCreateOrUpdateReqVO,skuCreateOrUpdateReqVO));
});
when(categoryService.getCategoryLevel(eq(createReqVO.getCategoryId()))).thenReturn(2);
Long spu = productSpuService.createSpu(createReqVO);
ProductSpuDO productSpuDO = productSpuMapper.selectById(spu);
assertPojoEquals(createReqVO, productSpuDO);
@@ -87,12 +118,55 @@ public class ProductSpuServiceImplTest extends BaseDbUnitTest {
@Test
public void testUpdateSpu_success() {
// 准备参数
ProductSpuDO createReqVO = randomPojo(ProductSpuDO.class);
ProductSpuDO createReqVO = randomPojo(ProductSpuDO.class,o->{
o.setCategoryId(generateId());
o.setBrandId(generateId());
o.setDeliveryTemplateId(generateId());
o.setUnit(RandomUtil.randomInt(1,20)); // 限制商品单位范围
o.setSort(RandomUtil.randomInt(1,100)); // 限制排序范围
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setVirtualSalesCount(generaInt()); // 限制范围为正整数
o.setActivityOrders(newArrayList(1,3,2,4,5)); // 活动排序
o.setPrice(generaInt()); // 限制范围为正整数
o.setMarketPrice(generaInt()); // 限制范围为正整数
o.setCostPrice(generaInt()); // 限制范围为正整数
o.setStock(generaInt()); // 限制范围为正整数
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setSalesCount(generaInt()); // 限制范围为正整数
o.setBrowseCount(generaInt()); // 限制范围为正整数
});
productSpuMapper.insert(createReqVO);
// 准备参数
ProductSkuCreateOrUpdateReqVO skuCreateOrUpdateReqVO = randomPojo(ProductSkuCreateOrUpdateReqVO.class,o->{
// 限制范围为正整数
o.setCostPrice(generaInt());
o.setPrice(generaInt());
o.setMarketPrice(generaInt());
o.setStock(generaInt());
o.setWarnStock(10);
o.setSubCommissionFirstPrice(generaInt());
o.setSubCommissionSecondPrice(generaInt());
// 限制分数为两位数
o.setWeight(RandomUtil.randomDouble(10,2, RoundingMode.HALF_UP));
o.setVolume(RandomUtil.randomDouble(10,2, RoundingMode.HALF_UP));
});
// 准备参数
ProductSpuUpdateReqVO reqVO = randomPojo(ProductSpuUpdateReqVO.class, o -> {
o.setId(createReqVO.getId()); // 设置更新的 ID
o.setCategoryId(generateId());
o.setBrandId(generateId());
o.setUnit(RandomUtil.randomInt(1,20)); // 限制商品单位范围
o.setSort(RandomUtil.randomInt(1,100)); // 限制排序范围
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setVirtualSalesCount(generaInt()); // 限制范围为正整数
o.setActivityOrders(newArrayList(1,3,2,4,5)); // 活动排序
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setSalesCount(generaInt()); // 限制范围为正整数
o.setBrowseCount(generaInt()); // 限制范围为正整数
o.setStatus(0);
o.setSkus(newArrayList(skuCreateOrUpdateReqVO,skuCreateOrUpdateReqVO,skuCreateOrUpdateReqVO));
});
when(categoryService.getCategoryLevel(eq(reqVO.getCategoryId()))).thenReturn(2);
// 调用
productSpuService.updateSpu(reqVO);
// 校验是否更新正确
@@ -110,7 +184,24 @@ public class ProductSpuServiceImplTest extends BaseDbUnitTest {
@Test
void deleteSpu() {
// 准备参数
ProductSpuDO createReqVO = randomPojo(ProductSpuDO.class);
ProductSpuDO createReqVO = randomPojo(ProductSpuDO.class,o->{
o.setCategoryId(generateId());
o.setBrandId(generateId());
o.setDeliveryTemplateId(generateId());
o.setUnit(RandomUtil.randomInt(1,20)); // 限制商品单位范围
o.setSort(RandomUtil.randomInt(1,100)); // 限制排序范围
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setVirtualSalesCount(generaInt()); // 限制范围为正整数
o.setActivityOrders(newArrayList(1,3,2,4,5)); // 活动排序
o.setPrice(generaInt()); // 限制范围为正整数
o.setMarketPrice(generaInt()); // 限制范围为正整数
o.setCostPrice(generaInt()); // 限制范围为正整数
o.setStock(generaInt()); // 限制范围为正整数
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setSalesCount(generaInt()); // 限制范围为正整数
o.setBrowseCount(generaInt()); // 限制范围为正整数
o.setStatus(-1); // 加入回收站才可删除
});
productSpuMapper.insert(createReqVO);
// 调用
@@ -122,7 +213,23 @@ public class ProductSpuServiceImplTest extends BaseDbUnitTest {
@Test
void getSpu() {
// 准备参数
ProductSpuDO createReqVO = randomPojo(ProductSpuDO.class);
ProductSpuDO createReqVO = randomPojo(ProductSpuDO.class,o->{
o.setCategoryId(generateId());
o.setBrandId(generateId());
o.setDeliveryTemplateId(generateId());
o.setUnit(RandomUtil.randomInt(1,20)); // 限制商品单位范围
o.setSort(RandomUtil.randomInt(1,100)); // 限制排序范围
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setVirtualSalesCount(generaInt()); // 限制范围为正整数
o.setActivityOrders(newArrayList(1,3,2,4,5)); // 活动排序
o.setPrice(generaInt()); // 限制范围为正整数
o.setMarketPrice(generaInt()); // 限制范围为正整数
o.setCostPrice(generaInt()); // 限制范围为正整数
o.setStock(generaInt()); // 限制范围为正整数
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setSalesCount(generaInt()); // 限制范围为正整数
o.setBrowseCount(generaInt()); // 限制范围为正整数
});
productSpuMapper.insert(createReqVO);
ProductSpuDO spu = productSpuService.getSpu(createReqVO.getId());
@@ -132,19 +239,86 @@ public class ProductSpuServiceImplTest extends BaseDbUnitTest {
@Test
void getSpuList() {
// 准备参数
ArrayList<ProductSpuDO> createReqVO = Lists.newArrayList(randomPojo(ProductSpuDO.class), randomPojo(ProductSpuDO.class));
productSpuMapper.insertBatch(createReqVO);
ArrayList<ProductSpuDO> createReqVOs = Lists.newArrayList(randomPojo(ProductSpuDO.class,o->{
o.setCategoryId(generateId());
o.setBrandId(generateId());
o.setDeliveryTemplateId(generateId());
o.setUnit(RandomUtil.randomInt(1,20)); // 限制商品单位范围
o.setSort(RandomUtil.randomInt(1,100)); // 限制排序范围
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setVirtualSalesCount(generaInt()); // 限制范围为正整数
o.setActivityOrders(newArrayList(1,3,2,4,5)); // 活动排序
o.setPrice(generaInt()); // 限制范围为正整数
o.setMarketPrice(generaInt()); // 限制范围为正整数
o.setCostPrice(generaInt()); // 限制范围为正整数
o.setStock(generaInt()); // 限制范围为正整数
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setSalesCount(generaInt()); // 限制范围为正整数
o.setBrowseCount(generaInt()); // 限制范围为正整数
}), randomPojo(ProductSpuDO.class,o->{
o.setCategoryId(generateId());
o.setBrandId(generateId());
o.setDeliveryTemplateId(generateId());
o.setUnit(RandomUtil.randomInt(1,20)); // 限制商品单位范围
o.setSort(RandomUtil.randomInt(1,100)); // 限制排序范围
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setVirtualSalesCount(generaInt()); // 限制范围为正整数
o.setActivityOrders(newArrayList(1,3,2,4,5)); // 活动排序
o.setPrice(generaInt()); // 限制范围为正整数
o.setMarketPrice(generaInt()); // 限制范围为正整数
o.setCostPrice(generaInt()); // 限制范围为正整数
o.setStock(generaInt()); // 限制范围为正整数
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setSalesCount(generaInt()); // 限制范围为正整数
o.setBrowseCount(generaInt()); // 限制范围为正整数
}));
productSpuMapper.insertBatch(createReqVOs);
// 调用
List<ProductSpuDO> spuList = productSpuService.getSpuList(createReqVO.stream().map(ProductSpuDO::getId).collect(Collectors.toList()));
Assertions.assertIterableEquals(createReqVO, spuList);
List<ProductSpuDO> spuList = productSpuService.getSpuList(createReqVOs.stream().map(ProductSpuDO::getId).collect(Collectors.toList()));
Assertions.assertIterableEquals(createReqVOs, spuList);
}
@Test
void getSpuPage_alarmStock_empty() {
// 准备参数
ArrayList<ProductSpuDO> createReqVOs = Lists.newArrayList(randomPojo(ProductSpuDO.class,o->{
o.setCategoryId(generateId());
o.setBrandId(generateId());
o.setDeliveryTemplateId(generateId());
o.setUnit(RandomUtil.randomInt(1,20)); // 限制商品单位范围
o.setSort(RandomUtil.randomInt(1,100)); // 限制排序范围
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setVirtualSalesCount(generaInt()); // 限制范围为正整数
o.setActivityOrders(newArrayList(1,3,2,4,5)); // 活动排序
o.setPrice(generaInt()); // 限制范围为正整数
o.setMarketPrice(generaInt()); // 限制范围为正整数
o.setCostPrice(generaInt()); // 限制范围为正整数
o.setStock(11); // 限制范围为正整数
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setSalesCount(generaInt()); // 限制范围为正整数
o.setBrowseCount(generaInt()); // 限制范围为正整数
}), randomPojo(ProductSpuDO.class,o->{
o.setCategoryId(generateId());
o.setBrandId(generateId());
o.setDeliveryTemplateId(generateId());
o.setUnit(RandomUtil.randomInt(1,20)); // 限制商品单位范围
o.setSort(RandomUtil.randomInt(1,100)); // 限制排序范围
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setVirtualSalesCount(generaInt()); // 限制范围为正整数
o.setActivityOrders(newArrayList(1,3,2,4,5)); // 活动排序
o.setPrice(generaInt()); // 限制范围为正整数
o.setMarketPrice(generaInt()); // 限制范围为正整数
o.setCostPrice(generaInt()); // 限制范围为正整数
o.setStock(11); // 限制范围为正整数
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setSalesCount(generaInt()); // 限制范围为正整数
o.setBrowseCount(generaInt()); // 限制范围为正整数
}));
productSpuMapper.insertBatch(createReqVOs);
// 调用
ProductSpuPageReqVO productSpuPageReqVO = new ProductSpuPageReqVO();
//productSpuPageReqVO.setAlarmStock(true);
productSpuPageReqVO.setTabType(ProductSpuPageTabEnum.ALERT_STOCK.getType());
PageResult<ProductSpuDO> spuPage = productSpuService.getSpuPage(productSpuPageReqVO);
@@ -155,76 +329,67 @@ public class ProductSpuServiceImplTest extends BaseDbUnitTest {
@Test
void getSpuPage_alarmStock() {
// mock 数据
Long brandId = generateId();
Long categoryId = generateId();
String code = generateNo();
// 准备参数
ProductSpuDO createReqVO = randomPojo(ProductSpuDO.class, o->{
o.setStatus(ProductSpuStatusEnum.ENABLE.getStatus());
//o.setTotalStock(500);
//o.setMinPrice(1); // TODO ProductSpuDO中已没有相关属性
//o.setMaxPrice(50);
o.setMarketPrice(25);
o.setBrandId(brandId);
o.setCategoryId(categoryId);
//o.setClickCount(100);
//o.setCode(code); // TODO ProductSpuDO中已没有相关属性
o.setDescription("测试商品");
o.setSliderPicUrls(new ArrayList<>());
o.setName("测试");
o.setSalesCount(100);
//o.setSellPoint("超级加倍");
//o.setShowStock(true); // TODO ProductSpuDO中已没有相关属性
o.setVideoUrl("");
});
productSpuMapper.insert(createReqVO);
//Set<Long> alarmStockSpuIds = SetUtils.asSet(createReqVO.getId()); TODO 查询接口已改变没有使用到这个变量
List<ProductSkuDO> productSpuDOS = Arrays.asList(randomPojo(ProductSkuDO.class, o -> {
o.setSpuId(createReqVO.getId());
}), randomPojo(ProductSkuDO.class, o -> {
o.setSpuId(createReqVO.getId());
ArrayList<ProductSpuDO> createReqVOs = Lists.newArrayList(randomPojo(ProductSpuDO.class,o->{
o.setCategoryId(generateId());
o.setBrandId(generateId());
o.setDeliveryTemplateId(generateId());
o.setUnit(RandomUtil.randomInt(1,20)); // 限制商品单位范围
o.setSort(RandomUtil.randomInt(1,100)); // 限制排序范围
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setVirtualSalesCount(generaInt()); // 限制范围为正整数
o.setActivityOrders(newArrayList(1,3,2,4,5)); // 活动排序
o.setPrice(generaInt()); // 限制范围为正整数
o.setMarketPrice(generaInt()); // 限制范围为正整数
o.setCostPrice(generaInt()); // 限制范围为正整数
o.setStock(5); // 限制范围为正整数
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setSalesCount(generaInt()); // 限制范围为正整数
o.setBrowseCount(generaInt()); // 限制范围为正整数
}), randomPojo(ProductSpuDO.class,o->{
o.setCategoryId(generateId());
o.setBrandId(generateId());
o.setDeliveryTemplateId(generateId());
o.setUnit(RandomUtil.randomInt(1,20)); // 限制商品单位范围
o.setSort(RandomUtil.randomInt(1,100)); // 限制排序范围
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setVirtualSalesCount(generaInt()); // 限制范围为正整数
o.setActivityOrders(newArrayList(1,3,2,4,5)); // 活动排序
o.setPrice(generaInt()); // 限制范围为正整数
o.setMarketPrice(generaInt()); // 限制范围为正整数
o.setCostPrice(generaInt()); // 限制范围为正整数
o.setStock(9); // 限制范围为正整数
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setSalesCount(generaInt()); // 限制范围为正整数
o.setBrowseCount(generaInt()); // 限制范围为正整数
}));
Mockito.when(productSkuService.getSkuListByAlarmStock()).thenReturn(productSpuDOS);
productSpuMapper.insertBatch(createReqVOs);
// 调用
ProductSpuPageReqVO productSpuPageReqVO = new ProductSpuPageReqVO();
//productSpuPageReqVO.setAlarmStock(true);
productSpuPageReqVO.setTabType(ProductSpuPageTabEnum.ALERT_STOCK.getType());
PageResult<ProductSpuDO> spuPage = productSpuService.getSpuPage(productSpuPageReqVO);
PageResult<ProductSpuPageRespVO> result = ProductSpuConvert.INSTANCE.convertPage(productSpuMapper.selectPage(productSpuPageReqVO));
Assertions.assertIterableEquals(result.getList(), spuPage.getList());
assertEquals(spuPage.getTotal(), result.getTotal());
assertEquals(createReqVOs.size(), spuPage.getTotal());
}
@Test
void getSpuPage() {
// mock 数据
Long brandId = generateId();
Long categoryId = generateId();
void testGetSpuPage() {
// 准备参数
ProductSpuDO createReqVO = randomPojo(ProductSpuDO.class, o->{
o.setStatus(ProductSpuStatusEnum.ENABLE.getStatus());
//o.setTotalStock(1);
//o.setMinPrice(1); // TODO ProductSpuDO中已没有相关属性
//o.setMaxPrice(1);
o.setMarketPrice(1);
o.setBrandId(brandId);
o.setSpecType(false);
o.setCategoryId(categoryId);
//o.setClickCount(1); // TODO ProductSpuDO中已没有相关属性
//o.setCode(generateNo());
o.setDescription("测试商品");
o.setSliderPicUrls(new ArrayList<>());
o.setName("测试");
o.setSalesCount(1);
//o.setSellPoint("卖点");
//o.setShowStock(true); // TODO ProductSpuDO中已没有相关属性
ProductSpuDO createReqVO = randomPojo(ProductSpuDO.class,o->{
o.setCategoryId(generateId());
o.setBrandId(generateId());
o.setDeliveryTemplateId(generateId());
o.setUnit(RandomUtil.randomInt(1,20)); // 限制商品单位范围
o.setSort(RandomUtil.randomInt(1,100)); // 限制排序范围
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setVirtualSalesCount(generaInt()); // 限制范围为正整数
o.setActivityOrders(newArrayList(1,3,2,4,5)); // 活动排序
o.setPrice(generaInt()); // 限制范围为正整数
o.setMarketPrice(generaInt()); // 限制范围为正整数
o.setCostPrice(generaInt()); // 限制范围为正整数
o.setStock(generaInt()); // 限制范围为正整数
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setSalesCount(generaInt()); // 限制范围为正整数
o.setBrowseCount(generaInt()); // 限制范围为正整数
});
// 准备参数
@@ -241,45 +406,21 @@ public class ProductSpuServiceImplTest extends BaseDbUnitTest {
// 调用
ProductSpuPageReqVO productSpuPageReqVO = new ProductSpuPageReqVO();
// TODO 已暂时没有相关属性,等用到时再添加
//productSpuPageReqVO.setAlarmStock(false);
//productSpuPageReqVO.setBrandId(brandId);
//productSpuPageReqVO.setStatus(ProductSpuStatusEnum.ENABLE.getStatus());
//productSpuPageReqVO.setCategoryId(categoryId);
// 查询条件 按需打开
//productSpuPageReqVO.setTabType(ProductSpuPageTabEnum.ALERT_STOCK.getType());
//productSpuPageReqVO.setTabType(ProductSpuPageTabEnum.RECYCLE_BIN.getType());
//productSpuPageReqVO.setTabType(ProductSpuPageTabEnum.FOR_SALE.getType());
//productSpuPageReqVO.setTabType(ProductSpuPageTabEnum.IN_WAREHOUSE.getType());
//productSpuPageReqVO.setTabType(ProductSpuPageTabEnum.SOLD_OUT.getType());
//productSpuPageReqVO.setName(createReqVO.getName());
//productSpuPageReqVO.setCategoryId(createReqVO.getCategoryId());
PageResult<ProductSpuDO> spuPage = productSpuService.getSpuPage(productSpuPageReqVO);
PageResult<ProductSpuPageRespVO> result = ProductSpuConvert.INSTANCE.convertPage(productSpuMapper.selectPage(productSpuPageReqVO));
assertEquals(result, spuPage);
PageResult<ProductSpuRespVO> result = ProductSpuConvert.INSTANCE.convertPage(productSpuMapper.selectPage(productSpuPageReqVO));
assertEquals(result.getTotal(), spuPage.getTotal());
}
@Test
void testGetSpuPage() {
// 准备参数
ProductSpuDO createReqVO = randomPojo(ProductSpuDO.class, o -> {
o.setCategoryId(2L);
});
productSpuMapper.insert(createReqVO);
// 调用
AppProductSpuPageReqVO appSpuPageReqVO = new AppProductSpuPageReqVO();
appSpuPageReqVO.setCategoryId(2L);
// PageResult<AppSpuPageItemRespVO> spuPage = productSpuService.getSpuPage(appSpuPageReqVO);
//
// PageResult<ProductSpuDO> result = productSpuMapper.selectPage(
// ProductSpuConvert.INSTANCE.convert(appSpuPageReqVO));
//
// List<AppSpuPageItemRespVO> collect = result.getList()
// .stream()
// .map(ProductSpuConvert.INSTANCE::convertAppResp)
// .collect(Collectors.toList());
//
// Assertions.assertIterableEquals(collect, spuPage.getList());
// assertEquals(spuPage.getTotal(), result.getTotal());
}
/**
* 生成笛卡尔积
*
@@ -316,14 +457,48 @@ public class ProductSpuServiceImplTest extends BaseDbUnitTest {
// 准备参数
Map<Long, Integer> stockIncrCounts = MapUtil.builder(1L, 10).put(2L, -20).build();
// mock 方法(数据) // TODO ProductSpuDO中已没有相关属性
//productSpuMapper.insert(randomPojo(ProductSpuDO.class, o -> o.setId(1L).setTotalStock(20)));
//productSpuMapper.insert(randomPojo(ProductSpuDO.class, o -> o.setId(2L).setTotalStock(30)));
productSpuMapper.insert(randomPojo(ProductSpuDO.class, o ->{
o.setCategoryId(generateId());
o.setBrandId(generateId());
o.setDeliveryTemplateId(generateId());
o.setUnit(RandomUtil.randomInt(1,20)); // 限制商品单位范围
o.setSort(RandomUtil.randomInt(1,100)); // 限制排序范围
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setVirtualSalesCount(generaInt()); // 限制范围为正整数
o.setActivityOrders(newArrayList(1,3,2,4,5)); // 活动排序
o.setPrice(generaInt()); // 限制范围为正整数
o.setMarketPrice(generaInt()); // 限制范围为正整数
o.setCostPrice(generaInt()); // 限制范围为正整数
o.setStock(generaInt()); // 限制范围为正整数
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setSalesCount(generaInt()); // 限制范围为正整数
o.setBrowseCount(generaInt()); // 限制范围为正整数
o.setId(1L).setStock(20);
}));
productSpuMapper.insert(randomPojo(ProductSpuDO.class, o -> {
o.setCategoryId(generateId());
o.setBrandId(generateId());
o.setDeliveryTemplateId(generateId());
o.setUnit(RandomUtil.randomInt(1,20)); // 限制商品单位范围
o.setSort(RandomUtil.randomInt(1,100)); // 限制排序范围
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setVirtualSalesCount(generaInt()); // 限制范围为正整数
o.setActivityOrders(newArrayList(1,3,2,4,5)); // 活动排序
o.setPrice(generaInt()); // 限制范围为正整数
o.setMarketPrice(generaInt()); // 限制范围为正整数
o.setCostPrice(generaInt()); // 限制范围为正整数
o.setStock(generaInt()); // 限制范围为正整数
o.setGiveIntegral(generaInt()); // 限制范围为正整数
o.setSalesCount(generaInt()); // 限制范围为正整数
o.setBrowseCount(generaInt()); // 限制范围为正整数
o.setId(2L).setStock(30);
}));
// 调用
productSpuService.updateSpuStock(stockIncrCounts);
// 断言 // TODO ProductSpuDO中已没有相关属性
//assertEquals(productSpuService.getSpu(1L).getTotalStock(), 30);
//assertEquals(productSpuService.getSpu(2L).getTotalStock(), 10);
assertEquals(productSpuService.getSpu(1L).getStock(), 30);
assertEquals(productSpuService.getSpu(2L).getStock(), 10);
}
}

View File

@@ -1,5 +1,7 @@
DELETE FROM "product_sku";
DELETE FROM "product_spu";
DELETE FROM "product_brand";
DELETE FROM "product_category";
DELETE FROM "product_brand";
DELETE FROM "product_property";
DELETE FROM "product_property_value";
DELETE FROM "product_comment";

View File

@@ -1,88 +1,131 @@
CREATE TABLE IF NOT EXISTS `product_sku` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
`id` bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
`spu_id` bigint NOT NULL COMMENT 'spu编号',
`spu_name` varchar DEFAULT NULL COMMENT '商品 SPU 名字',
`properties` varchar DEFAULT NULL COMMENT '规格值数组-json格式 [{propertId: , valueId: }, {propertId: , valueId: }]',
`price` int NOT NULL DEFAULT '-1' COMMENT '销售价格单位',
`market_price` int DEFAULT NULL COMMENT '市场价',
`properties` varchar(512) DEFAULT NULL COMMENT '属性数组JSON 格式',
`price` int NOT NULL DEFAULT '-1' COMMENT '商品价格单位',
`market_price` int DEFAULT NULL COMMENT '市场价单位',
`cost_price` int NOT NULL DEFAULT '-1' COMMENT '成本价单位 ',
`pic_url` varchar NOT NULL COMMENT '图片地址',
`bar_code` varchar(64) DEFAULT NULL COMMENT 'SKU 的条形码',
`pic_url` varchar(256) NOT NULL COMMENT '图片地址',
`stock` int DEFAULT NULL COMMENT '库存',
`warn_stock` int DEFAULT NULL COMMENT '预警库存',
`volume` double DEFAULT NULL COMMENT '商品体积',
`weight` double DEFAULT NULL COMMENT '商品重量',
`bar_code` varchar DEFAULT NULL COMMENT '条形码',
`status` tinyint DEFAULT NULL COMMENT '状态 0-正常 1-禁用',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`creator` varchar DEFAULT NULL COMMENT '创建人',
`updater` varchar DEFAULT NULL COMMENT '更新人',
`deleted` bit(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
PRIMARY KEY (`id`)
`weight` double DEFAULT NULL COMMENT '商品重量单位kg 千克',
`volume` double DEFAULT NULL COMMENT '商品体积单位m^3 平米',
`sub_commission_first_price` int DEFAULT NULL COMMENT '一级分销的佣金单位',
`sub_commission_second_price` int DEFAULT NULL COMMENT '二级分销的佣金单位',
`sales_count` int DEFAULT NULL COMMENT '商品销量',
"creator" varchar(64) DEFAULT '',
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updater" varchar(64) DEFAULT '',
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE,
"tenant_id" bigint not null default '0',
PRIMARY KEY("id")
) COMMENT '商品sku';
CREATE TABLE IF NOT EXISTS `product_spu` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
`tenant_id` bigint NOT NULL DEFAULT '0' COMMENT '租户编号',
`brand_id` bigint DEFAULT NULL COMMENT '商品品牌编号',
`category_id` bigint NOT NULL COMMENT '分类id',
`spec_type` int NOT NULL COMMENT '规格类型0 单规格 1 多规格',
`code` varchar(128) DEFAULT NULL COMMENT '商品编码',
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '商品 SPU 编号自增',
`name` varchar(128) NOT NULL COMMENT '商品名称',
`sell_point` varchar(128) DEFAULT NULL COMMENT '卖点',
`description` text COMMENT '描述',
`pic_urls` varchar(1024) DEFAULT '' COMMENT '商品轮播图地址数组以逗号分隔最多上传15张',
`video_url` varchar(128) DEFAULT NULL COMMENT '商品视频',
`market_price` int DEFAULT NULL COMMENT '市场价单位使用',
`min_price` int DEFAULT NULL COMMENT '最小价格单位使用',
`max_price` int DEFAULT NULL COMMENT '最大价格单位使用',
`total_stock` int NOT NULL DEFAULT '0' COMMENT '总库存',
`show_stock` int DEFAULT '0' COMMENT '是否展示库存',
`keyword` varchar(256) NOT NULL COMMENT '关键字',
`introduction` varchar(256) NOT NULL COMMENT '商品简介',
`description` text NOT NULL COMMENT '商品详情',
`bar_code` varchar(64) NOT NULL COMMENT '条形码',
`category_id` bigint NOT NULL COMMENT '商品分类编号',
`brand_id` int DEFAULT NULL COMMENT '商品品牌编号',
`pic_url` varchar(256) NOT NULL COMMENT '商品封面图',
`slider_pic_urls` varchar(2000) DEFAULT '' COMMENT '商品轮播图地址\n 数组以逗号分隔\n 最多上传15张',
`video_url` varchar(256) DEFAULT NULL COMMENT '商品视频',
`unit` tinyint NOT NULL COMMENT '单位',
`sort` int NOT NULL DEFAULT '0' COMMENT '排序字段',
`status` tinyint NOT NULL COMMENT '商品状态: 0 上架开启 1 下架禁用-1 回收',
`spec_type` bit(1) NOT NULL COMMENT '规格类型0 单规格 1 多规格',
`price` int NOT NULL DEFAULT '-1' COMMENT '商品价格单位使用',
`market_price` int NOT NULL COMMENT '市场价单位使用',
`cost_price` int NOT NULL DEFAULT '-1' COMMENT '成本价单位 ',
`stock` int NOT NULL DEFAULT '0' COMMENT '库存',
`delivery_template_id` bigint NOT NULL COMMENT '物流配置模板编号',
`recommend_hot` bit(1) NOT NULL COMMENT '是否热卖推荐: 0 默认 1 热卖',
`recommend_benefit` bit(1) NOT NULL COMMENT '是否优惠推荐: 0 默认 1 优选',
`recommend_best` bit(1) NOT NULL COMMENT '是否精品推荐: 0 默认 1 精品',
`recommend_new` bit(1) NOT NULL COMMENT '是否新品推荐: 0 默认 1 新品',
`recommend_good` bit(1) NOT NULL COMMENT '是否优品推荐',
`give_integral` int NOT NULL COMMENT '赠送积分',
`give_coupon_template_ids` varchar(512) DEFAULT '' COMMENT '赠送的优惠劵编号的数组',
`sub_commission_type` bit(1) NOT NULL COMMENT '分销类型',
`activity_orders` varchar(16) NOT NULL DEFAULT '' COMMENT '活动显示排序0=默认, 1=秒杀2=砍价3=拼团',
`sales_count` int DEFAULT '0' COMMENT '商品销量',
`virtual_sales_count` int DEFAULT '0' COMMENT '虚拟销量',
`click_count` int DEFAULT '0' COMMENT '商品点击量',
`status` bit(1) DEFAULT NULL COMMENT '上下架状态 0 上架开启 1 下架禁用-1 回收',
`sort` int NOT NULL DEFAULT '0' COMMENT '排序字段',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`creator` varchar DEFAULT NULL COMMENT '创建人',
`updater` varchar DEFAULT NULL COMMENT '更新人',
`deleted` bit(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
PRIMARY KEY (`id`)
`browse_count` int DEFAULT '0' COMMENT '商品点击量',
"creator" varchar(64) DEFAULT '',
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updater" varchar(64) DEFAULT '',
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE,
"tenant_id" bigint not null default '0',
PRIMARY KEY("id")
) COMMENT '商品spu';
CREATE TABLE IF NOT EXISTS `product_category` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '分类编号',
`parent_id` bigint DEFAULT NULL COMMENT '父分类编号',
`name` varchar(128) NOT NULL COMMENT '分类名称',
`pic_url` varchar DEFAULT NULL COMMENT '分类图',
`big_pic_url` varchar DEFAULT NULL COMMENT 'PC端分类图',
`sort` int NOT NULL DEFAULT '0' COMMENT '排序字段',
`status` bit(1) DEFAULT NULL COMMENT '状态',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`creator` varchar DEFAULT NULL COMMENT '创建人',
`updater` varchar DEFAULT NULL COMMENT '更新人',
`deleted` bit(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
PRIMARY KEY (`id`)
`parent_id` bigint NOT NULL COMMENT '父分类编号',
`name` varchar(255) NOT NULL COMMENT '分类名称',
`pic_url` varchar(255) NOT NULL COMMENT '移动端分类图',
`big_pic_url` varchar(255) DEFAULT NULL COMMENT 'PC 端分类图',
`sort` int DEFAULT '0' COMMENT '分类排序',
`status` tinyint NOT NULL COMMENT '开启状态',
"creator" varchar(64) DEFAULT '',
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updater" varchar(64) DEFAULT '',
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE,
"tenant_id" bigint not null default '0',
PRIMARY KEY("id")
) COMMENT '商品分类';
CREATE TABLE IF NOT EXISTS `product_brand` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '品牌编号',
`name` varchar(128) NOT NULL COMMENT '品牌名称',
`pic_url` varchar DEFAULT NULL COMMENT '品牌图片',
`sort` int NOT NULL DEFAULT '0' COMMENT '排序字段',
`description` varchar(256) NOT NULL DEFAULT '0' COMMENT '品牌描述',
`status` bit(1) DEFAULT NULL COMMENT '状态',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`creator` varchar DEFAULT NULL COMMENT '创建人',
`updater` varchar DEFAULT NULL COMMENT '更新人',
`deleted` bit(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
PRIMARY KEY (`id`)
`name` varchar(255) NOT NULL COMMENT '品牌名称',
`pic_url` varchar(255) NOT NULL COMMENT '品牌图片',
`sort` int DEFAULT '0' COMMENT '品牌排序',
`description` varchar(1024) DEFAULT NULL COMMENT '品牌描述',
`status` tinyint NOT NULL COMMENT '状态',
"creator" varchar(64) DEFAULT '',
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updater" varchar(64) DEFAULT '',
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE,
"tenant_id" bigint not null default '0',
PRIMARY KEY("id")
) COMMENT '商品品牌';
CREATE TABLE IF NOT EXISTS `product_property` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(64) DEFAULT NULL COMMENT '规格名称',
`status` tinyint DEFAULT NULL COMMENT '状态 0 开启 1 禁用',
"creator" varchar(64) DEFAULT '',
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updater" varchar(64) DEFAULT '',
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE,
"tenant_id" bigint not null default '0',
`remark` varchar(255) DEFAULT NULL COMMENT '备注',
PRIMARY KEY("id")
) COMMENT '规格名称';
CREATE TABLE IF NOT EXISTS `product_property_value` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
`property_id` bigint DEFAULT NULL COMMENT '规格键id',
`name` varchar(128) DEFAULT NULL COMMENT '规格值名字',
`status` tinyint DEFAULT NULL COMMENT '状态 1 开启 2 禁用',
"creator" varchar(64) DEFAULT '',
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updater" varchar(64) DEFAULT '',
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE,
"tenant_id" bigint not null default '0',
`remark` varchar(255) DEFAULT NULL COMMENT '备注',
PRIMARY KEY("id")
) COMMENT '规格值';
CREATE TABLE IF NOT EXISTS `product_comment` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '评价编号',
`user_id` bigint NOT NULL COMMENT ' 评价ID 用户编号',
@@ -108,10 +151,11 @@ CREATE TABLE IF NOT EXISTS `product_comment` (
`additional_content` varchar(2000) COMMENT '追加评价内容',
`additional_pic_urls` varchar(1024) COMMENT '追评评价图片地址数组以逗号分隔最多上传9张',
`additional_time` datetime COMMENT '追加评价时间',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`creator` varchar DEFAULT NULL COMMENT '创建人',
`updater` varchar DEFAULT NULL COMMENT '更新人',
`deleted` bit(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
"creator" varchar(64) DEFAULT '',
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updater" varchar(64) DEFAULT '',
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE,
"tenant_id" bigint not null default '0',
PRIMARY KEY (`id`)
) COMMENT '商品评价';