From 3088fd12666aa1bf0864eb9c8238a65c6e509032 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Fri, 15 Nov 2024 21:51:45 +0800 Subject: [PATCH] Add more test cases on ShardingInsertValuesToken (#33679) --- .../pojo/ShardingInsertValuesTokenTest.java | 44 ++++++++----------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingInsertValuesTokenTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingInsertValuesTokenTest.java index cc2ddf8817b6f..705f390f69122 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingInsertValuesTokenTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingInsertValuesTokenTest.java @@ -18,17 +18,15 @@ 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; @@ -36,36 +34,30 @@ 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 dataNodes = Collections.singleton(new DataNode("foo_ds", "tbl_0")); + List 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 = new LinkedList<>(); - expressionSegment.add(expressionSegment1); - expressionSegment.add(expressionSegment2); - Collection dataNodes = new LinkedList<>(); - dataNodes.add(new DataNode("logic_ds", "tbl_0")); - ShardingInsertValue shardingInsertValue = new ShardingInsertValue(expressionSegment, dataNodes); - List 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'), (), ()")); } }