mysql + postgres upsert完成并测试通过

This commit is contained in:
icefairy
2022-01-14 17:07:33 +08:00
parent 64d21372ec
commit e492cf2a73
5 changed files with 98 additions and 8 deletions

View File

@@ -1,6 +1,9 @@
package cn.hutool.db;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ArrayUtil;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
@@ -9,11 +12,16 @@ import java.util.List;
/**
* MySQL操作单元测试
*
* @author looly
*
* @author looly
*/
public class MySQLTest {
@BeforeClass
@Ignore
public static void createTable() throws SQLException {
Db db = Db.use("mysql");
db.executeBatch("drop table if exists testuser", "CREATE TABLE if not exists `testuser` ( `id` int(11) NOT NULL, `account` varchar(255) DEFAULT NULL, `pass` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8");
}
@Test
@Ignore
@@ -34,13 +42,13 @@ public class MySQLTest {
*
* @throws SQLException SQL异常
*/
@Test(expected=SQLException.class)
@Test(expected = SQLException.class)
@Ignore
public void txTest() throws SQLException {
Db.use("mysql").tx(db -> {
int update = db.update(Entity.create("user").set("text", "描述100"), Entity.create().set("id", 100));
db.update(Entity.create("user").set("text", "描述101"), Entity.create().set("id", 101));
if(1 == update) {
if (1 == update) {
// 手动指定异常,然后测试回滚触发
throw new RuntimeException("Error");
}
@@ -64,4 +72,14 @@ public class MySQLTest {
Console.log(all);
}
@Test
@Ignore
public void upsertTest() throws SQLException {
Db db = Db.use("mysql");
db.insert(Entity.create("testuser").set("id", 1).set("account", "ice").set("pass", "123456"));
db.upsert(Entity.create("testuser").set("id", 1).set("account", "icefairy").set("pass", "a123456"));
Entity user = db.get(Entity.create("testuser").set("id", 1));
System.out.println("user======="+user.getStr("account")+"___"+user.getStr("pass"));
Assert.assertEquals(user.getStr("account"), new String("icefairy"));
}
}

View File

@@ -2,6 +2,7 @@ package cn.hutool.db;
import java.sql.SQLException;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@@ -9,9 +10,8 @@ import cn.hutool.core.lang.Console;
/**
* PostgreSQL 单元测试
*
* @author looly
*
* @author looly
*/
public class PostgreTest {
@@ -34,4 +34,16 @@ public class PostgreTest {
Console.log(entity.get("id"));
}
}
@Test
@Ignore
public void upsertTest() throws SQLException {
Db db = Db.use("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.insert(Entity.create("ctest").set("id", 1).set("t1", "111").set("t2", "222").set("t3", "333"));
db.upsert(Entity.create("ctest").set("id", 1).set("t1", "new111").set("t2", "new222").set("t3", "bew333"),"id");
Entity et=db.get(Entity.create("ctest").set("id", 1));
Assert.assertEquals("new111",et.getStr("t1"));
}
}