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 63fd81a16de Add more test cases on MySQLPipelineSQLBuilder (#33411)
63fd81a16de is described below
commit 63fd81a16de74afcfcec308d73b12616a3f983a7
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Oct 26 16:14:59 2024 +0800
Add more test cases on MySQLPipelineSQLBuilder (#33411)
* Add more test cases on MySQLPipelineSQLBuilder
* Add more test cases on MySQLPipelineSQLBuilder
---
.../sqlbuilder/MySQLPipelineSQLBuilderTest.java | 58 ++++++++++++++++++----
1 file changed, 47 insertions(+), 11 deletions(-)
diff --git
a/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/sqlbuilder/MySQLPipelineSQLBuilderTest.java
b/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/sqlbuilder/MySQLPipelineSQLBuilderTest.java
index cff00818b40..3faae57270b 100644
---
a/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/sqlbuilder/MySQLPipelineSQLBuilderTest.java
+++
b/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/sqlbuilder/MySQLPipelineSQLBuilderTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.data.pipeline.mysql.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,13 +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 MySQLPipelineSQLBuilderTest {
@@ -39,19 +49,13 @@ class MySQLPipelineSQLBuilderTest {
@Test
void assertBuildInsertSQLOnDuplicateClause() {
- String actual =
sqlBuilder.buildInsertOnDuplicateClause(mockDataRecord("t1")).orElse(null);
- assertThat(actual, is("ON DUPLICATE KEY UPDATE
id=VALUES(id),sc=VALUES(sc),c1=VALUES(c1),c2=VALUES(c2),c3=VALUES(c3)"));
- }
-
- @Test
- void assertBuildSumCrc32SQL() {
- Optional<String> actual = sqlBuilder.buildCRC32SQL("t2", "id");
+ Optional<String> actual =
sqlBuilder.buildInsertOnDuplicateClause(createDataRecord());
assertTrue(actual.isPresent());
- assertThat(actual.get(), is("SELECT BIT_XOR(CAST(CRC32(id) AS
UNSIGNED)) AS checksum, COUNT(1) AS cnt FROM t2"));
+ assertThat(actual.get(), is("ON DUPLICATE KEY UPDATE
id=VALUES(id),sc=VALUES(sc),c1=VALUES(c1),c2=VALUES(c2),c3=VALUES(c3)"));
}
- private DataRecord mockDataRecord(final String tableName) {
- DataRecord result = new DataRecord(PipelineSQLOperationType.INSERT,
tableName, 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("sc", "", false, false));
result.addColumn(new Column("c1", "", true, false));
@@ -61,9 +65,41 @@ class MySQLPipelineSQLBuilderTest {
}
@Test
- void assertBuilderEstimateCountSQLWithoutKeyword() {
+ void assertBuildCheckEmptyTableSQL() {
+ assertThat(sqlBuilder.buildCheckEmptyTableSQL("foo_tbl"), is("SELECT *
FROM foo_tbl LIMIT 1"));
+ }
+
+ @Test
+ void assertBuildEstimateCountSQL() {
Optional<String> actual =
sqlBuilder.buildEstimatedCountSQL("foo_catalog", "foo_tbl");
assertTrue(actual.isPresent());
assertThat(actual.get(), is("SELECT TABLE_ROWS FROM
INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'foo_catalog' AND TABLE_NAME =
'foo_tbl'"));
}
+
+ @Test
+ void assertBuildCRC32SQL() {
+ Optional<String> actual = sqlBuilder.buildCRC32SQL("foo_tbl", "id");
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(), is("SELECT BIT_XOR(CAST(CRC32(id) AS
UNSIGNED)) AS checksum, COUNT(1) AS cnt FROM foo_tbl"));
+ }
+
+ @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("create table")).thenReturn("CREATE TABLE
foo_tbl (id INT PRIMARY KEY)");
+ when(connection.createStatement().executeQuery("SHOW CREATE TABLE
foo_tbl")).thenReturn(resultSet);
+ assertThat(sqlBuilder.buildCreateTableSQLs(new
MockedDataSource(connection), "foo_schema", "foo_tbl"),
is(Collections.singleton("CREATE TABLE foo_tbl (id INT PRIMARY KEY)")));
+ }
+
+ @Test
+ void assertBuildCreateTableSQLsFailed() {
+ assertThrows(CreateTableSQLGenerateException.class, () ->
sqlBuilder.buildCreateTableSQLs(new MockedDataSource(), "foo_schema",
"foo_tbl"));
+ }
+
+ @Test
+ void assertWrapWithPageQuery() {
+ assertThat(sqlBuilder.wrapWithPageQuery("SELECT * FROM foo_tbl"),
is("SELECT * FROM foo_tbl LIMIT ?"));
+ }
}