From c6674d4524fbf87e2c288b14d68df7118336d201 Mon Sep 17 00:00:00 2001 From: Sakshi Jain Date: Sat, 21 Oct 2023 22:05:55 +0530 Subject: [PATCH] add unit tests --- .../SubqueryExpressionSegmentBinderTest.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/SubqueryExpressionSegmentBinderTest.java diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/SubqueryExpressionSegmentBinderTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/SubqueryExpressionSegmentBinderTest.java new file mode 100644 index 0000000000000..5ae275f016e2f --- /dev/null +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/SubqueryExpressionSegmentBinderTest.java @@ -0,0 +1,60 @@ +/* + * 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.infra.binder.segment.expression.impl; + +import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubqueryExpressionSegment; +import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment; +import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment; +import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement; +import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerSelectStatement; +import org.apache.shardingsphere.infra.binder.segment.from.TableSegmentBinderContext; +import org.apache.shardingsphere.infra.binder.statement.SQLStatementBinderContext; + +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.CoreMatchers.isA; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +class SubqueryExpressionSegmentBinderTest { + + @Test + public void testBind() { + SelectStatement selectStatement = new SQLServerSelectStatement(); + selectStatement.setProjections(new ProjectionsSegment(0, 0)); + SubquerySegment subquery = new SubquerySegment(0, 10, selectStatement, "subquery"); + SubqueryExpressionSegment segment = new SubqueryExpressionSegment(subquery); + SQLStatementBinderContext statementBinderContext = new SQLStatementBinderContext(null, null, null, null); + Map tableBinderContexts = new HashMap<>(); + SubqueryExpressionSegment expected = new SubqueryExpressionSegment(SubquerySegmentBinder.bind(segment.getSubquery(), statementBinderContext, tableBinderContexts)); + SubqueryExpressionSegment actual = SubqueryExpressionSegmentBinder.bind(segment, statementBinderContext, tableBinderContexts); + assertThat(actual, isA(SubqueryExpressionSegment.class)); + assertThat(actual.getStartIndex(), is(expected.getStartIndex())); + assertThat(actual.getStopIndex(), is(expected.getStopIndex())); + assertThat(actual.getText(), is(expected.getText())); + assertThat(actual.getSubquery().getStartIndex(), is(expected.getSubquery().getStartIndex())); + assertThat(actual.getSubquery().getStopIndex(), is(expected.getSubquery().getStopIndex())); + assertThat(actual.getSubquery().getSubqueryType(), is(expected.getSubquery().getSubqueryType())); + assertThat(actual.getSubquery().getText(), is(expected.getSubquery().getText())); + assertThat(actual.getSubquery().getSelect().getDatabaseType(), is(expected.getSubquery().getSelect().getDatabaseType())); + assertThat(actual.getSubquery().getSelect().getProjections().getProjections(), is(expected.getSubquery().getSelect().getProjections().getProjections())); + } +}