潘志宝
8 天以前 e54ce4959bca1fdeda01701989f8debd9467d4bd
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.infra.service.codegen.inner;
H 2
3 import com.iailab.framework.test.core.ut.BaseMockitoUnitTest;
4 import com.iailab.module.infra.dal.dataobject.codegen.CodegenColumnDO;
5 import com.iailab.module.infra.dal.dataobject.codegen.CodegenTableDO;
6 import com.baomidou.mybatisplus.generator.config.po.TableField;
7 import com.baomidou.mybatisplus.generator.config.po.TableInfo;
8 import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
9 import org.apache.ibatis.type.JdbcType;
10 import org.junit.jupiter.api.Test;
11 import org.mockito.InjectMocks;
12
13 import java.util.Collections;
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.Mockito.mock;
19 import static org.mockito.Mockito.when;
20
21 public class CodegenBuilderTest extends BaseMockitoUnitTest {
22
23     @InjectMocks
24     private CodegenBuilder codegenBuilder;
25
26     @Test
27     public void testBuildTable() {
28         // 准备参数
29         TableInfo tableInfo = mock(TableInfo.class);
30         // mock 方法
31         when(tableInfo.getName()).thenReturn("system_user");
32         when(tableInfo.getComment()).thenReturn("用户");
33
34         // 调用
35         CodegenTableDO table = codegenBuilder.buildTable(tableInfo);
36         // 断言
37         assertEquals("system_user", table.getTableName());
38         assertEquals("用户", table.getTableComment());
39         assertEquals("system", table.getModuleName());
40         assertEquals("user", table.getBusinessName());
41         assertEquals("User", table.getClassName());
42         assertEquals("用户", table.getClassComment());
43     }
44
45     @Test
46     public void testBuildColumns() {
47         // 准备参数
48         Long tableId = randomLongId();
49         TableField tableField = mock(TableField.class);
50         List<TableField> tableFields = Collections.singletonList(tableField);
51         // mock 方法
52         TableField.MetaInfo metaInfo = mock(TableField.MetaInfo.class);
53         when(tableField.getMetaInfo()).thenReturn(metaInfo);
54         when(metaInfo.getJdbcType()).thenReturn(JdbcType.BIGINT);
55         when(tableField.getComment()).thenReturn("编号");
56         when(tableField.isKeyFlag()).thenReturn(true);
57         IColumnType columnType = mock(IColumnType.class);
58         when(tableField.getColumnType()).thenReturn(columnType);
59         when(columnType.getType()).thenReturn("Long");
60         when(tableField.getName()).thenReturn("id2");
61         when(tableField.getPropertyName()).thenReturn("id");
62
63         // 调用
64         List<CodegenColumnDO> columns = codegenBuilder.buildColumns(tableId, tableFields);
65         // 断言
66         assertEquals(1, columns.size());
67         CodegenColumnDO column = columns.get(0);
68         assertEquals(tableId, column.getTableId());
69         assertEquals("id2", column.getColumnName());
70         assertEquals("BIGINT", column.getDataType());
71         assertEquals("编号", column.getColumnComment());
72         assertFalse(column.getNullable());
73         assertTrue(column.getPrimaryKey());
74         assertEquals(1, column.getOrdinalPosition());
75         assertEquals("Long", column.getJavaType());
76         assertEquals("id", column.getJavaField());
77         assertNull(column.getDictType());
78         assertNotNull(column.getExample());
79         assertFalse(column.getCreateOperation());
80         assertTrue(column.getUpdateOperation());
81         assertFalse(column.getListOperation());
82         assertEquals("=", column.getListOperationCondition());
83         assertTrue(column.getListOperationResult());
84         assertEquals("input", column.getHtmlType());
85     }
86
87 }