提交 45fcc20b 编写于 作者: S sluk3r 提交者: Liang Zhang

improved coverage for package of preprocessor (#3199)

* testcase code added into classes of ProjectionsContextEngineTest and ProjectionEngineTest

* inline some variable into method invocation lines

* inline some variable into method invocation lines

* use assertThat and instanceOf to instead of assertTrue
上级 2f6e96c7
......@@ -20,12 +20,24 @@ package org.apache.shardingsphere.core.optimize.segment.select.projection.engine
import com.google.common.base.Optional;
import org.apache.shardingsphere.core.optimize.segment.select.projection.Projection;
import org.apache.shardingsphere.core.optimize.segment.select.projection.impl.ShorthandProjection;
import org.apache.shardingsphere.core.optimize.segment.select.projection.impl.ColumnProjection;
import org.apache.shardingsphere.core.optimize.segment.select.projection.impl.ExpressionProjection;
import org.apache.shardingsphere.core.optimize.segment.select.projection.impl.AggregationDistinctProjection;
import org.apache.shardingsphere.core.optimize.segment.select.projection.impl.AggregationProjection;
import org.apache.shardingsphere.core.parse.core.constant.AggregationType;
import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.ShorthandSelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.ColumnSelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.ExpressionSelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.AggregationDistinctSelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.AggregationSelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.generic.TableSegment;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
......@@ -43,6 +55,40 @@ public final class ProjectionEngineTest {
when(shorthandSelectItemSegment.getOwner()).thenReturn(Optional.of(mock(TableSegment.class)));
Optional<Projection> projection = new ProjectionEngine().createProjection(null, shorthandSelectItemSegment);
assertTrue(projection.isPresent());
assertTrue(projection.get() instanceof ShorthandProjection);
assertThat(projection.get(), instanceOf(ShorthandProjection.class));
}
@Test
public void assertProjectionCreatedWhenSelectItemSegmentInstanceOfColumnSelectItemSegment() {
ColumnSelectItemSegment columnSelectItemSegment = new ColumnSelectItemSegment("text", new ColumnSegment(0, 10, "name"));
columnSelectItemSegment.setAlias("alias");
Optional<Projection> projection = new ProjectionEngine().createProjection(null, columnSelectItemSegment);
assertTrue(projection.isPresent());
assertThat(projection.get(), instanceOf(ColumnProjection.class));
}
@Test
public void assertProjectionCreatedWhenSelectItemSegmentInstanceOfExpressionSelectItemSegment() {
ExpressionSelectItemSegment expressionSelectItemSegment = new ExpressionSelectItemSegment(0, 10, "text");
Optional<Projection> projection = new ProjectionEngine().createProjection(null, expressionSelectItemSegment);
assertTrue(projection.isPresent());
assertThat(projection.get(), instanceOf(ExpressionProjection.class));
}
@Test
public void assertProjectionCreatedWhenSelectItemSegmentInstanceOfAggregationDistinctSelectItemSegment() {
AggregationDistinctSelectItemSegment aggregationDistinctSelectItemSegment = new AggregationDistinctSelectItemSegment(0, 10, "text", AggregationType.COUNT, 0, "distinctExpression");
Optional<Projection> projection = new ProjectionEngine().createProjection("select count(1) from table_1", aggregationDistinctSelectItemSegment);
assertTrue(projection.isPresent());
assertThat(projection.get(), instanceOf(AggregationDistinctProjection.class));
}
@Test
public void assertProjectionCreatedWhenSelectItemSegmentInstanceOfAggregationSelectItemSegment() {
AggregationSelectItemSegment aggregationSelectItemSegment = new AggregationSelectItemSegment(0, 10, "text", AggregationType.COUNT, 0);
Optional<Projection> projection = new ProjectionEngine().createProjection("select count(1) from table_1", aggregationSelectItemSegment);
assertTrue(projection.isPresent());
assertThat(projection.get(), instanceOf(AggregationProjection.class));
}
}
......@@ -20,11 +20,17 @@ package org.apache.shardingsphere.core.optimize.segment.select.projection.engine
import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext;
import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext;
import org.apache.shardingsphere.core.optimize.segment.select.projection.ProjectionsContext;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.SelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.SelectItemsSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.ShorthandSelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.generic.SchemaSegment;
import org.apache.shardingsphere.core.parse.sql.segment.generic.TableSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.junit.Test;
import java.util.Collections;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
......@@ -39,4 +45,18 @@ public final class ProjectionsContextEngineTest {
ProjectionsContext projectionsContext = projectionsContextEngine.createProjectionsContext(null, selectStatement, mock(GroupByContext.class), mock(OrderByContext.class));
assertNotNull(projectionsContext);
}
@Test
public void assertProjectionsContextCreatedProperlyWhenSelectItemPresent() {
SelectStatement selectStatement = mock(SelectStatement.class);
SelectItemsSegment selectItemsSegment = mock(SelectItemsSegment.class);
when(selectStatement.getSelectItems()).thenReturn(selectItemsSegment);
ShorthandSelectItemSegment shorthandSelectItemSegment = new ShorthandSelectItemSegment(0, 10, "text");
TableSegment owner = new TableSegment(0, 10, "name");
owner.setOwner(new SchemaSegment(0, 10, "name"));
shorthandSelectItemSegment.setOwner(owner);
when(selectItemsSegment.getSelectItems()).thenReturn(Collections.<SelectItemSegment>singleton(shorthandSelectItemSegment));
ProjectionsContext projectionsContext = new ProjectionsContextEngine(null).createProjectionsContext(null, selectStatement, mock(GroupByContext.class), mock(OrderByContext.class));
assertNotNull(projectionsContext);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册