Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more test cases on ShardingTableToken #33676

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public String toString(final RouteUnit routeUnit) {

private String getConstraintValue(final RouteUnit routeUnit) {
StringBuilder result = new StringBuilder(identifier.getValue());
Map<String, String> logicAndActualTables = TokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule);
Map<String, String> logicAndActualTables = ShardingTokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule);
((TableAvailable) sqlStatementContext).getTablesContext().getTableNames().stream().findFirst().map(logicAndActualTables::get).ifPresent(optional -> result.append('_').append(optional));
return result.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public String toString(final RouteUnit routeUnit) {
}

private String getCursorValue(final RouteUnit routeUnit) {
Map<String, String> logicAndActualTables = TokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule);
Map<String, String> logicAndActualTables = ShardingTokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule);
String actualTableName = logicAndActualTables.values().stream().sorted().findFirst().orElse(null);
return Strings.isNullOrEmpty(actualTableName) ? identifier.getValue() : identifier.getValue() + "_" + actualTableName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private String getIndexValue(final RouteUnit routeUnit) {
if (logicTableName.isPresent() && !shardingRule.isShardingTable(logicTableName.get())) {
return identifier.getValue();
}
Map<String, String> logicAndActualTables = TokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule);
Map<String, String> logicAndActualTables = ShardingTokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule);
String actualTableName = logicTableName.map(logicAndActualTables::get).orElseGet(() -> logicAndActualTables.isEmpty() ? null : logicAndActualTables.values().iterator().next());
return IndexMetaDataUtils.getActualIndexName(identifier.getValue(), actualTableName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public String toString(final RouteUnit routeUnit) {
}

private String getActualTableName(final RouteUnit routeUnit) {
String actualTableName = TokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule).get(tableName.getValue());
String actualTableName = ShardingTokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule).get(tableName.getValue());
actualTableName = null == actualTableName ? tableName.getValue() : actualTableName;
return tableName.getQuoteCharacter().wrap(actualTableName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@
import java.util.Map;

/**
* Token utils.
* Sharding token utils.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TokenUtils {
public final class ShardingTokenUtils {

/**
* Get logic and actual table map.
*
* @param routeUnit route unit
* @param sqlStatementContext SQL statement context
* @param shardingRule sharding rule
* @param rule sharding rule
* @return key is logic table name, values is actual table belong to this data source
*/
public static Map<String, String> getLogicAndActualTableMap(final RouteUnit routeUnit, final SQLStatementContext sqlStatementContext, final ShardingRule shardingRule) {
public static Map<String, String> getLogicAndActualTableMap(final RouteUnit routeUnit, final SQLStatementContext sqlStatementContext, final ShardingRule rule) {
if (!(sqlStatementContext instanceof TableAvailable)) {
return Collections.emptyMap();
}
Collection<String> tableNames = ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames();
Map<String, String> result = new CaseInsensitiveMap<>(tableNames.size(), 1F);
for (RouteMapper each : routeUnit.getTableMappers()) {
result.put(each.getLogicName(), each.getActualName());
result.putAll(shardingRule.getLogicAndActualTablesFromBindingTable(routeUnit.getDataSourceMapper().getLogicName(), each.getLogicName(), each.getActualName(), tableNames));
result.putAll(rule.getLogicAndActualTablesFromBindingTable(routeUnit.getDataSourceMapper().getLogicName(), each.getLogicName(), each.getActualName(), tableNames));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

package org.apache.shardingsphere.sharding.rewrite.token.pojo;

import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
Expand All @@ -33,17 +31,27 @@
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;

class ShardingTableTokenTest {

@Test
void assertToString() {
ShardingTableToken tableToken = new ShardingTableToken(0, 0, new IdentifierValue("t_order"),
mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class).defaultAnswer(RETURNS_DEEP_STUBS)), mock(ShardingRule.class));
RouteUnit routeUnit = mock(RouteUnit.class);
when(routeUnit.getTableMappers()).thenReturn(Collections.singletonList(new RouteMapper("t_order", "t_order_0")));
when(routeUnit.getDataSourceMapper()).thenReturn(new RouteMapper(DefaultDatabase.LOGIC_NAME, "ds_0"));
assertThat(tableToken.toString(routeUnit), is("t_order_0"));
void assertToStringWithActualTableName() {
assertThat(createTableToken("foo_tbl").toString(mockRouteUnit()), is("foo_tbl_0"));
}

@Test
void assertToStringWithOriginalTableName() {
assertThat(createTableToken("bar_tbl").toString(mockRouteUnit()), is("bar_tbl"));
}

private ShardingTableToken createTableToken(final String tableName) {
return new ShardingTableToken(0, 0, new IdentifierValue(tableName), mock(SelectStatementContext.class, RETURNS_DEEP_STUBS), mock(ShardingRule.class));
}

private RouteUnit mockRouteUnit() {
RouteUnit result = mock(RouteUnit.class);
when(result.getDataSourceMapper()).thenReturn(new RouteMapper("foo_ds", "foo_ds_0"));
when(result.getTableMappers()).thenReturn(Collections.singletonList(new RouteMapper("foo_tbl", "foo_tbl_0")));
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,41 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;

class TokenUtilsTest {
class ShardingTokenUtilsTest {

@Test
void assertGetLogicAndActualTablesWithNotTableAvailable() {
Map<String, String> actual = ShardingTokenUtils.getLogicAndActualTableMap(mock(RouteUnit.class), mock(SQLStatementContext.class), mock(ShardingRule.class));
assertTrue(actual.isEmpty());
}

@Test
void assertGetLogicAndActualTablesFromRouteUnit() {
Map<String, String> actual = TokenUtils.getLogicAndActualTableMap(getRouteUnit(), mockSQLStatementContext(), mockShardingRule());
assertThat(actual.get("foo_table"), is("foo_table_0"));
assertThat(actual.get("bar_table"), is("bar_table_0"));
Map<String, String> actual = ShardingTokenUtils.getLogicAndActualTableMap(getRouteUnit(), mockSQLStatementContext(), mockShardingRule());
assertThat(actual.get("foo_tbl"), is("foo_tbl_0"));
assertThat(actual.get("bar_tbl"), is("bar_tbl_0"));
}

private RouteUnit getRouteUnit() {
return new RouteUnit(new RouteMapper(DefaultDatabase.LOGIC_NAME, "ds_0"), Collections.singleton(new RouteMapper("foo_table", "foo_table_0")));
return new RouteUnit(new RouteMapper(DefaultDatabase.LOGIC_NAME, "ds_0"), Collections.singleton(new RouteMapper("foo_tbl", "foo_tbl_0")));
}

private static SQLStatementContext mockSQLStatementContext() {
SQLStatementContext result = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class).defaultAnswer(RETURNS_DEEP_STUBS));
when(((TableAvailable) result).getTablesContext().getTableNames()).thenReturn(Arrays.asList("foo_table", "bar_table"));
when(((TableAvailable) result).getTablesContext().getTableNames()).thenReturn(Arrays.asList("foo_tbl", "bar_tbl"));
return result;
}

private static ShardingRule mockShardingRule() {
ShardingRule result = mock(ShardingRule.class);
when(result.getLogicAndActualTablesFromBindingTable(DefaultDatabase.LOGIC_NAME, "foo_table", "foo_table_0", Arrays.asList("foo_table", "bar_table")))
.thenReturn(Collections.singletonMap("bar_table", "bar_table_0"));
when(result.getLogicAndActualTablesFromBindingTable(DefaultDatabase.LOGIC_NAME, "foo_tbl", "foo_tbl_0", Arrays.asList("foo_tbl", "bar_tbl")))
.thenReturn(Collections.singletonMap("bar_tbl", "bar_tbl_0"));
return result;
}
}