|
18 | 18 | package org.apache.shardingsphere.sharding.rewrite.token.pojo; |
19 | 19 |
|
20 | 20 | import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; |
21 | | -import org.apache.shardingsphere.infra.binder.context.type.TableAvailable; |
| 21 | +import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateIndexStatementContext; |
| 22 | +import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; |
22 | 23 | import org.apache.shardingsphere.infra.database.core.DefaultDatabase; |
23 | 24 | import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; |
24 | 25 | import org.apache.shardingsphere.infra.route.context.RouteMapper; |
|
27 | 28 | import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; |
28 | 29 | import org.junit.jupiter.api.Test; |
29 | 30 |
|
| 31 | +import java.util.Arrays; |
30 | 32 | import java.util.Collections; |
31 | 33 |
|
32 | 34 | import static org.hamcrest.CoreMatchers.is; |
33 | 35 | import static org.hamcrest.MatcherAssert.assertThat; |
34 | 36 | import static org.mockito.Mockito.RETURNS_DEEP_STUBS; |
35 | 37 | import static org.mockito.Mockito.mock; |
36 | 38 | import static org.mockito.Mockito.when; |
37 | | -import static org.mockito.Mockito.withSettings; |
38 | 39 |
|
39 | 40 | class IndexTokenTest { |
40 | 41 |
|
41 | 42 | @Test |
42 | | - void assertToString() { |
43 | | - IndexToken indexToken = new IndexToken(0, 0, new IdentifierValue("t_order_index"), |
44 | | - mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class).defaultAnswer(RETURNS_DEEP_STUBS)), mock(ShardingRule.class), mock(ShardingSphereSchema.class)); |
45 | | - RouteUnit routeUnit = mock(RouteUnit.class); |
46 | | - when(routeUnit.getTableMappers()).thenReturn(Collections.singletonList(new RouteMapper("t_order", "t_order_0"))); |
47 | | - when(routeUnit.getDataSourceMapper()).thenReturn(new RouteMapper(DefaultDatabase.LOGIC_NAME, "ds_0")); |
48 | | - assertThat(indexToken.toString(routeUnit), is("t_order_index_t_order_0")); |
| 43 | + void assertToStringWithNotShardingTable() { |
| 44 | + IndexToken indexToken = new IndexToken(0, 0, new IdentifierValue("foo_idx"), mock(SelectStatementContext.class, RETURNS_DEEP_STUBS), mock(ShardingRule.class), mockSchema()); |
| 45 | + assertThat(indexToken.toString(mockRouteUnit()), is("foo_idx")); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void assertToStringWithShardingTable() { |
| 50 | + ShardingRule rule = mock(ShardingRule.class); |
| 51 | + when(rule.isShardingTable("foo_tbl")).thenReturn(true); |
| 52 | + IndexToken indexToken = new IndexToken(0, 0, new IdentifierValue("foo_idx"), mock(SelectStatementContext.class, RETURNS_DEEP_STUBS), rule, mockSchema()); |
| 53 | + assertThat(indexToken.toString(mockRouteUnit()), is("foo_idx_foo_tbl_0")); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void assertToStringWithShardingTableButNotTableAvailable() { |
| 58 | + ShardingRule rule = mock(ShardingRule.class); |
| 59 | + when(rule.isShardingTable("foo_tbl")).thenReturn(true); |
| 60 | + IndexToken indexToken = new IndexToken(0, 0, new IdentifierValue("foo_idx"), mock(SQLStatementContext.class, RETURNS_DEEP_STUBS), rule, mockSchema()); |
| 61 | + assertThat(indexToken.toString(mockRouteUnit()), is("foo_idx")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void assertToStringWithShardingTableAndGeneratedIndex() { |
| 66 | + CreateIndexStatementContext sqlStatementContext = mock(CreateIndexStatementContext.class, RETURNS_DEEP_STUBS); |
| 67 | + when(sqlStatementContext.isGeneratedIndex()).thenReturn(true); |
| 68 | + IndexToken indexToken = new IndexToken(0, 0, new IdentifierValue("bar_idx"), sqlStatementContext, mock(ShardingRule.class), mockSchema()); |
| 69 | + assertThat(indexToken.toString(mockRouteUnit()), is(" bar_idx_foo_tbl_0 ")); |
| 70 | + } |
| 71 | + |
| 72 | + private ShardingSphereSchema mockSchema() { |
| 73 | + ShardingSphereSchema result = mock(ShardingSphereSchema.class, RETURNS_DEEP_STUBS); |
| 74 | + when(result.getAllTableNames()).thenReturn(Arrays.asList("no_tbl", "foo_tbl")); |
| 75 | + when(result.getTable("foo_tbl").containsIndex("foo_idx")).thenReturn(true); |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + private RouteUnit mockRouteUnit() { |
| 80 | + RouteUnit result = mock(RouteUnit.class); |
| 81 | + when(result.getTableMappers()).thenReturn(Collections.singleton(new RouteMapper("foo_tbl", "foo_tbl_0"))); |
| 82 | + when(result.getDataSourceMapper()).thenReturn(new RouteMapper(DefaultDatabase.LOGIC_NAME, "ds_0")); |
| 83 | + return result; |
49 | 84 | } |
50 | 85 | } |
0 commit comments