You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2025/11/27 12:26:15 UTC

(shardingsphere) branch master updated: Add more test cases on SchemaPushDownMetaDataRefresherTest (#37201)

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 166e2941d6d Add more test cases on SchemaPushDownMetaDataRefresherTest (#37201)
166e2941d6d is described below

commit 166e2941d6d5938bc366ddde9dbce9b1882317b4
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu Nov 27 20:26:08 2025 +0800

    Add more test cases on SchemaPushDownMetaDataRefresherTest (#37201)
    
    * Add more test cases on SchemaPushDownMetaDataRefresherTest
    
    * Add more test cases on SchemaPushDownMetaDataRefresherTest
---
 AGENTS.md                                          |  4 +-
 .../AlterSchemaPushDownMetaDataRefresherTest.java  | 73 ++++++++++++++++++++++
 .../CreateSchemaPushDownMetaDataRefresherTest.java | 71 +++++++++++++++++++++
 .../DropSchemaPushDownMetaDataRefresherTest.java   | 60 ++++++++++++++++++
 4 files changed, 207 insertions(+), 1 deletion(-)

diff --git a/AGENTS.md b/AGENTS.md
index ab40e01927f..36c65654479 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -240,10 +240,12 @@ Always state which topology, registry, and engine versions (e.g., MySQL 5.7 vs 8
 - Mock heavy dependencies (database/cache/registry/network) and prefer mocking over building deep object graphs; avoid `RETURNS_DEEP_STUBS` unless chained interactions demand it.
 - Before changing how mocks are created, scan the repository for similar tests (e.g., other rule decorators or executor tests) and reuse their proven mocking pattern instead of inventing a new structure.
 - When constructors hide collaborators, use `Plugins.getMemberAccessor()` to inject mocks and document why SPI creation is bypassed.
-- Cache SPI loader results (`OrderedSPILoader`, `TypedSPILoader`, `DatabaseTypedSPILoader`, etc.) per key at the test-class level to avoid redundant lookups.
 - If a test already uses `@ExtendWith(AutoMockExtension.class)`, always declare the needed static collaborators via `@StaticMockSettings` instead of hand-written `mockStatic` blocks; justify any exception explicitly in the plan before coding.
 - Before adding coverage to a utility with multiple return paths, list every branch (no rule, non-Single config, wildcard blocks, missing data node, positive path, collection overload) and map each to a test; update the plan whenever this checklist changes.
 - Prefer imports over fully-qualified class names inside code and tests; if a class is used, add an import rather than using the full package path inline.
+- Before coding tests, prepare a concise branch-and-data checklist (all branches, inputs, expected outputs) and keep the plan in sync when the checklist changes.
+- When a component is available via SPI (e.g., `TypedSPILoader`, `DatabaseTypedSPILoader`, `PushDownMetaDataRefresher`), obtain the instance through SPI by default; note any exceptions in the plan.
+- Do not mix Mockito matchers with raw arguments; choose a single style per invocation, and ensure the Mockito extension aligns with the mocking approach.
 
 ## Brevity & Signal
 - Prefer tables/bullets over prose walls; cite file paths (`kernel/src/...`) directly.
diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/pushdown/type/schema/AlterSchemaPushDownMetaDataRefresherTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/pushdown/type/schema/AlterSchemaPushDownMetaDataRefresherTest.java
new file mode 100644
index 00000000000..2420b8e5b49
--- /dev/null
+++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/pushdown/type/schema/AlterSchemaPushDownMetaDataRefresherTest.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mode.metadata.refresher.pushdown.type.schema;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.mode.metadata.refresher.pushdown.PushDownMetaDataRefresher;
+import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.schema.AlterSchemaStatement;
+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.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collections;
+import java.util.Properties;
+
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+
+@ExtendWith(MockitoExtension.class)
+class AlterSchemaPushDownMetaDataRefresherTest {
+    
+    private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+    
+    private final AlterSchemaPushDownMetaDataRefresher refresher = (AlterSchemaPushDownMetaDataRefresher) TypedSPILoader.getService(PushDownMetaDataRefresher.class, AlterSchemaStatement.class);
+    
+    @Mock
+    private MetaDataManagerPersistService metaDataManagerPersistService;
+    
+    @Test
+    void assertRefreshRenamesSchema() {
+        ShardingSphereDatabase database = createDatabase();
+        AlterSchemaStatement sqlStatement = new AlterSchemaStatement(databaseType);
+        sqlStatement.setSchemaName(new IdentifierValue("FOO_SCHEMA"));
+        sqlStatement.setRenameSchema(new IdentifierValue("BAR_SCHEMA"));
+        refresher.refresh(metaDataManagerPersistService, database, "logic_ds", "foo_schema", databaseType, sqlStatement, new ConfigurationProperties(new Properties()));
+        verify(metaDataManagerPersistService).renameSchema(database, "foo_schema", "bar_schema");
+    }
+    
+    @Test
+    void assertRefreshDoesNothingWithoutRename() {
+        ShardingSphereDatabase database = createDatabase();
+        AlterSchemaStatement sqlStatement = new AlterSchemaStatement(databaseType);
+        sqlStatement.setSchemaName(new IdentifierValue("FOO_SCHEMA"));
+        refresher.refresh(metaDataManagerPersistService, database, "logic_ds", "foo_schema", databaseType, sqlStatement, new ConfigurationProperties(new Properties()));
+        verifyNoInteractions(metaDataManagerPersistService);
+    }
+    
+    private ShardingSphereDatabase createDatabase() {
+        return new ShardingSphereDatabase("foo_db", databaseType, new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), Collections.emptyList());
+    }
+}
diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/pushdown/type/schema/CreateSchemaPushDownMetaDataRefresherTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/pushdown/type/schema/CreateSchemaPushDownMetaDataRefresherTest.java
new file mode 100644
index 00000000000..857781a4bb7
--- /dev/null
+++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/pushdown/type/schema/CreateSchemaPushDownMetaDataRefresherTest.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mode.metadata.refresher.pushdown.type.schema;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.mode.metadata.refresher.pushdown.PushDownMetaDataRefresher;
+import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.schema.CreateSchemaStatement;
+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.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collections;
+import java.util.Properties;
+
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+
+@ExtendWith(MockitoExtension.class)
+class CreateSchemaPushDownMetaDataRefresherTest {
+    
+    private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+    
+    private final CreateSchemaPushDownMetaDataRefresher refresher = (CreateSchemaPushDownMetaDataRefresher) TypedSPILoader.getService(PushDownMetaDataRefresher.class, CreateSchemaStatement.class);
+    
+    @Mock
+    private MetaDataManagerPersistService metaDataManagerPersistService;
+    
+    @Test
+    void assertRefreshCreateSchemaWithSchemaName() {
+        ShardingSphereDatabase database = createDatabase();
+        CreateSchemaStatement sqlStatement = new CreateSchemaStatement(databaseType);
+        sqlStatement.setSchemaName(new IdentifierValue("FOO_SCHEMA"));
+        refresher.refresh(metaDataManagerPersistService, database, "logic_ds", "foo_schema", databaseType, sqlStatement, new ConfigurationProperties(new Properties()));
+        verify(metaDataManagerPersistService).createSchema(database, "foo_schema");
+    }
+    
+    @Test
+    void assertRefreshNoSchemaOrUserDoesNothing() {
+        ShardingSphereDatabase database = createDatabase();
+        CreateSchemaStatement sqlStatement = new CreateSchemaStatement(databaseType);
+        refresher.refresh(metaDataManagerPersistService, database, "logic_ds", "foo_schema", databaseType, sqlStatement, new ConfigurationProperties(new Properties()));
+        verifyNoInteractions(metaDataManagerPersistService);
+    }
+    
+    private ShardingSphereDatabase createDatabase() {
+        return new ShardingSphereDatabase("foo_db", databaseType, new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), Collections.emptyList());
+    }
+}
diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/pushdown/type/schema/DropSchemaPushDownMetaDataRefresherTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/pushdown/type/schema/DropSchemaPushDownMetaDataRefresherTest.java
new file mode 100644
index 00000000000..5979ba16a3f
--- /dev/null
+++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/pushdown/type/schema/DropSchemaPushDownMetaDataRefresherTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mode.metadata.refresher.pushdown.type.schema;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.mode.metadata.refresher.pushdown.PushDownMetaDataRefresher;
+import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
+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.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Properties;
+
+import static org.mockito.Mockito.verify;
+
+@ExtendWith(MockitoExtension.class)
+class DropSchemaPushDownMetaDataRefresherTest {
+    
+    private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+    
+    private final DropSchemaPushDownMetaDataRefresher refresher = (DropSchemaPushDownMetaDataRefresher) TypedSPILoader.getService(PushDownMetaDataRefresher.class, DropSchemaStatement.class);
+    
+    @Mock
+    private MetaDataManagerPersistService metaDataManagerPersistService;
+    
+    @Test
+    void assertRefresh() {
+        DropSchemaStatement sqlStatement = new DropSchemaStatement(databaseType);
+        sqlStatement.getSchemaNames().addAll(Arrays.asList(new IdentifierValue("FOO_SCHEMA"), new IdentifierValue("BAR_SCHEMA")));
+        ShardingSphereDatabase database = new ShardingSphereDatabase(
+                "foo_db", databaseType, new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), Collections.emptyList());
+        refresher.refresh(metaDataManagerPersistService, database, "logic_ds", "foo_schema", databaseType, sqlStatement, new ConfigurationProperties(new Properties()));
+        verify(metaDataManagerPersistService).dropSchema(database, Arrays.asList("foo_schema", "bar_schema"));
+    }
+}