提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.infra.service.codegen; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.ListUtil; |
|
4 |
import cn.hutool.core.map.MapUtil; |
|
5 |
import com.iailab.framework.common.pojo.PageResult; |
|
6 |
import com.iailab.framework.test.core.ut.BaseDbUnitTest; |
|
7 |
import com.iailab.module.infra.controller.admin.codegen.vo.CodegenCreateListReqVO; |
|
8 |
import com.iailab.module.infra.controller.admin.codegen.vo.CodegenUpdateReqVO; |
|
9 |
import com.iailab.module.infra.controller.admin.codegen.vo.column.CodegenColumnSaveReqVO; |
|
10 |
import com.iailab.module.infra.controller.admin.codegen.vo.table.CodegenTablePageReqVO; |
|
11 |
import com.iailab.module.infra.controller.admin.codegen.vo.table.DatabaseTableRespVO; |
|
12 |
import com.iailab.module.infra.dal.dataobject.codegen.CodegenColumnDO; |
|
13 |
import com.iailab.module.infra.dal.dataobject.codegen.CodegenTableDO; |
|
14 |
import com.iailab.module.infra.dal.mysql.codegen.CodegenColumnMapper; |
|
15 |
import com.iailab.module.infra.dal.mysql.codegen.CodegenTableMapper; |
|
16 |
import com.iailab.module.infra.enums.codegen.CodegenFrontTypeEnum; |
|
17 |
import com.iailab.module.infra.enums.codegen.CodegenSceneEnum; |
|
18 |
import com.iailab.module.infra.enums.codegen.CodegenTemplateTypeEnum; |
|
19 |
import com.iailab.module.infra.framework.codegen.config.CodegenProperties; |
|
20 |
import com.iailab.module.infra.service.codegen.inner.CodegenBuilder; |
|
21 |
import com.iailab.module.infra.service.codegen.inner.CodegenEngine; |
|
22 |
import com.iailab.module.infra.service.db.DatabaseTableService; |
|
23 |
import com.iailab.module.system.api.user.AdminUserApi; |
|
24 |
import com.iailab.module.system.api.user.dto.AdminUserRespDTO; |
|
25 |
import com.baomidou.mybatisplus.generator.config.po.TableField; |
|
26 |
import com.baomidou.mybatisplus.generator.config.po.TableInfo; |
|
27 |
import org.junit.jupiter.api.Disabled; |
|
28 |
import org.junit.jupiter.api.Test; |
|
29 |
import org.springframework.boot.test.mock.mockito.MockBean; |
|
30 |
import org.springframework.context.annotation.Import; |
|
31 |
|
|
32 |
import javax.annotation.Resource; |
|
33 |
import java.util.Arrays; |
|
34 |
import java.util.Collections; |
|
35 |
import java.util.List; |
|
36 |
import java.util.Map; |
|
37 |
|
|
38 |
import static com.iailab.framework.common.pojo.CommonResult.success; |
|
39 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; |
|
40 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildTime; |
|
41 |
import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
|
42 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
43 |
import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException; |
|
44 |
import static com.iailab.framework.test.core.util.RandomUtils.*; |
|
45 |
import static com.iailab.module.infra.enums.ErrorCodeConstants.*; |
|
46 |
import static org.junit.jupiter.api.Assertions.*; |
|
47 |
import static org.mockito.ArgumentMatchers.*; |
|
48 |
import static org.mockito.Mockito.mock; |
|
49 |
import static org.mockito.Mockito.when; |
|
50 |
|
|
51 |
/** |
|
52 |
* {@link CodegenServiceImpl} 的单元测试类 |
|
53 |
* |
|
54 |
* @author iailab |
|
55 |
*/ |
|
56 |
@Import(CodegenServiceImpl.class) |
|
57 |
public class CodegenServiceImplTest extends BaseDbUnitTest { |
|
58 |
|
|
59 |
@Resource |
|
60 |
private CodegenServiceImpl codegenService; |
|
61 |
|
|
62 |
@Resource |
|
63 |
private CodegenTableMapper codegenTableMapper; |
|
64 |
@Resource |
|
65 |
private CodegenColumnMapper codegenColumnMapper; |
|
66 |
|
|
67 |
@MockBean |
|
68 |
private DatabaseTableService databaseTableService; |
|
69 |
|
|
70 |
@MockBean |
|
71 |
private AdminUserApi userApi; |
|
72 |
|
|
73 |
@MockBean |
|
74 |
private CodegenBuilder codegenBuilder; |
|
75 |
@MockBean |
|
76 |
private CodegenEngine codegenEngine; |
|
77 |
|
|
78 |
@MockBean |
|
79 |
private CodegenProperties codegenProperties; |
|
80 |
|
|
81 |
@Test |
|
82 |
public void testCreateCodegenList() { |
|
83 |
// 准备参数 |
|
84 |
Long userId = randomLongId(); |
|
85 |
CodegenCreateListReqVO reqVO = randomPojo(CodegenCreateListReqVO.class, |
|
86 |
o -> o.setDataSourceConfigId(1L).setTableNames(Collections.singletonList("t_yunai"))); |
|
87 |
// mock 方法(TableInfo) |
|
88 |
TableInfo tableInfo = mock(TableInfo.class); |
|
89 |
when(databaseTableService.getTable(eq(1L), eq("t_yunai"))) |
|
90 |
.thenReturn(tableInfo); |
|
91 |
when(tableInfo.getComment()).thenReturn("iailab"); |
|
92 |
// mock 方法(TableInfo fields) |
|
93 |
TableField field01 = mock(TableField.class); |
|
94 |
when(field01.getComment()).thenReturn("主键"); |
|
95 |
TableField field02 = mock(TableField.class); |
|
96 |
when(field02.getComment()).thenReturn("名字"); |
|
97 |
List<TableField> fields = Arrays.asList(field01, field02); |
|
98 |
when(tableInfo.getFields()).thenReturn(fields); |
|
99 |
// mock 方法(CodegenTableDO) |
|
100 |
CodegenTableDO table = randomPojo(CodegenTableDO.class); |
|
101 |
when(codegenBuilder.buildTable(same(tableInfo))).thenReturn(table); |
|
102 |
// mock 方法(AdminUserRespDTO) |
|
103 |
AdminUserRespDTO user = randomPojo(AdminUserRespDTO.class, o -> o.setNickname("芋头")); |
|
104 |
when(userApi.getUser(eq(userId))).thenReturn(success(user)); |
|
105 |
// mock 方法(CodegenColumnDO) |
|
106 |
List<CodegenColumnDO> columns = randomPojoList(CodegenColumnDO.class); |
|
107 |
when(codegenBuilder.buildColumns(eq(table.getId()), same(fields))) |
|
108 |
.thenReturn(columns); |
|
109 |
// mock 方法(CodegenProperties) |
|
110 |
when(codegenProperties.getFrontType()).thenReturn(CodegenFrontTypeEnum.VUE3.getType()); |
|
111 |
|
|
112 |
// 调用 |
|
113 |
List<Long> result = codegenService.createCodegenList(userId, reqVO); |
|
114 |
// 断言 |
|
115 |
assertEquals(1, result.size()); |
|
116 |
// 断言(CodegenTableDO) |
|
117 |
CodegenTableDO dbTable = codegenTableMapper.selectList().get(0); |
|
118 |
assertPojoEquals(table, dbTable); |
|
119 |
assertEquals(1L, dbTable.getDataSourceConfigId()); |
|
120 |
assertEquals(CodegenSceneEnum.ADMIN.getScene(), dbTable.getScene()); |
|
121 |
assertEquals(CodegenFrontTypeEnum.VUE3.getType(), dbTable.getFrontType()); |
|
122 |
assertEquals("芋头", dbTable.getAuthor()); |
|
123 |
// 断言(CodegenColumnDO) |
|
124 |
List<CodegenColumnDO> dbColumns = codegenColumnMapper.selectList(); |
|
125 |
assertEquals(columns.size(), dbColumns.size()); |
|
126 |
assertTrue(dbColumns.get(0).getPrimaryKey()); |
|
127 |
for (int i = 0; i < dbColumns.size(); i++) { |
|
128 |
assertPojoEquals(columns.get(i), dbColumns.get(i)); |
|
129 |
} |
|
130 |
} |
|
131 |
|
|
132 |
@Test |
|
133 |
public void testValidateTableInfo() { |
|
134 |
// 情况一 |
|
135 |
assertServiceException(() -> codegenService.validateTableInfo(null), |
|
136 |
CODEGEN_IMPORT_TABLE_NULL); |
|
137 |
// 情况二 |
|
138 |
TableInfo tableInfo = mock(TableInfo.class); |
|
139 |
assertServiceException(() -> codegenService.validateTableInfo(tableInfo), |
|
140 |
CODEGEN_TABLE_INFO_TABLE_COMMENT_IS_NULL); |
|
141 |
// 情况三 |
|
142 |
when(tableInfo.getComment()).thenReturn("iailab"); |
|
143 |
assertServiceException(() -> codegenService.validateTableInfo(tableInfo), |
|
144 |
CODEGEN_IMPORT_COLUMNS_NULL); |
|
145 |
// 情况四 |
|
146 |
TableField field = mock(TableField.class); |
|
147 |
when(field.getName()).thenReturn("name"); |
|
148 |
when(tableInfo.getFields()).thenReturn(Collections.singletonList(field)); |
|
149 |
assertServiceException(() -> codegenService.validateTableInfo(tableInfo), |
|
150 |
CODEGEN_TABLE_INFO_COLUMN_COMMENT_IS_NULL, field.getName()); |
|
151 |
} |
|
152 |
|
|
153 |
@Test |
|
154 |
public void testUpdateCodegen_notExists() { |
|
155 |
// 准备参数 |
|
156 |
CodegenUpdateReqVO updateReqVO = randomPojo(CodegenUpdateReqVO.class); |
|
157 |
// mock 方法 |
|
158 |
|
|
159 |
// 调用,并断言 |
|
160 |
assertServiceException(() -> codegenService.updateCodegen(updateReqVO), |
|
161 |
CODEGEN_TABLE_NOT_EXISTS); |
|
162 |
} |
|
163 |
|
|
164 |
@Test |
|
165 |
public void testUpdateCodegen_sub_masterNotExists() { |
|
166 |
// mock 数据 |
|
167 |
CodegenTableDO table = randomPojo(CodegenTableDO.class, |
|
168 |
o -> o.setTemplateType(CodegenTemplateTypeEnum.SUB.getType()) |
|
169 |
.setScene(CodegenSceneEnum.ADMIN.getScene())); |
|
170 |
codegenTableMapper.insert(table); |
|
171 |
// 准备参数 |
|
172 |
CodegenUpdateReqVO updateReqVO = randomPojo(CodegenUpdateReqVO.class, |
|
173 |
o -> o.getTable().setId(table.getId()) |
|
174 |
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType())); |
|
175 |
|
|
176 |
// 调用,并断言 |
|
177 |
assertServiceException(() -> codegenService.updateCodegen(updateReqVO), |
|
178 |
CODEGEN_MASTER_TABLE_NOT_EXISTS, updateReqVO.getTable().getMasterTableId()); |
|
179 |
} |
|
180 |
|
|
181 |
@Test |
|
182 |
public void testUpdateCodegen_sub_columnNotExists() { |
|
183 |
// mock 数据 |
|
184 |
CodegenTableDO subTable = randomPojo(CodegenTableDO.class, |
|
185 |
o -> o.setTemplateType(CodegenTemplateTypeEnum.SUB.getType()) |
|
186 |
.setScene(CodegenSceneEnum.ADMIN.getScene())); |
|
187 |
codegenTableMapper.insert(subTable); |
|
188 |
// mock 数据(master) |
|
189 |
CodegenTableDO masterTable = randomPojo(CodegenTableDO.class, |
|
190 |
o -> o.setTemplateType(CodegenTemplateTypeEnum.MASTER_ERP.getType()) |
|
191 |
.setScene(CodegenSceneEnum.ADMIN.getScene())); |
|
192 |
codegenTableMapper.insert(masterTable); |
|
193 |
// 准备参数 |
|
194 |
CodegenUpdateReqVO updateReqVO = randomPojo(CodegenUpdateReqVO.class, |
|
195 |
o -> o.getTable().setId(subTable.getId()) |
|
196 |
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType()) |
|
197 |
.setMasterTableId(masterTable.getId())); |
|
198 |
|
|
199 |
// 调用,并断言 |
|
200 |
assertServiceException(() -> codegenService.updateCodegen(updateReqVO), |
|
201 |
CODEGEN_SUB_COLUMN_NOT_EXISTS, updateReqVO.getTable().getSubJoinColumnId()); |
|
202 |
} |
|
203 |
|
|
204 |
@Test |
|
205 |
public void testUpdateCodegen_success() { |
|
206 |
// mock 数据 |
|
207 |
CodegenTableDO table = randomPojo(CodegenTableDO.class, |
|
208 |
o -> o.setTemplateType(CodegenTemplateTypeEnum.ONE.getType()) |
|
209 |
.setScene(CodegenSceneEnum.ADMIN.getScene())); |
|
210 |
codegenTableMapper.insert(table); |
|
211 |
CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())); |
|
212 |
codegenColumnMapper.insert(column01); |
|
213 |
CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())); |
|
214 |
codegenColumnMapper.insert(column02); |
|
215 |
// 准备参数 |
|
216 |
CodegenUpdateReqVO updateReqVO = randomPojo(CodegenUpdateReqVO.class, |
|
217 |
o -> o.getTable().setId(table.getId()) |
|
218 |
.setTemplateType(CodegenTemplateTypeEnum.ONE.getType()) |
|
219 |
.setScene(CodegenSceneEnum.ADMIN.getScene())); |
|
220 |
CodegenColumnSaveReqVO columnVO01 = randomPojo(CodegenColumnSaveReqVO.class, |
|
221 |
o -> o.setId(column01.getId()).setTableId(table.getId())); |
|
222 |
CodegenColumnSaveReqVO columnVO02 = randomPojo(CodegenColumnSaveReqVO.class, |
|
223 |
o -> o.setId(column02.getId()).setTableId(table.getId())); |
|
224 |
updateReqVO.setColumns(Arrays.asList(columnVO01, columnVO02)); |
|
225 |
|
|
226 |
// 调用 |
|
227 |
codegenService.updateCodegen(updateReqVO); |
|
228 |
// 断言 |
|
229 |
CodegenTableDO dbTable = codegenTableMapper.selectById(table.getId()); |
|
230 |
assertPojoEquals(updateReqVO.getTable(), dbTable); |
|
231 |
List<CodegenColumnDO> dbColumns = codegenColumnMapper.selectList(); |
|
232 |
assertEquals(2, dbColumns.size()); |
|
233 |
assertPojoEquals(columnVO01, dbColumns.get(0)); |
|
234 |
assertPojoEquals(columnVO02, dbColumns.get(1)); |
|
235 |
} |
|
236 |
|
|
237 |
@Test |
|
238 |
@Disabled // TODO @iailab:这个单测会随机性失败,需要定位下; |
|
239 |
public void testSyncCodegenFromDB() { |
|
240 |
// mock 数据(CodegenTableDO) |
|
241 |
CodegenTableDO table = randomPojo(CodegenTableDO.class, o -> o.setTableName("t_yunai") |
|
242 |
.setDataSourceConfigId(1L).setScene(CodegenSceneEnum.ADMIN.getScene())); |
|
243 |
codegenTableMapper.insert(table); |
|
244 |
CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()) |
|
245 |
.setColumnName("id")); |
|
246 |
codegenColumnMapper.insert(column01); |
|
247 |
CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()) |
|
248 |
.setColumnName("name")); |
|
249 |
codegenColumnMapper.insert(column02); |
|
250 |
// 准备参数 |
|
251 |
Long tableId = table.getId(); |
|
252 |
// mock 方法(TableInfo) |
|
253 |
TableInfo tableInfo = mock(TableInfo.class); |
|
254 |
when(databaseTableService.getTable(eq(1L), eq("t_yunai"))) |
|
255 |
.thenReturn(tableInfo); |
|
256 |
when(tableInfo.getComment()).thenReturn("iailab"); |
|
257 |
// mock 方法(TableInfo fields) |
|
258 |
TableField field01 = mock(TableField.class); |
|
259 |
when(field01.getComment()).thenReturn("主键"); |
|
260 |
TableField field03 = mock(TableField.class); |
|
261 |
when(field03.getComment()).thenReturn("分类"); |
|
262 |
List<TableField> fields = Arrays.asList(field01, field03); |
|
263 |
when(tableInfo.getFields()).thenReturn(fields); |
|
264 |
when(databaseTableService.getTable(eq(1L), eq("t_yunai"))) |
|
265 |
.thenReturn(tableInfo); |
|
266 |
// mock 方法(CodegenTableDO) |
|
267 |
List<CodegenColumnDO> newColumns = randomPojoList(CodegenColumnDO.class); |
|
268 |
when(codegenBuilder.buildColumns(eq(table.getId()), argThat(tableFields -> { |
|
269 |
assertEquals(2, tableFields.size()); |
|
270 |
assertSame(tableInfo.getFields(), tableFields); |
|
271 |
return true; |
|
272 |
}))).thenReturn(newColumns); |
|
273 |
|
|
274 |
// 调用 |
|
275 |
codegenService.syncCodegenFromDB(tableId); |
|
276 |
// 断言 |
|
277 |
List<CodegenColumnDO> dbColumns = codegenColumnMapper.selectList(); |
|
278 |
assertEquals(newColumns.size(), dbColumns.size()); |
|
279 |
assertPojoEquals(newColumns.get(0), dbColumns.get(0)); |
|
280 |
assertPojoEquals(newColumns.get(1), dbColumns.get(1)); |
|
281 |
} |
|
282 |
|
|
283 |
@Test |
|
284 |
public void testDeleteCodegen_notExists() { |
|
285 |
assertServiceException(() -> codegenService.deleteCodegen(randomLongId()), |
|
286 |
CODEGEN_TABLE_NOT_EXISTS); |
|
287 |
} |
|
288 |
|
|
289 |
@Test |
|
290 |
public void testDeleteCodegen_success() { |
|
291 |
// mock 数据 |
|
292 |
CodegenTableDO table = randomPojo(CodegenTableDO.class, |
|
293 |
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene())); |
|
294 |
codegenTableMapper.insert(table); |
|
295 |
CodegenColumnDO column = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())); |
|
296 |
codegenColumnMapper.insert(column); |
|
297 |
// 准备参数 |
|
298 |
Long tableId = table.getId(); |
|
299 |
|
|
300 |
// 调用 |
|
301 |
codegenService.deleteCodegen(tableId); |
|
302 |
// 断言 |
|
303 |
assertNull(codegenTableMapper.selectById(tableId)); |
|
304 |
assertEquals(0, codegenColumnMapper.selectList().size()); |
|
305 |
} |
|
306 |
|
|
307 |
@Test |
|
308 |
public void testGetCodegenTableList() { |
|
309 |
// mock 数据 |
|
310 |
CodegenTableDO table01 = randomPojo(CodegenTableDO.class, |
|
311 |
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene())); |
|
312 |
codegenTableMapper.insert(table01); |
|
313 |
CodegenTableDO table02 = randomPojo(CodegenTableDO.class, |
|
314 |
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene())); |
|
315 |
codegenTableMapper.insert(table02); |
|
316 |
// 准备参数 |
|
317 |
Long dataSourceConfigId = table01.getDataSourceConfigId(); |
|
318 |
|
|
319 |
// 调用 |
|
320 |
List<CodegenTableDO> result = codegenService.getCodegenTableList(dataSourceConfigId); |
|
321 |
// 断言 |
|
322 |
assertEquals(1, result.size()); |
|
323 |
assertPojoEquals(table01, result.get(0)); |
|
324 |
} |
|
325 |
|
|
326 |
@Test |
|
327 |
public void testGetCodegenTablePage() { |
|
328 |
// mock 数据 |
|
329 |
CodegenTableDO tableDO = randomPojo(CodegenTableDO.class, o -> { |
|
330 |
o.setTableName("t_yunai"); |
|
331 |
o.setTableComment("iailab"); |
|
332 |
o.setClassName("SystemYunai"); |
|
333 |
o.setCreateTime(buildTime(2021, 3, 10)); |
|
334 |
}).setScene(CodegenSceneEnum.ADMIN.getScene()); |
|
335 |
codegenTableMapper.insert(tableDO); |
|
336 |
// 测试 tableName 不匹配 |
|
337 |
codegenTableMapper.insert(cloneIgnoreId(tableDO, o -> o.setTableName(randomString()))); |
|
338 |
// 测试 tableComment 不匹配 |
|
339 |
codegenTableMapper.insert(cloneIgnoreId(tableDO, o -> o.setTableComment(randomString()))); |
|
340 |
// 测试 className 不匹配 |
|
341 |
codegenTableMapper.insert(cloneIgnoreId(tableDO, o -> o.setClassName(randomString()))); |
|
342 |
// 测试 createTime 不匹配 |
|
343 |
codegenTableMapper.insert(cloneIgnoreId(tableDO, logDO -> logDO.setCreateTime(buildTime(2021, 4, 10)))); |
|
344 |
// 准备参数 |
|
345 |
CodegenTablePageReqVO reqVO = new CodegenTablePageReqVO(); |
|
346 |
reqVO.setTableName("yunai"); |
|
347 |
reqVO.setTableComment("芋"); |
|
348 |
reqVO.setClassName("Yunai"); |
|
349 |
reqVO.setCreateTime(buildBetweenTime(2021, 3, 1, 2021, 3, 31)); |
|
350 |
|
|
351 |
// 调用 |
|
352 |
PageResult<CodegenTableDO> pageResult = codegenService.getCodegenTablePage(reqVO); |
|
353 |
// 断言,只查到了一条符合条件的 |
|
354 |
assertEquals(1, pageResult.getTotal()); |
|
355 |
assertEquals(1, pageResult.getList().size()); |
|
356 |
assertPojoEquals(tableDO, pageResult.getList().get(0)); |
|
357 |
} |
|
358 |
|
|
359 |
@Test |
|
360 |
public void testGetCodegenTable() { |
|
361 |
// mock 数据 |
|
362 |
CodegenTableDO tableDO = randomPojo(CodegenTableDO.class, o -> o.setScene(CodegenSceneEnum.ADMIN.getScene())); |
|
363 |
codegenTableMapper.insert(tableDO); |
|
364 |
// 准备参数 |
|
365 |
Long id = tableDO.getId(); |
|
366 |
|
|
367 |
// 调用 |
|
368 |
CodegenTableDO result = codegenService.getCodegenTable(id); |
|
369 |
// 断言 |
|
370 |
assertPojoEquals(tableDO, result); |
|
371 |
} |
|
372 |
|
|
373 |
@Test |
|
374 |
public void testGetCodegenColumnListByTableId() { |
|
375 |
// mock 数据 |
|
376 |
CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class); |
|
377 |
codegenColumnMapper.insert(column01); |
|
378 |
CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class); |
|
379 |
codegenColumnMapper.insert(column02); |
|
380 |
// 准备参数 |
|
381 |
Long tableId = column01.getTableId(); |
|
382 |
|
|
383 |
// 调用 |
|
384 |
List<CodegenColumnDO> result = codegenService.getCodegenColumnListByTableId(tableId); |
|
385 |
// 断言 |
|
386 |
assertEquals(1, result.size()); |
|
387 |
assertPojoEquals(column01, result.get(0)); |
|
388 |
} |
|
389 |
|
|
390 |
@Test |
|
391 |
public void testGenerationCodes_tableNotExists() { |
|
392 |
assertServiceException(() -> codegenService.generationCodes(randomLongId()), |
|
393 |
CODEGEN_TABLE_NOT_EXISTS); |
|
394 |
} |
|
395 |
|
|
396 |
@Test |
|
397 |
public void testGenerationCodes_columnNotExists() { |
|
398 |
// mock 数据(CodegenTableDO) |
|
399 |
CodegenTableDO table = randomPojo(CodegenTableDO.class, |
|
400 |
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene()) |
|
401 |
.setTemplateType(CodegenTemplateTypeEnum.MASTER_NORMAL.getType())); |
|
402 |
codegenTableMapper.insert(table); |
|
403 |
// 准备参数 |
|
404 |
Long tableId = table.getId(); |
|
405 |
|
|
406 |
// 调用,并断言 |
|
407 |
assertServiceException(() -> codegenService.generationCodes(tableId), |
|
408 |
CODEGEN_COLUMN_NOT_EXISTS); |
|
409 |
} |
|
410 |
|
|
411 |
@Test |
|
412 |
public void testGenerationCodes_sub_tableNotExists() { |
|
413 |
// mock 数据(CodegenTableDO) |
|
414 |
CodegenTableDO table = randomPojo(CodegenTableDO.class, |
|
415 |
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene()) |
|
416 |
.setTemplateType(CodegenTemplateTypeEnum.MASTER_NORMAL.getType())); |
|
417 |
codegenTableMapper.insert(table); |
|
418 |
// mock 数据(CodegenColumnDO) |
|
419 |
CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())); |
|
420 |
codegenColumnMapper.insert(column01); |
|
421 |
// 准备参数 |
|
422 |
Long tableId = table.getId(); |
|
423 |
|
|
424 |
// 调用,并断言 |
|
425 |
assertServiceException(() -> codegenService.generationCodes(tableId), |
|
426 |
CODEGEN_MASTER_GENERATION_FAIL_NO_SUB_TABLE); |
|
427 |
} |
|
428 |
|
|
429 |
@Test |
|
430 |
public void testGenerationCodes_sub_columnNotExists() { |
|
431 |
// mock 数据(CodegenTableDO) |
|
432 |
CodegenTableDO table = randomPojo(CodegenTableDO.class, |
|
433 |
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene()) |
|
434 |
.setTemplateType(CodegenTemplateTypeEnum.MASTER_NORMAL.getType())); |
|
435 |
codegenTableMapper.insert(table); |
|
436 |
// mock 数据(CodegenColumnDO) |
|
437 |
CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())); |
|
438 |
codegenColumnMapper.insert(column01); |
|
439 |
// mock 数据(sub CodegenTableDO) |
|
440 |
CodegenTableDO subTable = randomPojo(CodegenTableDO.class, |
|
441 |
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene()) |
|
442 |
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType()) |
|
443 |
.setMasterTableId(table.getId())); |
|
444 |
codegenTableMapper.insert(subTable); |
|
445 |
// 准备参数 |
|
446 |
Long tableId = table.getId(); |
|
447 |
|
|
448 |
// 调用,并断言 |
|
449 |
assertServiceException(() -> codegenService.generationCodes(tableId), |
|
450 |
CODEGEN_SUB_COLUMN_NOT_EXISTS, subTable.getId()); |
|
451 |
} |
|
452 |
|
|
453 |
@Test |
|
454 |
public void testGenerationCodes_one_success() { |
|
455 |
// mock 数据(CodegenTableDO) |
|
456 |
CodegenTableDO table = randomPojo(CodegenTableDO.class, |
|
457 |
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene()) |
|
458 |
.setTemplateType(CodegenTemplateTypeEnum.ONE.getType())); |
|
459 |
codegenTableMapper.insert(table); |
|
460 |
// mock 数据(CodegenColumnDO) |
|
461 |
CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())); |
|
462 |
codegenColumnMapper.insert(column01); |
|
463 |
CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())); |
|
464 |
codegenColumnMapper.insert(column02); |
|
465 |
// mock 执行生成 |
|
466 |
Map<String, String> codes = MapUtil.of(randomString(), randomString()); |
|
467 |
when(codegenEngine.execute(eq(table), argThat(columns -> { |
|
468 |
assertEquals(2, columns.size()); |
|
469 |
assertEquals(column01, columns.get(0)); |
|
470 |
assertEquals(column02, columns.get(1)); |
|
471 |
return true; |
|
472 |
}), isNull(), isNull())).thenReturn(codes); |
|
473 |
// 准备参数 |
|
474 |
Long tableId = table.getId(); |
|
475 |
|
|
476 |
// 调用 |
|
477 |
Map<String, String> result = codegenService.generationCodes(tableId); |
|
478 |
// 断言 |
|
479 |
assertSame(codes, result); |
|
480 |
} |
|
481 |
|
|
482 |
@Test |
|
483 |
public void testGenerationCodes_master_success() { |
|
484 |
// mock 数据(CodegenTableDO) |
|
485 |
CodegenTableDO table = randomPojo(CodegenTableDO.class, |
|
486 |
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene()) |
|
487 |
.setTemplateType(CodegenTemplateTypeEnum.MASTER_NORMAL.getType())); |
|
488 |
codegenTableMapper.insert(table); |
|
489 |
// mock 数据(CodegenColumnDO) |
|
490 |
CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())); |
|
491 |
codegenColumnMapper.insert(column01); |
|
492 |
CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())); |
|
493 |
codegenColumnMapper.insert(column02); |
|
494 |
// mock 数据(sub CodegenTableDO) |
|
495 |
CodegenTableDO subTable = randomPojo(CodegenTableDO.class, |
|
496 |
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene()) |
|
497 |
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType()) |
|
498 |
.setMasterTableId(table.getId()) |
|
499 |
.setSubJoinColumnId(1024L)); |
|
500 |
codegenTableMapper.insert(subTable); |
|
501 |
// mock 数据(sub CodegenColumnDO) |
|
502 |
CodegenColumnDO subColumn01 = randomPojo(CodegenColumnDO.class, o -> o.setId(1024L).setTableId(subTable.getId())); |
|
503 |
codegenColumnMapper.insert(subColumn01); |
|
504 |
// mock 执行生成 |
|
505 |
Map<String, String> codes = MapUtil.of(randomString(), randomString()); |
|
506 |
when(codegenEngine.execute(eq(table), argThat(columns -> { |
|
507 |
assertEquals(2, columns.size()); |
|
508 |
assertEquals(column01, columns.get(0)); |
|
509 |
assertEquals(column02, columns.get(1)); |
|
510 |
return true; |
|
511 |
}), argThat(tables -> { |
|
512 |
assertEquals(1, tables.size()); |
|
513 |
assertPojoEquals(subTable, tables.get(0)); |
|
514 |
return true; |
|
515 |
}), argThat(columns -> { |
|
516 |
assertEquals(1, columns.size()); |
|
517 |
assertPojoEquals(subColumn01, columns.size()); |
|
518 |
return true; |
|
519 |
}))).thenReturn(codes); |
|
520 |
// 准备参数 |
|
521 |
Long tableId = table.getId(); |
|
522 |
|
|
523 |
// 调用 |
|
524 |
Map<String, String> result = codegenService.generationCodes(tableId); |
|
525 |
// 断言 |
|
526 |
assertSame(codes, result); |
|
527 |
} |
|
528 |
|
|
529 |
@Test |
|
530 |
public void testGetDatabaseTableList() { |
|
531 |
// 准备参数 |
|
532 |
Long dataSourceConfigId = randomLongId(); |
|
533 |
String name = randomString(); |
|
534 |
String comment = randomString(); |
|
535 |
// mock 方法 |
|
536 |
TableInfo tableInfo01 = mock(TableInfo.class); |
|
537 |
when(tableInfo01.getName()).thenReturn("t_yunai"); |
|
538 |
when(tableInfo01.getComment()).thenReturn("iailab"); |
|
539 |
TableInfo tableInfo02 = mock(TableInfo.class); |
|
540 |
when(tableInfo02.getName()).thenReturn("t_yunai_02"); |
|
541 |
when(tableInfo02.getComment()).thenReturn("iailab_02"); |
|
542 |
when(databaseTableService.getTableList(eq(dataSourceConfigId), eq(name), eq(comment))) |
|
543 |
.thenReturn(ListUtil.toList(tableInfo01, tableInfo02)); |
|
544 |
// mock 数据 |
|
545 |
CodegenTableDO tableDO = randomPojo(CodegenTableDO.class, |
|
546 |
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene()) |
|
547 |
.setTableName("t_yunai_02") |
|
548 |
.setDataSourceConfigId(dataSourceConfigId)); |
|
549 |
codegenTableMapper.insert(tableDO); |
|
550 |
|
|
551 |
// 调用 |
|
552 |
List<DatabaseTableRespVO> result = codegenService.getDatabaseTableList(dataSourceConfigId, name, comment); |
|
553 |
// 断言 |
|
554 |
assertEquals(1, result.size()); |
|
555 |
assertEquals("t_yunai", result.get(0).getName()); |
|
556 |
assertEquals("iailab", result.get(0).getComment()); |
|
557 |
} |
|
558 |
|
|
559 |
} |