Skip to content

Commit

Permalink
Add more test cases on ShardingInsertValuesToken (#33679)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 15, 2024
1 parent 2d2b6d6 commit 3088fd1
Showing 1 changed file with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,46 @@
package org.apache.shardingsphere.sharding.rewrite.token.pojo;

import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.pojo.generic.InsertValue;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.LiteralExpressionSegment;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class ShardingInsertValuesTokenTest {

private ShardingInsertValuesToken shardingInsertValuesToken;
@Test
void assertToStringWithRouteUnit() {
assertThat(createInsertValuesToken().toString(createRouteUnit()), is("('foo', 'bar'), ()"));
}

private RouteUnit routeUnit;
private ShardingInsertValuesToken createInsertValuesToken() {
ShardingInsertValuesToken result = new ShardingInsertValuesToken(0, 2);
Collection<DataNode> dataNodes = Collections.singleton(new DataNode("foo_ds", "tbl_0"));
List<ExpressionSegment> values = Arrays.asList(new LiteralExpressionSegment(0, 0, "foo"), new LiteralExpressionSegment(0, 0, "bar"));
result.getInsertValues().add(new ShardingInsertValue(values, dataNodes));
result.getInsertValues().add(new ShardingInsertValue(Collections.emptyList(), Collections.singleton(new DataNode("bar_ds", "tbl_1"))));
result.getInsertValues().add(new ShardingInsertValue(Collections.emptyList(), Collections.emptyList()));
return result;
}

@BeforeEach
void setup() {
shardingInsertValuesToken = new ShardingInsertValuesToken(0, 2);
RouteMapper routeMapper = new RouteMapper("logic_ds", "actual_ds");
private RouteUnit createRouteUnit() {
RouteMapper routeMapper = new RouteMapper("foo_ds", "actual_ds");
RouteMapper routeMapper1 = new RouteMapper("tbl", "tbl_0");
RouteMapper routeMapper2 = new RouteMapper("tbl", "tbl_1");
routeUnit = new RouteUnit(routeMapper, Arrays.asList(routeMapper1, routeMapper2));
ExpressionSegment expressionSegment1 = new LiteralExpressionSegment(0, 0, "shardingsphere");
ExpressionSegment expressionSegment2 = new LiteralExpressionSegment(0, 0, "test");
List<ExpressionSegment> expressionSegment = new LinkedList<>();
expressionSegment.add(expressionSegment1);
expressionSegment.add(expressionSegment2);
Collection<DataNode> dataNodes = new LinkedList<>();
dataNodes.add(new DataNode("logic_ds", "tbl_0"));
ShardingInsertValue shardingInsertValue = new ShardingInsertValue(expressionSegment, dataNodes);
List<InsertValue> insertValues = shardingInsertValuesToken.getInsertValues();
insertValues.add(shardingInsertValue);
}

@Test
void assertToStringWithRouteUnit() {
assertThat(shardingInsertValuesToken.toString(routeUnit), is("('shardingsphere', 'test')"));
return new RouteUnit(routeMapper, Arrays.asList(routeMapper1, routeMapper2));
}

@Test
void assertToStringWithoutRouteUnit() {
assertThat(shardingInsertValuesToken.toString(), is("('shardingsphere', 'test')"));
assertThat(createInsertValuesToken().toString(), is("('foo', 'bar'), (), ()"));
}
}

0 comments on commit 3088fd1

Please sign in to comment.