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 more test cases on ShardingConstraintTokenGenerator #33689

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
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 @@ -36,7 +36,7 @@
@RequiredArgsConstructor
public final class ShardingConstraintTokenGenerator implements CollectionSQLTokenGenerator<SQLStatementContext> {

private final ShardingRule shardingRule;
private final ShardingRule rule;

@Override
public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) {
Expand All @@ -49,8 +49,9 @@ public Collection<SQLToken> generateSQLTokens(final SQLStatementContext sqlState
if (sqlStatementContext instanceof ConstraintAvailable) {
for (ConstraintSegment each : ((ConstraintAvailable) sqlStatementContext).getConstraints()) {
IdentifierValue constraintIdentifier = each.getIdentifier();
// TODO make sure can remove null check here? @duanzhengqiang
if (null != constraintIdentifier) {
result.add(new ConstraintToken(each.getStartIndex(), each.getStopIndex(), constraintIdentifier, sqlStatementContext, shardingRule));
result.add(new ConstraintToken(each.getStartIndex(), each.getStopIndex(), constraintIdentifier, sqlStatementContext, rule));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.shardingsphere.sharding.rewrite.token.generator.impl;

import org.apache.shardingsphere.infra.binder.context.statement.UnknownSQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterTableStatementContext;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.pojo.SQLToken;
import org.apache.shardingsphere.sharding.rewrite.token.pojo.ConstraintToken;
Expand All @@ -28,29 +28,45 @@

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;

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

class ShardingConstraintTokenGeneratorTest {

@Test
void assertIsGenerateSQLToken() {
void assertIsNotGenerateSQLTokenWithNotConstraintAvailable() {
ShardingConstraintTokenGenerator generator = new ShardingConstraintTokenGenerator(mock(ShardingRule.class));
assertFalse(generator.isGenerateSQLToken(mock(UnknownSQLStatementContext.class)));
AlterTableStatementContext alterTableStatementContext = mock(AlterTableStatementContext.class);
Collection<ConstraintSegment> constraintSegments = new LinkedList<>();
when(alterTableStatementContext.getConstraints()).thenReturn(constraintSegments);
assertFalse(generator.isGenerateSQLToken(mock(SQLStatementContext.class)));
}

@Test
void assertIsNotGenerateSQLTokenWithEmptyConstraint() {
ShardingConstraintTokenGenerator generator = new ShardingConstraintTokenGenerator(mock(ShardingRule.class));
AlterTableStatementContext alterTableStatementContext = mock(AlterTableStatementContext.class, RETURNS_DEEP_STUBS);
when(alterTableStatementContext.getConstraints().isEmpty()).thenReturn(true);
assertFalse(generator.isGenerateSQLToken(alterTableStatementContext));
constraintSegments.add(mock(ConstraintSegment.class));
}

@Test
void assertIsGenerateSQLToken() {
ShardingConstraintTokenGenerator generator = new ShardingConstraintTokenGenerator(mock(ShardingRule.class));
AlterTableStatementContext alterTableStatementContext = mock(AlterTableStatementContext.class, RETURNS_DEEP_STUBS);
assertTrue(generator.isGenerateSQLToken(alterTableStatementContext));
}

@Test
void assertGenerateSQLTokensWithNotConstraintAvailable() {
ShardingConstraintTokenGenerator generator = new ShardingConstraintTokenGenerator(mock(ShardingRule.class));
Collection<SQLToken> actual = generator.generateSQLTokens(mock(SQLStatementContext.class));
assertTrue(actual.isEmpty());
}

@Test
void assertGenerateSQLTokens() {
ConstraintSegment constraintSegment = mock(ConstraintSegment.class);
Expand All @@ -66,7 +82,7 @@ void assertGenerateSQLTokens() {
assertConstraintToken((ConstraintToken) actual.iterator().next());
}

private static void assertConstraintToken(final ConstraintToken actual) {
private void assertConstraintToken(final ConstraintToken actual) {
assertThat(actual.getStartIndex(), is(1));
assertThat(actual.getStopIndex(), is(3));
}
Expand Down