添加通知公告模块单测

This commit is contained in:
budliang 2021-03-06 19:57:39 +08:00
parent ba34e3c987
commit 4438b6d4ab
2 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,111 @@
package cn.iocoder.dashboard.modules.system.service.notice;
import cn.iocoder.dashboard.BaseSpringBootUnitTest;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.modules.system.controller.notice.vo.SysNoticeCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.notice.vo.SysNoticeUpdateReqVO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.notice.SysNoticeDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.notice.SysNoticeMapper;
import cn.iocoder.dashboard.modules.system.enums.notice.SysNoticeTypeEnum;
import org.junit.jupiter.api.Test;
import javax.annotation.Resource;
import java.util.function.Consumer;
import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.dashboard.util.RandomUtils.randomPojo;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
class SysNoticeServiceImplTest extends BaseSpringBootUnitTest {
@Resource
private SysNoticeService sysNoticeService;
@Resource
private SysNoticeMapper sysNoticeMapper;
@Test
void testPageNotices_success() {
// todo: 待更新
}
@Test
void testGetNotice_success() {
// 插入前置数据
int noticeId = sysNoticeMapper.insert(randomSysNoticeDO());
// 查询
assertNotNull(sysNoticeService.getNotice(new Long(noticeId)));
}
@Test
void testCreateNotice_success() {
// 准备参数
SysNoticeCreateReqVO reqVO = randomSysNoticeCreateReqVO();
// 校验插入是否成功
Long noticeId = sysNoticeService.createNotice(reqVO);
assertNotNull(noticeId);
// 校验插入属性是否正确
SysNoticeDO notice = sysNoticeMapper.selectById(noticeId);
assertPojoEquals(reqVO, notice);
}
@Test
void testUpdateNotice_success() {
// 插入前置数据
int noticeId = sysNoticeMapper.insert(randomSysNoticeDO());
// 准备更新参数
SysNoticeUpdateReqVO reqVO = randomSysNoticeUpdateReqVO(o -> o.setId(new Long(noticeId)));
// 更新
sysNoticeService.updateNotice(reqVO);
// 检验是否更新成功
SysNoticeDO notice = sysNoticeMapper.selectById(noticeId);
assertPojoEquals(reqVO, notice);
}
@Test
void testDeleteNotice_success() {
// 插入前置数据
int noticeId = sysNoticeMapper.insert(randomSysNoticeDO());
// 删除
sysNoticeService.deleteNotice(new Long(noticeId));
// 检查是否删除成功
assertNull(sysNoticeMapper.selectById(noticeId));
}
@SafeVarargs
private static SysNoticeDO randomSysNoticeDO(Consumer<SysNoticeDO>... consumers) {
SysNoticeDO notice = randomPojo(SysNoticeDO.class, consumers);
notice.setType(randomEle(SysNoticeTypeEnum.values()).getType());
notice.setStatus(CommonStatusEnum.ENABLE.getStatus());
return notice;
}
@SafeVarargs
private static SysNoticeUpdateReqVO randomSysNoticeUpdateReqVO(Consumer<SysNoticeUpdateReqVO>... consumers) {
SysNoticeUpdateReqVO reqVO = randomPojo(SysNoticeUpdateReqVO.class, consumers);
reqVO.setType(randomEle(SysNoticeTypeEnum.values()).getType());
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
return reqVO;
}
private static SysNoticeCreateReqVO randomSysNoticeCreateReqVO() {
SysNoticeCreateReqVO reqVO = randomPojo(SysNoticeCreateReqVO.class);
reqVO.setType(randomEle(SysNoticeTypeEnum.values()).getType());
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
return reqVO;
}
}

View File

@ -100,3 +100,17 @@ CREATE TABLE IF NOT EXISTS "sys_menu" (
"deleted" bit NOT NULL DEFAULT FALSE, "deleted" bit NOT NULL DEFAULT FALSE,
PRIMARY KEY ("id") PRIMARY KEY ("id")
) COMMENT '菜单权限表'; ) COMMENT '菜单权限表';
CREATE TABLE IF NOT EXISTS "sys_notice" (
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"title" varchar(50) NOT NULL,
"content" text NOT NULL,
"notice_type" tinyint(4) NOT NULL,
"status" tinyint(4) NOT NULL DEFAULT '0',
"create_by" varchar(64) DEFAULT '',
"create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
"update_by" varchar(64) DEFAULT '',
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT '0',
PRIMARY KEY ("id")
) COMMENT='通知公告表';