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

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 String resourcesPath = ""; // 测试文件资源目录
public static final String OS_NAME = System.getProperty("os.name").toLowerCase().replace(" ", ""); // 操作系统名称
@InjectMocks
protected CodegenEngine codegenEngine;
@ -41,6 +44,10 @@ public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest {
@BeforeEach
public void setUp() {
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) {
@ -73,14 +80,14 @@ public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest {
@SuppressWarnings("rawtypes")
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);
assertEquals(asserts.size(), result.size());
// 校验每个文件
asserts.forEach(assertMap -> {
String contentPath = (String) assertMap.get("contentPath");
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 + ":不匹配");
});
}
@ -121,7 +128,7 @@ public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest {
FileUtil.writeUtf8String(fileContent, basePath + "/" + contentPath);
});
// 写入 assert.json 文件
FileUtil.writeUtf8String(JsonUtils.toJsonPrettyString(asserts), basePath +"/assert.json");
FileUtil.writeUtf8String(JsonUtils.toJsonPrettyString(asserts), basePath + "/assert.json");
}
}

View File

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

View File

@ -27,9 +27,10 @@ public class CodegenEngineVue3Test extends CodegenEngineAbstractTest {
// 调用
Map<String, String> result = codegenEngine.execute(table, columns, null, null);
// 生成测试文件
//writeResult(result, resourcesPath + "/vue3_one");
// 断言
assertResult(result, "codegen/vue3_one");
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/codegen/vue3_one");
assertResult(result, "/vue3_one");
}
@Test
@ -42,25 +43,26 @@ public class CodegenEngineVue3Test extends CodegenEngineAbstractTest {
// 调用
Map<String, String> result = codegenEngine.execute(table, columns, null, null);
// 生成测试文件
//writeResult(result, resourcesPath + "/vue3_tree");
// 断言
assertResult(result, "codegen/vue3_tree");
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/codegen/vue3_tree");
assertResult(result, "/vue3_tree");
// writeFile(result, "/Users/yunai/test/demo66.zip");
}
@Test
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
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
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,
@ -86,10 +88,11 @@ public class CodegenEngineVue3Test extends CodegenEngineAbstractTest {
// 调用
Map<String, String> result = codegenEngine.execute(table, columns,
Arrays.asList(contactTable, teacherTable), Arrays.asList(contactColumns, teacherColumns));
// 生成测试文件
//writeResult(result, resourcesPath + 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");
}
}