Commit ba4e90b5298c81977a462f360763ddeb88d76a37

Authored by YunaiV
1 parent 0ff786d0

infra:完善 DatabaseTableServiceImpl 单元测试

yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/codegen/CodegenColumnDO.java
... ... @@ -39,27 +39,38 @@ public class CodegenColumnDO extends BaseDO {
39 39  
40 40 /**
41 41 * 字段名
  42 + *
  43 + * 关联 {@link TableField#getName()}
42 44 */
43 45 private String columnName;
44 46 /**
45 47 * 数据库字段类型
  48 + *
46 49 * 关联 {@link TableField.MetaInfo#getJdbcType()}
47 50 */
48 51 private String dataType;
49 52 /**
50 53 * 字段描述
  54 + *
  55 + * 关联 {@link TableField#getComment()}
51 56 */
52 57 private String columnComment;
53 58 /**
54 59 * 是否允许为空
  60 + *
  61 + * 关联 {@link TableField.MetaInfo#isNullable()}
55 62 */
56 63 private Boolean nullable;
57 64 /**
58 65 * 是否主键
  66 + *
  67 + * 关联 {@link TableField#isKeyFlag()}
59 68 */
60 69 private Boolean primaryKey;
61 70 /**
62 71 * 是否自增
  72 + *
  73 + * 关联 {@link TableField#isKeyIdentityFlag()}
63 74 */
64 75 private Boolean autoIncrement;
65 76 /**
... ... @@ -71,12 +82,16 @@ public class CodegenColumnDO extends BaseDO {
71 82  
72 83 /**
73 84 * Java 属性类型
74   - * <p>
  85 + *
75 86 * 例如说 String、Boolean 等等
  87 + *
  88 + * 关联 {@link TableField#getColumnType()}
76 89 */
77 90 private String javaType;
78 91 /**
79 92 * Java 属性名
  93 + *
  94 + * 关联 {@link TableField#getPropertyName()}
80 95 */
81 96 private String javaField;
82 97 /**
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/codegen/CodegenTableDO.java
... ... @@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum;
6 6 import cn.iocoder.yudao.module.infra.enums.codegen.CodegenTemplateTypeEnum;
7 7 import com.baomidou.mybatisplus.annotation.KeySequence;
8 8 import com.baomidou.mybatisplus.annotation.TableName;
  9 +import com.baomidou.mybatisplus.generator.config.po.TableInfo;
9 10 import lombok.Data;
10 11 import lombok.EqualsAndHashCode;
11 12 import lombok.experimental.Accessors;
... ... @@ -44,10 +45,14 @@ public class CodegenTableDO extends BaseDO {
44 45  
45 46 /**
46 47 * 表名称
  48 + *
  49 + * 关联 {@link TableInfo#getName()}
47 50 */
48 51 private String tableName;
49 52 /**
50 53 * 表描述
  54 + *
  55 + * 关联 {@link TableInfo#getComment()}
51 56 */
52 57 private String tableComment;
53 58 /**
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableServiceImpl.java
... ... @@ -41,7 +41,7 @@ public class DatabaseTableServiceImpl implements DatabaseTableService {
41 41 return CollUtil.getFirst(getTableList0(dataSourceConfigId, name));
42 42 }
43 43  
44   - public List<TableInfo> getTableList0(Long dataSourceConfigId, String name) {
  44 + private List<TableInfo> getTableList0(Long dataSourceConfigId, String name) {
45 45 // 获得数据源配置
46 46 DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(dataSourceConfigId);
47 47 Assert.notNull(config, "数据源({}) 不存在!", dataSourceConfigId);
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableServiceImplTest.java 0 → 100644
  1 +package cn.iocoder.yudao.module.infra.service.db;
  2 +
  3 +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
  4 +import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO;
  5 +import com.baomidou.mybatisplus.generator.config.po.TableField;
  6 +import com.baomidou.mybatisplus.generator.config.po.TableInfo;
  7 +import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
  8 +import org.apache.ibatis.type.JdbcType;
  9 +import org.junit.jupiter.api.Test;
  10 +import org.springframework.boot.test.mock.mockito.MockBean;
  11 +import org.springframework.context.annotation.Import;
  12 +
  13 +import javax.annotation.Resource;
  14 +import java.util.List;
  15 +
  16 +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
  17 +import static org.junit.jupiter.api.Assertions.*;
  18 +import static org.mockito.ArgumentMatchers.eq;
  19 +import static org.mockito.Mockito.when;
  20 +
  21 +@Import(DatabaseTableServiceImpl.class)
  22 +public class DatabaseTableServiceImplTest extends BaseDbUnitTest {
  23 +
  24 + @Resource
  25 + private DatabaseTableServiceImpl databaseTableService;
  26 +
  27 + @MockBean
  28 + private DataSourceConfigService dataSourceConfigService;
  29 +
  30 + @Test
  31 + public void testGetTableList() {
  32 + // 准备参数
  33 + Long dataSourceConfigId = randomLongId();
  34 + // mock 方法
  35 + DataSourceConfigDO dataSourceConfig = new DataSourceConfigDO().setUsername("sa").setPassword("")
  36 + .setUrl("jdbc:h2:mem:testdb");
  37 + when(dataSourceConfigService.getDataSourceConfig(eq(dataSourceConfigId)))
  38 + .thenReturn(dataSourceConfig);
  39 +
  40 + // 调用
  41 + List<TableInfo> tables = databaseTableService.getTableList(dataSourceConfigId,
  42 + "config", "参数");
  43 + // 断言
  44 + assertEquals(1, tables.size());
  45 + assertTableInfo(tables.get(0));
  46 + }
  47 +
  48 + @Test
  49 + public void testGetTable() {
  50 + // 准备参数
  51 + Long dataSourceConfigId = randomLongId();
  52 + // mock 方法
  53 + DataSourceConfigDO dataSourceConfig = new DataSourceConfigDO().setUsername("sa").setPassword("")
  54 + .setUrl("jdbc:h2:mem:testdb");
  55 + when(dataSourceConfigService.getDataSourceConfig(eq(dataSourceConfigId)))
  56 + .thenReturn(dataSourceConfig);
  57 +
  58 + // 调用
  59 + TableInfo tableInfo = databaseTableService.getTable(dataSourceConfigId, "infra_config");
  60 + // 断言
  61 + assertTableInfo(tableInfo);
  62 + }
  63 +
  64 + private void assertTableInfo(TableInfo tableInfo) {
  65 + assertEquals("infra_config", tableInfo.getName());
  66 + assertEquals("参数配置表", tableInfo.getComment());
  67 + assertEquals(13, tableInfo.getFields().size());
  68 + // id 字段
  69 + TableField idField = tableInfo.getFields().get(0);
  70 + assertEquals("id", idField.getName());
  71 + assertEquals(JdbcType.BIGINT, idField.getMetaInfo().getJdbcType());
  72 + assertEquals("编号", idField.getComment());
  73 + assertFalse(idField.getMetaInfo().isNullable());
  74 + assertTrue(idField.isKeyFlag());
  75 + assertTrue(idField.isKeyIdentityFlag());
  76 + assertEquals(DbColumnType.LONG, idField.getColumnType());
  77 + assertEquals("id", idField.getPropertyName());
  78 + // name 字段
  79 + TableField nameField = tableInfo.getFields().get(3);
  80 + assertEquals("name", nameField.getName());
  81 + assertEquals(JdbcType.VARCHAR, nameField.getMetaInfo().getJdbcType());
  82 + assertEquals("名字", nameField.getComment());
  83 + assertFalse(nameField.getMetaInfo().isNullable());
  84 + assertFalse(nameField.isKeyFlag());
  85 + assertFalse(nameField.isKeyIdentityFlag());
  86 + assertEquals(DbColumnType.STRING, nameField.getColumnType());
  87 + assertEquals("name", nameField.getPropertyName());
  88 + }
  89 +}
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/resources/sql/create_tables.sql
1 1  
2 2 CREATE TABLE IF NOT EXISTS "infra_config" (
3   - "id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY,
  3 + "id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '编号',
4 4 "category" varchar(50) NOT NULL,
5 5 "type" tinyint NOT NULL,
6   - "name" varchar(100) NOT NULL DEFAULT '',
  6 + "name" varchar(100) NOT NULL DEFAULT '' COMMENT '名字',
7 7 "config_key" varchar(100) NOT NULL DEFAULT '',
8 8 "value" varchar(500) NOT NULL DEFAULT '',
9 9 "visible" bit NOT NULL,
... ...