From e0bdc4f4c1c5f25aa4930909f96c6a9e669e83a1 Mon Sep 17 00:00:00 2001 From: puhui999 Date: Wed, 6 Dec 2023 00:30:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=EF=BC=9A=E7=B3=BB=E7=BB=9F=E4=B8=8D=E4=B8=80=E6=A0=B7?= =?UTF-8?q?=E7=94=9F=E6=88=90=E7=9A=84=E6=96=87=E4=BB=B6=E4=B9=9F=E6=9C=89?= =?UTF-8?q?=E5=B7=AE=E5=BC=82=EF=BC=8C=E9=82=A3=E5=B0=B1=E5=90=84=E8=87=AA?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=90=84=E8=87=AA=E3=80=82=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=93=8D=E4=BD=9C=E7=B3=BB=E7=BB=9F=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E8=8E=B7=E5=8F=96=E6=B5=8B=E8=AF=95=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=B5=84=E6=BA=90=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inner/CodegenEngineAbstractTest.java | 17 ++++++++++----- .../codegen/inner/CodegenEngineVue2Test.java | 20 ++++++++++-------- .../codegen/inner/CodegenEngineVue3Test.java | 21 +++++++++++-------- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineAbstractTest.java b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineAbstractTest.java index 588f2b08b..f8b778e30 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineAbstractTest.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineAbstractTest.java @@ -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 result, String path) { - String assertContent = ResourceUtil.readUtf8Str(path + "/assert.json"); + String assertContent = ResourceUtil.readUtf8Str("codegen/" + OS_NAME + path + "/assert.json"); List 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 + ":不匹配"); }); } @@ -91,7 +98,7 @@ public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest { * 【调试使用】将生成的代码,写入到文件 * * @param result 生成的代码 - * @param path 写入文件的路径 + * @param path 写入文件的路径 */ protected void writeFile(Map result, String path) { // 生成压缩包 @@ -106,7 +113,7 @@ public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest { /** * 【调试使用】将生成的结果,写入到文件 * - * @param result 生成的代码 + * @param result 生成的代码 * @param basePath 写入文件的路径(绝对路径) */ protected void writeResult(Map result, String basePath) { @@ -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"); } } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineVue2Test.java b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineVue2Test.java index a8be74120..94d24bed1 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineVue2Test.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineVue2Test.java @@ -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 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 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 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"); } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineVue3Test.java b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineVue3Test.java index 56ad78aec..ce745c910 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineVue3Test.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineVue3Test.java @@ -27,9 +27,10 @@ public class CodegenEngineVue3Test extends CodegenEngineAbstractTest { // 调用 Map 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 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 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"); } }