mall:完善商品品牌的前后端实现
parent
efd4942129
commit
fa9823161e
@ -1,45 +0,0 @@
|
||||
package cn.iocoder.yudao.module.product.controller.admin.brand.vo;
|
||||
|
||||
import lombok.*;
|
||||
import java.util.*;
|
||||
import io.swagger.annotations.*;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
|
||||
import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
|
||||
|
||||
|
||||
/**
|
||||
* 品牌 Excel VO
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Data
|
||||
public class BrandExcelVO {
|
||||
|
||||
@ExcelProperty("品牌编号")
|
||||
private Long id;
|
||||
|
||||
@ExcelProperty("分类编号")
|
||||
private Long categoryId;
|
||||
|
||||
@ExcelProperty("品牌名称")
|
||||
private String name;
|
||||
|
||||
@ExcelProperty("品牌图片")
|
||||
private String bannerUrl;
|
||||
|
||||
@ExcelProperty("品牌排序")
|
||||
private Integer sort;
|
||||
|
||||
@ExcelProperty("品牌描述")
|
||||
private String description;
|
||||
|
||||
@ExcelProperty(value = "状态", converter = DictConvert.class)
|
||||
@DictFormat("common_status") // TODO 代码优化:建议设置到对应的 XXXDictTypeConstants 枚举类中
|
||||
private Integer status;
|
||||
|
||||
@ExcelProperty("创建时间")
|
||||
private Date createTime;
|
||||
|
||||
}
|
||||
@ -1,28 +0,0 @@
|
||||
package cn.iocoder.yudao.module.product.controller.admin.brand.vo;
|
||||
|
||||
import lombok.*;
|
||||
import java.util.*;
|
||||
import io.swagger.annotations.*;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
||||
|
||||
@ApiModel(value = "管理后台 - 品牌 Excel 导出 Request VO", description = "参数和 BrandPageReqVO 是一致的")
|
||||
@Data
|
||||
public class BrandExportReqVO {
|
||||
|
||||
@ApiModelProperty(value = "分类编号", example = "1")
|
||||
private Long categoryId;
|
||||
|
||||
@ApiModelProperty(value = "品牌名称", example = "芋道")
|
||||
private String name;
|
||||
|
||||
@ApiModelProperty(value = "状态", example = "0")
|
||||
private Integer status;
|
||||
|
||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
||||
@ApiModelProperty(value = "创建时间")
|
||||
private Date[] createTime;
|
||||
|
||||
}
|
||||
@ -1,14 +1,12 @@
|
||||
package cn.iocoder.yudao.module.product.controller.admin.brand.vo;
|
||||
|
||||
import lombok.*;
|
||||
import java.util.*;
|
||||
import io.swagger.annotations.*;
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
@ApiModel("管理后台 - 品牌创建 Request VO")
|
||||
@ApiModel("管理后台 - 商品品牌创建 Request VO")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
public class BrandCreateReqVO extends BrandBaseVO {
|
||||
public class ProductBrandCreateReqVO extends ProductBrandBaseVO {
|
||||
|
||||
}
|
||||
@ -1,21 +1,22 @@
|
||||
package cn.iocoder.yudao.module.product.controller.admin.brand.vo;
|
||||
|
||||
import lombok.*;
|
||||
import java.util.*;
|
||||
import io.swagger.annotations.*;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
||||
|
||||
@ApiModel("管理后台 - 品牌分页 Request VO")
|
||||
@ApiModel("管理后台 - 商品品牌分页 Request VO")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
public class BrandPageReqVO extends PageParam {
|
||||
|
||||
@ApiModelProperty(value = "分类编号", example = "1")
|
||||
private Long categoryId;
|
||||
public class ProductBrandPageReqVO extends PageParam {
|
||||
|
||||
@ApiModelProperty(value = "品牌名称", example = "芋道")
|
||||
private String name;
|
||||
@ -1,14 +1,18 @@
|
||||
package cn.iocoder.yudao.module.product.controller.admin.brand.vo;
|
||||
|
||||
import lombok.*;
|
||||
import java.util.*;
|
||||
import io.swagger.annotations.*;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@ApiModel("管理后台 - 品牌 Response VO")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
public class BrandRespVO extends BrandBaseVO {
|
||||
public class ProductBrandRespVO extends ProductBrandBaseVO {
|
||||
|
||||
@ApiModelProperty(value = "品牌编号", required = true, example = "1")
|
||||
private Long id;
|
||||
@ -1,15 +1,14 @@
|
||||
package cn.iocoder.yudao.module.product.controller.admin.brand.vo;
|
||||
|
||||
import lombok.*;
|
||||
import java.util.*;
|
||||
import io.swagger.annotations.*;
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
@ApiModel("管理后台 - 品牌更新 Request VO")
|
||||
@ApiModel("管理后台 - 商品品牌更新 Request VO")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
public class BrandUpdateReqVO extends BrandBaseVO {
|
||||
public class ProductBrandUpdateReqVO extends ProductBrandBaseVO {
|
||||
|
||||
@ApiModelProperty(value = "品牌编号", required = true, example = "1")
|
||||
@NotNull(message = "品牌编号不能为空")
|
||||
@ -1,34 +0,0 @@
|
||||
package cn.iocoder.yudao.module.product.convert.brand;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.*;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.brand.BrandDO;
|
||||
|
||||
/**
|
||||
* 品牌 Convert
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Mapper
|
||||
public interface BrandConvert {
|
||||
|
||||
BrandConvert INSTANCE = Mappers.getMapper(BrandConvert.class);
|
||||
|
||||
BrandDO convert(BrandCreateReqVO bean);
|
||||
|
||||
BrandDO convert(BrandUpdateReqVO bean);
|
||||
|
||||
BrandRespVO convert(BrandDO bean);
|
||||
|
||||
List<BrandRespVO> convertList(List<BrandDO> list);
|
||||
|
||||
PageResult<BrandRespVO> convertPage(PageResult<BrandDO> page);
|
||||
|
||||
List<BrandExcelVO> convertList02(List<BrandDO> list);
|
||||
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
package cn.iocoder.yudao.module.product.convert.brand;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandCreateReqVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandRespVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.brand.ProductBrandDO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 品牌 Convert
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Mapper
|
||||
public interface ProductBrandConvert {
|
||||
|
||||
ProductBrandConvert INSTANCE = Mappers.getMapper(ProductBrandConvert.class);
|
||||
|
||||
ProductBrandDO convert(ProductBrandCreateReqVO bean);
|
||||
|
||||
ProductBrandDO convert(ProductBrandUpdateReqVO bean);
|
||||
|
||||
ProductBrandRespVO convert(ProductBrandDO bean);
|
||||
|
||||
List<ProductBrandRespVO> convertList(List<ProductBrandDO> list);
|
||||
|
||||
PageResult<ProductBrandRespVO> convertPage(PageResult<ProductBrandDO> page);
|
||||
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
package cn.iocoder.yudao.module.product.dal.mysql.brand;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.brand.BrandDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.*;
|
||||
|
||||
/**
|
||||
* 品牌 Mapper
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Mapper
|
||||
public interface BrandMapper extends BaseMapperX<BrandDO> {
|
||||
|
||||
default PageResult<BrandDO> selectPage(BrandPageReqVO reqVO) {
|
||||
return selectPage(reqVO, new LambdaQueryWrapperX<BrandDO>()
|
||||
.eqIfPresent(BrandDO::getCategoryId, reqVO.getCategoryId())
|
||||
.likeIfPresent(BrandDO::getName, reqVO.getName())
|
||||
.eqIfPresent(BrandDO::getStatus, reqVO.getStatus())
|
||||
.betweenIfPresent(BrandDO::getCreateTime, reqVO.getCreateTime())
|
||||
.orderByDesc(BrandDO::getId));
|
||||
}
|
||||
|
||||
default List<BrandDO> selectList(BrandExportReqVO reqVO) {
|
||||
return selectList(new LambdaQueryWrapperX<BrandDO>()
|
||||
.eqIfPresent(BrandDO::getCategoryId, reqVO.getCategoryId())
|
||||
.likeIfPresent(BrandDO::getName, reqVO.getName())
|
||||
.eqIfPresent(BrandDO::getStatus, reqVO.getStatus())
|
||||
.betweenIfPresent(BrandDO::getCreateTime, reqVO.getCreateTime())
|
||||
.orderByDesc(BrandDO::getId));
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package cn.iocoder.yudao.module.product.dal.mysql.brand;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandPageReqVO;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.brand.ProductBrandDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface ProductBrandMapper extends BaseMapperX<ProductBrandDO> {
|
||||
|
||||
default PageResult<ProductBrandDO> selectPage(ProductBrandPageReqVO reqVO) {
|
||||
return selectPage(reqVO, new LambdaQueryWrapperX<ProductBrandDO>()
|
||||
.likeIfPresent(ProductBrandDO::getName, reqVO.getName())
|
||||
.eqIfPresent(ProductBrandDO::getStatus, reqVO.getStatus())
|
||||
.betweenIfPresent(ProductBrandDO::getCreateTime, reqVO.getCreateTime())
|
||||
.orderByDesc(ProductBrandDO::getId));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,173 +0,0 @@
|
||||
package cn.iocoder.yudao.module.product.service.brand;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
|
||||
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.*;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.brand.BrandDO;
|
||||
import cn.iocoder.yudao.module.product.dal.mysql.brand.BrandMapper;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import java.util.*;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.*;
|
||||
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* {@link BrandServiceImpl} 的单元测试类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Import(BrandServiceImpl.class)
|
||||
public class BrandServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private BrandServiceImpl brandService;
|
||||
|
||||
@Resource
|
||||
private BrandMapper brandMapper;
|
||||
|
||||
@Test
|
||||
public void testCreateBrand_success() {
|
||||
// 准备参数
|
||||
BrandCreateReqVO reqVO = randomPojo(BrandCreateReqVO.class);
|
||||
|
||||
// 调用
|
||||
Long brandId = brandService.createBrand(reqVO);
|
||||
// 断言
|
||||
assertNotNull(brandId);
|
||||
// 校验记录的属性是否正确
|
||||
BrandDO brand = brandMapper.selectById(brandId);
|
||||
assertPojoEquals(reqVO, brand);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateBrand_success() {
|
||||
// mock 数据
|
||||
BrandDO dbBrand = randomPojo(BrandDO.class);
|
||||
brandMapper.insert(dbBrand);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
BrandUpdateReqVO reqVO = randomPojo(BrandUpdateReqVO.class, o -> {
|
||||
o.setId(dbBrand.getId()); // 设置更新的 ID
|
||||
});
|
||||
|
||||
// 调用
|
||||
brandService.updateBrand(reqVO);
|
||||
// 校验是否更新正确
|
||||
BrandDO brand = brandMapper.selectById(reqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(reqVO, brand);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateBrand_notExists() {
|
||||
// 准备参数
|
||||
BrandUpdateReqVO reqVO = randomPojo(BrandUpdateReqVO.class);
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> brandService.updateBrand(reqVO), BRAND_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteBrand_success() {
|
||||
// mock 数据
|
||||
BrandDO dbBrand = randomPojo(BrandDO.class);
|
||||
brandMapper.insert(dbBrand);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbBrand.getId();
|
||||
|
||||
// 调用
|
||||
brandService.deleteBrand(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(brandMapper.selectById(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteBrand_notExists() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> brandService.deleteBrand(id), BRAND_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
|
||||
public void testGetBrandPage() {
|
||||
// mock 数据
|
||||
BrandDO dbBrand = randomPojo(BrandDO.class, o -> { // 等会查询到
|
||||
o.setCategoryId(null);
|
||||
o.setName(null);
|
||||
o.setStatus(null);
|
||||
o.setCreateTime(null);
|
||||
});
|
||||
brandMapper.insert(dbBrand);
|
||||
// 测试 categoryId 不匹配
|
||||
brandMapper.insert(cloneIgnoreId(dbBrand, o -> o.setCategoryId(null)));
|
||||
// 测试 name 不匹配
|
||||
brandMapper.insert(cloneIgnoreId(dbBrand, o -> o.setName(null)));
|
||||
// 测试 status 不匹配
|
||||
brandMapper.insert(cloneIgnoreId(dbBrand, o -> o.setStatus(null)));
|
||||
// 测试 createTime 不匹配
|
||||
brandMapper.insert(cloneIgnoreId(dbBrand, o -> o.setCreateTime(null)));
|
||||
// 准备参数
|
||||
BrandPageReqVO reqVO = new BrandPageReqVO();
|
||||
reqVO.setCategoryId(null);
|
||||
reqVO.setName(null);
|
||||
reqVO.setStatus(null);
|
||||
reqVO.setCreateTime((new Date[]{}));
|
||||
|
||||
// 调用
|
||||
PageResult<BrandDO> pageResult = brandService.getBrandPage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbBrand, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
|
||||
public void testGetBrandList() {
|
||||
// mock 数据
|
||||
BrandDO dbBrand = randomPojo(BrandDO.class, o -> { // 等会查询到
|
||||
o.setCategoryId(null);
|
||||
o.setName(null);
|
||||
o.setStatus(null);
|
||||
o.setCreateTime(null);
|
||||
});
|
||||
brandMapper.insert(dbBrand);
|
||||
// 测试 categoryId 不匹配
|
||||
brandMapper.insert(cloneIgnoreId(dbBrand, o -> o.setCategoryId(null)));
|
||||
// 测试 name 不匹配
|
||||
brandMapper.insert(cloneIgnoreId(dbBrand, o -> o.setName(null)));
|
||||
// 测试 status 不匹配
|
||||
brandMapper.insert(cloneIgnoreId(dbBrand, o -> o.setStatus(null)));
|
||||
// 测试 createTime 不匹配
|
||||
brandMapper.insert(cloneIgnoreId(dbBrand, o -> o.setCreateTime(null)));
|
||||
// 准备参数
|
||||
BrandExportReqVO reqVO = new BrandExportReqVO();
|
||||
reqVO.setCategoryId(null);
|
||||
reqVO.setName(null);
|
||||
reqVO.setStatus(null);
|
||||
reqVO.setCreateTime((new Date[]{}));
|
||||
|
||||
// 调用
|
||||
List<BrandDO> list = brandService.getBrandList(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(dbBrand, list.get(0));
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,132 @@
|
||||
package cn.iocoder.yudao.module.product.service.brand;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
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.brand.vo.ProductBrandCreateReqVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandPageReqVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.brand.ProductBrandDO;
|
||||
import cn.iocoder.yudao.module.product.dal.mysql.brand.ProductBrandMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime;
|
||||
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.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.enums.ErrorCodeConstants.PRODUCT_BRAND_NOT_EXISTS;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* {@link ProductBrandServiceImpl} 的单元测试类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Import(ProductBrandServiceImpl.class)
|
||||
public class ProductBrandServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private ProductBrandServiceImpl brandService;
|
||||
|
||||
@Resource
|
||||
private ProductBrandMapper brandMapper;
|
||||
|
||||
@Test
|
||||
public void testCreateBrand_success() {
|
||||
// 准备参数
|
||||
ProductBrandCreateReqVO reqVO = randomPojo(ProductBrandCreateReqVO.class);
|
||||
|
||||
// 调用
|
||||
Long brandId = brandService.createProductBrand(reqVO);
|
||||
// 断言
|
||||
assertNotNull(brandId);
|
||||
// 校验记录的属性是否正确
|
||||
ProductBrandDO brand = brandMapper.selectById(brandId);
|
||||
assertPojoEquals(reqVO, brand);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateBrand_success() {
|
||||
// mock 数据
|
||||
ProductBrandDO dbBrand = randomPojo(ProductBrandDO.class);
|
||||
brandMapper.insert(dbBrand);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
ProductBrandUpdateReqVO reqVO = randomPojo(ProductBrandUpdateReqVO.class, o -> {
|
||||
o.setId(dbBrand.getId()); // 设置更新的 ID
|
||||
});
|
||||
|
||||
// 调用
|
||||
brandService.updateBrand(reqVO);
|
||||
// 校验是否更新正确
|
||||
ProductBrandDO brand = brandMapper.selectById(reqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(reqVO, brand);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateBrand_notExists() {
|
||||
// 准备参数
|
||||
ProductBrandUpdateReqVO reqVO = randomPojo(ProductBrandUpdateReqVO.class);
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> brandService.updateBrand(reqVO), PRODUCT_BRAND_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteBrand_success() {
|
||||
// mock 数据
|
||||
ProductBrandDO dbBrand = randomPojo(ProductBrandDO.class);
|
||||
brandMapper.insert(dbBrand);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbBrand.getId();
|
||||
|
||||
// 调用
|
||||
brandService.deleteBrand(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(brandMapper.selectById(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteBrand_notExists() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> brandService.deleteBrand(id), PRODUCT_BRAND_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBrandPage() {
|
||||
// mock 数据
|
||||
ProductBrandDO dbBrand = randomPojo(ProductBrandDO.class, o -> { // 等会查询到
|
||||
o.setName("芋道源码");
|
||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
o.setCreateTime(buildTime(2022, 2, 1));
|
||||
});
|
||||
brandMapper.insert(dbBrand);
|
||||
// 测试 name 不匹配
|
||||
brandMapper.insert(cloneIgnoreId(dbBrand, o -> o.setName("源码")));
|
||||
// 测试 status 不匹配
|
||||
brandMapper.insert(cloneIgnoreId(dbBrand, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
// 测试 createTime 不匹配
|
||||
brandMapper.insert(cloneIgnoreId(dbBrand, o -> o.setCreateTime(buildTime(2022, 3, 1))));
|
||||
// 准备参数
|
||||
ProductBrandPageReqVO reqVO = new ProductBrandPageReqVO();
|
||||
reqVO.setName("芋道");
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
reqVO.setCreateTime((new Date[]{buildTime(2022, 1, 1), buildTime(2022, 2, 25)}));
|
||||
|
||||
// 调用
|
||||
PageResult<ProductBrandDO> pageResult = brandService.getBrandPage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbBrand, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue