From a208ba318363cc4eabfff2e5a8424e51e7dc1707 Mon Sep 17 00:00:00 2001 From: Liu Rui Date: Wed, 11 Sep 2024 15:47:06 +0800 Subject: [PATCH] =?UTF-8?q?feat-ITreeAbility-=E5=AF=B9=E5=AE=8C=E6=95=B4?= =?UTF-8?q?=E7=9A=84=E6=A0=91=E8=83=BD=E5=8A=9B=E6=8F=90=E4=BE=9B=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ximatai/muyun/core/TestTreeAbility.java | 88 +++++++++++-------- .../ximatai/muyun/util/TreeBuilderTest.java | 15 ++-- .../muyun/ability/ITableCreateAbility.java | 3 + .../ximatai/muyun/ability/ITreeAbility.java | 14 ++- .../net/ximatai/muyun/model/TreeNode.java | 26 +++--- .../muyun/database/builder/Column.java | 5 ++ .../muyun/database/builder/TableWrapper.java | 7 ++ 7 files changed, 95 insertions(+), 63 deletions(-) diff --git a/my-boot/src/test/java/net/ximatai/muyun/core/TestTreeAbility.java b/my-boot/src/test/java/net/ximatai/muyun/core/TestTreeAbility.java index 95b882c6..e441823e 100644 --- a/my-boot/src/test/java/net/ximatai/muyun/core/TestTreeAbility.java +++ b/my-boot/src/test/java/net/ximatai/muyun/core/TestTreeAbility.java @@ -1,29 +1,27 @@ package net.ximatai.muyun.core; -import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; import io.restassured.common.mapper.TypeRef; import jakarta.inject.Inject; import jakarta.ws.rs.Path; -import net.ximatai.muyun.ability.ISoftDeleteAbility; import net.ximatai.muyun.ability.ITableCreateAbility; +import net.ximatai.muyun.ability.ITreeAbility; import net.ximatai.muyun.ability.curd.std.ICURDAbility; import net.ximatai.muyun.database.IDatabaseAccess; import net.ximatai.muyun.database.builder.Column; import net.ximatai.muyun.database.builder.TableWrapper; -import net.ximatai.muyun.model.PageResult; -import net.ximatai.muyun.testcontainers.PostgresTestResource; +import net.ximatai.muyun.model.TreeNode; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.HashMap; +import java.util.List; import java.util.Map; import static io.restassured.RestAssured.given; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; @QuarkusTest -@QuarkusTestResource(value = PostgresTestResource.class, restrictToAnnotatedClass = true) +//@QuarkusTestResource(value = PostgresTestResource.class, restrictToAnnotatedClass = true) class TestTreeAbility { private String path = "/TestTreeAbility"; @@ -32,26 +30,42 @@ class TestTreeAbility { IDatabaseAccess databaseAccess; @Inject - TestTreeAbilityController testTreeAbilityController; + TestTreeAbilityController testController; + + String aID; + + @BeforeEach + void setUp() { + databaseAccess.execute("TRUNCATE TABLE test.%s".formatted(testController.getMainTable())); + + aID = testController.create(Map.of("v_name", "A")); + var bID = testController.create(Map.of("v_name", "B")); + var aaID = testController.create(Map.of("pid", aID, "v_name", "A.a")); + testController.create(Map.of("pid", aID, "v_name", "A.b")); + var baID = testController.create(Map.of("pid", bID, "v_name", "B.a")); + + var aa1ID = testController.create(Map.of("pid", aaID, "v_name", "A.a.1")); + } @Test - void testDelete() { - Map request = Map.of("name", "test"); - - String id = given() - .contentType("application/json") - .body(request) - .when() - .post("%s/create".formatted(path)) + void testTree() { + List response = given() + .get("%s/tree".formatted(path)) .then() .statusCode(200) .extract() - .response() - .asString(); - //.body(is(id)); + .as(new TypeRef<>() { + + }); + + assertEquals(response.size(), 2); + } - HashMap response = given() - .get("%s/view/%s".formatted(path, id)) + @Test + void testTreeA() { + List response = given() + .queryParam("rootID", aID) + .get("%s/tree".formatted(path)) .then() .statusCode(200) .extract() @@ -59,34 +73,30 @@ void testDelete() { }); - assertNotNull(response.get("name")); - - given().get("%s/delete/%s".formatted(path, id)).then().statusCode(200); - - given().get("/test/view/" + id).then().statusCode(404); - - Map row = (Map) databaseAccess.row("select * from testsoftdelete where id = ?", id); - - assertEquals(true, row.get("b_delete")); + assertEquals(response.size(), 1); + assertEquals(response.get(0).getChildren().size(), 2); + } - PageResult response2 = given() - .contentType("application/json") - .queryParam("noPage", true) - .when() - .get("%s/view".formatted(path)) + @Test + void testTreeANotShowMe() { + List response = given() + .queryParam("rootID", aID) + .queryParam("showMe", false) + .get("%s/tree".formatted(path)) .then() .statusCode(200) .extract() .as(new TypeRef<>() { + }); - assertEquals(0, response2.getTotal()); + assertEquals(response.size(), 2); } } @Path("/TestTreeAbility") -class TestTreeAbilityController extends Scaffold implements ICURDAbility, ITableCreateAbility, ISoftDeleteAbility { +class TestTreeAbilityController extends Scaffold implements ICURDAbility, ITableCreateAbility, ITreeAbility { @Override public String getSchemaName() { @@ -103,9 +113,9 @@ public TableWrapper fitOutTable() { return TableWrapper.withName(getMainTable()) .setSchema(getSchemaName()) .setPrimaryKey(Column.ID_POSTGRES) - .addColumn(Column.of("name").setType("varchar")) + .addColumn(Column.of("v_name").setType("varchar")) + .addColumn(Column.of("pid").setType("varchar")) .addColumn(Column.of("t_create").setDefaultValue("now()")); - } } diff --git a/my-boot/src/test/java/net/ximatai/muyun/util/TreeBuilderTest.java b/my-boot/src/test/java/net/ximatai/muyun/util/TreeBuilderTest.java index cc22b94f..6af19d89 100644 --- a/my-boot/src/test/java/net/ximatai/muyun/util/TreeBuilderTest.java +++ b/my-boot/src/test/java/net/ximatai/muyun/util/TreeBuilderTest.java @@ -9,7 +9,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; - class TreeBuilderTest { static List list = List.of( @@ -27,25 +26,25 @@ class TreeBuilderTest { void testTree() { List tree = TreeBuilder.build("id", "pid", list, null, true, "name", 10); TreeNode nodeA = tree.get(0); - assertEquals("A", nodeA.label()); - assertEquals(2, nodeA.children().size()); - assertEquals("A.a.1", nodeA.children().get(0).children().get(0).label()); + assertEquals("A", nodeA.getLabel()); + assertEquals(2, nodeA.getChildren().size()); + assertEquals("A.a.1", nodeA.getChildren().get(0).getChildren().get(0).getLabel()); } @Test void testTreeA() { List tree = TreeBuilder.build("id", "pid", list, "A", true, "name", 10); TreeNode nodeA = tree.get(0); - assertEquals("A", nodeA.label()); - assertEquals(2, nodeA.children().size()); - assertEquals("A.a.1", nodeA.children().get(0).children().get(0).label()); + assertEquals("A", nodeA.getLabel()); + assertEquals(2, nodeA.getChildren().size()); + assertEquals("A.a.1", nodeA.getChildren().get(0).getChildren().get(0).getLabel()); } @Test void testTreeANotShowMe() { List tree = TreeBuilder.build("id", "pid", list, "A", false, "name", 10); assertEquals(2, tree.size()); - assertEquals("A.a.1", tree.get(0).children().get(0).label()); + assertEquals("A.a.1", tree.get(0).getChildren().get(0).getLabel()); } @Test diff --git a/my-core/src/main/java/net/ximatai/muyun/ability/ITableCreateAbility.java b/my-core/src/main/java/net/ximatai/muyun/ability/ITableCreateAbility.java index b27b98ae..0552cc3b 100644 --- a/my-core/src/main/java/net/ximatai/muyun/ability/ITableCreateAbility.java +++ b/my-core/src/main/java/net/ximatai/muyun/ability/ITableCreateAbility.java @@ -18,6 +18,9 @@ default void create(IDatabaseAccess databaseAccess) { if (this instanceof ISoftDeleteAbility softDeleteAbility) { wrapper.addColumn(softDeleteAbility.getSoftDeleteColumn()); } + if (this instanceof ITreeAbility treeAbility) { + wrapper.addColumn(treeAbility.getParentKeyColumn()); + } new TableBuilder(databaseAccess).build(wrapper); } } diff --git a/my-core/src/main/java/net/ximatai/muyun/ability/ITreeAbility.java b/my-core/src/main/java/net/ximatai/muyun/ability/ITreeAbility.java index e489b438..75002fc7 100644 --- a/my-core/src/main/java/net/ximatai/muyun/ability/ITreeAbility.java +++ b/my-core/src/main/java/net/ximatai/muyun/ability/ITreeAbility.java @@ -4,6 +4,7 @@ import jakarta.ws.rs.Path; import jakarta.ws.rs.QueryParam; import net.ximatai.muyun.ability.curd.std.ISelectAbility; +import net.ximatai.muyun.database.builder.Column; import net.ximatai.muyun.model.TreeNode; import net.ximatai.muyun.util.TreeBuilder; @@ -12,14 +13,18 @@ public interface ITreeAbility extends ISelectAbility, IMetadataAbility { - default String getParentKeyColumn() { - return "pid"; + default Column getParentKeyColumn() { + return Column.TREE_PID; } default String getLabelColumn() { if (getDBTable().contains("v_name")) { return "v_name"; } + + if (getDBTable().contains("v_label")) { + return "v_label"; + } return null; } @@ -33,6 +38,9 @@ default List tree(@QueryParam("rootID") String rootID, if (showMe == null) { showMe = true; } + if (rootID == null) { + showMe = false; + } if (maxLevel == null) { maxLevel = Integer.MAX_VALUE; } @@ -43,7 +51,7 @@ default List tree(@QueryParam("rootID") String rootID, List list = this.view(null, null, true, null).getList(); - return TreeBuilder.build(getPK(), getParentKeyColumn(), list, rootID, showMe, labelColumn, maxLevel); + return TreeBuilder.build(getPK(), getParentKeyColumn().getName(), list, rootID, showMe, labelColumn, maxLevel); } } diff --git a/my-core/src/main/java/net/ximatai/muyun/model/TreeNode.java b/my-core/src/main/java/net/ximatai/muyun/model/TreeNode.java index b6246aef..1c0a320b 100644 --- a/my-core/src/main/java/net/ximatai/muyun/model/TreeNode.java +++ b/my-core/src/main/java/net/ximatai/muyun/model/TreeNode.java @@ -11,37 +11,37 @@ public class TreeNode { public TreeNode() { } - public String id() { + public String getId() { return id; } + public String getLabel() { + return label; + } + + public Object getData() { + return data; + } + + public List getChildren() { + return children; + } + public TreeNode setId(String id) { this.id = id; return this; } - public String label() { - return label; - } - public TreeNode setLabel(String label) { this.label = label; return this; } - public Object data() { - return data; - } - public TreeNode setData(Object data) { this.data = data; return this; } - public List children() { - return children; - } - public TreeNode setChildren(List children) { this.children = children; return this; diff --git a/my-database/src/main/java/net/ximatai/muyun/database/builder/Column.java b/my-database/src/main/java/net/ximatai/muyun/database/builder/Column.java index bf0cad97..6fb4d173 100644 --- a/my-database/src/main/java/net/ximatai/muyun/database/builder/Column.java +++ b/my-database/src/main/java/net/ximatai/muyun/database/builder/Column.java @@ -20,6 +20,9 @@ public class Column { .setType("boolean") .setDefaultValue("false"); + public static final Column TREE_PID = new Column("pid") + .setType("varchar").setIndexed(); + private Column(String name) { this.name = name; this.type = buildTypeWithColumnName(name); @@ -136,6 +139,8 @@ String buildTypeWithColumnName(String name) { if ("id".equals(name)) { type = "varchar"; + } else if ("pid".equals(name)) { + type = "varchar"; } else if (name.startsWith("v_")) { type = "varchar"; } else if (name.startsWith("i_")) { diff --git a/my-database/src/main/java/net/ximatai/muyun/database/builder/TableWrapper.java b/my-database/src/main/java/net/ximatai/muyun/database/builder/TableWrapper.java index 39ffc933..3a6f1e85 100644 --- a/my-database/src/main/java/net/ximatai/muyun/database/builder/TableWrapper.java +++ b/my-database/src/main/java/net/ximatai/muyun/database/builder/TableWrapper.java @@ -88,6 +88,13 @@ public TableWrapper addColumn(Column column) { } columns.add(column); + + if (column.isUnique()) { + addIndex(column.getName(), true); + } else if (column.isIndexed()) { + addIndex(column.getName()); + } + return this; }