test: 更新受重构影响的测试代码。

This commit is contained in:
Suomm 2024-03-11 15:32:13 +08:00
parent 81c7d5c101
commit fa2639537f
7 changed files with 115 additions and 42 deletions

View File

@ -30,33 +30,33 @@ import com.mybatisflex.core.query.SqlOperators;
import com.mybatisflex.core.table.TableInfo;
import com.mybatisflex.core.table.TableInfoFactory;
import com.mybatisflex.core.table.TableManager;
import com.mybatisflex.coretest.table.AccountTableDef;
import com.mybatisflex.coretest.table.ArticleTableDef;
import org.junit.Assert;
import org.junit.Test;
import java.util.Date;
import static com.mybatisflex.core.query.QueryMethods.avg;
import static com.mybatisflex.core.query.QueryMethods.case_;
import static com.mybatisflex.core.query.QueryMethods.column;
import static com.mybatisflex.core.query.QueryMethods.convert;
import static com.mybatisflex.core.query.QueryMethods.count;
import static com.mybatisflex.core.query.QueryMethods.distinct;
import static com.mybatisflex.core.query.QueryMethods.exists;
import static com.mybatisflex.core.query.QueryMethods.left;
import static com.mybatisflex.core.query.QueryMethods.max;
import static com.mybatisflex.core.query.QueryMethods.noCondition;
import static com.mybatisflex.core.query.QueryMethods.notExists;
import static com.mybatisflex.core.query.QueryMethods.raw;
import static com.mybatisflex.core.query.QueryMethods.select;
import static com.mybatisflex.core.query.QueryMethods.selectCountOne;
import static com.mybatisflex.core.query.QueryMethods.selectOne;
import static com.mybatisflex.core.query.QueryMethods.year;
import static com.mybatisflex.core.query.QueryMethods.*;
import static com.mybatisflex.coretest.table.Account01TableDef.ACCOUNT01;
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
import static com.mybatisflex.coretest.table.ArticleTableDef.ARTICLE;
public class AccountSqlTester {
@Test
public void testAlisa() {
AccountTableDef a1 = ACCOUNT.as("a1");
AccountTableDef a2 = ACCOUNT.as("a2");
ArticleTableDef ar = ARTICLE.as("ar");
QueryWrapper queryWrapper = new QueryWrapper()
.select(ar.CONTENT, a1.ID, a2.AGE)
.from(ar)
.leftJoin(a1).on(a1.ID.eq(ar.ACCOUNT_ID))
.leftJoin(a2).on(a2.ID.eq(ar.ACCOUNT_ID));
System.out.println(SqlFormatter.format(queryWrapper.toSQL()));
}
@Test
public void testOracleFrom() {
OracleDialect oracleDialect = new OracleDialect();
@ -852,11 +852,12 @@ public class AccountSqlTester {
QueryWrapper qw = QueryWrapper.create(account, operators);
Assert.assertEquals("SELECT `id`, `user_name`, `birthday`, `sex`, `age`, `is_normal`, `is_delete` FROM `tb_account` " +
"WHERE `user_name` LIKE 'michael%' A" +
"ND `sex` = 0 " +
"WHERE `user_name` LIKE 'michael%' " +
"AND `sex` = 0 " +
"AND `age` >= 18 " +
"AND `is_normal` = false"
, qw.toSQL());
System.out.println(SqlFormatter.format(qw.toSQL()));
}
}

View File

@ -1,3 +1,19 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.coretest;
import com.mybatisflex.core.dialect.DbType;
@ -9,7 +25,6 @@ import com.mybatisflex.core.row.RowKey;
import com.mybatisflex.core.table.TableInfoFactory;
import com.mybatisflex.coretest.auth.AuthDialectImpl;
import com.mybatisflex.coretest.auth.Project;
import com.mybatisflex.coretest.auth.table.ProjectTableDef;
import org.junit.Before;
import org.junit.Test;
@ -35,10 +50,10 @@ public class AuthTest {
public void test() {
// 1.单个删除
assert "DELETE FROM `tb_project` WHERE `id` = ? AND `insert_user_id` = 1"
.equals(dialect.forDeleteById(PROJECT.getSchema(), PROJECT.getTableName(), new String[]{PROJECT.ID.getName()}));
.equals(dialect.forDeleteById(PROJECT.getSchema(), PROJECT.getName(), new String[]{PROJECT.ID.getName()}));
// 2.批量删除
assert "DELETE FROM `tb_project` WHERE `id` = ? AND `insert_user_id` = 1"
.equals(dialect.forDeleteBatchByIds(PROJECT.getSchema(), PROJECT.getTableName(), new String[]{PROJECT.ID.getName()}, new Object[]{1L}));
.equals(dialect.forDeleteBatchByIds(PROJECT.getSchema(), PROJECT.getName(), new String[]{PROJECT.ID.getName()}, new Object[]{1L}));
// 3.查询
QueryWrapper deleteWrapper =
QueryWrapper.create(new Project()).where(PROJECT.ID.eq(1));
@ -46,7 +61,7 @@ public class AuthTest {
.equals(dialect.forDeleteByQuery(deleteWrapper));
// 4.更新
assert "UPDATE `tb_project` SET `name` = ? WHERE `id` = ? AND `insert_user_id` = 1"
.equals(dialect.forUpdateById(PROJECT.getSchema(), PROJECT.getTableName(),
.equals(dialect.forUpdateById(PROJECT.getSchema(), PROJECT.getName(),
Row.ofKey(RowKey.AUTO).set(PROJECT.NAME, "项目")));
// 5.更新
Row row = new Row();
@ -57,7 +72,7 @@ public class AuthTest {
.equals(dialect.forUpdateByQuery(updateWrapper, row));
// 6.ID查询
assert "SELECT * FROM `tb_project` WHERE `id` = ? AND `insert_user_id` = 1"
.equals(dialect.forSelectOneById(PROJECT.getSchema(), PROJECT.getTableName(), new String[]{PROJECT.ID.getName()}, new Object[]{1L}));
.equals(dialect.forSelectOneById(PROJECT.getSchema(), PROJECT.getName(), new String[]{PROJECT.ID.getName()}, new Object[]{1L}));
QueryWrapper queryWrapper = QueryWrapper.create(new Project()).where(PROJECT.ID.eq(1));
// 7.query查询
assert "SELECT `id`, `name`, `insert_user_id`, `is_delete` FROM `tb_project` WHERE `id` = ? AND `insert_user_id` = ?"

View File

@ -18,13 +18,7 @@ package com.mybatisflex.coretest;
import com.mybatisflex.core.constant.SqlConnector;
import com.mybatisflex.core.constant.SqlOperator;
import com.mybatisflex.core.query.CPI;
import com.mybatisflex.core.query.If;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryColumnBehavior;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryTable;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.*;
import com.mybatisflex.core.util.StringUtil;
import org.junit.Assert;
import org.junit.Test;
@ -39,9 +33,7 @@ import static com.mybatisflex.core.query.QueryColumnBehavior.getConditionCaster;
import static com.mybatisflex.core.query.QueryMethods.bracket;
import static com.mybatisflex.core.query.QueryMethods.raw;
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* 动态条件测试
@ -109,7 +101,7 @@ public class DynamicConditionTest {
boolean anyMatch = CPI.getQueryTables(queryWrapper)
.stream()
.map(QueryTable::getName)
.anyMatch(tableName -> tableName.equals(ACCOUNT.getTableName()));
.anyMatch(tableName -> tableName.equals(ACCOUNT.getName()));
if (anyMatch) {
CPI.addWhereQueryCondition(queryWrapper, ACCOUNT.AGE.ge(18), SqlConnector.AND);

View File

@ -1,6 +1,23 @@
package com.mybatisflex.core;
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.coretest;
import com.mybatisflex.annotation.UpdateListener;
import com.mybatisflex.core.FlexGlobalConfig;
import org.junit.Assert;
import org.junit.Test;

View File

@ -1,3 +1,19 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.coretest.auth;
import com.mybatisflex.core.dialect.OperateType;
@ -25,7 +41,7 @@ public class AuthDialectImpl extends CommonsDialectImpl {
return;
}
for (QueryTable queryTable : queryTables) {
if (PROJECT.getTableName().equals(queryTable.getName())) {
if (PROJECT.getName().equals(queryTable.getName())) {
queryWrapper.and(PROJECT.INSERT_USER_ID.eq(1));
}
}
@ -34,7 +50,7 @@ public class AuthDialectImpl extends CommonsDialectImpl {
@Override
public void prepareAuth(String schema, String tableName, StringBuilder sql, OperateType operateType) {
if (PROJECT.getTableName().equals(tableName)) {
if (PROJECT.getName().equals(tableName)) {
sql.append(AND).append(wrap("insert_user_id")).append(EQUALS).append(1);
}
super.prepareAuth(schema, tableName, sql, operateType);
@ -42,7 +58,7 @@ public class AuthDialectImpl extends CommonsDialectImpl {
@Override
public void prepareAuth(TableInfo tableInfo, StringBuilder sql, OperateType operateType) {
if (PROJECT.getTableName().equals(tableInfo.getTableName())) {
if (PROJECT.getName().equals(tableInfo.getTableName())) {
sql.append(AND).append(wrap("insert_user_id")).append(EQUALS).append(1);
}
super.prepareAuth(tableInfo, sql, operateType);

View File

@ -1,9 +1,25 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.test.issue113.def;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.table.TableDef;
import com.mybatisflex.core.query.QueryTable;
public class TClassTableDef extends TableDef {
public class TClassTableDef extends QueryTable {
public static final TClassTableDef TB_CLASS = new TClassTableDef();
public final QueryColumn ID = new QueryColumn(this, "id");

View File

@ -1,9 +1,25 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.test.issue113.def;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.table.TableDef;
import com.mybatisflex.core.query.QueryTable;
public class TUserTableDef extends TableDef {
public class TUserTableDef extends QueryTable {
public static final TUserTableDef TB_USER = new TUserTableDef();
public final QueryColumn ID = new QueryColumn(this, "id");