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 d2082d890ac Add more test cases on ShowSingleTablesExecutorTest
(#38095)
d2082d890ac is described below
commit d2082d890ac8a962950443f65208db3a62281fef
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Feb 19 13:05:02 2026 +0800
Add more test cases on ShowSingleTablesExecutorTest (#38095)
---
.../query/ShowSingleTablesExecutorTest.java | 93 ++++++++++++++++++----
1 file changed, 77 insertions(+), 16 deletions(-)
diff --git
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTablesExecutorTest.java
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTablesExecutorTest.java
index d1f86725da3..9f31a618fc2 100644
---
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTablesExecutorTest.java
+++
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTablesExecutorTest.java
@@ -17,35 +17,98 @@
package org.apache.shardingsphere.single.distsql.handler.query;
-import org.apache.shardingsphere.distsql.statement.DistSQLStatement;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import
org.apache.shardingsphere.database.connector.core.type.DatabaseTypeRegistry;
+import
org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecutor;
import org.apache.shardingsphere.infra.datanode.DataNode;
import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import
org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;
-import org.apache.shardingsphere.single.config.SingleRuleConfiguration;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.single.distsql.statement.rql.ShowSingleTablesStatement;
import org.apache.shardingsphere.single.rule.SingleRule;
-import
org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorAssert;
-import
org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLRuleQueryExecutorSettings;
-import
org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLRuleQueryExecutorTestCaseArgumentsProvider;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.ArgumentsSource;
+import org.junit.jupiter.api.Test;
+import org.mockito.Answers;
+import org.mockito.MockedConstruction;
-import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.withSettings;
-@DistSQLRuleQueryExecutorSettings("cases/show-single-tables.xml")
class ShowSingleTablesExecutorTest {
- private static SingleRule mockRule() {
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ private final ShowSingleTablesExecutor executor =
(ShowSingleTablesExecutor)
TypedSPILoader.getService(DistSQLQueryExecutor.class,
ShowSingleTablesStatement.class);
+
+ @Test
+ void assertGetColumnNamesWithoutSchema() {
+ executor.setDatabase(new ShardingSphereDatabase(
+ "foo_db", databaseType, new
ResourceMetaData(Collections.emptyMap(), Collections.emptyMap()), new
RuleMetaData(Collections.emptyList()), Collections.emptyList()));
+ assertThat(executor.getColumnNames(new ShowSingleTablesStatement(null,
null)), is(Arrays.asList("table_name", "storage_unit_name")));
+ }
+
+ @Test
+ void assertGetColumnNamesWithSchema() {
+ try (
+ MockedConstruction<DatabaseTypeRegistry> ignored =
mockConstruction(DatabaseTypeRegistry.class,
withSettings().defaultAnswer(Answers.RETURNS_DEEP_STUBS),
+ (mock, context) ->
when(mock.getDialectDatabaseMetaData().getSchemaOption().isSchemaAvailable()).thenReturn(true)))
{
+ executor.setDatabase(new ShardingSphereDatabase(
+ "foo_db", databaseType, new
ResourceMetaData(Collections.emptyMap(), Collections.emptyMap()), new
RuleMetaData(Collections.emptyList()), Collections.emptyList()));
+ assertThat(executor.getColumnNames(new
ShowSingleTablesStatement(null, null)), is(Arrays.asList("table_name",
"storage_unit_name", "schema_name")));
+ }
+ }
+
+ @Test
+ void assertGetRowsWithoutLikePattern() {
+ executor.setDatabase(new ShardingSphereDatabase(
+ "foo_db", databaseType, new
ResourceMetaData(Collections.emptyMap(), Collections.emptyMap()), new
RuleMetaData(Collections.emptyList()), Collections.emptyList()));
Map<String, Collection<DataNode>> singleTableDataNodeMap = new
HashMap<>(2, 1F);
- singleTableDataNodeMap.put("t_order", Collections.singleton(new
DataNode("ds_1", (String) null, "t_order")));
singleTableDataNodeMap.put("t_order_item", Collections.singleton(new
DataNode("ds_2", (String) null, "t_order_item")));
+ singleTableDataNodeMap.put("t_order", Collections.singleton(new
DataNode("ds_1", (String) null, "t_order")));
+ executor.setRule(mockRule(singleTableDataNodeMap));
+ List<LocalDataQueryResultRow> actualRowList = new
ArrayList<>(executor.getRows(new ShowSingleTablesStatement(null, null), null));
+ assertThat(actualRowList.size(), is(2));
+ assertThat(actualRowList.get(0).getCell(1), is("t_order"));
+ assertThat(actualRowList.get(0).getCell(2), is("ds_1"));
+ assertThat(actualRowList.get(1).getCell(1), is("t_order_item"));
+ assertThat(actualRowList.get(1).getCell(2), is("ds_2"));
+ }
+
+ @Test
+ void assertGetRowsWithLikePatternAndSchema() {
+ try (
+ MockedConstruction<DatabaseTypeRegistry> ignored =
mockConstruction(DatabaseTypeRegistry.class,
withSettings().defaultAnswer(Answers.RETURNS_DEEP_STUBS),
+ (mock, context) ->
when(mock.getDialectDatabaseMetaData().getSchemaOption().isSchemaAvailable()).thenReturn(true)))
{
+ executor.setDatabase(new ShardingSphereDatabase(
+ "foo_db", databaseType, new
ResourceMetaData(Collections.emptyMap(), Collections.emptyMap()), new
RuleMetaData(Collections.emptyList()), Collections.emptyList()));
+ Map<String, Collection<DataNode>> singleTableDataNodeMap = new
HashMap<>(2, 1F);
+ singleTableDataNodeMap.put("t_order", Collections.singleton(new
DataNode("ds_1", "public", "t_order")));
+ singleTableDataNodeMap.put("t_order_item",
Collections.singleton(new DataNode("ds_2", "public", "t_order_item")));
+ executor.setRule(mockRule(singleTableDataNodeMap));
+ List<LocalDataQueryResultRow> actualRowList = new
ArrayList<>(executor.getRows(new ShowSingleTablesStatement(null, "t_order"),
null));
+ assertThat(actualRowList.size(), is(1));
+ assertThat(actualRowList.get(0).getCell(1), is("t_order"));
+ assertThat(actualRowList.get(0).getCell(2), is("ds_1"));
+ assertThat(actualRowList.get(0).getCell(3), is("public"));
+ }
+ }
+
+ private SingleRule mockRule(final Map<String, Collection<DataNode>>
singleTableDataNodeMap) {
DataNodeRuleAttribute ruleAttribute =
mock(DataNodeRuleAttribute.class);
when(ruleAttribute.getAllDataNodes()).thenReturn(singleTableDataNodeMap);
SingleRule result = mock(SingleRule.class);
@@ -53,10 +116,8 @@ class ShowSingleTablesExecutorTest {
return result;
}
- @ParameterizedTest(name = "DistSQL -> {0}")
- @ArgumentsSource(DistSQLRuleQueryExecutorTestCaseArgumentsProvider.class)
- void assertExecuteQuery(@SuppressWarnings("unused") final String distSQL,
final DistSQLStatement sqlStatement,
- final SingleRuleConfiguration currentRuleConfig,
final Collection<LocalDataQueryResultRow> expected) throws SQLException {
- new
DistSQLDatabaseRuleQueryExecutorAssert(mockRule()).assertQueryResultRows(currentRuleConfig,
sqlStatement, expected);
+ @Test
+ void assertGetRuleClass() {
+ assertThat(executor.getRuleClass(), is(SingleRule.class));
}
}