Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加数据类型枚举 #30

Merged
merged 5 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package net.ximatai.muyun.test.core;

import io.quarkus.runtime.Startup;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.vertx.core.json.JsonObject;
import jakarta.inject.Inject;
import jakarta.ws.rs.Path;
import net.ximatai.muyun.ability.ITableCreateAbility;
import net.ximatai.muyun.ability.curd.std.ICURDAbility;
import net.ximatai.muyun.core.Scaffold;
import net.ximatai.muyun.database.builder.Column;
import net.ximatai.muyun.database.builder.DataType;
import net.ximatai.muyun.database.builder.TableWrapper;
import net.ximatai.muyun.test.testcontainers.PostgresTestResource;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;

@QuarkusTest
@QuarkusTestResource(value = PostgresTestResource.class, restrictToAnnotatedClass = true)
class TestSetTypeWithEnum {
@Inject
TestSetTypeWithEnumController testSetTypeWithEnumController;

@Test
void testCreat() {
String id = testSetTypeWithEnumController.create(Map.of(
"name", "小红",
"age", 23,
"creat_time", "1988-01-01",
"update_time", "2001-01-01"
));

Map<String, ?> map = testSetTypeWithEnumController.view(id);

assertEquals("小红", map.get("name").toString());
assertEquals(23, map.get("age"));
assertEquals("1988-01-01", map.get("creat_time").toString());
assertEquals("2001-01-01 00:00:00.0", map.get("update_time").toString());

String string = given()
.get("/TestSetTypeWithEnumController/view/" + id)
.then()
.extract()
.asString();

JsonObject entries = new JsonObject(string);
String dataInJson = entries.getString("name");
String dataInJson2 = entries.getString("age");
String dataInJson3 = entries.getString("creat_time");
String dataInJson4 = entries.getString("update_time");

assertEquals("小红", dataInJson);
assertEquals("23", dataInJson2);
assertEquals("1988-01-01", dataInJson3);
assertEquals("2001-01-01 00:00:00", dataInJson4);

}
}

@Startup
@Path("/TestSetTypeWithEnumController")
class TestSetTypeWithEnumController extends Scaffold implements ICURDAbility, ITableCreateAbility {

@Override
public String getSchemaName() {
return "test";
}

@Override
public String getMainTable() {
return "test_table";
}

@Override
public void fitOut(TableWrapper wrapper) {
wrapper
.setPrimaryKey(Column.ID_POSTGRES)
.addColumn(Column.of("name").setType(DataType.VARCHAR))
.addColumn(Column.of("age").setType(DataType.INT))
.addColumn(Column.of("creat_time").setType(DataType.DATE))
.addColumn(Column.of("update_time").setType(DataType.TIMESTAMP)); // 字段名
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package net.ximatai.muyun.test.core;

import io.quarkus.runtime.Startup;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.vertx.core.json.JsonObject;
import jakarta.inject.Inject;
import jakarta.ws.rs.Path;
import net.ximatai.muyun.ability.ITableCreateAbility;
import net.ximatai.muyun.ability.curd.std.ICURDAbility;
import net.ximatai.muyun.core.Scaffold;
import net.ximatai.muyun.database.builder.Column;
import net.ximatai.muyun.database.builder.TableWrapper;
import net.ximatai.muyun.test.testcontainers.PostgresTestResource;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;

@QuarkusTest
@QuarkusTestResource(value = PostgresTestResource.class, restrictToAnnotatedClass = true)
class TestTimeFormatCR {

@Inject
TestTimeFormatCRController testController;

@Test
void testCreate() {
String id = testController.create(Map.of(
"d_test", "1988-01-01",
"t_test2", "2001-01-01"
));

Map<String, ?> map = testController.view(id);

assertEquals("1988-01-01", map.get("d_test").toString());
assertEquals("2001-01-01 00:00:00.0", map.get("t_test2").toString());

String string = given()
.get("/TestTimeFormatCRController/view/" + id)
.then()
.extract()
.asString();

JsonObject entries = new JsonObject(string);
String dataInJson = entries.getString("d_test");
String dataInJson2 = entries.getString("t_test2");

assertEquals("1988-01-01", dataInJson);
assertEquals("2001-01-01 00:00:00", dataInJson2);

}

}

@Startup
@Path("/TestTimeFormatCRController")
class TestTimeFormatCRController extends Scaffold implements ICURDAbility, ITableCreateAbility {

@Override
public String getSchemaName() {
return "test";
}

@Override
public String getMainTable() {
return "test_table";
}

@Override
public void fitOut(TableWrapper wrapper) {
wrapper
.setPrimaryKey(Column.ID_POSTGRES)
.addColumn(Column.of("d_test"))
.addColumn(Column.of("t_test2"));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public Column setType(String type) {
return this;
}

public Column setType(DataType type) {
this.type = type.toString();
return this;
}

public Column setType(Type type) {
this.type = type.toString();
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.ximatai.muyun.database.builder;

public enum DataType {
VARCHAR("varchar"),
INT("int"),
BOOLEAN("boolean"),
TIMESTAMP("timestamp"),
DATE("date"),
NUMERIC("numeric"),
JSON("jsonb"),
VARCHAR_ARRAY("varchar[]");

private final String type;

DataType(String type) {
this.type = type;
}
}