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 72d5b6005e5 Add more test cases on PostgreSQLPipelineSQLBuilder 
(#33412)
72d5b6005e5 is described below

commit 72d5b6005e5b16c476feab73abeeb9dd0ad57b56
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Oct 26 16:45:23 2024 +0800

    Add more test cases on PostgreSQLPipelineSQLBuilder (#33412)
    
    * Add more test cases on MySQLPipelineSQLBuilder
    
    * Add more test cases on MySQLPipelineSQLBuilder
    
    * Add more test cases on PostgreSQLPipelineSQLBuilder
---
 kernel/data-pipeline/dialect/postgresql/pom.xml    |  6 +++
 .../PostgreSQLPipelineSQLBuilderTest.java          | 58 ++++++++++++++++++++--
 2 files changed, 60 insertions(+), 4 deletions(-)

diff --git a/kernel/data-pipeline/dialect/postgresql/pom.xml 
b/kernel/data-pipeline/dialect/postgresql/pom.xml
index 5d3a8a5dba1..011dc18a40d 100644
--- a/kernel/data-pipeline/dialect/postgresql/pom.xml
+++ b/kernel/data-pipeline/dialect/postgresql/pom.xml
@@ -33,6 +33,12 @@
             <version>${project.version}</version>
         </dependency>
         
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-test-fixture-database</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
             <artifactId>shardingsphere-test-util</artifactId>
diff --git 
a/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/sqlbuilder/PostgreSQLPipelineSQLBuilderTest.java
 
b/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/sqlbuilder/PostgreSQLPipelineSQLBuilderTest.java
index 1b0a3aca70f..5ba437ff210 100644
--- 
a/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/sqlbuilder/PostgreSQLPipelineSQLBuilderTest.java
+++ 
b/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/sqlbuilder/PostgreSQLPipelineSQLBuilderTest.java
@@ -29,24 +29,74 @@ import 
org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.junit.jupiter.api.Test;
 import org.postgresql.replication.LogSequenceNumber;
 
+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;
 
 class PostgreSQLPipelineSQLBuilderTest {
     
     private final DialectPipelineSQLBuilder sqlBuilder = 
DatabaseTypedSPILoader.getService(DialectPipelineSQLBuilder.class, 
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"));
     
+    @Test
+    void assertBuildCreateSchemaSQL() {
+        Optional<String> actual = 
sqlBuilder.buildCreateSchemaSQL("foo_schema");
+        assertTrue(actual.isPresent());
+        assertThat(actual.get(), is("CREATE SCHEMA IF NOT EXISTS foo_schema"));
+    }
+    
+    @Test
+    void assertBuildInsertSQLOnDuplicateClauseWithEmptyUniqueKey() {
+        Optional<String> actual = sqlBuilder.buildInsertOnDuplicateClause(
+                new DataRecord(PipelineSQLOperationType.INSERT, "foo_tbl", new 
WALPosition(new PostgreSQLLogSequenceNumber(LogSequenceNumber.valueOf(100L))), 
2));
+        assertFalse(actual.isPresent());
+    }
+    
     @Test
     void assertBuildInsertSQLOnDuplicateClause() {
-        String actual = 
sqlBuilder.buildInsertOnDuplicateClause(mockDataRecord()).orElse(null);
-        assertThat(actual, is("ON CONFLICT (order_id) DO UPDATE SET 
user_id=EXCLUDED.user_id,status=EXCLUDED.status"));
+        Optional<String> actual = 
sqlBuilder.buildInsertOnDuplicateClause(createDataRecord());
+        assertTrue(actual.isPresent());
+        assertThat(actual.get(), is("ON CONFLICT (order_id) DO UPDATE SET 
user_id=EXCLUDED.user_id,status=EXCLUDED.status"));
     }
     
-    private DataRecord mockDataRecord() {
-        DataRecord result = new DataRecord(PipelineSQLOperationType.INSERT, 
"t_order", new WALPosition(new 
PostgreSQLLogSequenceNumber(LogSequenceNumber.valueOf(100L))), 2);
+    private DataRecord createDataRecord() {
+        DataRecord result = new DataRecord(PipelineSQLOperationType.INSERT, 
"foo_tbl", new WALPosition(new 
PostgreSQLLogSequenceNumber(LogSequenceNumber.valueOf(100L))), 2);
         result.addColumn(new Column("order_id", 1, true, true));
         result.addColumn(new Column("user_id", 2, true, false));
         result.addColumn(new Column("status", "ok", true, false));
         return result;
     }
+    
+    @Test
+    void assertBuildCheckEmptyTableSQL() {
+        assertThat(sqlBuilder.buildCheckEmptyTableSQL("foo_tbl"), is("SELECT * 
FROM foo_tbl LIMIT 1"));
+    }
+    
+    @Test
+    void assertBuildEstimatedCountSQL() {
+        Optional<String> actual = 
sqlBuilder.buildEstimatedCountSQL("foo_catalog", "foo_tbl");
+        assertTrue(actual.isPresent());
+        assertThat(actual.get(), is("SELECT reltuples::integer FROM pg_class 
WHERE oid='foo_tbl'::regclass::oid;"));
+    }
+    
+    @Test
+    void assertBuildCRC32SQL() {
+        Optional<String> actual = sqlBuilder.buildCRC32SQL("foo_tbl", 
"foo_col");
+        assertTrue(actual.isPresent());
+        assertThat(actual.get(), is("SELECT 
pg_catalog.pg_checksum_table('foo_tbl', true)"));
+    }
+    
+    @Test
+    void assertBuildQueryCurrentPositionSQL() {
+        Optional<String> actual = sqlBuilder.buildQueryCurrentPositionSQL();
+        assertTrue(actual.isPresent());
+        assertThat(actual.get(), is("SELECT * FROM pg_current_wal_lsn()"));
+    }
+    
+    @Test
+    void assertWrapWithPageQuery() {
+        assertThat(sqlBuilder.wrapWithPageQuery("SELECT * FROM foo_tbl"), 
is("SELECT * FROM foo_tbl LIMIT ?"));
+    }
 }

Reply via email to