Skip to content

Commit

Permalink
Revise test cases for sharding checker (#33925)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Dec 5, 2024
1 parent e954120 commit f06caea
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
class ShardingAlterIndexSupportedCheckerTest {

@Mock
private ShardingRule shardingRule;
private ShardingRule rule;

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ShardingSphereDatabase database;
Expand All @@ -60,7 +60,7 @@ void assertCheckWhenIndexExistRenameIndexNotExistForPostgreSQL() {
when(schema.getAllTables()).thenReturn(Collections.singleton(table));
when(table.containsIndex("t_order_index")).thenReturn(true);
when(table.containsIndex("t_order_index_new")).thenReturn(false);
assertDoesNotThrow(() -> new ShardingAlterIndexSupportedChecker().check(shardingRule, database, schema, new AlterIndexStatementContext(sqlStatement, "foo_db")));
assertDoesNotThrow(() -> new ShardingAlterIndexSupportedChecker().check(rule, database, schema, new AlterIndexStatementContext(sqlStatement, "foo_db")));
}

@Test
Expand All @@ -70,7 +70,7 @@ void assertCheckWhenIndexNotExistRenameIndexNotExistForPostgreSQL() {
sqlStatement.setRenameIndex(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index_new"))));
ShardingSphereTable table = mock(ShardingSphereTable.class);
when(database.getSchema("public").getTable("t_order")).thenReturn(table);
assertThrows(IndexNotExistedException.class, () -> new ShardingAlterIndexSupportedChecker().check(shardingRule, database, mock(), new AlterIndexStatementContext(sqlStatement, "foo_db")));
assertThrows(IndexNotExistedException.class, () -> new ShardingAlterIndexSupportedChecker().check(rule, database, mock(), new AlterIndexStatementContext(sqlStatement, "foo_db")));
}

@Test
Expand All @@ -84,6 +84,6 @@ void assertCheckAlterIndexWhenIndexExistRenameIndexExistForPostgreSQL() {
when(table.containsIndex("t_order_index")).thenReturn(true);
when(table.containsIndex("t_order_index_new")).thenReturn(true);
assertThrows(DuplicateIndexException.class,
() -> new ShardingAlterIndexSupportedChecker().check(shardingRule, database, schema, new AlterIndexStatementContext(sqlStatement, "foo_db")));
() -> new ShardingAlterIndexSupportedChecker().check(rule, database, schema, new AlterIndexStatementContext(sqlStatement, "foo_db")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
class ShardingAlterTableSupportedCheckerTest {

@Mock
private ShardingRule shardingRule;
private ShardingRule rule;

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ShardingSphereDatabase database;
Expand All @@ -52,7 +52,7 @@ void assertCheckWithRenameTableWithShardingTable() {
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
sqlStatement.setRenameTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_new"))));
AlterTableStatementContext sqlStatementContext = new AlterTableStatementContext(sqlStatement, "foo_db");
when(shardingRule.containsShardingTable(Arrays.asList("t_order", "t_order_new"))).thenReturn(true);
assertThrows(UnsupportedShardingOperationException.class, () -> new ShardingAlterTableSupportedChecker().check(shardingRule, database, mock(), sqlStatementContext));
when(rule.containsShardingTable(Arrays.asList("t_order", "t_order_new"))).thenReturn(true);
assertThrows(UnsupportedShardingOperationException.class, () -> new ShardingAlterTableSupportedChecker().check(rule, database, mock(), sqlStatementContext));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
class ShardingCreateIndexSupportedCheckerTest {

@Mock
private ShardingRule shardingRule;
private ShardingRule rule;

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ShardingSphereDatabase database;
Expand All @@ -57,7 +57,7 @@ void assertCheckWhenTableExistIndexNotExistForPostgreSQL() {
sqlStatement.setIndex(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
when(schema.containsTable("t_order")).thenReturn(true);
assertDoesNotThrow(() -> new ShardingCreateIndexSupportedChecker().check(shardingRule, database, schema, new CreateIndexStatementContext(sqlStatement, "foo_db")));
assertDoesNotThrow(() -> new ShardingCreateIndexSupportedChecker().check(rule, database, schema, new CreateIndexStatementContext(sqlStatement, "foo_db")));
}

@Test
Expand All @@ -67,7 +67,7 @@ void assertCheckWhenTableNotExistIndexNotExistForPostgreSQL() {
sqlStatement.setIndex(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
when(schema.containsTable("t_order")).thenReturn(false);
assertThrows(NoSuchTableException.class, () -> new ShardingCreateIndexSupportedChecker().check(shardingRule, database, schema, new CreateIndexStatementContext(sqlStatement, "foo_db")));
assertThrows(NoSuchTableException.class, () -> new ShardingCreateIndexSupportedChecker().check(rule, database, schema, new CreateIndexStatementContext(sqlStatement, "foo_db")));
}

@Test
Expand All @@ -78,7 +78,7 @@ void assertCheckWhenTableExistIndexExistForPostgreSQL() {
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
when(schema.containsTable("t_order")).thenReturn(true);
when(schema.containsIndex("t_order", "t_order_index")).thenReturn(true);
assertThrows(DuplicateIndexException.class, () -> new ShardingCreateIndexSupportedChecker().check(shardingRule, database, schema, new CreateIndexStatementContext(sqlStatement, "foo_db")));
assertThrows(DuplicateIndexException.class, () -> new ShardingCreateIndexSupportedChecker().check(rule, database, schema, new CreateIndexStatementContext(sqlStatement, "foo_db")));
}

@Test
Expand All @@ -89,7 +89,7 @@ void assertCheckWithoutIndexNameWhenTableExistIndexNotExistForPostgreSQL() {
sqlStatement.setGeneratedIndexStartIndex(10);
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
when(schema.containsTable("t_order")).thenReturn(true);
assertDoesNotThrow(() -> new ShardingCreateIndexSupportedChecker().check(shardingRule, database, schema, new CreateIndexStatementContext(sqlStatement, "foo_db")));
assertDoesNotThrow(() -> new ShardingCreateIndexSupportedChecker().check(rule, database, schema, new CreateIndexStatementContext(sqlStatement, "foo_db")));
}

@Test
Expand All @@ -100,7 +100,7 @@ void assertCheckWithoutIndexNameWhenTableNotExistIndexNotExistForPostgreSQL() {
sqlStatement.setGeneratedIndexStartIndex(10);
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
when(schema.containsTable("t_order")).thenReturn(false);
assertThrows(NoSuchTableException.class, () -> new ShardingCreateIndexSupportedChecker().check(shardingRule, database, schema, new CreateIndexStatementContext(sqlStatement, "foo_db")));
assertThrows(NoSuchTableException.class, () -> new ShardingCreateIndexSupportedChecker().check(rule, database, schema, new CreateIndexStatementContext(sqlStatement, "foo_db")));
}

@Test
Expand All @@ -112,6 +112,6 @@ void assertCheckWithoutIndexNameWhenTableExistIndexExistForPostgreSQL() {
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
when(schema.containsTable("t_order")).thenReturn(true);
when(schema.containsIndex("t_order", "content_idx")).thenReturn(true);
assertThrows(DuplicateIndexException.class, () -> new ShardingCreateIndexSupportedChecker().check(shardingRule, database, schema, new CreateIndexStatementContext(sqlStatement, "foo_db")));
assertThrows(DuplicateIndexException.class, () -> new ShardingCreateIndexSupportedChecker().check(rule, database, schema, new CreateIndexStatementContext(sqlStatement, "foo_db")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ShardingCreateViewSupportedCheckerTest {
private RouteContext routeContext;

@Mock
private ShardingRule shardingRule;
private ShardingRule rule;

@Mock
private CreateViewStatementContext createViewStatementContext;
Expand All @@ -73,13 +73,13 @@ void setUp() {

@Test
void assertCheck() {
assertDoesNotThrow(() -> new ShardingCreateViewSupportedChecker().check(shardingRule, mock(), mock(), createViewStatementContext));
assertDoesNotThrow(() -> new ShardingCreateViewSupportedChecker().check(rule, mock(), mock(), createViewStatementContext));
}

@Test
void assertCheckWithException() {
when(shardingRule.isShardingTable(any())).thenReturn(true);
when(shardingRule.isAllBindingTables(any())).thenReturn(false);
assertThrows(EngagedViewException.class, () -> new ShardingCreateViewSupportedChecker().check(shardingRule, mock(), mock(), createViewStatementContext));
when(rule.isShardingTable(any())).thenReturn(true);
when(rule.isAllBindingTables(any())).thenReturn(false);
assertThrows(EngagedViewException.class, () -> new ShardingCreateViewSupportedChecker().check(rule, mock(), mock(), createViewStatementContext));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
class ShardingDropTableSupportedCheckerTest {

@Mock
private ShardingRule shardingRule;
private ShardingRule rule;

@BeforeEach
void init() {
Map<String, ShardingTable> shardingTables = new LinkedHashMap<>(2, 1F);
shardingTables.put("t_order_item", createShardingTable("t_order_item"));
shardingTables.put("t_order", createShardingTable("t_order"));
when(shardingRule.getShardingTables()).thenReturn(shardingTables);
when(rule.getShardingTables()).thenReturn(shardingTables);
}

@Test
Expand All @@ -69,7 +69,7 @@ void assertCheckForMySQL() {
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
when(schema.containsTable("t_order_item")).thenReturn(true);
ShardingDropTableSupportedChecker checker = new ShardingDropTableSupportedChecker();
assertDoesNotThrow(() -> checker.check(shardingRule, database, schema, sqlStatementContext));
assertDoesNotThrow(() -> checker.check(rule, database, schema, sqlStatementContext));
}

private ShardingTable createShardingTable(final String tableName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@
class ShardingRenameTableSupportedCheckerTest {

@Mock
private ShardingRule shardingRule;
private ShardingRule rule;

@Test
void assertCheckShardingTable() {
RenameTableStatementContext sqlStatementContext = createRenameTableStatementContext("t_order", "t_user_order");
when(shardingRule.containsShardingTable(argThat(tableNames -> tableNames.contains("t_order") || tableNames.contains("t_user_order")))).thenReturn(true);
assertThrows(UnsupportedShardingOperationException.class, () -> new ShardingRenameTableSupportedChecker().check(shardingRule, mock(), mock(), sqlStatementContext));
when(rule.containsShardingTable(argThat(tableNames -> tableNames.contains("t_order") || tableNames.contains("t_user_order")))).thenReturn(true);
assertThrows(UnsupportedShardingOperationException.class, () -> new ShardingRenameTableSupportedChecker().check(rule, mock(), mock(), sqlStatementContext));
}

@Test
void assertCheckNormalCase() {
RenameTableStatementContext sqlStatementContext = createRenameTableStatementContext("t_not_sharding_table", "t_not_sharding_table_new");
assertDoesNotThrow(() -> new ShardingRenameTableSupportedChecker().check(shardingRule, mock(), mock(), sqlStatementContext));
assertDoesNotThrow(() -> new ShardingRenameTableSupportedChecker().check(rule, mock(), mock(), sqlStatementContext));
}

private RenameTableStatementContext createRenameTableStatementContext(final String originTableName, final String newTableName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
class ShardingDeleteSupportedCheckerTest {

@Mock
private ShardingRule shardingRule;
private ShardingRule rule;

@Test
void assertCheckWhenDeleteMultiTablesForMySQL() {
Expand Down Expand Up @@ -81,9 +81,9 @@ private void assertCheckWhenDeleteMultiTables(final DeleteStatement sqlStatement
tableSegment.getActualDeleteTables().add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("order_item"))));
sqlStatement.setTable(tableSegment);
Collection<String> tableNames = new HashSet<>(Arrays.asList("user", "order", "order_item"));
when(shardingRule.isAllShardingTables(tableNames)).thenReturn(false);
when(shardingRule.containsShardingTable(tableNames)).thenReturn(true);
when(rule.isAllShardingTables(tableNames)).thenReturn(false);
when(rule.containsShardingTable(tableNames)).thenReturn(true);
DeleteStatementContext sqlStatementContext = new DeleteStatementContext(sqlStatement, "foo_db");
new ShardingDeleteSupportedChecker().check(shardingRule, mock(), mock(), sqlStatementContext);
new ShardingDeleteSupportedChecker().check(rule, mock(), mock(), sqlStatementContext);
}
}

0 comments on commit f06caea

Please sign in to comment.