潘志宝
2024-11-11 aa1aa68141e3ee33f98cdd785ddc5c244fedc592
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
package com.iailab.module.infra.service.codegen.inner;
 
import com.iailab.framework.test.core.ut.BaseMockitoUnitTest;
import com.iailab.module.infra.dal.dataobject.codegen.CodegenColumnDO;
import com.iailab.module.infra.dal.dataobject.codegen.CodegenTableDO;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import org.apache.ibatis.type.JdbcType;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
 
import java.util.Collections;
import java.util.List;
 
import static com.iailab.framework.test.core.util.RandomUtils.randomLongId;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
 
public class CodegenBuilderTest extends BaseMockitoUnitTest {
 
    @InjectMocks
    private CodegenBuilder codegenBuilder;
 
    @Test
    public void testBuildTable() {
        // 准备参数
        TableInfo tableInfo = mock(TableInfo.class);
        // mock 方法
        when(tableInfo.getName()).thenReturn("system_user");
        when(tableInfo.getComment()).thenReturn("用户");
 
        // 调用
        CodegenTableDO table = codegenBuilder.buildTable(tableInfo);
        // 断言
        assertEquals("system_user", table.getTableName());
        assertEquals("用户", table.getTableComment());
        assertEquals("system", table.getModuleName());
        assertEquals("user", table.getBusinessName());
        assertEquals("User", table.getClassName());
        assertEquals("用户", table.getClassComment());
    }
 
    @Test
    public void testBuildColumns() {
        // 准备参数
        Long tableId = randomLongId();
        TableField tableField = mock(TableField.class);
        List<TableField> tableFields = Collections.singletonList(tableField);
        // mock 方法
        TableField.MetaInfo metaInfo = mock(TableField.MetaInfo.class);
        when(tableField.getMetaInfo()).thenReturn(metaInfo);
        when(metaInfo.getJdbcType()).thenReturn(JdbcType.BIGINT);
        when(tableField.getComment()).thenReturn("编号");
        when(tableField.isKeyFlag()).thenReturn(true);
        IColumnType columnType = mock(IColumnType.class);
        when(tableField.getColumnType()).thenReturn(columnType);
        when(columnType.getType()).thenReturn("Long");
        when(tableField.getName()).thenReturn("id2");
        when(tableField.getPropertyName()).thenReturn("id");
 
        // 调用
        List<CodegenColumnDO> columns = codegenBuilder.buildColumns(tableId, tableFields);
        // 断言
        assertEquals(1, columns.size());
        CodegenColumnDO column = columns.get(0);
        assertEquals(tableId, column.getTableId());
        assertEquals("id2", column.getColumnName());
        assertEquals("BIGINT", column.getDataType());
        assertEquals("编号", column.getColumnComment());
        assertFalse(column.getNullable());
        assertTrue(column.getPrimaryKey());
        assertEquals(1, column.getOrdinalPosition());
        assertEquals("Long", column.getJavaType());
        assertEquals("id", column.getJavaField());
        assertNull(column.getDictType());
        assertNotNull(column.getExample());
        assertFalse(column.getCreateOperation());
        assertTrue(column.getUpdateOperation());
        assertFalse(column.getListOperation());
        assertEquals("=", column.getListOperationCondition());
        assertTrue(column.getListOperationResult());
        assertEquals("input", column.getHtmlType());
    }
 
}