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 IndexToken #33677

Merged
merged 1 commit into from
Nov 15, 2024
Merged
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 @@ -18,7 +18,8 @@
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.binder.context.statement.ddl.CreateIndexStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
Expand All @@ -27,24 +28,58 @@
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
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 IndexTokenTest {

@Test
void assertToString() {
IndexToken indexToken = new IndexToken(0, 0, new IdentifierValue("t_order_index"),
mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class).defaultAnswer(RETURNS_DEEP_STUBS)), mock(ShardingRule.class), mock(ShardingSphereSchema.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(indexToken.toString(routeUnit), is("t_order_index_t_order_0"));
void assertToStringWithNotShardingTable() {
IndexToken indexToken = new IndexToken(0, 0, new IdentifierValue("foo_idx"), mock(SelectStatementContext.class, RETURNS_DEEP_STUBS), mock(ShardingRule.class), mockSchema());
assertThat(indexToken.toString(mockRouteUnit()), is("foo_idx"));
}

@Test
void assertToStringWithShardingTable() {
ShardingRule rule = mock(ShardingRule.class);
when(rule.isShardingTable("foo_tbl")).thenReturn(true);
IndexToken indexToken = new IndexToken(0, 0, new IdentifierValue("foo_idx"), mock(SelectStatementContext.class, RETURNS_DEEP_STUBS), rule, mockSchema());
assertThat(indexToken.toString(mockRouteUnit()), is("foo_idx_foo_tbl_0"));
}

@Test
void assertToStringWithShardingTableButNotTableAvailable() {
ShardingRule rule = mock(ShardingRule.class);
when(rule.isShardingTable("foo_tbl")).thenReturn(true);
IndexToken indexToken = new IndexToken(0, 0, new IdentifierValue("foo_idx"), mock(SQLStatementContext.class, RETURNS_DEEP_STUBS), rule, mockSchema());
assertThat(indexToken.toString(mockRouteUnit()), is("foo_idx"));
}

@Test
void assertToStringWithShardingTableAndGeneratedIndex() {
CreateIndexStatementContext sqlStatementContext = mock(CreateIndexStatementContext.class, RETURNS_DEEP_STUBS);
when(sqlStatementContext.isGeneratedIndex()).thenReturn(true);
IndexToken indexToken = new IndexToken(0, 0, new IdentifierValue("bar_idx"), sqlStatementContext, mock(ShardingRule.class), mockSchema());
assertThat(indexToken.toString(mockRouteUnit()), is(" bar_idx_foo_tbl_0 "));
}

private ShardingSphereSchema mockSchema() {
ShardingSphereSchema result = mock(ShardingSphereSchema.class, RETURNS_DEEP_STUBS);
when(result.getAllTableNames()).thenReturn(Arrays.asList("no_tbl", "foo_tbl"));
when(result.getTable("foo_tbl").containsIndex("foo_idx")).thenReturn(true);
return result;
}

private RouteUnit mockRouteUnit() {
RouteUnit result = mock(RouteUnit.class);
when(result.getTableMappers()).thenReturn(Collections.singleton(new RouteMapper("foo_tbl", "foo_tbl_0")));
when(result.getDataSourceMapper()).thenReturn(new RouteMapper(DefaultDatabase.LOGIC_NAME, "ds_0"));
return result;
}
}