Skip to content

Commit

Permalink
Add more test cases on SingleRuleConfigurationToDistSQLConverter (#33459
Browse files Browse the repository at this point in the history
)

* Add more test cases on SingleRuleConfigurationToDistSQLConverter

* Add more test cases on SingleRuleConfigurationToDistSQLConverter
  • Loading branch information
terrymanu authored Oct 29, 2024
1 parent 8645c43 commit 5e27981
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class BroadcastConvertDistSQLConstants {

public static final String CREATE_BROADCAST_TABLE_RULE = "CREATE BROADCAST TABLE RULE %s;";
public static final String SQL_PATTERN = "CREATE BROADCAST TABLE RULE %s;";
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public final class BroadcastRuleConfigurationToDistSQLConverter implements RuleC

@Override
public String convert(final BroadcastRuleConfiguration ruleConfig) {
if (ruleConfig.getTables().isEmpty()) {
return "";
}
return String.format(BroadcastConvertDistSQLConstants.CREATE_BROADCAST_TABLE_RULE, Joiner.on(",").join(ruleConfig.getTables()));
return ruleConfig.getTables().isEmpty() ? "" : String.format(BroadcastConvertDistSQLConstants.SQL_PATTERN, Joiner.on(",").join(ruleConfig.getTables()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SingleConvertDistSQLConstants {

public static final String LOAD_SINGLE_TABLE = "LOAD SINGLE TABLE %s;";
public static final String LOAD_SQL_PATTERN = "LOAD SINGLE TABLE %s;";

public static final String SET_DEFAULT_SINGLE_TABLE_STORAGE_UNIT = "SET DEFAULT SINGLE TABLE STORAGE UNIT = %s;";
public static final String SET_DEFAULT_SQL_PATTERN = "SET DEFAULT SINGLE TABLE STORAGE UNIT = %s;";
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public final class SingleRuleConfigurationToDistSQLConverter implements RuleConf

@Override
public String convert(final SingleRuleConfiguration ruleConfig) {
if (ruleConfig.getTables().isEmpty() && !ruleConfig.getDefaultDataSource().isPresent()) {
return "";
}
StringBuilder result = new StringBuilder();
if (!ruleConfig.getTables().isEmpty()) {
result.append(convertLoadTable(ruleConfig));
Expand All @@ -45,11 +42,11 @@ public String convert(final SingleRuleConfiguration ruleConfig) {
}

private String convertLoadTable(final SingleRuleConfiguration ruleConfig) {
return String.format(SingleConvertDistSQLConstants.LOAD_SINGLE_TABLE, Joiner.on(",").join(ruleConfig.getTables()));
return String.format(SingleConvertDistSQLConstants.LOAD_SQL_PATTERN, Joiner.on(",").join(ruleConfig.getTables()));
}

private String convertSetDefaultSingleTableStorageUnit(final String defaultStorageUnitName) {
return String.format(SingleConvertDistSQLConstants.SET_DEFAULT_SINGLE_TABLE_STORAGE_UNIT, defaultStorageUnitName);
return String.format(SingleConvertDistSQLConstants.SET_DEFAULT_SQL_PATTERN, defaultStorageUnitName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,38 @@

package org.apache.shardingsphere.single.distsql.handler.converter;

import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.RuleConfigurationToDistSQLConverter;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.single.config.SingleRuleConfiguration;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class SingleRuleConfigurationToDistSQLConverterTest {

@SuppressWarnings("unchecked")
private final RuleConfigurationToDistSQLConverter<SingleRuleConfiguration> converter = TypedSPILoader.getService(RuleConfigurationToDistSQLConverter.class, SingleRuleConfiguration.class);

@Test
void assertConvert() {
SingleRuleConfiguration singleRuleConfig = new SingleRuleConfiguration(new LinkedList<>(Arrays.asList("t_0", "t_1")), "foo_ds");
SingleRuleConfigurationToDistSQLConverter singleRuleConfigurationToDistSQLConverter = new SingleRuleConfigurationToDistSQLConverter();
assertThat(singleRuleConfigurationToDistSQLConverter.convert(singleRuleConfig),
is("LOAD SINGLE TABLE t_0,t_1;" + System.lineSeparator() + System.lineSeparator() + "SET DEFAULT SINGLE TABLE STORAGE UNIT = foo_ds;"));
SingleRuleConfiguration singleRuleConfig = new SingleRuleConfiguration(Arrays.asList("foo_tbl", "bar_tbl"), "foo_ds");
assertThat(converter.convert(singleRuleConfig),
is("LOAD SINGLE TABLE foo_tbl,bar_tbl;" + System.lineSeparator() + System.lineSeparator() + "SET DEFAULT SINGLE TABLE STORAGE UNIT = foo_ds;"));
}

@Test
void assertConvertWithDefaultDatasourceOnly() {
SingleRuleConfiguration singleRuleConfig = new SingleRuleConfiguration(Collections.emptyList(), "foo_ds");
assertThat(converter.convert(singleRuleConfig), is("SET DEFAULT SINGLE TABLE STORAGE UNIT = foo_ds;"));
}

@Test
void assertConvertWithoutDefaultDatasourceAndTables() {
SingleRuleConfiguration singleRuleConfig = mock(SingleRuleConfiguration.class);
when(singleRuleConfig.getDefaultDataSource()).thenReturn(Optional.empty());
when(singleRuleConfig.getTables()).thenReturn(Collections.emptyList());
SingleRuleConfigurationToDistSQLConverter singleRuleConfigurationToDistSQLConverter = new SingleRuleConfigurationToDistSQLConverter();
assertThat(singleRuleConfigurationToDistSQLConverter.convert(singleRuleConfig), is(""));
void assertConvertWithEmptyTablesAndDefaultDatasource() {
SingleRuleConfiguration ruleConfig = new SingleRuleConfiguration();
assertThat(converter.convert(ruleConfig), is(""));
}
}

0 comments on commit 5e27981

Please sign in to comment.