This commit is contained in:
Looly
2022-04-30 22:53:06 +08:00
parent a7ede583ec
commit 40aa543edd
36 changed files with 740 additions and 1189 deletions

View File

@@ -13,7 +13,6 @@ import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.SQLException;
import java.util.List;
/**
@@ -23,91 +22,91 @@ import java.util.List;
*/
public class CRUDTest {
private static final Db db = Db.use("test");
private static final Db db = Db.of("test");
@Test
public void findIsNullTest() throws SQLException {
public void findIsNullTest() {
final List<Entity> results = db.findAll(Entity.create("user").set("age", "is null"));
Assert.assertEquals(0, results.size());
}
@Test
public void findIsNullTest2() throws SQLException {
public void findIsNullTest2() {
final List<Entity> results = db.findAll(Entity.create("user").set("age", "= null"));
Assert.assertEquals(0, results.size());
}
@Test
public void findIsNullTest3() throws SQLException {
public void findIsNullTest3() {
final List<Entity> results = db.findAll(Entity.create("user").set("age", null));
Assert.assertEquals(0, results.size());
}
@Test
public void findBetweenTest() throws SQLException {
public void findBetweenTest() {
final List<Entity> results = db.findAll(Entity.create("user").set("age", "between '18' and '40'"));
Assert.assertEquals(1, results.size());
}
@Test
public void findByBigIntegerTest() throws SQLException {
public void findByBigIntegerTest() {
final List<Entity> results = db.findAll(Entity.create("user").set("age", new BigInteger("12")));
Assert.assertEquals(2, results.size());
}
@Test
public void findByBigDecimalTest() throws SQLException {
public void findByBigDecimalTest() {
final List<Entity> results = db.findAll(Entity.create("user").set("age", new BigDecimal("12")));
Assert.assertEquals(2, results.size());
}
@Test
public void findLikeTest() throws SQLException {
public void findLikeTest() {
final List<Entity> results = db.findAll(Entity.create("user").set("name", "like \"%三%\""));
Assert.assertEquals(2, results.size());
}
@Test
public void findLikeTest2() throws SQLException {
public void findLikeTest2() {
final List<Entity> results = db.findAll(Entity.create("user").set("name", new Condition("name", "", LikeType.Contains)));
Assert.assertEquals(2, results.size());
}
@Test
public void findLikeTest3() throws SQLException {
public void findLikeTest3() {
final List<Entity> results = db.findAll(Entity.create("user").set("name", new Condition("name", null, LikeType.Contains)));
Assert.assertEquals(0, results.size());
}
@Test
public void findInTest() throws SQLException {
public void findInTest() {
final List<Entity> results = db.findAll(Entity.create("user").set("id", "in 1,2,3"));
Console.log(results);
Assert.assertEquals(2, results.size());
}
@Test
public void findInTest2() throws SQLException {
public void findInTest2() {
final List<Entity> results = db.findAll(Entity.create("user")
.set("id", new Condition("id", new long[]{1, 2, 3})));
Assert.assertEquals(2, results.size());
}
@Test
public void findInTest3() throws SQLException {
public void findInTest3() {
final List<Entity> results = db.findAll(Entity.create("user")
.set("id", new long[]{1, 2, 3}));
Assert.assertEquals(2, results.size());
}
@Test
public void findAllTest() throws SQLException {
public void findAllTest() {
final List<Entity> results = db.findAll("user");
Assert.assertEquals(4, results.size());
}
@Test
public void findTest() throws SQLException {
public void findTest() {
final List<Entity> find = db.find(CollUtil.newArrayList("name AS name2"), Entity.create("user"), new EntityListHandler());
Assert.assertFalse(find.isEmpty());
}
@@ -123,11 +122,10 @@ public class CRUDTest {
/**
* 对增删改查做单元测试
*
* @throws SQLException SQL异常
*/
@Test
@Ignore
public void crudTest() throws SQLException {
public void crudTest() {
// 增
final Long id = db.insertForGeneratedKey(Entity.create("user").set("name", "unitTestUser").set("age", 66));
@@ -150,7 +148,7 @@ public class CRUDTest {
@Test
@Ignore
public void insertBatchTest() throws SQLException {
public void insertBatchTest() {
final User user1 = new User();
user1.setName("张三");
user1.setAge(12);
@@ -176,7 +174,7 @@ public class CRUDTest {
@Test
@Ignore
public void insertBatchOneTest() throws SQLException {
public void insertBatchOneTest() {
final User user1 = new User();
user1.setName("张三");
user1.setAge(12);
@@ -192,7 +190,7 @@ public class CRUDTest {
}
@Test
public void selectInTest() throws SQLException {
public void selectInTest() {
final List<Entity> results = db.query("select * from user where id in (:ids)",
MapUtil.of("ids", new int[]{1, 2, 3}));
Assert.assertEquals(2, results.size());

View File

@@ -8,7 +8,6 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.sql.SQLException;
import java.util.List;
/**
@@ -24,7 +23,7 @@ public class ConcurentTest {
@Before
public void init() {
db = Db.use("test");
db = Db.of("test");
}
@Test
@@ -32,11 +31,7 @@ public class ConcurentTest {
for(int i = 0; i < 10000; i++) {
ThreadUtil.execute(() -> {
final List<Entity> find;
try {
find = db.find(CollUtil.newArrayList("name AS name2"), Entity.create("user"), new EntityListHandler());
} catch (final SQLException e) {
throw new DbRuntimeException(e);
}
find = db.find(CollUtil.newArrayList("name AS name2"), Entity.create("user"), new EntityListHandler());
Console.log(find);
});
}

View File

@@ -20,43 +20,43 @@ import java.util.List;
public class DbTest {
@Test
public void queryTest() throws SQLException {
final List<Entity> find = Db.use().query("select * from user where age = ?", 18);
public void queryTest() {
final List<Entity> find = Db.of().query("select * from ofr where age = ?", 18);
Assert.assertEquals("王五", find.get(0).get("name"));
}
@Test
public void findTest() throws SQLException {
final List<Entity> find = Db.use().find(Entity.create("user").set("age", 18));
public void findTest() {
final List<Entity> find = Db.of().find(Entity.create("ofr").set("age", 18));
Assert.assertEquals("王五", find.get(0).get("name"));
}
@Test
public void pageTest() throws SQLException {
public void pageTest() {
// 测试数据库中一共4条数据第0页有3条第1页有1条
final List<Entity> page0 = Db.use().page(Entity.create("user"), 0, 3);
final List<Entity> page0 = Db.of().page(Entity.create("ofr"), 0, 3);
Assert.assertEquals(3, page0.size());
final List<Entity> page1 = Db.use().page(Entity.create("user"), 1, 3);
final List<Entity> page1 = Db.of().page(Entity.create("ofr"), 1, 3);
Assert.assertEquals(1, page1.size());
}
@Test
public void pageTest2() throws SQLException {
final String sql = "select * from user order by name";
public void pageTest2() {
final String sql = "select * from ofr order by name";
// 测试数据库中一共4条数据第0页有3条第1页有1条
final List<Entity> page0 = Db.use().page(
final List<Entity> page0 = Db.of().page(
sql, Page.of(0, 3));
Assert.assertEquals(3, page0.size());
final List<Entity> page1 = Db.use().page(
final List<Entity> page1 = Db.of().page(
sql, Page.of(1, 3));
Assert.assertEquals(1, page1.size());
}
@Test
public void pageWithParamsTest() throws SQLException {
final String sql = "select * from user where name = ?";
final PageResult<Entity> result = Db.use().page(
public void pageWithParamsTest() {
final String sql = "select * from ofr where name = ?";
final PageResult<Entity> result = Db.of().page(
sql, Page.of(0, 3), "张三");
Assert.assertEquals(2, result.getTotal());
@@ -65,59 +65,59 @@ public class DbTest {
}
@Test
public void countTest() throws SQLException {
final long count = Db.use().count("select * from user");
public void countTest() {
final long count = Db.of().count("select * from ofr");
Assert.assertEquals(4, count);
}
@Test
public void countTest2() throws SQLException {
final long count = Db.use().count("select * from user order by name DESC");
public void countTest2() {
final long count = Db.of().count("select * from ofr order by name DESC");
Assert.assertEquals(4, count);
}
@Test
public void findLikeTest() throws SQLException {
public void findLikeTest() {
// 方式1
List<Entity> find = Db.use().find(Entity.create("user").set("name", "like 王%"));
List<Entity> find = Db.of().find(Entity.create("ofr").set("name", "like 王%"));
Assert.assertEquals("王五", find.get(0).get("name"));
// 方式2
find = Db.use().findLike("user", "name", "", Condition.LikeType.StartWith);
find = Db.of().findLike("ofr", "name", "", Condition.LikeType.StartWith);
Assert.assertEquals("王五", find.get(0).get("name"));
// 方式3
find = Db.use().query("select * from user where name like ?", "王%");
find = Db.of().query("select * from ofr where name like ?", "王%");
Assert.assertEquals("王五", find.get(0).get("name"));
}
@Test
public void findByTest() throws SQLException {
final List<Entity> find = Db.use().findBy("user",
public void findByTest() {
final List<Entity> find = Db.of().findBy("ofr",
Condition.parse("age", "> 18"),
Condition.parse("age", "< 100")
);
for (final Entity entity : find) {
StaticLog.debug("{}", entity);
}
Assert.assertEquals("unitTestUser", find.get(0).get("name"));
Assert.assertEquals("unitTestofr", find.get(0).get("name"));
}
@Test
@Ignore
public void txTest() throws SQLException {
Db.use().tx(db -> {
db.insert(Entity.create("user").set("name", "unitTestUser2"));
db.update(Entity.create().set("age", 79), Entity.create("user").set("name", "unitTestUser2"));
db.del("user", "name", "unitTestUser2");
Db.of().tx(db -> {
db.insert(Entity.create("ofr").set("name", "unitTestofr2"));
db.update(Entity.create().set("age", 79), Entity.create("ofr").set("name", "unitTestofr2"));
db.del("ofr", "name", "unitTestofr2");
});
}
@Test
@Ignore
public void queryFetchTest() throws SQLException {
public void queryFetchTest() {
// https://gitee.com/dromara/hutool/issues/I4JXWN
Db.use().query((conn->{
Db.of().query((conn->{
final PreparedStatement ps = conn.prepareStatement("select * from table",
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);

View File

@@ -20,13 +20,8 @@ public class DerbyTest {
@BeforeClass
public static void init() throws SQLException {
final Db db = Db.use(DS_GROUP_NAME);
try{
db.execute("CREATE TABLE test(a INTEGER, b BIGINT)");
}catch (final SQLException e){
// 数据库已存在
return;
}
final Db db = Db.of(DS_GROUP_NAME);
db.execute("CREATE TABLE test(a INTEGER, b BIGINT)");
db.insert(Entity.create("test").set("a", 1).set("b", 11));
db.insert(Entity.create("test").set("a", 2).set("b", 21));
@@ -37,14 +32,14 @@ public class DerbyTest {
@Test
@Ignore
public void queryTest() throws SQLException {
final List<Entity> query = Db.use(DS_GROUP_NAME).query("select * from test");
final List<Entity> query = Db.of(DS_GROUP_NAME).query("select * from test");
Assert.assertEquals(4, query.size());
}
@Test
@Ignore
public void findTest() throws SQLException {
final List<Entity> query = Db.use(DS_GROUP_NAME).find(Entity.create("test"));
final List<Entity> query = Db.of(DS_GROUP_NAME).find(Entity.create("test"));
Assert.assertEquals(4, query.size());
}
}

View File

@@ -15,7 +15,6 @@ import org.junit.Assert;
import org.junit.Test;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;
/**
@@ -27,70 +26,70 @@ import java.util.List;
public class DsTest {
@Test
public void defaultDsTest() throws SQLException {
public void defaultDsTest() {
final DataSource ds = DSFactory.get("test");
final Db db = Db.use(ds);
final Db db = Db.of(ds);
final List<Entity> all = db.findAll("user");
Assert.assertTrue(CollUtil.isNotEmpty(all));
}
@Test
public void hikariDsTest() throws SQLException {
public void hikariDsTest() {
DSFactory.setCurrentDSFactory(new HikariDSFactory());
final DataSource ds = DSFactory.get("test");
final Db db = Db.use(ds);
final Db db = Db.of(ds);
final List<Entity> all = db.findAll("user");
Assert.assertTrue(CollUtil.isNotEmpty(all));
}
@Test
public void druidDsTest() throws SQLException {
public void druidDsTest() {
DSFactory.setCurrentDSFactory(new DruidDSFactory());
final DataSource ds = DSFactory.get("test");
final Db db = Db.use(ds);
final Db db = Db.of(ds);
final List<Entity> all = db.findAll("user");
Assert.assertTrue(CollUtil.isNotEmpty(all));
}
@Test
public void tomcatDsTest() throws SQLException {
public void tomcatDsTest() {
DSFactory.setCurrentDSFactory(new TomcatDSFactory());
final DataSource ds = DSFactory.get("test");
final Db db = Db.use(ds);
final Db db = Db.of(ds);
final List<Entity> all = db.findAll("user");
Assert.assertTrue(CollUtil.isNotEmpty(all));
}
@Test
public void beeCPDsTest() throws SQLException {
public void beeCPDsTest() {
DSFactory.setCurrentDSFactory(new BeeDSFactory());
final DataSource ds = DSFactory.get("test");
final Db db = Db.use(ds);
final Db db = Db.of(ds);
final List<Entity> all = db.findAll("user");
Assert.assertTrue(CollUtil.isNotEmpty(all));
}
@Test
public void dbcpDsTest() throws SQLException {
public void dbcpDsTest() {
DSFactory.setCurrentDSFactory(new DbcpDSFactory());
final DataSource ds = DSFactory.get("test");
final Db db = Db.use(ds);
final Db db = Db.of(ds);
final List<Entity> all = db.findAll("user");
Assert.assertTrue(CollUtil.isNotEmpty(all));
}
@Test
public void c3p0DsTest() throws SQLException {
public void c3p0DsTest() {
DSFactory.setCurrentDSFactory(new C3p0DSFactory());
final DataSource ds = DSFactory.get("test");
final Db db = Db.use(ds);
final Db db = Db.of(ds);
final List<Entity> all = db.findAll("user");
Assert.assertTrue(CollUtil.isNotEmpty(all));
}
@Test
public void c3p0DsUserAndPassTest() {
public void c3p0DsuserAndPassTest() {
// https://gitee.com/dromara/hutool/issues/I4T7XZ
DSFactory.setCurrentDSFactory(new C3p0DSFactory());
final ComboPooledDataSource ds = (ComboPooledDataSource) ((DataSourceWrapper) DSFactory.get("mysql")).getRaw();
@@ -99,10 +98,10 @@ public class DsTest {
}
@Test
public void hutoolPoolTest() throws SQLException {
public void hutoolPoolTest() {
DSFactory.setCurrentDSFactory(new PooledDSFactory());
final DataSource ds = DSFactory.get("test");
final Db db = Db.use(ds);
final Db db = Db.of(ds);
final List<Entity> all = db.findAll("user");
Assert.assertTrue(CollUtil.isNotEmpty(all));
}

View File

@@ -20,11 +20,11 @@ public class FindBeanTest {
@Before
public void init() {
db = Db.use("test");
db = Db.of("test");
}
@Test
public void findAllBeanTest() throws SQLException {
public void findAllBeanTest() {
final List<User> results = db.findAll(Entity.create("user"), User.class);
Assert.assertEquals(4, results.size());
@@ -34,7 +34,7 @@ public class FindBeanTest {
@Test
@SuppressWarnings("rawtypes")
public void findAllListTest() throws SQLException {
public void findAllListTest() {
final List<List> results = db.findAll(Entity.create("user"), List.class);
Assert.assertEquals(4, results.size());
@@ -43,7 +43,7 @@ public class FindBeanTest {
}
@Test
public void findAllArrayTest() throws SQLException {
public void findAllArrayTest() {
final List<Object[]> results = db.findAll(Entity.create("user"), Object[].class);
Assert.assertEquals(4, results.size());
@@ -52,13 +52,13 @@ public class FindBeanTest {
}
@Test
public void findAllStringTest() throws SQLException {
public void findAllStringTest() {
final List<String> results = db.findAll(Entity.create("user"), String.class);
Assert.assertEquals(4, results.size());
}
@Test
public void findAllStringArrayTest() throws SQLException {
public void findAllStringArrayTest() {
final List<String[]> results = db.findAll(Entity.create("user"), String[].class);
Assert.assertEquals(4, results.size());

View File

@@ -4,7 +4,6 @@ import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.sql.SQLException;
import java.util.List;
/**
@@ -18,8 +17,8 @@ public class H2Test {
private static final String DS_GROUP_NAME = "h2";
@BeforeClass
public static void init() throws SQLException {
final Db db = Db.use(DS_GROUP_NAME);
public static void init() {
final Db db = Db.of(DS_GROUP_NAME);
db.execute("CREATE TABLE test(a INTEGER, b BIGINT)");
db.insert(Entity.create("test").set("a", 1).set("b", 11));
@@ -29,20 +28,20 @@ public class H2Test {
}
@Test
public void queryTest() throws SQLException {
final List<Entity> query = Db.use(DS_GROUP_NAME).query("select * from test");
public void queryTest() {
final List<Entity> query = Db.of(DS_GROUP_NAME).query("select * from test");
Assert.assertEquals(4, query.size());
}
@Test
public void findTest() throws SQLException {
final List<Entity> query = Db.use(DS_GROUP_NAME).find(Entity.create("test"));
public void findTest() {
final List<Entity> query = Db.of(DS_GROUP_NAME).find(Entity.create("test"));
Assert.assertEquals(4, query.size());
}
@Test
public void upsertTest() throws SQLException {
final Db db=Db.use(DS_GROUP_NAME);
public void upsertTest() {
final Db db=Db.of(DS_GROUP_NAME);
db.upsert(Entity.create("test").set("a",1).set("b",111),"a");
final Entity a1=db.get("test","a",1);
Assert.assertEquals(Long.valueOf(111),a1.getLong("b"));

View File

@@ -4,7 +4,6 @@ import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.sql.SQLException;
import java.util.List;
/**
@@ -18,8 +17,8 @@ public class HsqldbTest {
private static final String DS_GROUP_NAME = "hsqldb";
@BeforeClass
public static void init() throws SQLException {
final Db db = Db.use(DS_GROUP_NAME);
public static void init() {
final Db db = Db.of(DS_GROUP_NAME);
db.execute("CREATE TABLE test(a INTEGER, b BIGINT)");
db.insert(Entity.create("test").set("a", 1).set("b", 11));
db.insert(Entity.create("test").set("a", 2).set("b", 21));
@@ -28,14 +27,14 @@ public class HsqldbTest {
}
@Test
public void connTest() throws SQLException {
final List<Entity> query = Db.use(DS_GROUP_NAME).query("select * from test");
public void connTest() {
final List<Entity> query = Db.of(DS_GROUP_NAME).query("select * from test");
Assert.assertEquals(4, query.size());
}
@Test
public void findTest() throws SQLException {
final List<Entity> query = Db.use(DS_GROUP_NAME).find(Entity.create("test"));
public void findTest() {
final List<Entity> query = Db.of(DS_GROUP_NAME).find(Entity.create("test"));
Assert.assertEquals(4, query.size());
}
}

View File

@@ -17,16 +17,16 @@ import java.util.List;
public class MySQLTest {
@BeforeClass
@Ignore
public static void createTable() throws SQLException {
final Db db = Db.use("mysql");
public static void createTable() {
final Db db = Db.of("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
public void insertTest() throws SQLException {
public void insertTest() {
for (int id = 100; id < 200; id++) {
Db.use("mysql").insert(Entity.create("user")//
Db.of("mysql").insert(Entity.create("user")//
.set("id", id)//
.set("name", "测试用户" + id)//
.set("text", "描述" + id)//
@@ -44,7 +44,7 @@ public class MySQLTest {
@Test(expected = SQLException.class)
@Ignore
public void txTest() throws SQLException {
Db.use("mysql").tx(db -> {
Db.of("mysql").tx(db -> {
final 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) {
@@ -57,8 +57,8 @@ public class MySQLTest {
@Test
@Ignore
public void pageTest() throws SQLException {
final PageResult<Entity> result = Db.use("mysql").page(Entity.create("user"), new Page(2, 10));
public void pageTest() {
final PageResult<Entity> result = Db.of("mysql").page(Entity.create("user"), new Page(2, 10));
for (final Entity entity : result) {
Console.log(entity.get("id"));
}
@@ -66,15 +66,15 @@ public class MySQLTest {
@Test
@Ignore
public void getTimeStampTest() throws SQLException {
final List<Entity> all = Db.use("mysql").findAll("test");
public void getTimeStampTest() {
final List<Entity> all = Db.of("mysql").findAll("test");
Console.log(all);
}
@Test
@Ignore
public void upsertTest() throws SQLException {
final Db db = Db.use("mysql");
public void upsertTest() {
final Db db = Db.of("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"));
final Entity user = db.get(Entity.create("testuser").set("id", 1));

View File

@@ -5,7 +5,6 @@ import cn.hutool.db.sql.NamedSql;
import org.junit.Assert;
import org.junit.Test;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -87,17 +86,17 @@ public class NamedSqlTest {
}
@Test
public void queryTest() throws SQLException {
public void queryTest() {
final Map<String, Object> paramMap = MapUtil
.builder("name1", (Object)"王五")
.put("age1", 18).build();
final String sql = "select * from user where name = @name1 and age = @age1";
List<Entity> query = Db.use().query(sql, paramMap);
List<Entity> query = Db.of().query(sql, paramMap);
Assert.assertEquals(1, query.size());
// 采用传统方式查询是否能识别Map类型参数
query = Db.use().query(sql, new Object[]{paramMap});
query = Db.of().query(sql, new Object[]{paramMap});
Assert.assertEquals(1, query.size());
}
}

View File

@@ -8,8 +8,6 @@ import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.sql.SQLException;
/**
* Oracle操作单元测试
*
@@ -41,9 +39,9 @@ public class OracleTest {
@Test
@Ignore
public void insertTest() throws SQLException {
public void insertTest() {
for (int id = 100; id < 200; id++) {
Db.use("orcl").insert(Entity.create("T_USER")//
Db.of("orcl").insert(Entity.create("T_USER")//
.set("ID", id)//
.set("name", "测试用户" + id)//
.set("TEXT", "描述" + id)//
@@ -54,8 +52,8 @@ public class OracleTest {
@Test
@Ignore
public void pageTest() throws SQLException {
final PageResult<Entity> result = Db.use("orcl").page(Entity.create("T_USER"), new Page(2, 10));
public void pageTest() {
final PageResult<Entity> result = Db.of("orcl").page(Entity.create("T_USER"), new Page(2, 10));
for (final Entity entity : result) {
Console.log(entity.get("ID"));
}

View File

@@ -13,8 +13,8 @@ public class PicTransferTest {
@Test
@Ignore
public void findTest() throws SQLException {
Db.use().find(
public void findTest() {
Db.of().find(
ListUtil.of("NAME", "TYPE", "GROUP", "PIC"),
Entity.create("PIC_INFO").set("TYPE", 1),
rs -> {

View File

@@ -1,13 +1,10 @@
package cn.hutool.db;
import java.sql.SQLException;
import cn.hutool.core.lang.Console;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import cn.hutool.core.lang.Console;
/**
* PostgreSQL 单元测试
*
@@ -17,9 +14,9 @@ public class PostgreTest {
@Test
@Ignore
public void insertTest() throws SQLException {
public void insertTest() {
for (int id = 100; id < 200; id++) {
Db.use("postgre").insert(Entity.create("user")//
Db.of("postgre").insert(Entity.create("user")//
.set("id", id)//
.set("name", "测试用户" + id)//
);
@@ -28,8 +25,8 @@ public class PostgreTest {
@Test
@Ignore
public void pageTest() throws SQLException {
final PageResult<Entity> result = Db.use("postgre").page(Entity.create("user"), new Page(2, 10));
public void pageTest() {
final PageResult<Entity> result = Db.of("postgre").page(Entity.create("user"), new Page(2, 10));
for (final Entity entity : result) {
Console.log(entity.get("id"));
}
@@ -37,8 +34,8 @@ public class PostgreTest {
@Test
@Ignore
public void upsertTest() throws SQLException {
final Db db = Db.use("postgre");
public void upsertTest() {
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.insert(Entity.create("ctest").set("id", 1).set("t1", "111").set("t2", "222").set("t3", "333"));

View File

@@ -3,8 +3,6 @@ package cn.hutool.db;
import org.junit.Ignore;
import org.junit.Test;
import java.sql.SQLException;
/**
* 事务性数据库操作单元测试
* @author looly
@@ -15,19 +13,15 @@ public class SessionTest {
@Test
@Ignore
public void transTest() {
final Session session = Session.create("test");
try {
session.beginTransaction();
session.update(Entity.create().set("age", 76), Entity.create("user").set("name", "unitTestUser"));
session.commit();
} catch (final SQLException e) {
session.quietRollback();
}
final Session session = Session.of("test");
session.beginTransaction();
session.update(Entity.create().set("age", 76), Entity.create("user").set("name", "unitTestUser"));
session.commit();
}
@Test
@Ignore
public void txTest() throws SQLException {
Session.create("test").tx(session -> session.update(Entity.create().set("age", 78), Entity.create("user").set("name", "unitTestUser")));
public void txTest() {
Session.of("test").tx(session -> session.update(Entity.create().set("age", 78), Entity.create("user").set("name", "unitTestUser")));
}
}

View File

@@ -1,12 +1,9 @@
package cn.hutool.db;
import java.sql.SQLException;
import cn.hutool.core.lang.Console;
import org.junit.Ignore;
import org.junit.Test;
import cn.hutool.core.lang.Console;
/**
* SQL Server操作单元测试
*
@@ -17,15 +14,15 @@ public class SqlServerTest {
@Test
@Ignore
public void createTableTest() throws SQLException {
Db.use("sqlserver").execute("create table T_USER(ID bigint, name varchar(255))");
public void createTableTest() {
Db.of("sqlserver").execute("create table T_USER(ID bigint, name varchar(255))");
}
@Test
@Ignore
public void insertTest() throws SQLException {
public void insertTest() {
for (int id = 100; id < 200; id++) {
Db.use("sqlserver").insert(Entity.create("T_USER")//
Db.of("sqlserver").insert(Entity.create("T_USER")//
.set("ID", id)//
.set("name", "测试用户" + id)//
);
@@ -34,8 +31,8 @@ public class SqlServerTest {
@Test
@Ignore
public void pageTest() throws SQLException {
final PageResult<Entity> result = Db.use("sqlserver").page(Entity.create("T_USER"), new Page(2, 10));
public void pageTest() {
final PageResult<Entity> result = Db.of("sqlserver").page(Entity.create("T_USER"), new Page(2, 10));
for (final Entity entity : result) {
Console.log(entity.get("ID"));
}

View File

@@ -5,25 +5,22 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.sql.SQLException;
public class UpdateTest {
Db db;
@Before
public void init() {
db = Db.use("test");
db = Db.of("test");
}
/**
* 对更新做单元测试
*
* @throws SQLException SQL异常
*/
@Test
@Ignore
public void updateTest() throws SQLException {
public void updateTest() {
// 改
final int update = db.update(Entity.create("user").set("age", 88), Entity.create().set("name", "unitTestUser"));