mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
add test for postgre
This commit is contained in:
@@ -42,6 +42,7 @@
|
||||
<hikariCP.version>7.0.2</hikariCP.version>
|
||||
<sqlite.version>3.50.3.0</sqlite.version>
|
||||
<hsqldb.version>2.7.4</hsqldb.version>
|
||||
<postgresql.version>42.7.8</postgresql.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -154,5 +155,11 @@
|
||||
<version>2.26.6</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${postgresql.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -142,4 +143,27 @@ public class NamedSqlTest {
|
||||
assertEquals("select * from user where comment = 'include in text' and id = ?", namedSql.getSql());
|
||||
assertArrayEquals(new int[]{5, 6}, (int[]) namedSql.getParamArray()[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void selectCaseInTest() {
|
||||
final HashMap<String, Object> paramMap = MapUtil.of("number", new int[]{1, 2, 3});
|
||||
|
||||
NamedSql namedSql = new NamedSql("select case when 2 = any(ARRAY[:number]) and 1 in (1) then 1 else 0 end", paramMap);
|
||||
assertEquals("select case when 2 = any(ARRAY[?]) and 1 in (1) then 1 else 0 end", namedSql.getSql());
|
||||
assertArrayEquals(new int[]{1, 2, 3}, (int[])namedSql.getParamArray()[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseInsertMultiRowTest() {
|
||||
// 多行 INSERT 语句
|
||||
final Map<String, Object> paramMap = new LinkedHashMap<>();
|
||||
paramMap.put("user1", new Object[]{1, "looly"});
|
||||
paramMap.put("user2", new Object[]{2, "xxxtea"});
|
||||
|
||||
String sql = "INSERT INTO users (id, name) VALUES (:user1), (:user2)";
|
||||
NamedSql namedSql = new NamedSql(sql, paramMap);
|
||||
|
||||
assertEquals("INSERT INTO users (id, name) VALUES (?), (?)", namedSql.getSql());
|
||||
assertArrayEquals(new Object[]{new Object[]{1, "looly"}, new Object[]{2, "xxxtea"}}, namedSql.getParamArray());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,15 @@
|
||||
package cn.hutool.v7.db;
|
||||
|
||||
import cn.hutool.v7.core.lang.Console;
|
||||
import cn.hutool.v7.core.map.MapUtil;
|
||||
import cn.hutool.v7.db.sql.NamedSql;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* PostgreSQL 单元测试
|
||||
*
|
||||
@@ -33,8 +38,8 @@ public class PostgreTest {
|
||||
public void insertTest() {
|
||||
for (int id = 100; id < 200; id++) {
|
||||
Db.of("postgre").insert(Entity.of("user")//
|
||||
.set("id", id)//
|
||||
.set("name", "测试用户" + id)//
|
||||
.set("id", id)//
|
||||
.set("name", "测试用户" + id)//
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -51,12 +56,52 @@ public class PostgreTest {
|
||||
@Test
|
||||
@Disabled
|
||||
public void upsertTest() {
|
||||
String createTableStr = """
|
||||
CREATE TABLE
|
||||
IF
|
||||
NOT EXISTS "ctest" (
|
||||
"id" serial4,
|
||||
"t1" VARCHAR ( 255 ) COLLATE "pg_catalog"."default",
|
||||
"t2" VARCHAR ( 255 ) COLLATE "pg_catalog"."default",
|
||||
"t3" VARCHAR ( 255 ) COLLATE "pg_catalog"."default",
|
||||
CONSTRAINT "ctest_pkey" PRIMARY KEY ( "id" )\s
|
||||
)
|
||||
""";
|
||||
|
||||
final Db db = Db.of("postgre");
|
||||
db.executeBatch("drop table if exists ctest",
|
||||
"create table if not exists \"ctest\" ( \"id\" serial4, \"t1\" varchar(255) COLLATE \"pg_catalog\".\"default\", \"t2\" varchar(255) COLLATE \"pg_catalog\".\"default\", \"t3\" varchar(255) COLLATE \"pg_catalog\".\"default\", CONSTRAINT \"ctest_pkey\" PRIMARY KEY (\"id\") ) ");
|
||||
db.executeBatch("drop table if exists ctest", createTableStr);
|
||||
db.insert(Entity.of("ctest").set("id", 1).set("t1", "111").set("t2", "222").set("t3", "333"));
|
||||
db.upsert(Entity.of("ctest").set("id", 1).set("t1", "new111").set("t2", "new222").set("t3", "bew333"),"id");
|
||||
final Entity et=db.get(Entity.of("ctest").set("id", 1));
|
||||
Assertions.assertEquals("new111",et.getStr("t1"));
|
||||
db.upsert(Entity.of("ctest").set("id", 1).set("t1", "new111").set("t2", "new222").set("t3", "bew333"), "id");
|
||||
final Entity et = db.get(Entity.of("ctest").set("id", 1));
|
||||
Assertions.assertEquals("new111", et.getStr("t1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
void selectInTest() {
|
||||
final Db db = Db.of("postgre");
|
||||
final List<Entity> query = db.query("select * from ctest where id in(?, ?, ?)", 1, 2, 3);
|
||||
Console.log(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://github.com/chinabugotech/hutool/issues/4111">issue#4111</a>
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
void selectCaseWhenTest() {
|
||||
final Db db = Db.of("postgre");
|
||||
final List<Entity> query = db.query("select case when 2 = any(ARRAY[?]) and 1 in (1) then 1 else 0 end", new Object[]{new int[]{1, 2, 3}});
|
||||
Console.log(query);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
void namedSqlWithInTest() {
|
||||
final HashMap<String, Object> paramMap = MapUtil.of("number", new int[]{1, 2, 3});
|
||||
NamedSql namedSql = new NamedSql("select case when 2 = any(ARRAY[:number]) and 1 in (1) then 1 else 0 end", paramMap);
|
||||
final Db db = Db.of("postgre");
|
||||
final List<Entity> query = db.query(namedSql.getSql(), namedSql.getParamArray());
|
||||
Console.log(query);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ pass = 123456
|
||||
remarks = true
|
||||
|
||||
[postgre]
|
||||
url = jdbc:postgresql://Looly.centos:5432/test_hutool
|
||||
url = jdbc:postgresql://localhost:5432/test_hutool
|
||||
user = postgres
|
||||
pass = 123456
|
||||
remarks = true
|
||||
|
||||
Reference in New Issue
Block a user