Skip to content

Commit 723f20e

Browse files
committed
Failing tests for Issue 239
1 parent c4a05d1 commit 723f20e

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

src/test/java/examples/animal/data/AnimalDataTest.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
import java.sql.Connection;
2828
import java.sql.DriverManager;
2929
import java.util.ArrayList;
30+
import java.util.Arrays;
3031
import java.util.Collection;
3132
import java.util.Collections;
3233
import java.util.List;
3334
import java.util.Map;
35+
import java.util.Objects;
3436

3537
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
3638
import org.apache.ibatis.exceptions.PersistenceException;
@@ -57,6 +59,7 @@
5759
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
5860
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
5961
import org.mybatis.dynamic.sql.where.condition.IsIn;
62+
import org.mybatis.dynamic.sql.where.condition.IsNotIn;
6063
import org.mybatis.dynamic.sql.where.render.WhereClauseProvider;
6164

6265
class AnimalDataTest {
@@ -569,6 +572,47 @@ void testInCondition() {
569572
}
570573
}
571574

575+
@Test
576+
void testInConditionWithEventuallyEmptyList() {
577+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
578+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
579+
580+
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
581+
.from(animalData)
582+
.where(id, isIn(null, 22, null).then(s -> s.filter(Objects::nonNull).filter(i -> i != 22)))
583+
.build()
584+
.render(RenderingStrategies.MYBATIS3);
585+
586+
assertThat(selectStatement.getSelectStatement())
587+
.isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData");
588+
List<AnimalData> animals = mapper.selectMany(selectStatement);
589+
assertThat(animals).hasSize(65);
590+
}
591+
}
592+
593+
@Test
594+
void testInConditionWithEventuallyEmptyListForceRendering() {
595+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
596+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
597+
598+
List<Integer> inValues = new ArrayList<>();
599+
inValues.add(null);
600+
inValues.add(22);
601+
inValues.add(null);
602+
603+
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
604+
.from(animalData)
605+
.where(id, IsInRequired.isIn(inValues).then(s -> s.filter(Objects::nonNull).filter(i -> i != 22)))
606+
.build()
607+
.render(RenderingStrategies.MYBATIS3);
608+
609+
assertThat(selectStatement.getSelectStatement())
610+
.isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData where id in ()");
611+
612+
assertThatExceptionOfType(PersistenceException.class).isThrownBy(() -> mapper.selectMany(selectStatement));
613+
}
614+
}
615+
572616
@Test
573617
void testInConditionWithEmptyList() {
574618
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
@@ -659,6 +703,54 @@ void testNotInCaseSensitiveConditionWithNull() {
659703
}
660704
}
661705

706+
@Test
707+
void testNotInConditionWithEventuallyEmptyList() {
708+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
709+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
710+
711+
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
712+
.from(animalData)
713+
.where(id, isNotIn(null, 22, null).then(s -> s.filter(Objects::nonNull).filter(i -> i != 22)))
714+
.build()
715+
.render(RenderingStrategies.MYBATIS3);
716+
717+
assertThat(selectStatement.getSelectStatement())
718+
.isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData");
719+
List<AnimalData> animals = mapper.selectMany(selectStatement);
720+
assertThat(animals).hasSize(65);
721+
}
722+
}
723+
724+
@Test
725+
void testNotInConditionWithEventuallyEmptyListForceRendering() {
726+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
727+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
728+
729+
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
730+
.from(animalData)
731+
.where(id, IsNotInRequired.isNotIn(null, 22, null)
732+
.then(s -> s.filter(Objects::nonNull).filter(i -> i != 22)))
733+
.build()
734+
.render(RenderingStrategies.MYBATIS3);
735+
736+
assertThat(selectStatement.getSelectStatement())
737+
.isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData where id not in ()");
738+
739+
assertThatExceptionOfType(PersistenceException.class).isThrownBy(() -> mapper.selectMany(selectStatement));
740+
}
741+
}
742+
743+
public static class IsNotInRequired<T> extends IsNotIn<T> {
744+
protected IsNotInRequired(Collection<T> values) {
745+
super(values);
746+
forceRenderingWhenEmpty();
747+
}
748+
749+
@SafeVarargs
750+
public static <T> IsNotInRequired<T> isNotIn(T...values) {
751+
return new IsNotInRequired<>(Arrays.asList(values));
752+
}
753+
}
662754
@Test
663755
void testLikeCondition() {
664756
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

0 commit comments

Comments
 (0)