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/01 05:21:34 UTC

(shardingsphere) branch master updated: Enhance test coverage for infra-common module core components (#36983)

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 c0df5af0516 Enhance test coverage for infra-common module core components (#36983)
c0df5af0516 is described below

commit c0df5af0516cac0e17daad357ae69429cdde27ed
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Nov 1 13:21:28 2025 +0800

    Enhance test coverage for infra-common module core components (#36983)
    
    * Enhance test coverage for infra-common module core components
    
    - Add ShardingSphereDatabaseFactoryTest with precise assertions using Hamcrest matchers
    - Add MetaDataReviseEngineTest with comprehensive schema revision testing
    - Add ColumnReviseEngineTest with column metadata revision validation
    - Add PostgreSQL table statistics collector tests for pg_class and pg_namespace
    
    🤖 Generated with [Claude Code](https://claude.com/claude-code)
    
    Co-Authored-By: Claude <no...@anthropic.com>
    
    * Refactor ShardingSphereStatisticsCollector
    
    * Refactor ShardingSphereStatisticsCollector
    
    ---------
    
    Co-authored-by: Claude <no...@anthropic.com>
---
 .../ShardingSphereDatabaseFactoryTest.java         | 58 ++++++++++++++++
 .../schema/reviser/MetaDataReviseEngineTest.java   | 62 +++++++++++++++++
 .../reviser/column/ColumnReviseEngineTest.java     | 66 ++++++++++++++++++
 ...tgreSQLPgClassTableStatisticsCollectorTest.java | 79 ++++++++++++++++++++++
 ...SQLPgNamespaceTableStatisticsCollectorTest.java | 72 ++++++++++++++++++++
 5 files changed, 337 insertions(+)

diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactoryTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactoryTest.java
new file mode 100644
index 00000000000..e564fb4da24
--- /dev/null
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactoryTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.infra.metadata.database;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+class ShardingSphereDatabaseFactoryTest {
+    
+    private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+    
+    @Test
+    void assertCreateSystemDatabase() {
+        ShardingSphereDatabase actual = ShardingSphereDatabaseFactory.create("system_db", databaseType, new ConfigurationProperties(new Properties()));
+        assertThat(actual.getName(), is("system_db"));
+        assertThat(actual.getProtocolType(), is(databaseType));
+        assertTrue(actual.getRuleMetaData().getRules().isEmpty());
+    }
+    
+    @Test
+    void assertCreateDatabaseWithComputeNodeInstanceContext() {
+        assertThrows(ServiceProviderNotFoundException.class, () -> ShardingSphereDatabaseFactory.create(null, mock(), mock(), new ConfigurationProperties(new Properties()), mock()));
+    }
+    
+    @Test
+    void assertCreateDatabaseWithSchemas() {
+        ShardingSphereDatabase actual = ShardingSphereDatabaseFactory.create("foo_db", databaseType, mock(), mock(), Collections.emptyList());
+        assertThat(actual.getName(), is("foo_db"));
+        assertThat(actual.getProtocolType(), is(databaseType));
+    }
+}
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/MetaDataReviseEngineTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/MetaDataReviseEngineTest.java
new file mode 100644
index 00000000000..57682e0d4b4
--- /dev/null
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/MetaDataReviseEngineTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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.infra.metadata.database.schema.reviser;
+
+import org.apache.shardingsphere.database.connector.core.metadata.data.model.SchemaMetaData;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilderMaterial;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class MetaDataReviseEngineTest {
+    
+    private final MetaDataReviseEngine engine = new MetaDataReviseEngine(Collections.emptyList());
+    
+    @Test
+    void assertRevise() {
+        Map<String, ShardingSphereSchema> actual = engine.revise(Collections.singletonMap("foo_schema", new SchemaMetaData("foo_schema", Collections.emptyList())), createBuilderMaterial());
+        assertThat(actual.size(), is(1));
+        ShardingSphereSchema schema = actual.get("foo_schema");
+        assertThat(schema.getName(), is("foo_schema"));
+        assertTrue(schema.getAllTables().isEmpty());
+        assertTrue(schema.getAllViews().isEmpty());
+    }
+    
+    @Test
+    void assertReviseWithEmptySchemaMetaDataMap() {
+        Map<String, ShardingSphereSchema> actual = engine.revise(Collections.emptyMap(), createBuilderMaterial());
+        assertThat(actual.size(), is(1));
+        ShardingSphereSchema schema = actual.get("default_schema");
+        assertThat(schema.getName(), is("default_schema"));
+        assertTrue(schema.getAllTables().isEmpty());
+        assertTrue(schema.getAllViews().isEmpty());
+        assertTrue(schema.isEmpty());
+    }
+    
+    private GenericSchemaBuilderMaterial createBuilderMaterial() {
+        return new GenericSchemaBuilderMaterial(Collections.emptyMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()), "default_schema");
+    }
+}
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/column/ColumnReviseEngineTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/column/ColumnReviseEngineTest.java
new file mode 100644
index 00000000000..53868c0ebec
--- /dev/null
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/column/ColumnReviseEngineTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.infra.metadata.database.schema.reviser.column;
+
+import org.apache.shardingsphere.database.connector.core.metadata.data.model.ColumnMetaData;
+import org.apache.shardingsphere.infra.metadata.database.schema.reviser.MetaDataReviseEntry;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+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.Collection;
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class ColumnReviseEngineTest {
+    
+    @Mock
+    private ShardingSphereRule rule;
+    
+    @Mock
+    private MetaDataReviseEntry<ShardingSphereRule> reviseEntry;
+    
+    @Test
+    void assertRevise() {
+        String tableName = "foo_tbl";
+        when(reviseEntry.getColumnExistedReviser(rule, tableName)).thenReturn(Optional.empty());
+        when(reviseEntry.getColumnNameReviser(rule, tableName)).thenReturn(Optional.empty());
+        when(reviseEntry.getColumnGeneratedReviser(rule, tableName)).thenReturn(Optional.empty());
+        ColumnMetaData columnMetaData = new ColumnMetaData("foo_col", 1, true, false, true, false, false, false);
+        Collection<ColumnMetaData> actual = new ColumnReviseEngine<>(rule, reviseEntry).revise(tableName, Collections.singleton(columnMetaData));
+        assertThat(actual.size(), is(1));
+        ColumnMetaData revisedColumn = actual.iterator().next();
+        assertThat(revisedColumn.getName(), is("foo_col"));
+        assertThat(revisedColumn.getDataType(), is(1));
+        assertTrue(revisedColumn.isPrimaryKey());
+        assertFalse(revisedColumn.isGenerated());
+        assertTrue(revisedColumn.isCaseSensitive());
+        assertFalse(revisedColumn.isVisible());
+        assertFalse(revisedColumn.isUnsigned());
+        assertFalse(revisedColumn.isNullable());
+    }
+}
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/collector/postgresql/table/PostgreSQLPgClassTableStatisticsCollectorTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/collector/postgresql/table/PostgreSQLPgClassTableStatisticsCollectorTest.java
new file mode 100644
index 00000000000..c0e62d59134
--- /dev/null
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/collector/postgresql/table/PostgreSQLPgClassTableStatisticsCollectorTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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.infra.metadata.statistics.collector.postgresql.table;
+
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
+import org.apache.shardingsphere.infra.metadata.statistics.collector.postgresql.PostgreSQLTableStatisticsCollector;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class PostgreSQLPgClassTableStatisticsCollectorTest {
+    
+    private final PostgreSQLTableStatisticsCollector collector = TypedSPILoader.getService(PostgreSQLTableStatisticsCollector.class, "pg_catalog.pg_class");
+    
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private ShardingSphereMetaData metaData;
+    
+    @Test
+    void assertCollectWithPublicSchemaExists() throws SQLException {
+        ShardingSphereTable table = mock(ShardingSphereTable.class);
+        when(table.getName()).thenReturn("foo_tbl");
+        when(metaData.getDatabase("foo_db").getSchema("public").getAllTables()).thenReturn(Collections.singleton(table));
+        Collection<Map<String, Object>> actual = collector.collect("foo_db", "pg_catalog", "pg_class", metaData);
+        assertThat(actual.size(), is(1));
+        Map<String, Object> rowData = actual.iterator().next();
+        assertThat(rowData.get("oid"), is(0L));
+        assertThat(rowData.get("relnamespace"), is(0L));
+        assertThat(rowData.get("relname"), is("foo_tbl"));
+        assertThat(rowData.get("relkind"), is("r"));
+    }
+    
+    @Test
+    void assertCollectWithPublicSchemaNotExists() throws SQLException {
+        when(metaData.getDatabase("foo_db").getSchema("public")).thenReturn(null);
+        Collection<Map<String, Object>> actual = collector.collect("foo_db", "pg_catalog", "pg_class", metaData);
+        assertTrue(actual.isEmpty());
+    }
+    
+    @Test
+    void assertGetSchemaName() {
+        assertThat(collector.getSchemaName(), is("pg_catalog"));
+    }
+    
+    @Test
+    void assertGetTableName() {
+        assertThat(collector.getTableName(), is("pg_class"));
+    }
+}
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/collector/postgresql/table/PostgreSQLPgNamespaceTableStatisticsCollectorTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/collector/postgresql/table/PostgreSQLPgNamespaceTableStatisticsCollectorTest.java
new file mode 100644
index 00000000000..c5a6ea88a56
--- /dev/null
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/collector/postgresql/table/PostgreSQLPgNamespaceTableStatisticsCollectorTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.infra.metadata.statistics.collector.postgresql.table;
+
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.metadata.statistics.collector.postgresql.PostgreSQLTableStatisticsCollector;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class PostgreSQLPgNamespaceTableStatisticsCollectorTest {
+    
+    private final PostgreSQLTableStatisticsCollector collector = TypedSPILoader.getService(PostgreSQLTableStatisticsCollector.class, "pg_catalog.pg_namespace");
+    
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private ShardingSphereMetaData metaData;
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    void assertCollectWithMultipleSchemas() throws SQLException {
+        when(metaData.getDatabase("foo_db").getAllSchemas())
+                .thenReturn(Arrays.asList(new ShardingSphereSchema("public"), new ShardingSphereSchema("foo_schema"), new ShardingSphereSchema("bar_schema")));
+        Collection<Map<String, Object>> actual = collector.collect("foo_db", "pg_catalog", "pg_namespace", metaData);
+        assertThat(actual.size(), is(3));
+        Map<String, Object>[] results = actual.toArray(new Map[0]);
+        assertThat(results[0].get("oid"), is(0L));
+        assertThat(results[0].get("nspname"), is("public"));
+        assertThat(results[1].get("oid"), is(1L));
+        assertThat(results[1].get("nspname"), is("foo_schema"));
+        assertThat(results[2].get("oid"), is(2L));
+        assertThat(results[2].get("nspname"), is("bar_schema"));
+    }
+    
+    @Test
+    void assertGetSchemaName() {
+        assertThat(collector.getSchemaName(), is("pg_catalog"));
+    }
+    
+    @Test
+    void assertGetTableName() {
+        assertThat(collector.getTableName(), is("pg_namespace"));
+    }
+}