Skip to content

Commit

Permalink
Add more test cases on PipelineDataConsistencyCalculateSQLBuilder (#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Oct 18, 2024
1 parent 4a3d46b commit 88c202e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -39,34 +39,40 @@ class PipelineDataConsistencyCalculateSQLBuilderTest {

private static final List<String> 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<String> actual = sqlBuilder.buildCRC32SQL("foo_schema", "foo_tbl", "foo_col");
assertThat(actual, is(Optional.of("SELECT CRC32(foo_col) FROM foo_tbl")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"));
}
}

0 comments on commit 88c202e

Please sign in to comment.