Skip to content

Commit

Permalink
Update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
RaigorJiang committed Aug 22, 2024
1 parent e874851 commit 2dca25f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

package org.apache.shardingsphere.distsql.handler.executor.rql.resource;

import org.apache.groovy.util.Maps;
import org.apache.shardingsphere.distsql.statement.rql.resource.ShowLogicalTablesStatement;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.TableType;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
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.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -32,9 +35,9 @@
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -56,7 +59,8 @@ void setUp() {
when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
when(database.getSchema("foo_db")).thenReturn(schema);
when(schema.getAllTableNames()).thenReturn(Arrays.asList("t_order", "t_order_item"));
Map<String, ShardingSphereTable> tables = Maps.of("t_order", mockShardingSphereTable("t_order"), "t_order_item", mockShardingSphereTable("t_order_item"));
when(schema.getTables()).thenReturn(tables);
executor.setDatabase(database);
}

Expand All @@ -78,4 +82,20 @@ void assertRowDataWithLike() {
Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
assertThat(iterator.next().getCell(1), is("t_order_item"));
}

@Test
void assertRowDataWithFullAndLike() {
Collection<LocalDataQueryResultRow> actual = executor.getRows(new ShowLogicalTablesStatement(true, null, "t_order_%"), mock(ContextManager.class));
assertThat(actual.size(), is(1));
LocalDataQueryResultRow row = actual.iterator().next();
assertThat(row.getCell(1), is("t_order_item"));
assertThat(row.getCell(2), is("TABLE"));
}

private ShardingSphereTable mockShardingSphereTable(final String tableName) {
ShardingSphereTable result = mock(ShardingSphereTable.class);
when(result.getName()).thenReturn(tableName);
when(result.getType()).thenReturn(TableType.TABLE);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,22 @@ void assertGetRowsWithAllStorageUnits() {
nameMap.put(2, "ds_0");
Collection<LocalDataQueryResultRow> actual = executor.getRows(new ShowStorageUnitsStatement(mock(DatabaseSegment.class), null, null), mock(ContextManager.class));
assertThat(actual.size(), is(3));
Iterator<LocalDataQueryResultRow> rowData = actual.iterator();
Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
int index = 0;
while (rowData.hasNext()) {
LocalDataQueryResultRow data = rowData.next();
assertThat(data.getCell(1), is(nameMap.get(index)));
assertThat(data.getCell(2), is("MySQL"));
assertThat(data.getCell(3), is("localhost"));
assertThat(data.getCell(4), is("3307"));
assertThat(data.getCell(5), is(nameMap.get(index)));
assertThat(data.getCell(6), is(""));
assertThat(data.getCell(7), is(""));
assertThat(data.getCell(8), is(""));
assertThat(data.getCell(9), is("100"));
assertThat(data.getCell(10), is("10"));
assertThat(data.getCell(11), is(""));
assertThat(data.getCell(12), is("{\"openedConnections\":[],\"closed\":false}"));
while (iterator.hasNext()) {
LocalDataQueryResultRow row = iterator.next();
assertThat(row.getCell(1), is(nameMap.get(index)));
assertThat(row.getCell(2), is("MySQL"));
assertThat(row.getCell(3), is("localhost"));
assertThat(row.getCell(4), is("3307"));
assertThat(row.getCell(5), is(nameMap.get(index)));
assertThat(row.getCell(6), is(""));
assertThat(row.getCell(7), is(""));
assertThat(row.getCell(8), is(""));
assertThat(row.getCell(9), is("100"));
assertThat(row.getCell(10), is("10"));
assertThat(row.getCell(11), is(""));
assertThat(row.getCell(12), is("{\"openedConnections\":[],\"closed\":false}"));
index++;
}
}
Expand All @@ -102,19 +102,19 @@ void assertGetRowsWithAllStorageUnits() {
void assertGetRowsWithLikePattern() {
Collection<LocalDataQueryResultRow> actual = executor.getRows(new ShowStorageUnitsStatement(mock(DatabaseSegment.class), "_0", null), mock(ContextManager.class));
assertThat(actual.size(), is(1));
LocalDataQueryResultRow data = actual.iterator().next();
assertThat(data.getCell(1), is("ds_0"));
assertThat(data.getCell(2), is("MySQL"));
assertThat(data.getCell(3), is("localhost"));
assertThat(data.getCell(4), is("3307"));
assertThat(data.getCell(5), is("ds_0"));
assertThat(data.getCell(6), is(""));
assertThat(data.getCell(7), is(""));
assertThat(data.getCell(8), is(""));
assertThat(data.getCell(9), is("100"));
assertThat(data.getCell(10), is("10"));
assertThat(data.getCell(11), is(""));
assertThat(data.getCell(12), is("{\"openedConnections\":[],\"closed\":false}"));
LocalDataQueryResultRow row = actual.iterator().next();
assertThat(row.getCell(1), is("ds_0"));
assertThat(row.getCell(2), is("MySQL"));
assertThat(row.getCell(3), is("localhost"));
assertThat(row.getCell(4), is("3307"));
assertThat(row.getCell(5), is("ds_0"));
assertThat(row.getCell(6), is(""));
assertThat(row.getCell(7), is(""));
assertThat(row.getCell(8), is(""));
assertThat(row.getCell(9), is("100"));
assertThat(row.getCell(10), is("10"));
assertThat(row.getCell(11), is(""));
assertThat(row.getCell(12), is("{\"openedConnections\":[],\"closed\":false}"));
}

@Test
Expand All @@ -123,20 +123,19 @@ void assertGetRowsWithUnusedStorageUnits() {
when(database.getRuleMetaData()).thenReturn(metaData);
Collection<LocalDataQueryResultRow> actual = executor.getRows(new ShowStorageUnitsStatement(mock(DatabaseSegment.class), null, 0), mock(ContextManager.class));
assertThat(actual.size(), is(1));
Iterator<LocalDataQueryResultRow> rowData = actual.iterator();
LocalDataQueryResultRow data = rowData.next();
assertThat(data.getCell(1), is("ds_2"));
assertThat(data.getCell(2), is("MySQL"));
assertThat(data.getCell(3), is("localhost"));
assertThat(data.getCell(4), is("3307"));
assertThat(data.getCell(5), is("ds_2"));
assertThat(data.getCell(6), is(""));
assertThat(data.getCell(7), is(""));
assertThat(data.getCell(8), is(""));
assertThat(data.getCell(9), is("100"));
assertThat(data.getCell(10), is("10"));
assertThat(data.getCell(11), is(""));
assertThat(data.getCell(12), is("{\"openedConnections\":[],\"closed\":false}"));
LocalDataQueryResultRow row = actual.iterator().next();
assertThat(row.getCell(1), is("ds_2"));
assertThat(row.getCell(2), is("MySQL"));
assertThat(row.getCell(3), is("localhost"));
assertThat(row.getCell(4), is("3307"));
assertThat(row.getCell(5), is("ds_2"));
assertThat(row.getCell(6), is(""));
assertThat(row.getCell(7), is(""));
assertThat(row.getCell(8), is(""));
assertThat(row.getCell(9), is("100"));
assertThat(row.getCell(10), is("10"));
assertThat(row.getCell(11), is(""));
assertThat(row.getCell(12), is("{\"openedConnections\":[],\"closed\":false}"));
}

private RuleMetaData mockUnusedStorageUnitsRuleMetaData() {
Expand Down
2 changes: 1 addition & 1 deletion test/it/parser/src/main/resources/case/rql/show.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,6 @@
</show-logical-tables>

<show-logical-tables sql-case-id="show-logical-tables-like" like-pattern="t_%">
<database name="sharding_db" start-index="36" stop-index="46" />
<database name="sharding_db" start-index="25" stop-index="35" />
</show-logical-tables>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@
<sql-case id="show-storage-units-where-usage-count" value="SHOW STORAGE UNITS FROM sharding_db WHERE USAGE_COUNT = 0" db-types="ShardingSphere" />
<sql-case id="show-logical-tables" value="SHOW LOGICAL TABLES" db-types="ShardingSphere" />
<sql-case id="show-logical-tables-from" value="SHOW LOGICAL TABLES FROM sharding_db" db-types="ShardingSphere" />
<sql-case id="show-logical-tables-like" value="SHOW LOGICAL TABLES LIKE 't_%' FROM sharding_db" db-types="ShardingSphere" />
<sql-case id="show-logical-tables-like" value="SHOW LOGICAL TABLES FROM sharding_db LIKE 't_%' " db-types="ShardingSphere" />
</sql-cases>

0 comments on commit 2dca25f

Please sign in to comment.