提交 2a20af42 编写于 作者: T tianbin

add test case

上级 fdb5f099
......@@ -17,12 +17,17 @@
package io.shardingjdbc.core.rewrite;
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
import io.shardingjdbc.core.api.config.TableRuleConfiguration;
import io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration;
import io.shardingjdbc.core.exception.ShardingJdbcException;
import io.shardingjdbc.core.rewrite.placeholder.IndexPlaceholder;
import io.shardingjdbc.core.rewrite.placeholder.SchemaPlaceholder;
import io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder;
import io.shardingjdbc.core.rule.ShardingRule;
import org.junit.Test;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
......@@ -63,4 +68,71 @@ public final class SQLBuilderTest {
tableTokens.put("table_x", "table_x_1");
assertThat(sqlBuilder.toSQL(tableTokens, null), is("SELECT table_x_1.id FROM table_x_1"));
}
@Test
public void assertIndexPlaceholderAppendTableWithoutTableToken() {
SQLBuilder sqlBuilder = new SQLBuilder();
sqlBuilder.appendLiterals("CREATE INDEX ");
sqlBuilder.appendPlaceholder(new IndexPlaceholder("index_name", "table_x"));
sqlBuilder.appendLiterals(" ON ");
sqlBuilder.appendPlaceholder(new TablePlaceholder("table_x"));
sqlBuilder.appendLiterals(" ('cloumn')");
assertThat(sqlBuilder.toSQL(Collections.<String, String>emptyMap(), null), is("CREATE INDEX index_name ON table_x ('cloumn')"));
}
@Test
public void assertIndexPlaceholderAppendTableWithTableToken() {
SQLBuilder sqlBuilder = new SQLBuilder();
sqlBuilder.appendLiterals("CREATE INDEX ");
sqlBuilder.appendPlaceholder(new IndexPlaceholder("index_name", "table_x"));
sqlBuilder.appendLiterals(" ON ");
sqlBuilder.appendPlaceholder(new TablePlaceholder("table_x"));
sqlBuilder.appendLiterals(" ('cloumn')");
Map<String, String> tableTokens = new HashMap<>(1, 1);
tableTokens.put("table_x", "table_x_1");
assertThat(sqlBuilder.toSQL(tableTokens, null), is("CREATE INDEX index_name_table_x_1 ON table_x_1 ('cloumn')"));
}
@Test(expected = ShardingJdbcException.class)
public void assertSchemaPlaceholderAppendTableWithoutTableToken() {
SQLBuilder sqlBuilder = new SQLBuilder();
sqlBuilder.appendLiterals("SHOW ");
sqlBuilder.appendLiterals("CREATE TABLE ");
sqlBuilder.appendPlaceholder(new TablePlaceholder("table_x"));
sqlBuilder.appendLiterals("ON ");
sqlBuilder.appendPlaceholder(new SchemaPlaceholder("dx", "table_x"));
sqlBuilder.toSQL(Collections.<String, String>emptyMap(), createShardingRule());
}
@Test
public void assertSchemaPlaceholderAppendTableWithTableToken() {
SQLBuilder sqlBuilder = new SQLBuilder();
sqlBuilder.appendLiterals("SHOW ");
sqlBuilder.appendLiterals("CREATE TABLE ");
sqlBuilder.appendPlaceholder(new TablePlaceholder("table_0"));
sqlBuilder.appendLiterals(" ON ");
sqlBuilder.appendPlaceholder(new SchemaPlaceholder("ds", "table_0"));
Map<String, String> tableTokens = new HashMap<>(1, 1);
tableTokens.put("table_0", "table_1");
assertThat(sqlBuilder.toSQL(tableTokens, createShardingRule()), is("SHOW CREATE TABLE table_1 ON ds0"));
}
private ShardingRule createShardingRule() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
TableRuleConfiguration tableRuleConfig = createTableRuleConfig();
shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
ShardingRule actual = new ShardingRule(shardingRuleConfig, createDataSourceNames());
return actual;
}
private Collection<String> createDataSourceNames() {
return Arrays.asList("ds0", "ds1");
}
private TableRuleConfiguration createTableRuleConfig() {
TableRuleConfiguration result = new TableRuleConfiguration();
result.setLogicTable("LOGIC_TABLE");
result.setActualDataNodes("ds${0..1}.table_${0..2}");
return result;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册