infra:完善 DatabaseTableServiceImpl 单元测试

This commit is contained in:
YunaiV 2023-02-04 09:57:40 +08:00
parent d5bd499899
commit 7ef6209c13
5 changed files with 113 additions and 4 deletions

View File

@ -39,27 +39,38 @@ public class CodegenColumnDO extends BaseDO {
/** /**
* 字段名 * 字段名
*
* 关联 {@link TableField#getName()}
*/ */
private String columnName; private String columnName;
/** /**
* 数据库字段类型 * 数据库字段类型
*
* 关联 {@link TableField.MetaInfo#getJdbcType()} * 关联 {@link TableField.MetaInfo#getJdbcType()}
*/ */
private String dataType; private String dataType;
/** /**
* 字段描述 * 字段描述
*
* 关联 {@link TableField#getComment()}
*/ */
private String columnComment; private String columnComment;
/** /**
* 是否允许为空 * 是否允许为空
*
* 关联 {@link TableField.MetaInfo#isNullable()}
*/ */
private Boolean nullable; private Boolean nullable;
/** /**
* 是否主键 * 是否主键
*
* 关联 {@link TableField#isKeyFlag()}
*/ */
private Boolean primaryKey; private Boolean primaryKey;
/** /**
* 是否自增 * 是否自增
*
* 关联 {@link TableField#isKeyIdentityFlag()}
*/ */
private Boolean autoIncrement; private Boolean autoIncrement;
/** /**
@ -71,12 +82,16 @@ public class CodegenColumnDO extends BaseDO {
/** /**
* Java 属性类型 * Java 属性类型
* <p> *
* 例如说 StringBoolean 等等 * 例如说 StringBoolean 等等
*
* 关联 {@link TableField#getColumnType()}
*/ */
private String javaType; private String javaType;
/** /**
* Java 属性名 * Java 属性名
*
* 关联 {@link TableField#getPropertyName()}
*/ */
private String javaField; private String javaField;
/** /**

View File

@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum;
import cn.iocoder.yudao.module.infra.enums.codegen.CodegenTemplateTypeEnum; import cn.iocoder.yudao.module.infra.enums.codegen.CodegenTemplateTypeEnum;
import com.baomidou.mybatisplus.annotation.KeySequence; import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
@ -44,10 +45,14 @@ public class CodegenTableDO extends BaseDO {
/** /**
* 表名称 * 表名称
*
* 关联 {@link TableInfo#getName()}
*/ */
private String tableName; private String tableName;
/** /**
* 表描述 * 表描述
*
* 关联 {@link TableInfo#getComment()}
*/ */
private String tableComment; private String tableComment;
/** /**

View File

@ -41,7 +41,7 @@ public class DatabaseTableServiceImpl implements DatabaseTableService {
return CollUtil.getFirst(getTableList0(dataSourceConfigId, name)); return CollUtil.getFirst(getTableList0(dataSourceConfigId, name));
} }
public List<TableInfo> getTableList0(Long dataSourceConfigId, String name) { private List<TableInfo> getTableList0(Long dataSourceConfigId, String name) {
// 获得数据源配置 // 获得数据源配置
DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(dataSourceConfigId); DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(dataSourceConfigId);
Assert.notNull(config, "数据源({}) 不存在!", dataSourceConfigId); Assert.notNull(config, "数据源({}) 不存在!", dataSourceConfigId);

View File

@ -0,0 +1,89 @@
package cn.iocoder.yudao.module.infra.service.db;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import org.apache.ibatis.type.JdbcType;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import java.util.List;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
@Import(DatabaseTableServiceImpl.class)
public class DatabaseTableServiceImplTest extends BaseDbUnitTest {
@Resource
private DatabaseTableServiceImpl databaseTableService;
@MockBean
private DataSourceConfigService dataSourceConfigService;
@Test
public void testGetTableList() {
// 准备参数
Long dataSourceConfigId = randomLongId();
// mock 方法
DataSourceConfigDO dataSourceConfig = new DataSourceConfigDO().setUsername("sa").setPassword("")
.setUrl("jdbc:h2:mem:testdb");
when(dataSourceConfigService.getDataSourceConfig(eq(dataSourceConfigId)))
.thenReturn(dataSourceConfig);
// 调用
List<TableInfo> tables = databaseTableService.getTableList(dataSourceConfigId,
"config", "参数");
// 断言
assertEquals(1, tables.size());
assertTableInfo(tables.get(0));
}
@Test
public void testGetTable() {
// 准备参数
Long dataSourceConfigId = randomLongId();
// mock 方法
DataSourceConfigDO dataSourceConfig = new DataSourceConfigDO().setUsername("sa").setPassword("")
.setUrl("jdbc:h2:mem:testdb");
when(dataSourceConfigService.getDataSourceConfig(eq(dataSourceConfigId)))
.thenReturn(dataSourceConfig);
// 调用
TableInfo tableInfo = databaseTableService.getTable(dataSourceConfigId, "infra_config");
// 断言
assertTableInfo(tableInfo);
}
private void assertTableInfo(TableInfo tableInfo) {
assertEquals("infra_config", tableInfo.getName());
assertEquals("参数配置表", tableInfo.getComment());
assertEquals(13, tableInfo.getFields().size());
// id 字段
TableField idField = tableInfo.getFields().get(0);
assertEquals("id", idField.getName());
assertEquals(JdbcType.BIGINT, idField.getMetaInfo().getJdbcType());
assertEquals("编号", idField.getComment());
assertFalse(idField.getMetaInfo().isNullable());
assertTrue(idField.isKeyFlag());
assertTrue(idField.isKeyIdentityFlag());
assertEquals(DbColumnType.LONG, idField.getColumnType());
assertEquals("id", idField.getPropertyName());
// name 字段
TableField nameField = tableInfo.getFields().get(3);
assertEquals("name", nameField.getName());
assertEquals(JdbcType.VARCHAR, nameField.getMetaInfo().getJdbcType());
assertEquals("名字", nameField.getComment());
assertFalse(nameField.getMetaInfo().isNullable());
assertFalse(nameField.isKeyFlag());
assertFalse(nameField.isKeyIdentityFlag());
assertEquals(DbColumnType.STRING, nameField.getColumnType());
assertEquals("name", nameField.getPropertyName());
}
}

View File

@ -1,9 +1,9 @@
CREATE TABLE IF NOT EXISTS "infra_config" ( CREATE TABLE IF NOT EXISTS "infra_config" (
"id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY, "id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '编号',
"category" varchar(50) NOT NULL, "category" varchar(50) NOT NULL,
"type" tinyint NOT NULL, "type" tinyint NOT NULL,
"name" varchar(100) NOT NULL DEFAULT '', "name" varchar(100) NOT NULL DEFAULT '' COMMENT '名字',
"config_key" varchar(100) NOT NULL DEFAULT '', "config_key" varchar(100) NOT NULL DEFAULT '',
"value" varchar(500) NOT NULL DEFAULT '', "value" varchar(500) NOT NULL DEFAULT '',
"visible" bit NOT NULL, "visible" bit NOT NULL,