Skip to content

Commit

Permalink
Refactor DistSQL unit tests (#30797)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pace2Car authored Apr 7, 2024
1 parent a05397c commit 9e68371
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void assertUpdateCurrentRuleConfiguration() {
when(rule.getConfiguration()).thenReturn(config);
executor.setRule(rule);
DropBroadcastTableRuleStatement sqlStatement = new DropBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
BroadcastRuleConfiguration toBeDroppedConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement);
assertTrue(toBeDroppedConfig.getTables().isEmpty());
BroadcastRuleConfiguration toBeAlteredConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement);
assertTrue(toBeAlteredConfig.getTables().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ void assertUpdateCurrentRuleConfigurationWithInUsedEncryptor() {
when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration());
executor.setRule(rule);
EncryptRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(createSQLStatement("MD5"));
assertThat(toBeAlteredRuleConfig.getTables().size(), is(1));
assertThat(toBeAlteredRuleConfig.getTables().iterator().next().getName(), is("t_encrypt"));
assertThat(toBeAlteredRuleConfig.getTables().iterator().next().getColumns().iterator().next().getName(), is("user_id"));
assertThat(toBeAlteredRuleConfig.getEncryptors().size(), is(3));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ void assertUpdate() {
when(rule.getConfiguration()).thenReturn(currentRuleConfig);
executor.setRule(rule);
MaskRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement);
assertThat(toBeAlteredRuleConfig.getTables().size(), is(1));
assertThat(toBeAlteredRuleConfig.getTables().iterator().next().getName(), is("t_order"));
assertThat(toBeAlteredRuleConfig.getTables().iterator().next().getColumns().iterator().next().getLogicColumn(), is("order_id"));
assertThat(toBeAlteredRuleConfig.getMaskAlgorithms().size(), is(1));
assertTrue(toBeAlteredRuleConfig.getMaskAlgorithms().containsKey("t_order_order_id_md5"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ void assertUpdateCurrentRuleConfiguration() {
when(rule.getConfiguration()).thenReturn(ruleConfig);
executor.setRule(rule);
MaskRuleConfiguration toBeDroppedRuleConfig = executor.buildToBeDroppedRuleConfiguration(createSQLStatement(false, "t_mask"));
assertTrue(toBeDroppedRuleConfig.getMaskAlgorithms().isEmpty());
assertThat(toBeDroppedRuleConfig.getTables().size(), is(1));
assertTrue(toBeDroppedRuleConfig.getMaskAlgorithms().isEmpty());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ void assertUpdateSuccess() {
executor.setRule(mock(ReadwriteSplittingRule.class));
ReadwriteSplittingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement);
assertThat(toBeCreatedRuleConfig.getDataSources().size(), is(1));
assertThat(toBeCreatedRuleConfig.getLoadBalancers().size(), is(1));
}

private CreateReadwriteSplittingRuleStatement createSQLStatement(final String loadBalancerName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.RuleDefinitionException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.InUsedRuleException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.RuleDefinitionException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;
Expand Down Expand Up @@ -98,6 +98,7 @@ void assertBuildToBeDroppedRuleConfiguration() {
when(rule.getConfiguration()).thenReturn(ruleConfig);
executor.setRule(rule);
ReadwriteSplittingRuleConfiguration actual = executor.buildToBeDroppedRuleConfiguration(createSQLStatement());
assertThat(actual.getDataSources().size(), is(1));
assertThat(actual.getLoadBalancers().size(), is(1));
}

Expand All @@ -108,6 +109,7 @@ void assertBuildToBeDroppedRuleConfigurationWithInUsedLoadBalancer() {
when(rule.getConfiguration()).thenReturn(ruleConfig);
executor.setRule(rule);
ReadwriteSplittingRuleConfiguration actual = executor.buildToBeDroppedRuleConfiguration(createSQLStatement());
assertThat(actual.getDataSources().size(), is(1));
assertThat(actual.getLoadBalancers().size(), is(0));
}

Expand All @@ -118,6 +120,7 @@ void assertBuildToBeDroppedRuleConfigurationWithoutLoadBalancerName() {
when(rule.getConfiguration()).thenReturn(ruleConfig);
executor.setRule(rule);
ReadwriteSplittingRuleConfiguration actual = executor.buildToBeDroppedRuleConfiguration(createSQLStatement());
assertThat(actual.getDataSources().size(), is(1));
assertThat(actual.getLoadBalancers().size(), is(1));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
Expand Down Expand Up @@ -77,7 +76,7 @@ void assertUpdate() {
DropDefaultShadowAlgorithmStatement sqlStatement = new DropDefaultShadowAlgorithmStatement(false);
assertTrue(executor.hasAnyOneToBeDropped(sqlStatement));
ShadowRuleConfiguration toBeDroppedRuleConfig = executor.buildToBeDroppedRuleConfiguration(sqlStatement);
assertFalse(toBeDroppedRuleConfig.getDefaultShadowAlgorithmName().isEmpty());
assertThat(toBeDroppedRuleConfig.getDefaultShadowAlgorithmName(), is("default"));
assertThat(toBeDroppedRuleConfig.getShadowAlgorithms().size(), is(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ void assertCheckWithNotExistedTables() {

@Test
void assertBuildToBeAlteredRuleConfiguration() {
ShardingRuleConfiguration actual = executor.buildToBeAlteredRuleConfiguration(createSQLStatement("reference_0", "t_3,t_4"));
assertThat(actual.getBindingTableGroups().size(), is(1));
assertThat(actual.getBindingTableGroups().iterator().next().getName(), is("reference_0"));
assertThat(actual.getBindingTableGroups().iterator().next().getReference(), is("t_3,t_4"));
ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(createSQLStatement("reference_0", "t_3,t_4"));
assertThat(toBeAlteredRuleConfig.getBindingTableGroups().size(), is(1));
assertThat(toBeAlteredRuleConfig.getBindingTableGroups().iterator().next().getName(), is("reference_0"));
assertThat(toBeAlteredRuleConfig.getBindingTableGroups().iterator().next().getReference(), is("t_3,t_4"));
}

private AlterShardingTableReferenceRuleStatement createSQLStatement(final String name, final String reference) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ void assertUpdate() {
when(rule.getConfiguration()).thenReturn(currentRuleConfig);
executor.setRule(rule);
executor.checkBeforeUpdate(sqlStatement);
ShardingRuleConfiguration actual = executor.buildToBeAlteredRuleConfiguration(sqlStatement);
assertThat(actual.getTables().size(), is(1));
ShardingTableRuleConfiguration tableRule = actual.getTables().iterator().next();
ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement);
assertThat(toBeAlteredRuleConfig.getTables().size(), is(1));
ShardingTableRuleConfiguration tableRule = toBeAlteredRuleConfig.getTables().iterator().next();
assertThat(tableRule.getLogicTable(), is("t_order"));
assertThat(tableRule.getActualDataNodes(), is("ds_${0..1}.t_order${0..1}"));
assertThat(tableRule.getTableShardingStrategy(), instanceOf(StandardShardingStrategyConfiguration.class));
assertThat(((StandardShardingStrategyConfiguration) tableRule.getTableShardingStrategy()).getShardingColumn(), is("product_id"));
assertThat(tableRule.getTableShardingStrategy().getShardingAlgorithmName(), is("t_order_table_core.standard.fixture"));
assertThat(tableRule.getDatabaseShardingStrategy(), instanceOf(StandardShardingStrategyConfiguration.class));
assertThat(tableRule.getDatabaseShardingStrategy().getShardingAlgorithmName(), is("t_order_database_inline"));
assertThat(actual.getTables().size(), is(1));
ShardingAutoTableRuleConfiguration autoTableRule = actual.getAutoTables().iterator().next();
assertThat(toBeAlteredRuleConfig.getTables().size(), is(1));
ShardingAutoTableRuleConfiguration autoTableRule = toBeAlteredRuleConfig.getAutoTables().iterator().next();
assertThat(autoTableRule.getLogicTable(), is("t_order_item"));
assertThat(autoTableRule.getActualDataSources(), is("ds_0,ds_1"));
assertThat(autoTableRule.getShardingStrategy().getShardingAlgorithmName(), is("t_order_item_foo.distsql.fixture"));
Expand All @@ -105,18 +105,18 @@ void assertUpdateWithDifferentCase() {
when(rule.getConfiguration()).thenReturn(currentRuleConfig);
executor.setRule(rule);
executor.checkBeforeUpdate(sqlStatement);
ShardingRuleConfiguration actual = executor.buildToBeAlteredRuleConfiguration(sqlStatement);
assertThat(actual.getTables().size(), is(1));
ShardingTableRuleConfiguration tableRule = actual.getTables().iterator().next();
ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement);
assertThat(toBeAlteredRuleConfig.getTables().size(), is(1));
ShardingTableRuleConfiguration tableRule = toBeAlteredRuleConfig.getTables().iterator().next();
assertThat(tableRule.getLogicTable(), is("T_ORDER"));
assertThat(tableRule.getActualDataNodes(), is("ds_${0..1}.t_order${0..1}"));
assertThat(tableRule.getTableShardingStrategy(), instanceOf(StandardShardingStrategyConfiguration.class));
assertThat(((StandardShardingStrategyConfiguration) tableRule.getTableShardingStrategy()).getShardingColumn(), is("product_id"));
assertThat(tableRule.getTableShardingStrategy().getShardingAlgorithmName(), is("t_order_table_core.standard.fixture"));
assertThat(tableRule.getDatabaseShardingStrategy(), instanceOf(StandardShardingStrategyConfiguration.class));
assertThat(tableRule.getDatabaseShardingStrategy().getShardingAlgorithmName(), is("t_order_database_inline"));
assertThat(actual.getTables().size(), is(1));
ShardingAutoTableRuleConfiguration autoTableRule = actual.getAutoTables().iterator().next();
assertThat(toBeAlteredRuleConfig.getTables().size(), is(1));
ShardingAutoTableRuleConfiguration autoTableRule = toBeAlteredRuleConfig.getAutoTables().iterator().next();
assertThat(autoTableRule.getLogicTable(), is("T_ORDER_ITEM"));
assertThat(autoTableRule.getActualDataSources(), is("ds_0,ds_1"));
assertThat(autoTableRule.getShardingStrategy().getShardingAlgorithmName(), is("t_order_item_foo.distsql.fixture"));
Expand All @@ -132,18 +132,18 @@ void assertUpdateTableType() {
when(rule.getConfiguration()).thenReturn(currentRuleConfig);
executor.setRule(rule);
executor.checkBeforeUpdate(sqlStatement);
ShardingRuleConfiguration actual = executor.buildToBeAlteredRuleConfiguration(sqlStatement);
assertThat(actual.getTables().size(), is(1));
ShardingTableRuleConfiguration tableRule = actual.getTables().iterator().next();
ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement);
assertThat(toBeAlteredRuleConfig.getTables().size(), is(1));
ShardingTableRuleConfiguration tableRule = toBeAlteredRuleConfig.getTables().iterator().next();
assertThat(tableRule.getLogicTable(), is("t_order_item"));
assertThat(tableRule.getActualDataNodes(), is("ds_${0..1}.t_order${0..1}"));
assertThat(tableRule.getTableShardingStrategy(), instanceOf(StandardShardingStrategyConfiguration.class));
assertThat(((StandardShardingStrategyConfiguration) tableRule.getTableShardingStrategy()).getShardingColumn(), is("product_id"));
assertThat(tableRule.getTableShardingStrategy().getShardingAlgorithmName(), is("t_order_item_table_core.standard.fixture"));
assertThat(tableRule.getDatabaseShardingStrategy(), instanceOf(StandardShardingStrategyConfiguration.class));
assertThat(tableRule.getDatabaseShardingStrategy().getShardingAlgorithmName(), is("t_order_item_database_inline"));
assertThat(actual.getTables().size(), is(1));
ShardingAutoTableRuleConfiguration autoTableRule = actual.getAutoTables().iterator().next();
assertThat(toBeAlteredRuleConfig.getTables().size(), is(1));
ShardingAutoTableRuleConfiguration autoTableRule = toBeAlteredRuleConfig.getAutoTables().iterator().next();
assertThat(autoTableRule.getLogicTable(), is("t_order"));
assertThat(autoTableRule.getActualDataSources(), is("ds_0,ds_1"));
assertThat(autoTableRule.getShardingStrategy().getShardingAlgorithmName(), is("t_order_foo.distsql.fixture"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ void assertCreateDefaultTableShardingStrategy() {
AlgorithmSegment algorithm = new AlgorithmSegment("order_id_algorithm", new Properties());
CreateDefaultShardingStrategyStatement sqlStatement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "standard", "order_id", algorithm);
executor.checkBeforeUpdate(sqlStatement);
ShardingRuleConfiguration actual = executor.buildToBeCreatedRuleConfiguration(sqlStatement);
StandardShardingStrategyConfiguration defaultTableShardingStrategy = (StandardShardingStrategyConfiguration) actual.getDefaultTableShardingStrategy();
ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement);
StandardShardingStrategyConfiguration defaultTableShardingStrategy = (StandardShardingStrategyConfiguration) toBeCreatedRuleConfig.getDefaultTableShardingStrategy();
assertThat(defaultTableShardingStrategy.getShardingAlgorithmName(), is("default_table_order_id_algorithm"));
assertThat(defaultTableShardingStrategy.getShardingColumn(), is("order_id"));
}
Expand All @@ -119,8 +119,8 @@ void assertCreateDefaultDatabaseShardingStrategy() {
when(rule.getConfiguration()).thenReturn(currentRuleConfig);
executor.setRule(rule);
executor.checkBeforeUpdate(statement);
ShardingRuleConfiguration actual = executor.buildToBeCreatedRuleConfiguration(statement);
StandardShardingStrategyConfiguration defaultDatabaseShardingStrategy = (StandardShardingStrategyConfiguration) actual.getDefaultDatabaseShardingStrategy();
ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(statement);
StandardShardingStrategyConfiguration defaultDatabaseShardingStrategy = (StandardShardingStrategyConfiguration) toBeCreatedRuleConfig.getDefaultDatabaseShardingStrategy();
assertThat(defaultDatabaseShardingStrategy.getShardingAlgorithmName(), is("default_database_inline"));
assertThat(defaultDatabaseShardingStrategy.getShardingColumn(), is("user_id"));
}
Expand All @@ -139,8 +139,8 @@ void assertCreateDefaultTableShardingStrategyWithIfNotExists() {
algorithm = new AlgorithmSegment("user_id_algorithm", new Properties());
CreateDefaultShardingStrategyStatement statementWithIfNotExists = new CreateDefaultShardingStrategyStatement(true, "TABLE", "standard", "order_id", algorithm);
executor.checkBeforeUpdate(statementWithIfNotExists);
ShardingRuleConfiguration actual = executor.buildToBeCreatedRuleConfiguration(statementWithIfNotExists);
StandardShardingStrategyConfiguration defaultTableShardingStrategy = (StandardShardingStrategyConfiguration) actual.getDefaultTableShardingStrategy();
ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(statementWithIfNotExists);
StandardShardingStrategyConfiguration defaultTableShardingStrategy = (StandardShardingStrategyConfiguration) toBeCreatedRuleConfig.getDefaultTableShardingStrategy();
assertThat(defaultTableShardingStrategy.getShardingAlgorithmName(), is("default_table_user_id_algorithm"));
assertThat(defaultTableShardingStrategy.getShardingColumn(), is("order_id"));
}
Expand All @@ -155,8 +155,8 @@ void assertCreateDefaultTableShardingStrategyWithNoneShardingStrategyType() {
executor.setRule(rule);
CreateDefaultShardingStrategyStatement sqlStatement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "none", null, null);
executor.checkBeforeUpdate(sqlStatement);
ShardingRuleConfiguration actual = executor.buildToBeCreatedRuleConfiguration(sqlStatement);
NoneShardingStrategyConfiguration defaultTableShardingStrategy = (NoneShardingStrategyConfiguration) actual.getDefaultTableShardingStrategy();
ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement);
NoneShardingStrategyConfiguration defaultTableShardingStrategy = (NoneShardingStrategyConfiguration) toBeCreatedRuleConfig.getDefaultTableShardingStrategy();
assertThat(defaultTableShardingStrategy.getType(), is(""));
assertThat(defaultTableShardingStrategy.getShardingAlgorithmName(), is(""));
}
Expand All @@ -169,8 +169,8 @@ void assertCreateDefaultDatabaseShardingStrategyWithNoneShardingStrategyType() {
when(rule.getConfiguration()).thenReturn(currentRuleConfig);
executor.setRule(rule);
executor.checkBeforeUpdate(sqlStatement);
ShardingRuleConfiguration actual = executor.buildToBeCreatedRuleConfiguration(sqlStatement);
NoneShardingStrategyConfiguration defaultDatabaseShardingStrategy = (NoneShardingStrategyConfiguration) actual.getDefaultDatabaseShardingStrategy();
ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement);
NoneShardingStrategyConfiguration defaultDatabaseShardingStrategy = (NoneShardingStrategyConfiguration) toBeCreatedRuleConfig.getDefaultDatabaseShardingStrategy();
assertThat(defaultDatabaseShardingStrategy.getType(), is(""));
assertThat(defaultDatabaseShardingStrategy.getShardingAlgorithmName(), is(""));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ void assertUpdateWithIfNotExists() {
when(rule.getConfiguration()).thenReturn(currentRuleConfig);
executor.setRule(rule);
executor.checkBeforeUpdate(sqlStatement);
ShardingRuleConfiguration actual = executor.buildToBeCreatedRuleConfiguration(sqlStatement);
Collection<ShardingTableReferenceRuleConfiguration> referenceRuleConfigs = actual.getBindingTableGroups();
ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement);
Collection<ShardingTableReferenceRuleConfiguration> referenceRuleConfigs = toBeCreatedRuleConfig.getBindingTableGroups();
assertThat(referenceRuleConfigs.size(), is(0));
}

Expand Down
Loading

0 comments on commit 9e68371

Please sign in to comment.