#1643 - ENH: Add expression inOrEmpty() ... as a convenience for conditionally added a IN expression

This commit is contained in:
rob bygrave
2019-03-01 18:24:00 +13:00
parent 888384f7db
commit a4698ea1b6
9 changed files with 301 additions and 21 deletions
@@ -11,7 +11,25 @@ import static org.assertj.core.api.StrictAssertions.assertThat;
public class InExpressionTest extends BaseExpressionTest {
@Test
public void queryPlanHash_given_diffPropertyName_should_differentPlanHash() throws Exception {
public void queryPlanHash_given_diffEmpty_should_differentPlanHash() {
List<Integer> emptyValues = values();
InExpression ex1 = new InExpression("foo", emptyValues, false, true);
InExpression ex2 = new InExpression("foo", emptyValues, false);
InExpression ex3 = new InExpression("foo", emptyValues, false, true);
InExpression ex4 = new InExpression("foo", null, false, true);
ex1.prepareExpression(multi());
ex2.prepareExpression(multi());
different(ex1, ex2);
same(ex1, ex3); // same empty
same(ex1, ex4); // same null
}
@Test
public void queryPlanHash_given_diffPropertyName_should_differentPlanHash() {
List<Integer> values = values(42, 92);
@@ -25,7 +43,7 @@ public class InExpressionTest extends BaseExpressionTest {
}
@Test
public void queryPlanHash_given_diffBindCount_should_differentPlanHash() throws Exception {
public void queryPlanHash_given_diffBindCount_should_differentPlanHash() {
List<Integer> values1 = values(42, 92);
List<Integer> values2 = values(42, 92, 82);
@@ -39,7 +57,7 @@ public class InExpressionTest extends BaseExpressionTest {
}
@Test
public void queryPlanHash_given_diffBindCount_withMultiSupport_samePlanHash() throws Exception {
public void queryPlanHash_given_diffBindCount_withMultiSupport_samePlanHash() {
List<Integer> values1 = values(42, 92);
List<Integer> values2 = values(42, 92, 82);
@@ -53,7 +71,7 @@ public class InExpressionTest extends BaseExpressionTest {
}
@Test
public void queryPlanHash_given_diffNotFlag_should_differentPlanHash() throws Exception {
public void queryPlanHash_given_diffNotFlag_should_differentPlanHash() {
List<Integer> values = values(42, 92);
@@ -67,7 +85,7 @@ public class InExpressionTest extends BaseExpressionTest {
}
@Test
public void queryPlanHash_given_sameNotFlag_should_samePlanHash() throws Exception {
public void queryPlanHash_given_sameNotFlag_should_samePlanHash() {
List<Integer> values = values(42, 92);
+112 -6
View File
@@ -1,12 +1,19 @@
package org.tests.query;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.DB;
import io.ebean.Query;
import org.junit.Test;
import org.tests.model.basic.Country;
import org.tests.model.basic.Order;
import org.tests.model.basic.ResetBasicData;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import static org.assertj.core.api.Assertions.assertThat;
public class TestWhereIn extends BaseTestCase {
@@ -15,25 +22,124 @@ public class TestWhereIn extends BaseTestCase {
ResetBasicData.reset();
Query<Country> query = Ebean.find(Country.class)
Query<Country> query = DB.find(Country.class)
.where().in("code", "NZ", "AU")
.query();
query.findList();
platformAssertIn(sqlOf(query), "");
platformAssertIn(sqlOf(query), "where t0.code");
}
@Test
public void testNotInVarchar() {
ResetBasicData.reset();
Query<Country> query = Ebean.find(Country.class)
Query<Country> query = DB.find(Country.class)
.where().notIn("code", "NZ", "SA", "US")
.query();
query.findList();
platformAssertNotIn(sqlOf(query), "");
platformAssertNotIn(sqlOf(query), "where t0.code");
}
@Test
public void testInOrEmpty_expect_noJoinWhenEmpty() {
ResetBasicData.reset();
Query<Order> query = DB.find(Order.class)
.select("id")
.where().inOrEmpty("customer.billingAddress.id", new ArrayList<>()).query();
query.findList();
assertThat(sqlOf(query)).isEqualTo("select t0.id from o_order t0 where 1=1");
}
@Test
public void testInOrEmpty_expect_joinWhenNotEmpty() {
ResetBasicData.reset();
Query<Order> query = DB.find(Order.class)
.select("id")
.where().inOrEmpty("customer.billingAddress.id", Arrays.asList(1)).query();
query.findList();
assertThat(sqlOf(query)).contains("select t0.id from o_order t0 join o_customer t1 on t1.id = t0.kcustomer_id where t1.billing_address_id ");
}
@Test
public void testInOrEmpty_when_null() {
ResetBasicData.reset();
Query<Country> query = DB.find(Country.class)
.where().inOrEmpty("code", null).query();
query.findList();
assertThat(sqlOf(query)).contains("where 1=1");
}
@Test
public void testInOrEmpty_when_empty() {
ResetBasicData.reset();
Query<Country> query = DB.find(Country.class)
.where().inOrEmpty("code", new ArrayList<>()).query();
query.findList();
assertThat(sqlOf(query)).contains("where 1=1");
}
@Test
public void testIn_when_empty() {
ResetBasicData.reset();
Query<Country> query = DB.find(Country.class)
.where().in("code", new ArrayList<>()).query();
query.findList();
assertThat(sqlOf(query)).contains("where 1=0");
}
@Test
public void testIn_when_null() {
ResetBasicData.reset();
Query<Country> query = DB.find(Country.class)
.where().in("code", (Collection)null).query();
query.findList();
assertThat(sqlOf(query)).contains("where 1=0");
}
@Test
public void testNotIn_when_empty() {
ResetBasicData.reset();
Query<Country> query = DB.find(Country.class)
.where().notIn("code", new ArrayList<>()).query();
query.findList();
assertThat(sqlOf(query)).contains("where 1=1");
}
@Test
public void testNotIn_when_null() {
ResetBasicData.reset();
Query<Country> query = DB.find(Country.class)
.where().notIn("code", (Collection)null).query();
query.findList();
assertThat(sqlOf(query)).contains("where 1=1");
}
}