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 5f4c41f2920 Add more test cases on OpenGaussPipelineSQLBuilder (#33413)
5f4c41f2920 is described below

commit 5f4c41f29203910b587bd0bd86e61ae401018a70
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Oct 26 17:05:00 2024 +0800

    Add more test cases on OpenGaussPipelineSQLBuilder (#33413)
---
 kernel/data-pipeline/dialect/opengauss/pom.xml     |  7 +++
 .../OpenGaussPipelineSQLBuilderTest.java           | 61 ++++++++++++++++++++--
 2 files changed, 64 insertions(+), 4 deletions(-)

diff --git a/kernel/data-pipeline/dialect/opengauss/pom.xml 
b/kernel/data-pipeline/dialect/opengauss/pom.xml
index 1c9c33756e9..041ea55d244 100644
--- a/kernel/data-pipeline/dialect/opengauss/pom.xml
+++ b/kernel/data-pipeline/dialect/opengauss/pom.xml
@@ -33,6 +33,13 @@
             <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.opengauss</groupId>
             <artifactId>opengauss-jdbc</artifactId>
diff --git 
a/kernel/data-pipeline/dialect/opengauss/src/test/java/org/apache/shardingsphere/data/pipeline/opengauss/sqlbuilder/OpenGaussPipelineSQLBuilderTest.java
 
b/kernel/data-pipeline/dialect/opengauss/src/test/java/org/apache/shardingsphere/data/pipeline/opengauss/sqlbuilder/OpenGaussPipelineSQLBuilderTest.java
index d447ea368fc..bf9fa1a037a 100644
--- 
a/kernel/data-pipeline/dialect/opengauss/src/test/java/org/apache/shardingsphere/data/pipeline/opengauss/sqlbuilder/OpenGaussPipelineSQLBuilderTest.java
+++ 
b/kernel/data-pipeline/dialect/opengauss/src/test/java/org/apache/shardingsphere/data/pipeline/opengauss/sqlbuilder/OpenGaussPipelineSQLBuilderTest.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.data.pipeline.opengauss.sqlbuilder;
 
 import 
org.apache.shardingsphere.data.pipeline.core.constant.PipelineSQLOperationType;
+import 
org.apache.shardingsphere.data.pipeline.core.exception.job.CreateTableSQLGenerateException;
 import 
org.apache.shardingsphere.data.pipeline.core.ingest.position.type.placeholder.IngestPlaceholderPosition;
 import org.apache.shardingsphere.data.pipeline.core.ingest.record.Column;
 import org.apache.shardingsphere.data.pipeline.core.ingest.record.DataRecord;
@@ -25,10 +26,22 @@ import 
org.apache.shardingsphere.data.pipeline.core.sqlbuilder.dialect.DialectPi
 import 
org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
 import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
 import org.junit.jupiter.api.Test;
 
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+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.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 class OpenGaussPipelineSQLBuilderTest {
     
@@ -36,12 +49,13 @@ class OpenGaussPipelineSQLBuilderTest {
     
     @Test
     void assertBuildInsertOnDuplicateClause() {
-        String actual = 
sqlBuilder.buildInsertOnDuplicateClause(mockDataRecord()).orElse(null);
-        assertThat(actual, is("ON DUPLICATE KEY UPDATE 
c0=EXCLUDED.c0,c1=EXCLUDED.c1,c2=EXCLUDED.c2,c3=EXCLUDED.c3"));
+        Optional<String> actual = 
sqlBuilder.buildInsertOnDuplicateClause(createDataRecord());
+        assertTrue(actual.isPresent());
+        assertThat(actual.get(), is("ON DUPLICATE KEY UPDATE 
c0=EXCLUDED.c0,c1=EXCLUDED.c1,c2=EXCLUDED.c2,c3=EXCLUDED.c3"));
     }
     
-    private DataRecord mockDataRecord() {
-        DataRecord result = new DataRecord(PipelineSQLOperationType.INSERT, 
"t1", new IngestPlaceholderPosition(), 4);
+    private DataRecord createDataRecord() {
+        DataRecord result = new DataRecord(PipelineSQLOperationType.INSERT, 
"foo_tbl", new IngestPlaceholderPosition(), 4);
         result.addColumn(new Column("id", "", false, true));
         result.addColumn(new Column("c0", "", false, false));
         result.addColumn(new Column("c1", "", true, false));
@@ -49,4 +63,43 @@ class OpenGaussPipelineSQLBuilderTest {
         result.addColumn(new Column("c3", "", 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 assertBuildCreateTableSQLs() throws SQLException {
+        Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+        ResultSet resultSet = mock(ResultSet.class);
+        when(resultSet.next()).thenReturn(true);
+        when(resultSet.getString("pg_get_tabledef")).thenReturn("CREATE TABLE 
foo_tbl (id INT PRIMARY KEY);");
+        when(connection.createStatement().executeQuery("SELECT * FROM 
pg_get_tabledef('foo_schema.foo_tbl')")).thenReturn(resultSet);
+        assertThat(sqlBuilder.buildCreateTableSQLs(new 
MockedDataSource(connection), "foo_schema", "foo_tbl"), 
is(Collections.singletonList("CREATE TABLE foo_tbl (id INT PRIMARY KEY)")));
+    }
+    
+    @Test
+    void assertBuildCreateTableSQLsFailed() {
+        assertThrows(CreateTableSQLGenerateException.class, () -> 
sqlBuilder.buildCreateTableSQLs(new MockedDataSource(), "foo_schema", 
"foo_tbl"));
+    }
+    
+    @Test
+    void assertBuildQueryCurrentPositionSQL() {
+        Optional<String> actual = sqlBuilder.buildQueryCurrentPositionSQL();
+        assertTrue(actual.isPresent());
+        assertThat(actual.get(), is("SELECT * FROM 
pg_current_xlog_location()"));
+    }
+    
+    @Test
+    void assertWrapWithPageQuery() {
+        assertThat(sqlBuilder.wrapWithPageQuery("SELECT * FROM foo_tbl"), 
is("SELECT * FROM foo_tbl LIMIT ?"));
+    }
 }

Reply via email to