代码生成测试:系统不一样生成的文件也有差异,那就各自生成各自。新增根据操作系统动态获取测试文件资源路径

This commit is contained in:
puhui999 2023-12-06 00:30:18 +08:00
parent 9a898a81fd
commit e0bdc4f4c1
3 changed files with 35 additions and 23 deletions

View File

@ -31,6 +31,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
*/ */
public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest { public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest {
public String resourcesPath = ""; // 测试文件资源目录
public static final String OS_NAME = System.getProperty("os.name").toLowerCase().replace(" ", ""); // 操作系统名称
@InjectMocks @InjectMocks
protected CodegenEngine codegenEngine; protected CodegenEngine codegenEngine;
@ -41,6 +44,10 @@ public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest {
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
codegenEngine.initGlobalBindingMap(); codegenEngine.initGlobalBindingMap();
// 获取测试文件 resources 路径
String absolutePath = FileUtil.getAbsolutePath("application-unit-test.yaml");
// 系统不一样生成的文件也有差异那就各自生成各自的
resourcesPath = absolutePath.split("/target")[0] + "/src/test/resources/codegen/" + OS_NAME;
} }
protected static CodegenTableDO getTable(String name) { protected static CodegenTableDO getTable(String name) {
@ -73,14 +80,14 @@ public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
protected static void assertResult(Map<String, String> result, String path) { protected static void assertResult(Map<String, String> result, String path) {
String assertContent = ResourceUtil.readUtf8Str(path + "/assert.json"); String assertContent = ResourceUtil.readUtf8Str("codegen/" + OS_NAME + path + "/assert.json");
List<HashMap> asserts = JsonUtils.parseArray(assertContent, HashMap.class); List<HashMap> asserts = JsonUtils.parseArray(assertContent, HashMap.class);
assertEquals(asserts.size(), result.size()); assertEquals(asserts.size(), result.size());
// 校验每个文件 // 校验每个文件
asserts.forEach(assertMap -> { asserts.forEach(assertMap -> {
String contentPath = (String) assertMap.get("contentPath"); String contentPath = (String) assertMap.get("contentPath");
String filePath = (String) assertMap.get("filePath"); String filePath = (String) assertMap.get("filePath");
String content = ResourceUtil.readUtf8Str(path + "/" + contentPath); String content = ResourceUtil.readUtf8Str("codegen/" + OS_NAME + path + "/" + contentPath);
assertEquals(content, result.get(filePath), filePath + ":不匹配"); assertEquals(content, result.get(filePath), filePath + ":不匹配");
}); });
} }

View File

@ -11,7 +11,6 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
// TODO @puhui999单测需要 fix
/** /**
* {@link CodegenEngine} Vue2 + Element UI 单元测试 * {@link CodegenEngine} Vue2 + Element UI 单元测试
* *
@ -30,9 +29,10 @@ public class CodegenEngineVue2Test extends CodegenEngineAbstractTest {
// 调用 // 调用
Map<String, String> result = codegenEngine.execute(table, columns, null, null); Map<String, String> result = codegenEngine.execute(table, columns, null, null);
// 生成测试文件
//writeResult(result, resourcesPath + "/vue2_one");
// 断言 // 断言
assertResult(result, "codegen/vue2_one"); assertResult(result, "/vue2_one");
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/codegen/vue2_one");
} }
@Test @Test
@ -45,25 +45,26 @@ public class CodegenEngineVue2Test extends CodegenEngineAbstractTest {
// 调用 // 调用
Map<String, String> result = codegenEngine.execute(table, columns, null, null); Map<String, String> result = codegenEngine.execute(table, columns, null, null);
// 生成测试文件
//writeResult(result, resourcesPath + "/vue2_tree");
// 断言 // 断言
assertResult(result, "codegen/vue2_tree"); assertResult(result, "/vue2_tree");
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/codegen/vue2_tree");
// writeFile(result, "/Users/yunai/test/demo66.zip"); // writeFile(result, "/Users/yunai/test/demo66.zip");
} }
@Test @Test
public void testExecute_vue2_master_normal() { public void testExecute_vue2_master_normal() {
testExecute_vue2_master(CodegenTemplateTypeEnum.MASTER_NORMAL, "codegen/vue2_master_normal"); testExecute_vue2_master(CodegenTemplateTypeEnum.MASTER_NORMAL, "/vue2_master_normal");
} }
@Test @Test
public void testExecute_vue2_master_erp() { public void testExecute_vue2_master_erp() {
testExecute_vue2_master(CodegenTemplateTypeEnum.MASTER_ERP, "codegen/vue2_master_erp"); testExecute_vue2_master(CodegenTemplateTypeEnum.MASTER_ERP, "/vue2_master_erp");
} }
@Test @Test
public void testExecute_vue2_master_inner() { public void testExecute_vue2_master_inner() {
testExecute_vue2_master(CodegenTemplateTypeEnum.MASTER_INNER, "codegen/vue2_master_inner"); testExecute_vue2_master(CodegenTemplateTypeEnum.MASTER_INNER, "/vue2_master_inner");
} }
private void testExecute_vue2_master(CodegenTemplateTypeEnum templateType, private void testExecute_vue2_master(CodegenTemplateTypeEnum templateType,
@ -89,9 +90,10 @@ public class CodegenEngineVue2Test extends CodegenEngineAbstractTest {
// 调用 // 调用
Map<String, String> result = codegenEngine.execute(table, columns, Map<String, String> result = codegenEngine.execute(table, columns,
Arrays.asList(contactTable, teacherTable), Arrays.asList(contactColumns, teacherColumns)); Arrays.asList(contactTable, teacherTable), Arrays.asList(contactColumns, teacherColumns));
// 生成测试文件
//writeResult(result, resourcesPath + path);
// 断言 // 断言
assertResult(result, path); assertResult(result, path);
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/" + path);
// writeFile(result, "/Users/yunai/test/demo11.zip"); // writeFile(result, "/Users/yunai/test/demo11.zip");
} }

View File

@ -27,9 +27,10 @@ public class CodegenEngineVue3Test extends CodegenEngineAbstractTest {
// 调用 // 调用
Map<String, String> result = codegenEngine.execute(table, columns, null, null); Map<String, String> result = codegenEngine.execute(table, columns, null, null);
// 生成测试文件
//writeResult(result, resourcesPath + "/vue3_one");
// 断言 // 断言
assertResult(result, "codegen/vue3_one"); assertResult(result, "/vue3_one");
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/codegen/vue3_one");
} }
@Test @Test
@ -42,25 +43,26 @@ public class CodegenEngineVue3Test extends CodegenEngineAbstractTest {
// 调用 // 调用
Map<String, String> result = codegenEngine.execute(table, columns, null, null); Map<String, String> result = codegenEngine.execute(table, columns, null, null);
// 生成测试文件
//writeResult(result, resourcesPath + "/vue3_tree");
// 断言 // 断言
assertResult(result, "codegen/vue3_tree"); assertResult(result, "/vue3_tree");
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/codegen/vue3_tree");
// writeFile(result, "/Users/yunai/test/demo66.zip"); // writeFile(result, "/Users/yunai/test/demo66.zip");
} }
@Test @Test
public void testExecute_vue3_master_normal() { public void testExecute_vue3_master_normal() {
testExecute_vue3_master(CodegenTemplateTypeEnum.MASTER_NORMAL, "codegen/vue3_master_normal"); testExecute_vue3_master(CodegenTemplateTypeEnum.MASTER_NORMAL, "/vue3_master_normal");
} }
@Test @Test
public void testExecute_vue3_master_erp() { public void testExecute_vue3_master_erp() {
testExecute_vue3_master(CodegenTemplateTypeEnum.MASTER_ERP, "codegen/vue3_master_erp"); testExecute_vue3_master(CodegenTemplateTypeEnum.MASTER_ERP, "/vue3_master_erp");
} }
@Test @Test
public void testExecute_vue3_master_inner() { public void testExecute_vue3_master_inner() {
testExecute_vue3_master(CodegenTemplateTypeEnum.MASTER_INNER, "codegen/vue3_master_inner"); testExecute_vue3_master(CodegenTemplateTypeEnum.MASTER_INNER, "/vue3_master_inner");
} }
private void testExecute_vue3_master(CodegenTemplateTypeEnum templateType, private void testExecute_vue3_master(CodegenTemplateTypeEnum templateType,
@ -86,9 +88,10 @@ public class CodegenEngineVue3Test extends CodegenEngineAbstractTest {
// 调用 // 调用
Map<String, String> result = codegenEngine.execute(table, columns, Map<String, String> result = codegenEngine.execute(table, columns,
Arrays.asList(contactTable, teacherTable), Arrays.asList(contactColumns, teacherColumns)); Arrays.asList(contactTable, teacherTable), Arrays.asList(contactColumns, teacherColumns));
// 生成测试文件
//writeResult(result, resourcesPath + path);
// 断言 // 断言
assertResult(result, path); assertResult(result, path);
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/" + path);
// writeFile(result, "/Users/yunai/test/demo11.zip"); // writeFile(result, "/Users/yunai/test/demo11.zip");
} }