Skip to content

Commit

Permalink
feat-ITreeAbility-对完整的树能力提供单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Sep 11, 2024
1 parent 9e15ea3 commit a208ba3
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 63 deletions.
88 changes: 49 additions & 39 deletions my-boot/src/test/java/net/ximatai/muyun/core/TestTreeAbility.java
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -32,61 +30,73 @@ 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<String, Object> request = Map.of("name", "test");

String id = given()
.contentType("application/json")
.body(request)
.when()
.post("%s/create".formatted(path))
void testTree() {
List<TreeNode> 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<TreeNode> response = given()
.queryParam("rootID", aID)
.get("%s/tree".formatted(path))
.then()
.statusCode(200)
.extract()
.as(new TypeRef<>() {

});

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<Map> response2 = given()
.contentType("application/json")
.queryParam("noPage", true)
.when()
.get("%s/view".formatted(path))
@Test
void testTreeANotShowMe() {
List<TreeNode> 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() {
Expand All @@ -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()"));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;


class TreeBuilderTest {

static List list = List.of(
Expand All @@ -27,25 +26,25 @@ class TreeBuilderTest {
void testTree() {
List<TreeNode> 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<TreeNode> 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<TreeNode> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
14 changes: 11 additions & 3 deletions my-core/src/main/java/net/ximatai/muyun/ability/ITreeAbility.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand All @@ -33,6 +38,9 @@ default List<TreeNode> tree(@QueryParam("rootID") String rootID,
if (showMe == null) {
showMe = true;
}
if (rootID == null) {
showMe = false;
}
if (maxLevel == null) {
maxLevel = Integer.MAX_VALUE;
}
Expand All @@ -43,7 +51,7 @@ default List<TreeNode> 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);
}

}
26 changes: 13 additions & 13 deletions my-core/src/main/java/net/ximatai/muyun/model/TreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<TreeNode> 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<TreeNode> children() {
return children;
}

public TreeNode setChildren(List<TreeNode> children) {
this.children = children;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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_")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit a208ba3

Please sign in to comment.