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