完善:给core的所有的测试用例加上断言

This commit is contained in:
chenjh3 2023-10-10 11:48:23 +08:00
parent 32409b871b
commit 4644dfaad8
13 changed files with 180 additions and 9 deletions

View File

@ -24,6 +24,7 @@ import com.mybatisflex.core.table.TableInfoFactory;
import com.mybatisflex.core.util.CollectionUtil; import com.mybatisflex.core.util.CollectionUtil;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static com.mybatisflex.coretest.table.ArticleTableDef.ARTICLE; import static com.mybatisflex.coretest.table.ArticleTableDef.ARTICLE;
@ -39,6 +40,7 @@ public class ArticleSqlTester {
IDialect dialect = new CommonsDialectImpl(); IDialect dialect = new CommonsDialectImpl();
String sql = dialect.forSelectByQuery(query); String sql = dialect.forSelectByQuery(query);
System.out.println(sql); System.out.println(sql);
assertEquals("SELECT * FROM `tb_article`", sql);
} }
@Test @Test
@ -51,6 +53,8 @@ public class ArticleSqlTester {
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class); TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
String sql = dialect.forInsertEntity(tableInfo, article, false); String sql = dialect.forInsertEntity(tableInfo, article, false);
System.out.println(sql); System.out.println(sql);
assertEquals("INSERT INTO `tb_article`(`uuid`, `account_id`, `title`, `content`, `created`, `modified`, `is_delete`, `version`) " +
"VALUES (?, ?, ?, ?, now(), now(), ?, ?)", sql);
} }
@ -64,6 +68,8 @@ public class ArticleSqlTester {
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class); TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
String sql = dialect.forInsertEntity(tableInfo, article, true); String sql = dialect.forInsertEntity(tableInfo, article, true);
System.out.println(sql); System.out.println(sql);
assertEquals("INSERT INTO `tb_article`(`uuid`, `account_id`, `content`, `created`, `modified`) " +
"VALUES (?, ?, ?, now(), now())", sql);
} }
@ -81,6 +87,8 @@ public class ArticleSqlTester {
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class); TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
String sql = dialect.forInsertEntityBatch(tableInfo, CollectionUtil.newArrayList(article1, article2)); String sql = dialect.forInsertEntityBatch(tableInfo, CollectionUtil.newArrayList(article1, article2));
System.out.println(sql); System.out.println(sql);
assertEquals("INSERT INTO `tb_article`(`uuid`, `account_id`, `title`, `content`, `created`, `modified`, `is_delete`, `version`) " +
"VALUES (?, ?, ?, ?, now(), now(), ?, ?), (?, ?, ?, ?, now(), now(), ?, ?)", sql);
} }
@ -90,6 +98,7 @@ public class ArticleSqlTester {
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class); TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
String sql = dialect.forDeleteEntityById(tableInfo); String sql = dialect.forDeleteEntityById(tableInfo);
System.out.println(sql); System.out.println(sql);
assertEquals("UPDATE `tb_article` SET `is_delete` = 1 WHERE `id` = ? AND `uuid` = ? AND `is_delete` = 0", sql);
} }
@ -99,6 +108,7 @@ public class ArticleSqlTester {
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class); TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
String sql = dialect.forDeleteEntityBatchByIds(tableInfo, new Object[]{1, 2, 3}); String sql = dialect.forDeleteEntityBatchByIds(tableInfo, new Object[]{1, 2, 3});
System.out.println(sql); System.out.println(sql);
assertEquals("UPDATE `tb_article` SET `is_delete` = 1 WHERE ((`id` = ? AND `uuid` = ? )) AND `is_delete` = 0", sql);
} }
@ -113,6 +123,9 @@ public class ArticleSqlTester {
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class); TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
String sql = dialect.forUpdateEntity(tableInfo, article, true); String sql = dialect.forUpdateEntity(tableInfo, article, true);
System.out.println(sql); System.out.println(sql);
assertEquals("UPDATE `tb_article` " +
"SET `account_id` = ? , `content` = ? , `modified` = now(), `version` = `version` + 1 " +
"WHERE `id` = ? AND `uuid` = ? AND `is_delete` = 0 AND `version` = 1", sql);
} }
@ -130,6 +143,7 @@ public class ArticleSqlTester {
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class); TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
String sql = dialect.forUpdateEntityByQuery(tableInfo, article, true, queryWrapper); String sql = dialect.forUpdateEntityByQuery(tableInfo, article, true, queryWrapper);
System.out.println(sql); System.out.println(sql);
assertEquals("UPDATE `tb_article` SET `account_id` = ? , `content` = ? , `modified` = now(), `version` = `version` + 1 WHERE `id` >= ?", sql);
} }
} }

View File

@ -20,6 +20,7 @@ import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.MapperUtil; import com.mybatisflex.core.util.MapperUtil;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT; import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
import static com.mybatisflex.coretest.table.ArticleTableDef.ARTICLE; import static com.mybatisflex.coretest.table.ArticleTableDef.ARTICLE;
@ -43,6 +44,12 @@ public class CountSqlTest {
QueryWrapper optimized = MapperUtil.optimizeCountQueryWrapper(queryWrapper); QueryWrapper optimized = MapperUtil.optimizeCountQueryWrapper(queryWrapper);
System.out.println(optimized.toSQL()); System.out.println(optimized.toSQL());
assertEquals("SELECT COUNT(*) AS `total` " +
"FROM `tb_account` " +
"LEFT JOIN `tb_article` AS `a1` ON `a1`.`account_id` = `tb_account`.`id` " +
"LEFT JOIN `tb_article` AS `a2` ON `a2`.`account_id` = `tb_account`.`id` " +
"WHERE `a1`.`account_id` IN (1, 2, 3)", optimized.toSQL());
} }
@Test @Test
@ -59,6 +66,12 @@ public class CountSqlTest {
QueryWrapper optimized = MapperUtil.optimizeCountQueryWrapper(queryWrapper); QueryWrapper optimized = MapperUtil.optimizeCountQueryWrapper(queryWrapper);
System.out.println(optimized.toSQL()); System.out.println(optimized.toSQL());
assertEquals("SELECT COUNT(*) AS `total` " +
"FROM `tb_account` " +
"LEFT JOIN `tb_article` AS `a1` ON `a1`.`account_id` = `tb_account`.`id` " +
"LEFT JOIN `tb_article` AS `a2` ON `a2`.`account_id` = `tb_account`.`id` " +
"WHERE a1.account_id IN (1, 2, 3) ", optimized.toSQL());
} }
@Test @Test
@ -75,6 +88,12 @@ public class CountSqlTest {
QueryWrapper optimized = MapperUtil.optimizeCountQueryWrapper(queryWrapper); QueryWrapper optimized = MapperUtil.optimizeCountQueryWrapper(queryWrapper);
System.out.println(optimized.toSQL()); System.out.println(optimized.toSQL());
assertEquals("SELECT COUNT(*) AS `total` " +
"FROM `tb_account` AS `a` " +
"LEFT JOIN `tb_article` AS `a1` ON a1.account_id = a.id " +
"LEFT JOIN `tb_article` AS `a2` ON a1.account_id = a.id " +
"WHERE a1.account_id IN (1, 2, 3) ", optimized.toSQL());
} }
} }

View File

@ -18,8 +18,11 @@ package com.mybatisflex.coretest;
import com.mybatisflex.core.dialect.DbType; import com.mybatisflex.core.dialect.DbType;
import com.mybatisflex.core.dialect.DbTypeUtil; import com.mybatisflex.core.dialect.DbTypeUtil;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static com.mybatisflex.core.dialect.DbType.SQLSERVER_2005;
public class DbTypeUtilTest { public class DbTypeUtilTest {
@Test @Test
@ -27,6 +30,7 @@ public class DbTypeUtilTest {
String url01 = "jdbc:sqlserver://127.0.0.1"; String url01 = "jdbc:sqlserver://127.0.0.1";
DbType dbType01 = DbTypeUtil.parseDbType(url01); DbType dbType01 = DbTypeUtil.parseDbType(url01);
System.out.println(dbType01); System.out.println(dbType01);
Assert.assertEquals(dbType01, SQLSERVER_2005);
} }
} }

View File

@ -23,7 +23,9 @@ import org.junit.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects;
import static org.junit.Assert.assertEquals;
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT; import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
/** /**
@ -43,6 +45,7 @@ public class DynamicConditionTest {
.toSQL(); .toSQL();
System.out.println(sql); System.out.println(sql);
assertEquals("SELECT * FROM `tb_account` WHERE `age` >= 18", sql);
} }
@Test @Test
@ -57,6 +60,7 @@ public class DynamicConditionTest {
.toSQL(); .toSQL();
System.out.println(sql); System.out.println(sql);
assertEquals("SELECT * FROM `tb_account` WHERE `id` IN (1, 2, 3)", sql);
} }
@Test @Test
@ -67,6 +71,7 @@ public class DynamicConditionTest {
.toSQL(); .toSQL();
System.out.println(sql); System.out.println(sql);
assertEquals("SELECT * FROM `tb_account` WHERE `id` = '1'", sql);
} }
@Test @Test
@ -77,6 +82,7 @@ public class DynamicConditionTest {
.toSQL(); .toSQL();
System.out.println(sql); System.out.println(sql);
assertEquals("SELECT * FROM `tb_account` WHERE `id` BETWEEN '1' AND '2' ", sql);
} }
@Test @Test
@ -95,6 +101,7 @@ public class DynamicConditionTest {
} }
System.out.println(queryWrapper.toSQL()); System.out.println(queryWrapper.toSQL());
assertEquals("SELECT * FROM `tb_account` WHERE `id` IN (1, 2, 3) AND `age` >= 18", queryWrapper.toSQL());
} }
@Test @Test
@ -112,6 +119,7 @@ public class DynamicConditionTest {
} }
System.out.println(queryWrapper.toSQL()); System.out.println(queryWrapper.toSQL());
assertEquals("SELECT * FROM `tb_account` WHERE `id` IN (1, 2, 3) OR `user_name` = 'zhang san'", queryWrapper.toSQL());
} }
@Test @Test
@ -129,6 +137,7 @@ public class DynamicConditionTest {
} }
System.out.println(queryWrapper.toSQL()); System.out.println(queryWrapper.toSQL());
assertEquals("SELECT * FROM `tb_account` WHERE `id` IN (1, 2, 3) OR `user_name` = 'zhang san'", queryWrapper.toSQL());
} }
@Test @Test
@ -146,6 +155,7 @@ public class DynamicConditionTest {
} }
System.out.println(queryWrapper.toSQL()); System.out.println(queryWrapper.toSQL());
assertEquals("SELECT * FROM `tb_account` WHERE `id` = 1 AND (`age` IN (17, 18, 19) OR `user_name` = 'zhang san')", queryWrapper.toSQL());
} }
@Test @Test
@ -158,6 +168,9 @@ public class DynamicConditionTest {
.where(ACCOUNT.USER_NAME.in( "")); .where(ACCOUNT.USER_NAME.in( ""));
System.out.println(queryWrapper.toSQL()); System.out.println(queryWrapper.toSQL());
assertEquals("SELECT * FROM `tb_account`", queryWrapper.toSQL());
// 重置QueryColumnBehavior
QueryColumnBehavior.setIgnoreFunction(Objects::isNull);
} }
} }

View File

@ -17,6 +17,7 @@
package com.mybatisflex.coretest; package com.mybatisflex.coretest;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT; import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
@ -40,6 +41,10 @@ public class DynamicOrderByTest {
.orderBy(ACCOUNT.BIRTHDAY, null); .orderBy(ACCOUNT.BIRTHDAY, null);
System.out.println(queryWrapper.toSQL()); System.out.println(queryWrapper.toSQL());
Assert.assertEquals("SELECT `ac`.`id`, `ac`.`user_name` AS `name`, `ac`.`age` " +
"FROM `tb_account` AS `ac` " +
"LEFT JOIN `tb_article` AS `ar` ON `ar`.`account_id` = `ac`.`id` " +
"ORDER BY `ac`.`user_name` ASC, `ac`.`age` DESC, name ASC", queryWrapper.toSQL());
} }
} }

View File

@ -22,6 +22,7 @@ import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.RawQueryColumn; import com.mybatisflex.core.query.RawQueryColumn;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static com.mybatisflex.core.query.QueryMethods.*; import static com.mybatisflex.core.query.QueryMethods.*;
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT; import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
@ -40,6 +41,7 @@ public class FunctionSqlTest {
.from(ACCOUNT) .from(ACCOUNT)
.toSQL(); .toSQL();
System.out.println(sql); System.out.println(sql);
assertEquals("SELECT NOW() AS `n1`, NOW() AS `n2`, CONCAT(`user_name`, `age`) AS `c1` FROM `tb_account`", sql);
} }
@Test @Test
@ -51,6 +53,7 @@ public class FunctionSqlTest {
.where(not(ACCOUNT.ID.eq(1))) .where(not(ACCOUNT.ID.eq(1)))
.toSQL(); .toSQL();
System.out.println(sql); System.out.println(sql);
assertEquals("SELECT CONCAT_WS('abc', `user_name`, `birthday`), ABS(-3) FROM `tb_account` WHERE NOT (`id` = 1)", sql);
} }
@Test @Test
@ -62,6 +65,7 @@ public class FunctionSqlTest {
.toSQL(); .toSQL();
System.out.println(sql); System.out.println(sql);
assertEquals("SELECT * FROM `tb_account` WHERE UPPER(`user_name`) LIKE UPPER('ws')", sql);
} }
@Test @Test
@ -73,6 +77,7 @@ public class FunctionSqlTest {
.toSQL(); .toSQL();
System.out.println(sql); System.out.println(sql);
assertEquals("SELECT * FROM `tb_account` WHERE FIND_IN_SET(100, `id`) > 0", sql);
} }
@Test @Test
@ -84,6 +89,7 @@ public class FunctionSqlTest {
.toSQL(); .toSQL();
System.out.println(sql); System.out.println(sql);
assertEquals("SELECT * FROM `tb_account` ORDER BY RAND()", sql);
} }
@Test @Test
@ -95,6 +101,7 @@ public class FunctionSqlTest {
.toSQL(); .toSQL();
System.out.println(sql); System.out.println(sql);
assertEquals("SELECT (select role_name from tb_role where id = 1), `user_name` FROM `tb_account`", sql);
} }
@Test @Test
public void testReplaceString() { public void testReplaceString() {
@ -105,6 +112,7 @@ public class FunctionSqlTest {
.toSQL(); .toSQL();
System.out.println(sql); System.out.println(sql);
assertEquals("SELECT `user_name` FROM `tb_account` WHERE `user_name` = REPLACE(nsg_contract.primer_name, ' ', '')", sql);
} }
} }

View File

@ -19,6 +19,7 @@ package com.mybatisflex.coretest;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static com.mybatisflex.core.query.QueryMethods.*; import static com.mybatisflex.core.query.QueryMethods.*;
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT; import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
@ -35,6 +36,7 @@ public class IfFunctionTest {
.from(ACCOUNT) .from(ACCOUNT)
.where(ACCOUNT.ID.eq(1)); .where(ACCOUNT.ID.eq(1));
System.out.println(queryWrapper.toSQL()); System.out.println(queryWrapper.toSQL());
assertEquals("SELECT IF(`age` >= 6, `is_normal`, `is_delete`) AS `type` FROM `tb_account` WHERE `id` = 1", queryWrapper.toSQL());
} }
@Test @Test
@ -45,6 +47,7 @@ public class IfFunctionTest {
.from(ACCOUNT) .from(ACCOUNT)
.where(ACCOUNT.ID.eq(1)); .where(ACCOUNT.ID.eq(1));
System.out.println(queryWrapper.toSQL()); System.out.println(queryWrapper.toSQL());
assertEquals("SELECT IF(`age` >= 18, '成年人', IF(`age` <= 8, '未上学', '已上学')) AS `type` FROM `tb_account` WHERE `id` = 1", queryWrapper.toSQL());
} }
@Test @Test
@ -54,6 +57,7 @@ public class IfFunctionTest {
.from(ACCOUNT) .from(ACCOUNT)
.where(ACCOUNT.ID.eq(1)); .where(ACCOUNT.ID.eq(1));
System.out.println(queryWrapper.toSQL()); System.out.println(queryWrapper.toSQL());
assertEquals("SELECT IFNULL(`id`, 0) FROM `tb_account` WHERE `id` = 1", queryWrapper.toSQL());
} }
@Test @Test
@ -62,6 +66,7 @@ public class IfFunctionTest {
.select(ifNull(null_(), number(0))) .select(ifNull(null_(), number(0)))
.from(ACCOUNT); .from(ACCOUNT);
System.out.println(queryWrapper.toSQL()); System.out.println(queryWrapper.toSQL());
assertEquals("SELECT IFNULL(NULL, 0) FROM `tb_account`", queryWrapper.toSQL());
} }
} }

View File

@ -20,6 +20,7 @@ import com.github.vertical_blank.sqlformatter.SqlFormatter;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static com.mybatisflex.core.query.QueryMethods.allColumns; import static com.mybatisflex.core.query.QueryMethods.allColumns;
import static com.mybatisflex.core.query.QueryMethods.defaultColumns; import static com.mybatisflex.core.query.QueryMethods.defaultColumns;
@ -44,6 +45,13 @@ public class LambdaSqlTest {
.where(Account::getAge).ge(18); .where(Account::getAge).ge(18);
printSQL(queryWrapper); printSQL(queryWrapper);
assertEquals("SELECT\n" +
" *\n" +
"FROM\n" +
" ` tb_account `\n" +
" JOIN ` tb_article ` ON ` tb_article `.` account_id ` = ` tb_account `.` id `\n" +
"WHERE\n" +
" ` tb_account `.` age ` >= 18", SqlFormatter.format(queryWrapper.toSQL()));
} }
@Test @Test
@ -55,6 +63,14 @@ public class LambdaSqlTest {
.where(Account::getAge).ge(18); .where(Account::getAge).ge(18);
printSQL(queryWrapper); printSQL(queryWrapper);
assertEquals("SELECT\n" +
" ` tb_account `.*,\n" +
" ` tb_article `.*\n" +
"FROM\n" +
" ` tb_account `\n" +
" JOIN ` tb_article ` ON ` tb_article `.` account_id ` = ` tb_account `.` id `\n" +
"WHERE\n" +
" ` tb_account `.` age ` >= 18", SqlFormatter.format(queryWrapper.toSQL()));
} }
@Test @Test
@ -66,6 +82,14 @@ public class LambdaSqlTest {
.where(Account::getAge).ge(18); .where(Account::getAge).ge(18);
printSQL(queryWrapper); printSQL(queryWrapper);
assertEquals("SELECT\n" +
" ` ac `.*,\n" +
" ` ar `.*\n" +
"FROM\n" +
" ` tb_account ` AS ` ac `\n" +
" JOIN ` tb_article ` AS ` ar ` ON ` ar `.` account_id ` = ` ac `.` id `\n" +
"WHERE\n" +
" ` ac `.` age ` >= 18", SqlFormatter.format(queryWrapper.toSQL()));
} }
@Test @Test
@ -79,6 +103,21 @@ public class LambdaSqlTest {
.where(Account::getAge).ge(18); .where(Account::getAge).ge(18);
printSQL(queryWrapper); printSQL(queryWrapper);
assertEquals("SELECT\n" +
" ` ac `.` id `,\n" +
" ` ac `.` user_name `,\n" +
" ` ac `.` birthday `,\n" +
" ` ac `.` sex `,\n" +
" ` ac `.` age `,\n" +
" ` ac `.` is_normal `,\n" +
" ` ac `.` is_delete `,\n" +
" ` ar `.` title `,\n" +
" ` ar `.` content `\n" +
"FROM\n" +
" ` tb_account ` AS ` ac `\n" +
" JOIN ` tb_article ` AS ` ar ` ON ` ar `.` account_id ` = ` ac `.` id `\n" +
"WHERE\n" +
" ` ac `.` age ` >= 18", SqlFormatter.format(queryWrapper.toSQL()));
} }
@Test @Test
@ -91,6 +130,20 @@ public class LambdaSqlTest {
.where(Account::getAge).ge(18); .where(Account::getAge).ge(18);
printSQL(queryWrapper); printSQL(queryWrapper);
assertEquals("SELECT\n" +
" ` ac `.` id `,\n" +
" ` ac `.` user_name `,\n" +
" ` ac `.` birthday `,\n" +
" ` ac `.` sex `,\n" +
" ` ac `.` age `,\n" +
" ` ac `.` is_normal `,\n" +
" ` ac `.` is_delete `,\n" +
" ` ar `.*\n" +
"FROM\n" +
" ` tb_account ` AS ` ac `\n" +
" JOIN ` tb_article ` AS ` ar ` ON ` ar `.` account_id ` = ` ac `.` id `\n" +
"WHERE\n" +
" ` ac `.` age ` >= 18", SqlFormatter.format(queryWrapper.toSQL()));
} }
} }

View File

@ -24,6 +24,7 @@ import com.mybatisflex.core.table.TableInfo;
import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.table.TableInfoFactory;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
/** /**
* 逻辑删除测试 * 逻辑删除测试
* *
@ -38,19 +39,25 @@ public class LogicDeleteTest {
@Test @Test
public void test() { public void test() {
print("DefaultLogicDeleteProcessor", new DefaultLogicDeleteProcessor()); print("DefaultLogicDeleteProcessor", new DefaultLogicDeleteProcessor(), "`deleted` = 1", "`deleted` = 0");
print("BooleanLogicDeleteProcessor", new BooleanLogicDeleteProcessor()); print("BooleanLogicDeleteProcessor", new BooleanLogicDeleteProcessor(), "`deleted` = true", "`deleted` = false");
print("IntegerLogicDeleteProcessor", new IntegerLogicDeleteProcessor()); print("IntegerLogicDeleteProcessor", new IntegerLogicDeleteProcessor(), "`deleted` = 1", "`deleted` = 0");
print("DateTimeLogicDeleteProcessor", new DateTimeLogicDeleteProcessor()); print("DateTimeLogicDeleteProcessor", new DateTimeLogicDeleteProcessor(), "`deleted` = NOW()", "`deleted` IS NULL");
print("TimeStampLogicDeleteProcessor", new TimeStampLogicDeleteProcessor()); print("TimeStampLogicDeleteProcessor", new TimeStampLogicDeleteProcessor(), null, "`deleted` = 0");
print("PrimaryKeyLogicDeleteProcessor", new PrimaryKeyLogicDeleteProcessor()); print("PrimaryKeyLogicDeleteProcessor", new PrimaryKeyLogicDeleteProcessor(), "`deleted` = `id`", "`deleted` IS NULL");
} }
public void print(String type, LogicDeleteProcessor processor) { public void print(String type, LogicDeleteProcessor processor, String logicDeletedCondition, String logicNormalCondition) {
System.out.println("===== " + type + " ====="); System.out.println("===== " + type + " =====");
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Account.class); TableInfo tableInfo = TableInfoFactory.ofEntityClass(Account.class);
System.out.println(processor.buildLogicDeletedSet(logicColumn, tableInfo, dialect)); String actualLogicDeletedCondition = processor.buildLogicDeletedSet(logicColumn, tableInfo, dialect);
System.out.println(processor.buildLogicNormalCondition(logicColumn, tableInfo, dialect)); System.out.println(actualLogicDeletedCondition);
if (logicDeletedCondition != null) {
assertEquals(actualLogicDeletedCondition, logicDeletedCondition);
}
String actualLogicNormalCondition = processor.buildLogicNormalCondition(logicColumn, tableInfo, dialect);
System.out.println(actualLogicNormalCondition);
assertEquals(actualLogicNormalCondition, logicNormalCondition);
} }
} }

View File

@ -26,6 +26,7 @@ import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals;
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT; import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
public class OracleDialectTester { public class OracleDialectTester {
@ -40,6 +41,7 @@ public class OracleDialectTester {
IDialect dialect = new OracleDialect(); IDialect dialect = new OracleDialect();
String sql = dialect.forSelectByQuery(query); String sql = dialect.forSelectByQuery(query);
System.out.println(sql); System.out.println(sql);
assertEquals("SELECT * FROM TB_ACCOUNT", sql);
} }
@Test @Test
@ -67,6 +69,11 @@ public class OracleDialectTester {
IDialect dialect = new OracleDialect(); IDialect dialect = new OracleDialect();
String sql = dialect.forInsertEntityBatch(TableInfoFactory.ofEntityClass(Account.class), accounts); String sql = dialect.forInsertEntityBatch(TableInfoFactory.ofEntityClass(Account.class), accounts);
System.out.println(sql); System.out.println(sql);
assertEquals("INSERT ALL " +
"INTO TB_ACCOUNT (ID, USER_NAME, BIRTHDAY, SEX, AGE, IS_NORMAL, IS_DELETE) VALUES (?, ?, ?, ?, ?, ?, ?) " +
"INTO TB_ACCOUNT (ID, USER_NAME, BIRTHDAY, SEX, AGE, IS_NORMAL, IS_DELETE) VALUES (?, ?, ?, ?, ?, ?, ?) " +
"INTO TB_ACCOUNT (ID, USER_NAME, BIRTHDAY, SEX, AGE, IS_NORMAL, IS_DELETE) VALUES (?, ?, ?, ?, ?, ?, ?) " +
"SELECT 1 FROM DUAL", sql);
} }
@ -95,6 +102,11 @@ public class OracleDialectTester {
IDialect dialect = new OracleDialect(); IDialect dialect = new OracleDialect();
String sql = dialect.forInsertBatchWithFirstRowColumns(null, "tb_account", accounts); String sql = dialect.forInsertBatchWithFirstRowColumns(null, "tb_account", accounts);
System.out.println(sql); System.out.println(sql);
assertEquals("INSERT ALL " +
"INTO TB_ACCOUNT (USERNAME, AGE, SEX) VALUES (?, ?, ?) " +
"INTO TB_ACCOUNT (USERNAME, AGE, SEX) VALUES (?, ?, ?) " +
"INTO TB_ACCOUNT (USERNAME, AGE, SEX) VALUES (?, ?, ?) " +
"SELECT 1 FROM DUAL", sql);
} }
} }

View File

@ -21,6 +21,7 @@ import com.mybatisflex.core.dialect.KeywordWrap;
import com.mybatisflex.core.dialect.LimitOffsetProcessor; import com.mybatisflex.core.dialect.LimitOffsetProcessor;
import com.mybatisflex.core.dialect.impl.CommonsDialectImpl; import com.mybatisflex.core.dialect.impl.CommonsDialectImpl;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT; import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
@ -41,6 +42,10 @@ public class SqlServer2005DialectTester {
IDialect dialect = new CommonsDialectImpl(KeywordWrap.SQUARE_BRACKETS, LimitOffsetProcessor.SQLSERVER_2005); IDialect dialect = new CommonsDialectImpl(KeywordWrap.SQUARE_BRACKETS, LimitOffsetProcessor.SQLSERVER_2005);
String sql = dialect.forSelectByQuery(query); String sql = dialect.forSelectByQuery(query);
System.out.println(sql); System.out.println(sql);
Assert.assertEquals("WITH temp_datas AS(" +
"SELECT ROW_NUMBER() OVER ( ORDER BY [id] DESC) as __rn, * FROM [tb_account] WHERE [id] IN (?, ?) AND [sex] = ?" +
") " +
"SELECT * FROM temp_datas WHERE __rn BETWEEN 11 AND 20 ORDER BY __rn", sql);
} }

View File

@ -17,6 +17,7 @@
package com.mybatisflex.coretest; package com.mybatisflex.coretest;
import com.mybatisflex.core.util.StringUtil; import com.mybatisflex.core.util.StringUtil;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
public class StringUtilTest { public class StringUtilTest {
@ -26,9 +27,11 @@ public class StringUtilTest {
String testString1 = "aa_bb_"; String testString1 = "aa_bb_";
String underlineToCamel = StringUtil.underlineToCamel(testString1); String underlineToCamel = StringUtil.underlineToCamel(testString1);
System.out.println(underlineToCamel); System.out.println(underlineToCamel);
Assert.assertEquals("aaBb", underlineToCamel);
String underline = StringUtil.camelToUnderline(underlineToCamel); String underline = StringUtil.camelToUnderline(underlineToCamel);
System.out.println(underline); System.out.println(underline);
Assert.assertEquals("aa_bb", underline);
} }
} }

View File

@ -21,6 +21,7 @@ import org.junit.Test;
import java.util.Arrays; import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import static com.mybatisflex.core.query.QueryMethods.select; import static com.mybatisflex.core.query.QueryMethods.select;
import static com.mybatisflex.core.query.QueryMethods.union; import static com.mybatisflex.core.query.QueryMethods.union;
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT; import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
@ -40,6 +41,10 @@ public class WithSQLTester {
.where(ACCOUNT.SEX.eq(1)); .where(ACCOUNT.SEX.eq(1));
System.out.println(query.toSQL()); System.out.println(query.toSQL());
assertEquals("WITH CTE AS (" +
"SELECT * FROM `tb_article` WHERE `id` >= 100" +
") " +
"SELECT * FROM `tb_account` WHERE `sex` = 1", query.toSQL());
} }
@ -53,6 +58,10 @@ public class WithSQLTester {
.where(ACCOUNT.SEX.eq(1)); .where(ACCOUNT.SEX.eq(1));
System.out.println(query.toSQL()); System.out.println(query.toSQL());
assertEquals("WITH RECURSIVE CTE AS (" +
"SELECT * FROM `tb_article` WHERE `id` >= 100" +
") " +
"SELECT * FROM `tb_account` WHERE `sex` = 1", query.toSQL());
} }
@Test @Test
@ -66,6 +75,10 @@ public class WithSQLTester {
.where(ACCOUNT.SEX.eq(1)); .where(ACCOUNT.SEX.eq(1));
System.out.println(query.toSQL()); System.out.println(query.toSQL());
assertEquals("WITH RECURSIVE CTE(id, value) AS (" +
"SELECT * FROM `tb_article` WHERE `id` >= 100" +
") " +
"SELECT * FROM `tb_account` WHERE `sex` = 1", query.toSQL());
} }
@ -83,6 +96,12 @@ public class WithSQLTester {
.where(ACCOUNT.SEX.eq(1)); .where(ACCOUNT.SEX.eq(1));
System.out.println(query.toSQL()); System.out.println(query.toSQL());
assertEquals("WITH CTE AS (" +
"SELECT * FROM `tb_article` WHERE `id` >= 100" +
"), xxx AS (" +
"SELECT * FROM `tb_article` WHERE `id` >= 200" +
") " +
"SELECT * FROM `tb_account` WHERE `sex` = 1", query.toSQL());
} }
@Test @Test
@ -102,6 +121,10 @@ public class WithSQLTester {
.where(ACCOUNT.SEX.eq(1)); .where(ACCOUNT.SEX.eq(1));
System.out.println(query.toSQL()); System.out.println(query.toSQL());
assertEquals("WITH RECURSIVE CTE AS (" +
"SELECT * FROM `tb_article` WHERE `id` >= 100), xxx(id, name) AS (VALUES (a, b) UNION (SELECT * FROM `tb_article` WHERE `id` >= 200)" +
") " +
"SELECT * FROM `tb_account` WHERE `sex` = 1", query.toSQL());
} }
} }