Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test cases for SelectStatementContext.findColumnProjection() #28843

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

package org.apache.shardingsphere.infra.binder.context.statement.dml;

import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ColumnProjection;
import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ExpressionProjection;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
Expand Down Expand Up @@ -64,6 +66,7 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -409,6 +412,29 @@ private void assertSetWhere(final SelectStatement selectStatement) {
assertThat(actual.getWhereSegments(), is(Collections.singletonList(whereSegment)));
}

@Test
void assertFindColumnProjectionWithoutExpandProjections() {
SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove useless blank line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack, I ran spotless:apply so I thought the formatting should be good.

Is this something that we could potentially consider move into formatter rules or maybe this is the conventional code style that we used here?

when(selectStatementContext.getProjectionsContext().getExpandProjections()).thenReturn(Collections.emptyList());

Optional<ColumnProjection> result = selectStatementContext.findColumnProjection(1);

assertFalse(result.isPresent());
}

@Test
void assertFindColumnProjectionInvalidProjectionType() {
ExpressionProjection projection = mock(ExpressionProjection.class);
SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);

when(selectStatementContext.getProjectionsContext().getExpandProjections()).thenReturn(Collections.singletonList(projection));

Optional<ColumnProjection> result = selectStatementContext.findColumnProjection(1);

assertFalse(result.isPresent());
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add an unit test under normal scenarios?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried with mocking, and was getting NPE after it, so I was considering at least getting the unit test for failed scenarios. If you prefer, I can try to get the successful cases in unit tests as well. Though I dont have a specific timeline for this.

@Test
void assertContainsSubqueryForMySQL() {
assertContainsSubquery(new MySQLSelectStatement(), new MySQLSelectStatement());
Expand Down