From fa2639537f375cb0390bcbb2e90c8f5d312edc30 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Mon, 11 Mar 2024 15:32:13 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E5=8F=97=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E5=BD=B1=E5=93=8D=E7=9A=84=E6=B5=8B=E8=AF=95=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coretest/AccountSqlTester.java | 37 ++++++++++--------- .../com/mybatisflex/coretest/AuthTest.java | 25 ++++++++++--- .../coretest/DynamicConditionTest.java | 14 ++----- .../FlexGlobalConfigTest.java | 19 +++++++++- .../coretest/auth/AuthDialectImpl.java | 22 +++++++++-- .../test/issue113/def/TClassTableDef.java | 20 +++++++++- .../test/issue113/def/TUserTableDef.java | 20 +++++++++- 7 files changed, 115 insertions(+), 42 deletions(-) rename mybatis-flex-core/src/test/java/com/mybatisflex/{core => coretest}/FlexGlobalConfigTest.java (78%) diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java index 36d9be9b..2560df07 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java @@ -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())); } + } diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AuthTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AuthTest.java index e636b2f2..4fd95997 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AuthTest.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AuthTest.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *
+ * 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 + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + *
+ * 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` = ?" diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java index c655d1a1..481c6eb7 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java @@ -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); diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/core/FlexGlobalConfigTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/FlexGlobalConfigTest.java similarity index 78% rename from mybatis-flex-core/src/test/java/com/mybatisflex/core/FlexGlobalConfigTest.java rename to mybatis-flex-core/src/test/java/com/mybatisflex/coretest/FlexGlobalConfigTest.java index f2941376..ebd9edf7 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/core/FlexGlobalConfigTest.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/FlexGlobalConfigTest.java @@ -1,6 +1,23 @@ -package com.mybatisflex.core; +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *
+ * 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 + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + *
+ * 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; diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/auth/AuthDialectImpl.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/auth/AuthDialectImpl.java index 7081c796..bcb0989d 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/auth/AuthDialectImpl.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/auth/AuthDialectImpl.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *
+ * 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 + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + *
+ * 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); diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/issue113/def/TClassTableDef.java b/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/issue113/def/TClassTableDef.java index 6e858ce0..e234f606 100644 --- a/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/issue113/def/TClassTableDef.java +++ b/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/issue113/def/TClassTableDef.java @@ -1,9 +1,25 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *
+ * 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 + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + *
+ * 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"); diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/issue113/def/TUserTableDef.java b/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/issue113/def/TUserTableDef.java index 065688a3..9e8d10ae 100644 --- a/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/issue113/def/TUserTableDef.java +++ b/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/issue113/def/TUserTableDef.java @@ -1,9 +1,25 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *
+ * 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 + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + *
+ * 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");