diff --git a/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/projection/engine/ProjectionEngineTest.java b/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/projection/engine/ProjectionEngineTest.java index a47cb22b212754d0e7fbfdbae3fcfe38ba36312a..63deb55e1a72c1b6a62b33b5e0f325360da11bcc 100644 --- a/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/projection/engine/ProjectionEngineTest.java +++ b/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/projection/engine/ProjectionEngineTest.java @@ -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 = 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 = 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 = 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 = 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 = new ProjectionEngine().createProjection("select count(1) from table_1", aggregationSelectItemSegment); + assertTrue(projection.isPresent()); + assertThat(projection.get(), instanceOf(AggregationProjection.class)); + } + } diff --git a/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/projection/engine/ProjectionsContextEngineTest.java b/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/projection/engine/ProjectionsContextEngineTest.java index 829a94332288bce75d7a7565767470decc374500..36584dc63590fb3853354e289ad4c424de23bdfa 100644 --- a/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/projection/engine/ProjectionsContextEngineTest.java +++ b/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/projection/engine/ProjectionsContextEngineTest.java @@ -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.singleton(shorthandSelectItemSegment)); + ProjectionsContext projectionsContext = new ProjectionsContextEngine(null).createProjectionsContext(null, selectStatement, mock(GroupByContext.class), mock(OrderByContext.class)); + assertNotNull(projectionsContext); + } }