This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new c08e738f34d Add more test cases on
SingleDropSchemaSupportedCheckerTest (#38075)
c08e738f34d is described below
commit c08e738f34d1f786791c25b9edda8e48d73ce2d9
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Feb 18 00:45:13 2026 +0800
Add more test cases on SingleDropSchemaSupportedCheckerTest (#38075)
---
.../SingleDropSchemaSupportedCheckerTest.java | 75 +++++++++++++++-------
1 file changed, 51 insertions(+), 24 deletions(-)
diff --git
a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/sql/schema/SingleDropSchemaSupportedCheckerTest.java
b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/sql/schema/SingleDropSchemaSupportedCheckerTest.java
index ca4d242d738..e2a66c45e8d 100644
---
a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/sql/schema/SingleDropSchemaSupportedCheckerTest.java
+++
b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/sql/schema/SingleDropSchemaSupportedCheckerTest.java
@@ -17,69 +17,96 @@
package org.apache.shardingsphere.single.checker.sql.schema;
-import
org.apache.shardingsphere.database.connector.core.metadata.database.enums.TableType;
-import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import
org.apache.shardingsphere.infra.binder.context.statement.type.CommonSQLStatementContext;
import
org.apache.shardingsphere.infra.exception.kernel.metadata.SchemaNotFoundException;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.single.exception.DropNotEmptySchemaException;
import org.apache.shardingsphere.single.rule.SingleRule;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.schema.DropSchemaStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.Answers;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Collections;
+import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class SingleDropSchemaSupportedCheckerTest {
- @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ @Mock
private SingleRule rule;
+ private final SingleDropSchemaSupportedChecker checker = new
SingleDropSchemaSupportedChecker();
+
@Test
- void assertCheckWithoutCascadeSchema() {
- assertThrows(DropNotEmptySchemaException.class, () -> new
SingleDropSchemaSupportedChecker().check(rule, mockDatabase(), mock(),
createSQLStatementContext("foo_schema", false)));
+ void assertIsCheckWithDropSchemaStatement() {
+ SQLStatementContext sqlStatementContext =
mock(SQLStatementContext.class);
+
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DropSchemaStatement.class));
+ assertTrue(checker.isCheck(sqlStatementContext));
}
@Test
- void assertCheckWithNotExistedSchema() {
- ShardingSphereDatabase database = mockDatabase();
- when(database.getSchema("not_existed_schema")).thenReturn(null);
- assertThrows(SchemaNotFoundException.class, () -> new
SingleDropSchemaSupportedChecker().check(rule, database, mock(),
createSQLStatementContext("not_existed_schema", true)));
+ void assertIsCheckWithNonDropSchemaStatement() {
+ SQLStatementContext sqlStatementContext =
mock(SQLStatementContext.class);
+
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(SQLStatement.class));
+ assertFalse(checker.isCheck(sqlStatementContext));
}
- @Test
- void assertCheck() {
- assertDoesNotThrow(() -> new
SingleDropSchemaSupportedChecker().check(rule, mockDatabase(), mock(),
createSQLStatementContext("foo_schema", true)));
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("checkCases")
+ void assertCheck(final String scenario, final String schemaName, final
boolean containsCascade, final boolean schemaExisted,
+ final boolean schemaEmpty, final Class<? extends
Throwable> expectedThrowableType) {
+ ShardingSphereDatabase database = mockDatabase(schemaName,
schemaExisted, containsCascade, schemaEmpty);
+ SQLStatementContext sqlStatementContext =
createSQLStatementContext(schemaName, containsCascade);
+ if (null == expectedThrowableType) {
+ assertDoesNotThrow(() -> checker.check(rule, database,
mock(ShardingSphereSchema.class), sqlStatementContext));
+ } else {
+ assertThrows(expectedThrowableType, () -> checker.check(rule,
database, mock(ShardingSphereSchema.class), sqlStatementContext));
+ }
}
- private ShardingSphereDatabase mockDatabase() {
- ShardingSphereDatabase result = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
- ShardingSphereSchema schema = new ShardingSphereSchema("foo_schema",
mock(DatabaseType.class),
- Collections.singleton(new ShardingSphereTable("foo_tbl",
Collections.emptyList(), Collections.emptyList(), Collections.emptyList(),
TableType.TABLE)), Collections.emptyList());
- when(result.getAllSchemas()).thenReturn(Collections.singleton(schema));
+ private static Stream<Arguments> checkCases() {
+ return Stream.of(
+ Arguments.of("not empty schema without cascade should throw",
"foo_schema", false, true, false, DropNotEmptySchemaException.class),
+ Arguments.of("not existed schema should throw",
"not_existed_schema", true, false, false, SchemaNotFoundException.class),
+ Arguments.of("not empty schema with cascade should pass",
"foo_schema", true, true, false, null),
+ Arguments.of("empty schema without cascade should pass",
"empty_schema", false, true, true, null));
+ }
+
+ private ShardingSphereDatabase mockDatabase(final String schemaName, final
boolean schemaExisted, final boolean containsCascade, final boolean
schemaEmpty) {
+ ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
+ if (!schemaExisted) {
+ return result;
+ }
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ if (!containsCascade) {
+ when(schema.getAllTables()).thenReturn(schemaEmpty ?
Collections.emptyList() :
Collections.singleton(mock(ShardingSphereTable.class)));
+ }
+ when(result.getSchema(schemaName)).thenReturn(schema);
return result;
}
private SQLStatementContext createSQLStatementContext(final String
schemaName, final boolean containsCascade) {
- DropSchemaStatement dropSchemaStatement =
mock(DropSchemaStatement.class, RETURNS_DEEP_STUBS);
+ SQLStatementContext result = mock(SQLStatementContext.class);
+ DropSchemaStatement dropSchemaStatement =
mock(DropSchemaStatement.class);
when(dropSchemaStatement.isContainsCascade()).thenReturn(containsCascade);
when(dropSchemaStatement.getSchemaNames()).thenReturn(Collections.singleton(new
IdentifierValue(schemaName)));
- when(dropSchemaStatement.getAttributes()).thenReturn(new
SQLStatementAttributes());
- return new CommonSQLStatementContext(dropSchemaStatement);
+ when(result.getSqlStatement()).thenReturn(dropSchemaStatement);
+ return result;
}
}