From 88c202eae27d40f54984985499f6585b9bd4c418 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Fri, 18 Oct 2024 14:40:13 +0800 Subject: [PATCH] Add more test cases on PipelineDataConsistencyCalculateSQLBuilder (#33294) --- ...ataConsistencyCalculateSQLBuilderTest.java | 28 +++++++++++-------- .../PipelineImportSQLBuilderTest.java | 19 ++++++------- .../PipelineInventoryDumpSQLBuilderTest.java | 22 +++++++-------- .../PipelineSQLSegmentBuilderTest.java | 18 ++++++------ 4 files changed, 45 insertions(+), 42 deletions(-) rename kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/{ => sql}/PipelineDataConsistencyCalculateSQLBuilderTest.java (70%) rename kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/{ => sql}/PipelineImportSQLBuilderTest.java (76%) rename kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/{ => sql}/PipelineInventoryDumpSQLBuilderTest.java (57%) rename kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/{ => sql}/PipelineSQLSegmentBuilderTest.java (61%) diff --git a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/PipelineDataConsistencyCalculateSQLBuilderTest.java b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/sql/PipelineDataConsistencyCalculateSQLBuilderTest.java similarity index 70% rename from kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/PipelineDataConsistencyCalculateSQLBuilderTest.java rename to kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/sql/PipelineDataConsistencyCalculateSQLBuilderTest.java index 86754b92e37e1..9ed7bf2e5562f 100644 --- a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/PipelineDataConsistencyCalculateSQLBuilderTest.java +++ b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/sql/PipelineDataConsistencyCalculateSQLBuilderTest.java @@ -15,10 +15,9 @@ * limitations under the License. */ -package org.apache.shardingsphere.data.pipeline.core.sqlbuilder; +package org.apache.shardingsphere.data.pipeline.core.sqlbuilder.sql; import org.apache.shardingsphere.data.pipeline.core.ingest.dumper.inventory.QueryRange; -import org.apache.shardingsphere.data.pipeline.core.sqlbuilder.sql.PipelineDataConsistencyCalculateSQLBuilder; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.junit.jupiter.api.Test; @@ -27,6 +26,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Optional; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -39,34 +39,40 @@ class PipelineDataConsistencyCalculateSQLBuilderTest { private static final List SHARDING_COLUMNS_NAMES = Collections.singletonList("user_id"); - private final PipelineDataConsistencyCalculateSQLBuilder pipelineSQLBuilder = new PipelineDataConsistencyCalculateSQLBuilder(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + private final PipelineDataConsistencyCalculateSQLBuilder sqlBuilder = new PipelineDataConsistencyCalculateSQLBuilder(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); @Test void assertBuildQueryRangeOrderingSQLWithoutQueryCondition() { - String actual = pipelineSQLBuilder.buildQueryRangeOrderingSQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, + String actual = sqlBuilder.buildQueryRangeOrderingSQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, new QueryRange(1, true, 5), SHARDING_COLUMNS_NAMES); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order WHERE order_id>=? AND order_id<=? ORDER BY order_id ASC, status ASC, user_id ASC")); - actual = pipelineSQLBuilder.buildQueryRangeOrderingSQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, + actual = sqlBuilder.buildQueryRangeOrderingSQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, new QueryRange(1, false, 5), SHARDING_COLUMNS_NAMES); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order WHERE order_id>? AND order_id<=? ORDER BY order_id ASC, status ASC, user_id ASC")); - actual = pipelineSQLBuilder.buildQueryRangeOrderingSQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, + actual = sqlBuilder.buildQueryRangeOrderingSQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, new QueryRange(1, false, null), SHARDING_COLUMNS_NAMES); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order WHERE order_id>? ORDER BY order_id ASC, status ASC, user_id ASC")); - actual = pipelineSQLBuilder.buildQueryRangeOrderingSQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, + actual = sqlBuilder.buildQueryRangeOrderingSQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, new QueryRange(null, false, 5), SHARDING_COLUMNS_NAMES); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order WHERE order_id<=? ORDER BY order_id ASC, status ASC, user_id ASC")); - actual = pipelineSQLBuilder.buildQueryRangeOrderingSQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, + actual = sqlBuilder.buildQueryRangeOrderingSQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, new QueryRange(null, false, null), SHARDING_COLUMNS_NAMES); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order ORDER BY order_id ASC, status ASC, user_id ASC")); } @Test void assertBuildPointQuerySQLWithoutQueryCondition() { - String actual = pipelineSQLBuilder.buildPointQuerySQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, null); + String actual = sqlBuilder.buildPointQuerySQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, null); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order WHERE order_id=? AND status=?")); - actual = pipelineSQLBuilder.buildPointQuerySQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, Collections.emptyList()); + actual = sqlBuilder.buildPointQuerySQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, Collections.emptyList()); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order WHERE order_id=? AND status=?")); - actual = pipelineSQLBuilder.buildPointQuerySQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, Collections.singletonList("user_id")); + actual = sqlBuilder.buildPointQuerySQL(null, "t_order", COLUMN_NAMES, UNIQUE_KEYS, Collections.singletonList("user_id")); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order WHERE order_id=? AND status=? AND user_id=?")); } + + @Test + void assertBuildCRC32SQL() { + Optional actual = sqlBuilder.buildCRC32SQL("foo_schema", "foo_tbl", "foo_col"); + assertThat(actual, is(Optional.of("SELECT CRC32(foo_col) FROM foo_tbl"))); + } } diff --git a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/PipelineImportSQLBuilderTest.java b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/sql/PipelineImportSQLBuilderTest.java similarity index 76% rename from kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/PipelineImportSQLBuilderTest.java rename to kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/sql/PipelineImportSQLBuilderTest.java index 48ee072459636..c1eb17d99ffa5 100644 --- a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/PipelineImportSQLBuilderTest.java +++ b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/sql/PipelineImportSQLBuilderTest.java @@ -15,14 +15,13 @@ * limitations under the License. */ -package org.apache.shardingsphere.data.pipeline.core.sqlbuilder; +package org.apache.shardingsphere.data.pipeline.core.sqlbuilder.sql; import org.apache.shardingsphere.data.pipeline.core.constant.PipelineSQLOperationType; 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; import org.apache.shardingsphere.data.pipeline.core.ingest.record.RecordUtils; -import org.apache.shardingsphere.data.pipeline.core.sqlbuilder.sql.PipelineImportSQLBuilder; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.junit.jupiter.api.Test; @@ -35,50 +34,50 @@ class PipelineImportSQLBuilderTest { - private final PipelineImportSQLBuilder importSQLBuilder = new PipelineImportSQLBuilder(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + private final PipelineImportSQLBuilder sqlBuilder = new PipelineImportSQLBuilder(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); @Test void assertBuildInsertSQL() { - String actual = importSQLBuilder.buildInsertSQL(null, createDataRecordWithUniqueKey()); + String actual = sqlBuilder.buildInsertSQL(null, createDataRecordWithUniqueKey()); assertThat(actual, is("INSERT INTO foo_tbl(id,foo_col,col1,col2,col3) VALUES(?,?,?,?,?)")); } @Test void assertBuildUpdateSQLWithUniqueKey() { DataRecord dataRecord = createDataRecordWithUniqueKey(); - String actual = importSQLBuilder.buildUpdateSQL(null, dataRecord, mockConditionColumns(dataRecord)); + String actual = sqlBuilder.buildUpdateSQL(null, dataRecord, mockConditionColumns(dataRecord)); assertThat(actual, is("UPDATE foo_tbl SET col1 = ?,col2 = ?,col3 = ? WHERE id = ? AND foo_col = ?")); } @Test void assertBuildUpdateSQLWithoutUniqueKey() { DataRecord dataRecord = createDataRecordWithoutUniqueKey(); - String actual = importSQLBuilder.buildUpdateSQL(null, dataRecord, mockConditionColumns(dataRecord)); + String actual = sqlBuilder.buildUpdateSQL(null, dataRecord, mockConditionColumns(dataRecord)); assertThat(actual, is("UPDATE foo_tbl SET foo_col = ? WHERE id = ? AND foo_col = ?")); } @Test void assertBuildUpdateSQLWithoutConditionColumns() { - String actual = importSQLBuilder.buildUpdateSQL(null, createDataRecordWithUniqueKey(), Collections.emptyList()); + String actual = sqlBuilder.buildUpdateSQL(null, createDataRecordWithUniqueKey(), Collections.emptyList()); assertThat(actual, is("UPDATE foo_tbl SET col1 = ?,col2 = ?,col3 = ?")); } @Test void assertBuildDeleteSQLWithUniqueKey() { DataRecord dataRecord = createDataRecordWithUniqueKey(); - String actual = importSQLBuilder.buildDeleteSQL(null, dataRecord, mockConditionColumns(dataRecord)); + String actual = sqlBuilder.buildDeleteSQL(null, dataRecord, mockConditionColumns(dataRecord)); assertThat(actual, is("DELETE FROM foo_tbl WHERE id = ? AND foo_col = ?")); } @Test void assertBuildDeleteSQLWithoutUniqueKey() { - String actual = importSQLBuilder.buildDeleteSQL(null, createDataRecordWithoutUniqueKey(), RecordUtils.extractConditionColumns(createDataRecordWithoutUniqueKey(), Collections.emptySet())); + String actual = sqlBuilder.buildDeleteSQL(null, createDataRecordWithoutUniqueKey(), RecordUtils.extractConditionColumns(createDataRecordWithoutUniqueKey(), Collections.emptySet())); assertThat(actual, is("DELETE FROM foo_tbl WHERE id = ? AND foo_col = ?")); } @Test void assertBuildDeleteSQLWithoutConditionColumns() { - String actual = importSQLBuilder.buildDeleteSQL(null, createDataRecordWithUniqueKey(), Collections.emptyList()); + String actual = sqlBuilder.buildDeleteSQL(null, createDataRecordWithUniqueKey(), Collections.emptyList()); assertThat(actual, is("DELETE FROM foo_tbl")); } diff --git a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/PipelineInventoryDumpSQLBuilderTest.java b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/sql/PipelineInventoryDumpSQLBuilderTest.java similarity index 57% rename from kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/PipelineInventoryDumpSQLBuilderTest.java rename to kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/sql/PipelineInventoryDumpSQLBuilderTest.java index 4984b9ad0a1c0..f721527dc1c4c 100644 --- a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/PipelineInventoryDumpSQLBuilderTest.java +++ b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/sql/PipelineInventoryDumpSQLBuilderTest.java @@ -15,10 +15,8 @@ * limitations under the License. */ -package org.apache.shardingsphere.data.pipeline.core.sqlbuilder; +package org.apache.shardingsphere.data.pipeline.core.sqlbuilder.sql; -import org.apache.shardingsphere.data.pipeline.core.sqlbuilder.sql.BuildDivisibleSQLParameter; -import org.apache.shardingsphere.data.pipeline.core.sqlbuilder.sql.PipelineInventoryDumpSQLBuilder; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.junit.jupiter.api.Test; @@ -31,41 +29,41 @@ class PipelineInventoryDumpSQLBuilderTest { - private final PipelineInventoryDumpSQLBuilder inventoryDumpSQLBuilder = new PipelineInventoryDumpSQLBuilder(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + private final PipelineInventoryDumpSQLBuilder sqlBuilder = new PipelineInventoryDumpSQLBuilder(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); @Test void assertBuildDivisibleSQL() { - String actual = inventoryDumpSQLBuilder.buildDivisibleSQL(new BuildDivisibleSQLParameter(null, "t_order", Arrays.asList("order_id", "user_id", "status"), "order_id", true, true)); + String actual = sqlBuilder.buildDivisibleSQL(new BuildDivisibleSQLParameter(null, "t_order", Arrays.asList("order_id", "user_id", "status"), "order_id", true, true)); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order WHERE order_id>=? AND order_id<=? ORDER BY order_id ASC")); - actual = inventoryDumpSQLBuilder.buildDivisibleSQL(new BuildDivisibleSQLParameter(null, "t_order", Arrays.asList("order_id", "user_id", "status"), "order_id", false, true)); + actual = sqlBuilder.buildDivisibleSQL(new BuildDivisibleSQLParameter(null, "t_order", Arrays.asList("order_id", "user_id", "status"), "order_id", false, true)); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order WHERE order_id>? AND order_id<=? ORDER BY order_id ASC")); } @Test void assertBuildUnlimitedDivisibleSQL() { - String actual = inventoryDumpSQLBuilder.buildDivisibleSQL(new BuildDivisibleSQLParameter(null, "t_order", Arrays.asList("order_id", "user_id", "status"), "order_id", true, false)); + String actual = sqlBuilder.buildDivisibleSQL(new BuildDivisibleSQLParameter(null, "t_order", Arrays.asList("order_id", "user_id", "status"), "order_id", true, false)); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order WHERE order_id>=? ORDER BY order_id ASC")); - actual = inventoryDumpSQLBuilder.buildDivisibleSQL(new BuildDivisibleSQLParameter(null, "t_order", Arrays.asList("order_id", "user_id", "status"), "order_id", false, false)); + actual = sqlBuilder.buildDivisibleSQL(new BuildDivisibleSQLParameter(null, "t_order", Arrays.asList("order_id", "user_id", "status"), "order_id", false, false)); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order WHERE order_id>? ORDER BY order_id ASC")); } @Test void assertBuildIndivisibleSQL() { - String actual = inventoryDumpSQLBuilder.buildIndivisibleSQL(null, "t_order", Arrays.asList("order_id", "user_id", "status"), "order_id"); + String actual = sqlBuilder.buildIndivisibleSQL(null, "t_order", Arrays.asList("order_id", "user_id", "status"), "order_id"); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order ORDER BY order_id ASC")); } @Test void assertBuildPointQuerySQL() { - String actual = inventoryDumpSQLBuilder.buildPointQuerySQL(null, "t_order", Arrays.asList("order_id", "user_id", "status"), "order_id"); + String actual = sqlBuilder.buildPointQuerySQL(null, "t_order", Arrays.asList("order_id", "user_id", "status"), "order_id"); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order WHERE order_id=?")); } @Test void assertBuildFetchAllSQL() { - String actual = inventoryDumpSQLBuilder.buildFetchAllSQL(null, "t_order", Arrays.asList("order_id", "user_id", "status")); + String actual = sqlBuilder.buildFetchAllSQL(null, "t_order", Arrays.asList("order_id", "user_id", "status")); assertThat(actual, is("SELECT order_id,user_id,status FROM t_order")); - actual = inventoryDumpSQLBuilder.buildFetchAllSQL(null, "t_order", Collections.singletonList("*")); + actual = sqlBuilder.buildFetchAllSQL(null, "t_order", Collections.singletonList("*")); assertThat(actual, is("SELECT * FROM t_order")); } } diff --git a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/PipelineSQLSegmentBuilderTest.java b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/sql/PipelineSQLSegmentBuilderTest.java similarity index 61% rename from kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/PipelineSQLSegmentBuilderTest.java rename to kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/sql/PipelineSQLSegmentBuilderTest.java index 0390dff9213e1..5c97cb66d7d08 100644 --- a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/PipelineSQLSegmentBuilderTest.java +++ b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/sqlbuilder/sql/PipelineSQLSegmentBuilderTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.data.pipeline.core.sqlbuilder; +package org.apache.shardingsphere.data.pipeline.core.sqlbuilder.sql; import org.apache.shardingsphere.data.pipeline.core.sqlbuilder.segment.PipelineSQLSegmentBuilder; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; @@ -27,27 +27,27 @@ class PipelineSQLSegmentBuilderTest { + private final PipelineSQLSegmentBuilder mySQLBuilder = new PipelineSQLSegmentBuilder(TypedSPILoader.getService(DatabaseType.class, "MySQL")); + + private final PipelineSQLSegmentBuilder postgreSQLBuilder = new PipelineSQLSegmentBuilder(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")); + @Test void assertGetEscapedIdentifier() { - PipelineSQLSegmentBuilder sqlSegmentBuilder = new PipelineSQLSegmentBuilder(TypedSPILoader.getService(DatabaseType.class, "MySQL")); - assertThat(sqlSegmentBuilder.getEscapedIdentifier("SELECT"), is("`SELECT`")); + assertThat(mySQLBuilder.getEscapedIdentifier("SELECT"), is("`SELECT`")); } @Test void assertGetUnescapedIdentifier() { - PipelineSQLSegmentBuilder sqlSegmentBuilder = new PipelineSQLSegmentBuilder(TypedSPILoader.getService(DatabaseType.class, "MySQL")); - assertThat(sqlSegmentBuilder.getEscapedIdentifier("SELECT1"), is("SELECT1")); + assertThat(mySQLBuilder.getEscapedIdentifier("SELECT1"), is("SELECT1")); } @Test void assertGetQualifiedTableNameWithUnsupportedSchema() { - PipelineSQLSegmentBuilder sqlSegmentBuilder = new PipelineSQLSegmentBuilder(TypedSPILoader.getService(DatabaseType.class, "MySQL")); - assertThat(sqlSegmentBuilder.getQualifiedTableName("foo_schema", "foo_tbl"), is("foo_tbl")); + assertThat(mySQLBuilder.getQualifiedTableName("foo_schema", "foo_tbl"), is("foo_tbl")); } @Test void assertGetQualifiedTableNameWithSupportedSchema() { - PipelineSQLSegmentBuilder sqlSegmentBuilder = new PipelineSQLSegmentBuilder(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")); - assertThat(sqlSegmentBuilder.getQualifiedTableName("foo_schema", "foo_tbl"), is("foo_schema.foo_tbl")); + assertThat(postgreSQLBuilder.getQualifiedTableName("foo_schema", "foo_tbl"), is("foo_schema.foo_tbl")); } }