|
18 | 18 | package org.apache.shardingsphere.sharding.rewrite.token.pojo; |
19 | 19 |
|
20 | 20 | import org.apache.shardingsphere.infra.datanode.DataNode; |
21 | | -import org.apache.shardingsphere.infra.rewrite.sql.token.common.pojo.generic.InsertValue; |
22 | 21 | import org.apache.shardingsphere.infra.route.context.RouteMapper; |
23 | 22 | import org.apache.shardingsphere.infra.route.context.RouteUnit; |
24 | 23 | import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment; |
25 | 24 | import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.LiteralExpressionSegment; |
26 | | -import org.junit.jupiter.api.BeforeEach; |
27 | 25 | import org.junit.jupiter.api.Test; |
28 | 26 |
|
29 | 27 | import java.util.Arrays; |
30 | 28 | import java.util.Collection; |
31 | | -import java.util.LinkedList; |
| 29 | +import java.util.Collections; |
32 | 30 | import java.util.List; |
33 | 31 |
|
34 | 32 | import static org.hamcrest.CoreMatchers.is; |
35 | 33 | import static org.hamcrest.MatcherAssert.assertThat; |
36 | 34 |
|
37 | 35 | class ShardingInsertValuesTokenTest { |
38 | 36 |
|
39 | | - private ShardingInsertValuesToken shardingInsertValuesToken; |
| 37 | + @Test |
| 38 | + void assertToStringWithRouteUnit() { |
| 39 | + assertThat(createInsertValuesToken().toString(createRouteUnit()), is("('foo', 'bar'), ()")); |
| 40 | + } |
40 | 41 |
|
41 | | - private RouteUnit routeUnit; |
| 42 | + private ShardingInsertValuesToken createInsertValuesToken() { |
| 43 | + ShardingInsertValuesToken result = new ShardingInsertValuesToken(0, 2); |
| 44 | + Collection<DataNode> dataNodes = Collections.singleton(new DataNode("foo_ds", "tbl_0")); |
| 45 | + List<ExpressionSegment> values = Arrays.asList(new LiteralExpressionSegment(0, 0, "foo"), new LiteralExpressionSegment(0, 0, "bar")); |
| 46 | + result.getInsertValues().add(new ShardingInsertValue(values, dataNodes)); |
| 47 | + result.getInsertValues().add(new ShardingInsertValue(Collections.emptyList(), Collections.singleton(new DataNode("bar_ds", "tbl_1")))); |
| 48 | + result.getInsertValues().add(new ShardingInsertValue(Collections.emptyList(), Collections.emptyList())); |
| 49 | + return result; |
| 50 | + } |
42 | 51 |
|
43 | | - @BeforeEach |
44 | | - void setup() { |
45 | | - shardingInsertValuesToken = new ShardingInsertValuesToken(0, 2); |
46 | | - RouteMapper routeMapper = new RouteMapper("logic_ds", "actual_ds"); |
| 52 | + private RouteUnit createRouteUnit() { |
| 53 | + RouteMapper routeMapper = new RouteMapper("foo_ds", "actual_ds"); |
47 | 54 | RouteMapper routeMapper1 = new RouteMapper("tbl", "tbl_0"); |
48 | 55 | RouteMapper routeMapper2 = new RouteMapper("tbl", "tbl_1"); |
49 | | - routeUnit = new RouteUnit(routeMapper, Arrays.asList(routeMapper1, routeMapper2)); |
50 | | - ExpressionSegment expressionSegment1 = new LiteralExpressionSegment(0, 0, "shardingsphere"); |
51 | | - ExpressionSegment expressionSegment2 = new LiteralExpressionSegment(0, 0, "test"); |
52 | | - List<ExpressionSegment> expressionSegment = new LinkedList<>(); |
53 | | - expressionSegment.add(expressionSegment1); |
54 | | - expressionSegment.add(expressionSegment2); |
55 | | - Collection<DataNode> dataNodes = new LinkedList<>(); |
56 | | - dataNodes.add(new DataNode("logic_ds", "tbl_0")); |
57 | | - ShardingInsertValue shardingInsertValue = new ShardingInsertValue(expressionSegment, dataNodes); |
58 | | - List<InsertValue> insertValues = shardingInsertValuesToken.getInsertValues(); |
59 | | - insertValues.add(shardingInsertValue); |
60 | | - } |
61 | | - |
62 | | - @Test |
63 | | - void assertToStringWithRouteUnit() { |
64 | | - assertThat(shardingInsertValuesToken.toString(routeUnit), is("('shardingsphere', 'test')")); |
| 56 | + return new RouteUnit(routeMapper, Arrays.asList(routeMapper1, routeMapper2)); |
65 | 57 | } |
66 | 58 |
|
67 | 59 | @Test |
68 | 60 | void assertToStringWithoutRouteUnit() { |
69 | | - assertThat(shardingInsertValuesToken.toString(), is("('shardingsphere', 'test')")); |
| 61 | + assertThat(createInsertValuesToken().toString(), is("('foo', 'bar'), (), ()")); |
70 | 62 | } |
71 | 63 | } |
0 commit comments