提交 0664a5cc 编写于 作者: S sluk3r 提交者: Liang Zhang

improved coverage for packages of projection.engine and statement (#3184)

* SQLStatementContextFactoryTest added

* ProjectionsContextEngineTest added

* ProjectionEngineTest added

* inline some variable into method invocation lines

* use projection, instead of projectionOptional

* remove unnecessary blank lines
上级 f201a35a
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.parse.sql.segment.dml.item.ShorthandSelectItemSegment;
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.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public final class ProjectionEngineTest {
@Test
public void assertProjectionCreatedWhenSelectItemSegmentNotMatched() {
Optional<Projection> projection = new ProjectionEngine().createProjection(null, null);
assertFalse(projection.isPresent());
}
@Test
public void assertProjectionCreatedWhenSelectItemSegmentInstanceOfShorthandSelectItemSegment() {
ShorthandSelectItemSegment shorthandSelectItemSegment = mock(ShorthandSelectItemSegment.class);
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);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.SelectItemsSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public final class ProjectionsContextEngineTest {
@Test
public void assertProjectionsContextCreatedProperly() {
ProjectionsContextEngine projectionsContextEngine = new ProjectionsContextEngine(null);
SelectStatement selectStatement = mock(SelectStatement.class);
when(selectStatement.getSelectItems()).thenReturn(mock(SelectItemsSegment.class));
ProjectionsContext projectionsContext = projectionsContextEngine.createProjectionsContext(null, selectStatement, mock(GroupByContext.class), mock(OrderByContext.class));
assertNotNull(projectionsContext);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shardingsphere.core.optimize.statement;
import com.google.common.base.Optional;
import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext;
import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext;
import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext;
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.order.GroupBySegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.order.OrderBySegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.optimize.SQLStatementContextFactory;
import org.junit.Test;
import java.util.Collections;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.junit.Assert.assertNotNull;
public final class SQLStatementContextFactoryTest {
@Test
public void assertSQLStatementContextCreatedWhenSQLStatementInstanceOfSelectStatement() {
SelectStatement selectStatement = mock(SelectStatement.class);
when(selectStatement.getGroupBy()).thenReturn(Optional.<GroupBySegment>absent());
when(selectStatement.getOrderBy()).thenReturn(Optional.<OrderBySegment>absent());
when(selectStatement.findSQLSegment(LimitSegment.class)).thenReturn(Optional.of(new LimitSegment(0, 10, null, null)));
SelectItemsSegment selectItemsSegment = mock(SelectItemsSegment.class);
when(selectItemsSegment.getSelectItems()).thenReturn(Collections.<SelectItemSegment>emptyList());
when(selectStatement.getSelectItems()).thenReturn(selectItemsSegment);
SQLStatementContext sqlStatementContext = SQLStatementContextFactory.newInstance(null, null, null, selectStatement);
assertNotNull(sqlStatementContext);
assertTrue(sqlStatementContext instanceof SelectSQLStatementContext);
}
@Test
public void assertSQLStatementContextCreatedWhenSQLStatementInstanceOfInsertStatement() {
InsertStatement insertStatement = mock(InsertStatement.class);
when(insertStatement.useDefaultColumns()).thenReturn(false);
when(insertStatement.findSQLSegment(LimitSegment.class)).thenReturn(Optional.of(new LimitSegment(0, 10, null, null)));
SQLStatementContext sqlStatementContext = SQLStatementContextFactory.newInstance(null, null, null, insertStatement);
assertNotNull(sqlStatementContext);
assertTrue(sqlStatementContext instanceof InsertSQLStatementContext);
}
@Test
public void assertSQLStatementContextCreatedWhenSQLStatementNotInstanceOfSelectStatementAndInsertStatement() {
SQLStatementContext sqlStatementContext = SQLStatementContextFactory.newInstance(null, null, null, mock(SQLStatement.class));
assertNotNull(sqlStatementContext);
assertTrue(sqlStatementContext instanceof CommonSQLStatementContext);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册