Skip to content

Commit

Permalink
Add more test cases on ShardingConstraintTokenGenerator (#33689)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 16, 2024
1 parent eca7bd0 commit 1488b80
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
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

0 comments on commit 1488b80

Please sign in to comment.