From f06caea5eb1299c66f489c56b9422248fc92241e Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Thu, 5 Dec 2024 23:58:33 +0800 Subject: [PATCH] Revise test cases for sharding checker (#33925) --- .../ShardingAlterIndexSupportedCheckerTest.java | 8 ++++---- .../ShardingAlterTableSupportedCheckerTest.java | 6 +++--- .../ShardingCreateIndexSupportedCheckerTest.java | 14 +++++++------- .../ShardingCreateViewSupportedCheckerTest.java | 10 +++++----- .../ddl/ShardingDropTableSupportedCheckerTest.java | 6 +++--- .../ShardingRenameTableSupportedCheckerTest.java | 8 ++++---- .../dml/ShardingDeleteSupportedCheckerTest.java | 8 ++++---- 7 files changed, 30 insertions(+), 30 deletions(-) diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterIndexSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterIndexSupportedCheckerTest.java index 4d5baa355eede..9161b8842a15a 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterIndexSupportedCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterIndexSupportedCheckerTest.java @@ -45,7 +45,7 @@ class ShardingAlterIndexSupportedCheckerTest { @Mock - private ShardingRule shardingRule; + private ShardingRule rule; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ShardingSphereDatabase database; @@ -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 @@ -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 @@ -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"))); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterTableSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterTableSupportedCheckerTest.java index 1a76344e7a52d..0ed445f918ce7 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterTableSupportedCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterTableSupportedCheckerTest.java @@ -41,7 +41,7 @@ class ShardingAlterTableSupportedCheckerTest { @Mock - private ShardingRule shardingRule; + private ShardingRule rule; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ShardingSphereDatabase database; @@ -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)); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateIndexSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateIndexSupportedCheckerTest.java index b01e5d7070051..60e86be90a0b3 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateIndexSupportedCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateIndexSupportedCheckerTest.java @@ -45,7 +45,7 @@ class ShardingCreateIndexSupportedCheckerTest { @Mock - private ShardingRule shardingRule; + private ShardingRule rule; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ShardingSphereDatabase database; @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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"))); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateViewSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateViewSupportedCheckerTest.java index 1f7544cdb5877..e4a2f33984d8d 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateViewSupportedCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateViewSupportedCheckerTest.java @@ -51,7 +51,7 @@ class ShardingCreateViewSupportedCheckerTest { private RouteContext routeContext; @Mock - private ShardingRule shardingRule; + private ShardingRule rule; @Mock private CreateViewStatementContext createViewStatementContext; @@ -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)); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropTableSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropTableSupportedCheckerTest.java index 4efeb700a4e5f..38489b6b6dff0 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropTableSupportedCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropTableSupportedCheckerTest.java @@ -50,14 +50,14 @@ class ShardingDropTableSupportedCheckerTest { @Mock - private ShardingRule shardingRule; + private ShardingRule rule; @BeforeEach void init() { Map 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 @@ -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) { diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingRenameTableSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingRenameTableSupportedCheckerTest.java index fdfec20f08e74..d4aaae3d616ea 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingRenameTableSupportedCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingRenameTableSupportedCheckerTest.java @@ -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) { diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingDeleteSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingDeleteSupportedCheckerTest.java index 2224f969f7a89..7644a8f410994 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingDeleteSupportedCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingDeleteSupportedCheckerTest.java @@ -47,7 +47,7 @@ class ShardingDeleteSupportedCheckerTest { @Mock - private ShardingRule shardingRule; + private ShardingRule rule; @Test void assertCheckWhenDeleteMultiTablesForMySQL() { @@ -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 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); } }