infra:完善 DatabaseTableServiceImpl 单元测试

plp
YunaiV 3 years ago
parent 0ff786d0ea
commit ba4e90b529

@ -39,27 +39,38 @@ public class CodegenColumnDO extends BaseDO {
/**
*
*
* {@link TableField#getName()}
*/
private String columnName;
/**
*
*
* {@link TableField.MetaInfo#getJdbcType()}
*/
private String dataType;
/**
*
*
* {@link TableField#getComment()}
*/
private String columnComment;
/**
*
*
* {@link TableField.MetaInfo#isNullable()}
*/
private Boolean nullable;
/**
*
*
* {@link TableField#isKeyFlag()}
*/
private Boolean primaryKey;
/**
*
*
* {@link TableField#isKeyIdentityFlag()}
*/
private Boolean autoIncrement;
/**
@ -71,12 +82,16 @@ public class CodegenColumnDO extends BaseDO {
/**
* Java
* <p>
*
* StringBoolean
*
* {@link TableField#getColumnType()}
*/
private String javaType;
/**
* Java
*
* {@link TableField#getPropertyName()}
*/
private String javaField;
/**

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

@ -41,7 +41,7 @@ public class DatabaseTableServiceImpl implements DatabaseTableService {
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);
Assert.notNull(config, "数据源({}) 不存在!", dataSourceConfigId);

@ -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());
}
}

@ -1,9 +1,9 @@
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,
"type" tinyint NOT NULL,
"name" varchar(100) NOT NULL DEFAULT '',
"name" varchar(100) NOT NULL DEFAULT '' COMMENT '',
"config_key" varchar(100) NOT NULL DEFAULT '',
"value" varchar(500) NOT NULL DEFAULT '',
"visible" bit NOT NULL,

Loading…
Cancel
Save