This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 9bfea2506c0 Add unit test for
ShardingRemoveTokenGenerator#generateSQLTokens (#19720)
9bfea2506c0 is described below
commit 9bfea2506c072c7ef61f4538ac4b5a57e3827886
Author: skai <[email protected]>
AuthorDate: Sun Jul 31 21:06:59 2022 +0800
Add unit test for ShardingRemoveTokenGenerator#generateSQLTokens (#19720)
* add unit test for ShardingRemoveTokenGenerator.generateSQLTokens
* add test
* fix checkstyle
Co-authored-by: skai <[email protected]>
---
.../token/ShardingRemoveTokenGeneratorTest.java | 43 ++++++++++++++++++++++
.../dml/order/item/IndexOrderByItemSegment.java | 3 +-
2 files changed, 44 insertions(+), 2 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ShardingRemoveTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ShardingRemoveTokenGeneratorTest.java
index b3117dcc6de..3dff400e9a0 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ShardingRemoveTokenGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ShardingRemoveTokenGeneratorTest.java
@@ -19,15 +19,36 @@ package org.apache.shardingsphere.sharding.rewrite.token;
import
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.database.DefaultDatabase;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken;
+import
org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic.RemoveToken;
import
org.apache.shardingsphere.sharding.rewrite.token.generator.impl.ShardingRemoveTokenGenerator;
+import
org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
+import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationDistinctProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.GroupBySegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.IndexOrderByItemSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
+import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement;
import org.junit.Test;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
public final class ShardingRemoveTokenGeneratorTest {
@Test
@@ -47,4 +68,26 @@ public final class ShardingRemoveTokenGeneratorTest {
SelectStatementContext selectStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
assertTrue(new
ShardingRemoveTokenGenerator().isGenerateSQLToken(selectStatementContext));
}
+
+ @Test
+ public void assertGenerateSQLTokens() {
+ Collection<? extends SQLToken> sqlTokens = new
ShardingRemoveTokenGenerator().generateSQLTokens(createUpdatesStatementContext());
+ assertThat(sqlTokens.size(), is(1));
+ assertThat(sqlTokens.iterator().next(), instanceOf(RemoveToken.class));
+ }
+
+ private static SelectStatementContext createUpdatesStatementContext() {
+ MySQLSelectStatement selectStatement = new MySQLSelectStatement();
+ selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0,
0, new IdentifierValue("t_user"))));
+ selectStatement.setGroupBy(new GroupBySegment(0, 0, Arrays.asList(new
IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC))));
+ selectStatement.setProjections(createProjectionsSegment());
+ return new SelectStatementContext(
+ Collections.singletonMap(DefaultDatabase.LOGIC_NAME,
mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)),
Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME);
+ }
+
+ private static ProjectionsSegment createProjectionsSegment() {
+ ProjectionsSegment result = new ProjectionsSegment(0, 8);
+ result.getProjections().add(new
AggregationDistinctProjectionSegment(0, 8, AggregationType.SUM, "number",
"(DISTINCT number)"));
+ return result;
+ }
}
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/order/item/IndexOrderByItemSegment.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/order/item/IndexOrderByItemSegment.java
index 1bc55889ca7..eea2a7cc449 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/order/item/IndexOrderByItemSegment.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/order/item/IndexOrderByItemSegment.java
@@ -36,7 +36,6 @@ public final class IndexOrderByItemSegment extends
OrderByItemSegment {
}
public IndexOrderByItemSegment(final int startIndex, final int stopIndex,
final int columnIndex, final OrderDirection orderDirection) {
- super(startIndex, stopIndex, orderDirection, OrderDirection.ASC);
- this.columnIndex = columnIndex;
+ this(startIndex, stopIndex, columnIndex, orderDirection,
OrderDirection.ASC);
}
}