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 9407367b15b Add PostgreSQLStatisticsCollectorTest (#37041)
9407367b15b is described below
commit 9407367b15b8d67aec5c2140dbb70b6855cd56ed
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Nov 8 18:03:18 2025 +0800
Add PostgreSQLStatisticsCollectorTest (#37041)
---
CLAUDE.md | 17 ++++++
.../postgresql/PostgreSQLStatisticsCollector.java | 15 ++---
.../PostgreSQLStatisticsCollectorTest.java | 70 ++++++++++++++++++++++
.../reachability-metadata.json | 4 +-
4 files changed, 94 insertions(+), 12 deletions(-)
diff --git a/CLAUDE.md b/CLAUDE.md
index eb7575e85cc..bd6baacf4f0 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -174,6 +174,8 @@ Key areas covered by coding standards file:
- Apply consistent exception handling and return value patterns (see Style
Consistency Application)
- **Test Method Organization**: Group test methods by functional scenarios,
avoid redundancy
- **Dependency Injection Simplification**: Keep dependency injection in tests
concise, focus on test targets
+- **SPI Loading Consistency**: Use `DatabaseTypedSPILoader` and
`TypedSPILoader` for database-specific components in tests
+- **Interface-Based Testing**: Test against interfaces rather than concrete
implementations when SPI is available
## AI Testing Strategies
*AI-specific testing organization and design capabilities*
@@ -184,6 +186,8 @@ Key areas covered by coding standards file:
- Avoid hard-coding; use parameterized tests
- **Configuration Object Building**: Use `PropertiesBuilder` and `Property`
for type-safe configuration construction
- **Mock Minimization**: Mock only necessary dependencies, use
`RETURNS_DEEP_STUBS` for chained calls
+- **Inline Mock Creation**: Create mock objects directly where needed rather
than declaring fields
+- **Framework Dependency Reduction**: Minimize test framework extensions and
annotations when not necessary
- **Logic Extraction**: Extract repetitive mock setup and assertion logic into
private methods
*For detailed test organization standards, see CODE_OF_CONDUCT.md reference in
code standards section*
@@ -191,6 +195,12 @@ Key areas covered by coding standards file:
### Testing Case Development Standards
For comprehensive testing case development requirements, see [AI Testing Case
Development Standards](#ai-testing-case-development-standards) above.
+### Test Structure Minimalism Standards
+- **Framework Dependency Reduction**: Avoid `@ExtendWith` and similar
framework extensions when simple mocks suffice
+- **Import Organization**: Group imports by source (java.*, org.*, static),
keep them minimal and relevant
+- **Class Structure Simplification**: Focus on test logic rather than
ceremonial code and annotations
+- **Code Density Optimization**: Maximize meaningful code per line while
maintaining readability
+
### Test Scenario Design Capabilities
- Identify business-critical paths for focused testing
- Design integration tests for complex business scenarios
@@ -199,6 +209,10 @@ For comprehensive testing case development requirements,
see [AI Testing Case De
- **Modern Tool Usage**: Use `Plugins.getMemberAccessor()` instead of
traditional reflection APIs
- **State Management Strategy**: Leverage `@BeforeEach` and `@AfterEach` for
shared reset logic
- **Assertive Naming**: Test method names directly express verification intent
+- **Method Naming Pattern**: Use `assert[MethodName]With[Condition]` pattern
for clarity and consistency
+- **Single-Line Assertions**: Combine method calls and assertions into single
statements when clear
+- **Chain Method Assertions**: Assert directly on method call results,
avoiding intermediate variables
+- **Expression over Construction**: Use expressions and utility methods rather
than step-by-step construction
### Test Code Optimization Core Principles
*Core principles for writing clean, efficient test code based on proven
practices*
@@ -207,6 +221,9 @@ For comprehensive testing case development requirements,
see [AI Testing Case De
- **Self-Contained Tests**: Ensure each test method is self-contained and
doesn't rely on complex external setup unless truly shared
- **Utilize Tool Methods**: Leverage utility methods like
`Collections.singleton()` and `Arrays.asList()` for efficient collection
creation
- **Simplify Dependency Chains**: Reduce multi-layer nested Mock
configurations to maintain test code readability and maintainability
+- **Collection Utility Optimization**: Prefer `Collections.emptyMap()`,
`Collections.singleton()`, and `Arrays.asList()` for test data creation
+- **Immutable Test Data**: Use immutable collections for test data when
possible to ensure test reliability
+- **Import Statement Minimization**: Keep imports minimal and relevant,
avoiding framework dependencies when not needed
## Dependency Injection Patterns
*Standard dependency injection methods in ShardingSphere*
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/postgresql/PostgreSQLStatisticsCollector.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/postgresql/PostgreSQLStatisticsCollector.java
index e0d895cd854..4b846c44750 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/postgresql/PostgreSQLStatisticsCollector.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/postgresql/PostgreSQLStatisticsCollector.java
@@ -27,7 +27,6 @@ import
org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;
-import java.util.Map.Entry;
import java.util.Optional;
/**
@@ -58,15 +57,11 @@ public final class PostgreSQLStatisticsCollector implements
DialectDatabaseStati
if (schemaTables.isEmpty()) {
return false;
}
- for (Entry<String, Collection<String>> entry :
schemaTables.entrySet()) {
- if (!STATISTICS_SCHEMA_TABLES.containsKey(entry.getKey())) {
- return false;
- }
- if
(!STATISTICS_SCHEMA_TABLES.get(entry.getKey()).containsAll(entry.getValue())) {
- return false;
- }
- }
- return true;
+ return schemaTables.entrySet().stream().allMatch(entry ->
isStatisticsTables(entry.getKey(), entry.getValue()));
+ }
+
+ private boolean isStatisticsTables(final String schemaName, final
Collection<String> tableNames) {
+ return STATISTICS_SCHEMA_TABLES.containsKey(schemaName) &&
STATISTICS_SCHEMA_TABLES.get(schemaName).containsAll(tableNames);
}
@Override
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/collector/postgresql/PostgreSQLStatisticsCollectorTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/collector/postgresql/PostgreSQLStatisticsCollectorTest.java
new file mode 100644
index 00000000000..a39a6cf7d4c
--- /dev/null
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/collector/postgresql/PostgreSQLStatisticsCollectorTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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;
+
+import
org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import
org.apache.shardingsphere.infra.metadata.statistics.collector.DialectDatabaseStatisticsCollector;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.api.Test;
+
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+
+class PostgreSQLStatisticsCollectorTest {
+
+ private final DialectDatabaseStatisticsCollector collector =
DatabaseTypedSPILoader.getService(
+ DialectDatabaseStatisticsCollector.class,
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"));
+
+ @Test
+ void assertCollectRowColumnValuesWithExistingCollector() throws
SQLException {
+ assertTrue(collector.collectRowColumnValues("foo_db", "pg_catalog",
"pg_class", mock(ShardingSphereMetaData.class,
RETURNS_DEEP_STUBS)).isPresent());
+ }
+
+ @Test
+ void assertCollectRowColumnValuesWithNonExistingCollector() throws
SQLException {
+ assertFalse(collector.collectRowColumnValues("foo_db",
"unknown_schema", "unknown_table", mock()).isPresent());
+ }
+
+ @Test
+ void assertIsStatisticsTablesWithEmptySchemaTables() {
+ assertFalse(collector.isStatisticsTables(Collections.emptyMap()));
+ }
+
+ @Test
+ void assertIsStatisticsTablesWithNonExistingSchema() {
+
assertFalse(collector.isStatisticsTables(Collections.singletonMap("unknown_schema",
Collections.singleton("unknown_table"))));
+ }
+
+ @Test
+ void assertIsStatisticsTablesWithSchemaButMissingTable() {
+
assertFalse(collector.isStatisticsTables(Collections.singletonMap("pg_catalog",
Collections.singleton("unknown_table"))));
+ }
+
+ @Test
+ void assertIsStatisticsTablesWithValidSchemaAndTables() {
+
assertTrue(collector.isStatisticsTables(Collections.singletonMap("pg_catalog",
Arrays.asList("pg_class", "pg_namespace"))));
+ }
+}
diff --git
a/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/reachability-metadata.json
b/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/reachability-metadata.json
index 3500c3d9425..f681c2e4ecf 100644
---
a/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/reachability-metadata.json
+++
b/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/reachability-metadata.json
@@ -4213,7 +4213,7 @@
},
{
"condition": {
- "typeReached":
"org.apache.shardingsphere.infra.url.core.ShardingSphereURLLoadEngine"
+ "typeReached":
"org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLLoadEngine"
},
"type":
"org.apache.shardingsphere.infra.url.classpath.ClassPathURLLoader"
},
@@ -11966,7 +11966,7 @@
},
{
"condition": {
- "typeReached":
"org.apache.shardingsphere.infra.url.core.ShardingSphereURLLoadEngine"
+ "typeReached":
"org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLLoadEngine"
},
"glob":
"META-INF/services/org.apache.shardingsphere.infra.url.spi.ShardingSphereURLLoader"
},