Skip to content

Commit

Permalink
Fix broken unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhenye-Na committed Oct 25, 2023
1 parent 180a4cd commit aacee55
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

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.binder.context.segment.select.projection.impl.SubqueryProjection;
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 +67,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 +413,55 @@ private void assertSetWhere(final SelectStatement selectStatement) {
assertThat(actual.getWhereSegments(), is(Collections.singletonList(whereSegment)));
}

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

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());
}

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

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

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

assertTrue(result.isPresent());
}

@Test
void assertFindColumnProjectionFromSubqueryProjection() {
SubqueryProjection projection = mock(SubqueryProjection.class, RETURNS_DEEP_STUBS);
ColumnProjection columnProjection = mock(ColumnProjection.class);
SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);

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

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

assertTrue(result.isPresent());
}

@Test
void assertContainsSubqueryForMySQL() {
assertContainsSubquery(new MySQLSelectStatement(), new MySQLSelectStatement());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@

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

import org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection;
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.binder.context.segment.select.projection.impl.SubqueryProjection;
import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementBinder;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
Expand All @@ -45,85 +40,19 @@
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class SelectStatementBinderTest {

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

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

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

assertFalse(result.isPresent());
}

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

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

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

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());
}

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

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

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

assertTrue(result.isPresent());
assertThat(result.get(), is(projection));
}

@Test
void assertFindColumnProjectionFromSubqueryProjection() {
SubqueryProjection projection = mock(SubqueryProjection.class, RETURNS_DEEP_STUBS);
ColumnProjection columnProjection = mock(ColumnProjection.class);
SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);

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

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

assertTrue(result.isPresent());
assertThat(result.get(), is(columnProjection));
}

@Test
void assertBind() {
SelectStatement selectStatement = new MySQLSelectStatement();
Expand Down

0 comments on commit aacee55

Please sign in to comment.