潘志宝
2024-11-11 aa1aa68141e3ee33f98cdd785ddc5c244fedc592
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.infra.service.db;
H 2
3 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
4 import com.iailab.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 com.iailab.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 }