new comparison method Ine (caseInsensitive notEquals) (#1551)

This commit is contained in:
Roland Praml
2018-11-19 22:22:46 +13:00
committed by Rob Bygrave
parent 7be4675d3e
commit 4fe2016891
22 changed files with 1487 additions and 1008 deletions
@@ -8,7 +8,7 @@ import static org.assertj.core.api.StrictAssertions.assertThat;
public class CaseInsensitiveEqualExpressionTest extends BaseExpressionTest {
CaseInsensitiveEqualExpression exp(String propName, String value) {
return new CaseInsensitiveEqualExpression(propName, value);
return new CaseInsensitiveEqualExpression(propName, value, false);
}
@Test
@@ -1,6 +1,8 @@
package io.ebeaninternal.server.expression;
import io.ebean.Expression;
import io.ebeaninternal.api.SpiExpression;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
@@ -8,6 +10,12 @@ import static org.assertj.core.api.StrictAssertions.assertThat;
public class DefaultExpressionFactoryTest {
private String toQueryPlanHash(Expression expression) {
StringBuilder sb = new StringBuilder();
((SpiExpression)expression).queryPlanHash(sb);
return sb.toString();
}
@Test
public void testLowerILike() throws Exception {
@@ -30,6 +38,7 @@ public class DefaultExpressionFactoryTest {
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
Expression expression = factory.eq("name", null);
assertThat(expression).isInstanceOf(NullExpression.class);
assertThat(toQueryPlanHash(expression)).isEqualTo("Null[name]");
}
@Test
@@ -38,14 +47,24 @@ public class DefaultExpressionFactoryTest {
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
Expression expression = factory.ne("name", null);
assertThat(expression).isInstanceOf(NullExpression.class);
assertThat(toQueryPlanHash(expression)).isEqualTo("NotNull[name]");
}
@Test
public void testIeq() throws Exception {
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
Expression expression = factory.ieq("name", null);
assertThat(expression).isInstanceOf(NullExpression.class);
assertThat(toQueryPlanHash(expression)).isEqualTo("Null[name]");
}
@Test
public void testIne() throws Exception {
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
Expression expression = factory.ine("name", null);
assertThat(expression).isInstanceOf(NullExpression.class);
assertThat(toQueryPlanHash(expression)).isEqualTo("NotNull[name]");
}
@Test
@@ -83,6 +83,15 @@ public class EqlParserTest extends BaseTestCase {
assertThat(query.getGeneratedSql()).contains("where lower(t0.name) =?");
}
@Test
public void where_ine() {
Query<Customer> query = parse("where name ine 'Rob'");
query.findList();
assertThat(query.getGeneratedSql()).contains("where lower(t0.name) !=?");
}
@Test
public void where_ieq_reverse() {
@@ -91,6 +100,15 @@ public class EqlParserTest extends BaseTestCase {
assertThat(query.getGeneratedSql()).contains("where lower(t0.name) =?");
}
@Test
public void where_ine_reverse() {
Query<Customer> query = parse("where 'Rob' ine name");
query.findList();
assertThat(query.getGeneratedSql()).contains("where lower(t0.name) !=?");
}
@Test
public void where_eq2() {
@@ -0,0 +1,116 @@
package org.tests.query;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import org.tests.model.basic.Customer;
import org.tests.model.basic.ResetBasicData;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestQueryFilterCaseInsensitive extends BaseTestCase {
@BeforeClass
public static void setup() {
ResetBasicData.reset();
}
@Test
public void testEq() {
// Note: this test uses only customer#1..#4
List<Customer> customers = Ebean.find(Customer.class).where()
.eq("name", "ROB") // case match
.le("id", 4).findList();
assertThat(customers).isEmpty();
customers = Ebean.find(Customer.class).where()
.ieq("name", "ROB") // case insensitive match
.le("id", 4).findList();
assertThat(customers).hasSize(1);
}
@Test
public void testNe() {
List<Customer> customers = Ebean.find(Customer.class).where()
.ne("name", "ROB") // case match
.le("id", 4).findList();
assertThat(customers).hasSize(4);
customers = Ebean.find(Customer.class).where()
.ine("name", "ROB") // case insensitive match
.le("id", 4).findList();
assertThat(customers).hasSize(3);
}
@Test
public void testLike() {
List<Customer> customers = Ebean.find(Customer.class).where()
.like("name", "%O%") // case match
.le("id", 4).findList();
assertThat(customers).isEmpty();
customers = Ebean.find(Customer.class).where()
.ilike("name", "%O%") // case insensitive match
.le("id", 4).findList();
assertThat(customers).hasSize(4); // Rob / Fiona / Cust No address / NocCust
}
@Test
public void testContains() {
List<Customer> customers = Ebean.find(Customer.class).where()
.contains("name", "O") // case match
.le("id", 4).findList();
assertThat(customers).isEmpty();
customers = Ebean.find(Customer.class).where()
.icontains("name", "O") // case insensitive match
.le("id", 4).findList();
assertThat(customers).hasSize(4); // Rob / Fiona / Cust No address / NocCust
}
@Test
public void testStartsWith() {
List<Customer> customers = Ebean.find(Customer.class).where()
.startsWith("name", "RO") // case match
.le("id", 4).findList();
assertThat(customers).isEmpty();
customers = Ebean.find(Customer.class).where()
.istartsWith("name", "RO") // case insensitive match
.le("id", 4).findList();
assertThat(customers).hasSize(1);
}
@Test
public void testEndsWith() {
List<Customer> customers = Ebean.find(Customer.class).where()
.endsWith("name", "OB") // case match
.le("id", 4).findList();
assertThat(customers).isEmpty();
customers = Ebean.find(Customer.class).where()
.iendsWith("name", "OB") // case insensitive match
.le("id", 4).findList();
assertThat(customers).hasSize(1);
}
}