#743 - Fix issue with ElasticSearch nested path query with multiple nested path expressions

This commit is contained in:
Robin Bygrave
2016-06-20 20:57:45 +12:00
parent 6bbf93aba8
commit 3477f590dd
23 changed files with 742 additions and 6 deletions
@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.Expr;
import com.avaje.ebean.Expression;
import com.avaje.tests.model.basic.Order;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
@@ -75,4 +76,54 @@ public class LogicExpressionTest extends BaseExpressionTest {
assertThat(and(eq("a", 10), eq("b", 10))
.isSameByBind(and(eq("a", 10), eq("c", 20)))).isFalse();
}
@Test
public void nestedPath_when_notNested() {
LogicExpression and = and(eq("orderDate", 10), eq("shipDate", 10));
and.nestedPath(getBeanDescriptor(Order.class));
assertThat(and.expOne).isInstanceOf(SimpleExpression.class);
assertThat(and.expTwo).isInstanceOf(SimpleExpression.class);
}
@Test
public void nestedPath_when_nestedSame() {
LogicExpression and = and(eq("details.orderQty", 10), eq("details.unitPrice", 10));
String path = and.nestedPath(getBeanDescriptor(Order.class));
assertThat(path).isEqualTo("details");
assertThat(and.expOne).isInstanceOf(SimpleExpression.class);
assertThat(and.expTwo).isInstanceOf(SimpleExpression.class);
}
@Test
public void nestedPath_when_nestedDifferent() {
LogicExpression and = and(eq("details.orderQty", 10), eq("shipments.shipTime", 10));
String path = and.nestedPath(getBeanDescriptor(Order.class));
assertThat(path).isNull();
assertThat(and.expOne).isInstanceOf(NestedPathWrapperExpression.class);
assertThat(((NestedPathWrapperExpression)and.expOne).nestedPath).isEqualTo("details");
assertThat(and.expTwo).isInstanceOf(NestedPathWrapperExpression.class);
assertThat(((NestedPathWrapperExpression)and.expTwo).nestedPath).isEqualTo("shipments");
}
@Test
public void nestedPath_when_oneNested() {
LogicExpression and = and(eq("details.orderQty", 10), eq("orderDate", 10));
String path = and.nestedPath(getBeanDescriptor(Order.class));
assertThat(path).isNull();
assertThat(and.expOne).isInstanceOf(NestedPathWrapperExpression.class);
assertThat(((NestedPathWrapperExpression)and.expOne).nestedPath).isEqualTo("details");
assertThat(and.expTwo).isInstanceOf(SimpleExpression.class);
}
}
@@ -0,0 +1,148 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.tests.model.basic.Order;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class PrepareDocNestedTest extends BaseTestCase {
@Test
public void prepare() throws Exception {
ExpressionList<Order> where = Ebean.find(Order.class)
.where()
.gt("details.orderQty", 1)
.query().where();
DefaultExpressionList<?> exp = (DefaultExpressionList<?>)where;
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
List<SpiExpression> underlyingList = exp.getUnderlyingList();
assertEquals(underlyingList.size(), 1);
assertEquals(exp.allDocNestedPath, "details");
}
@Test
public void prepare_when_multipleOfSamePath() throws Exception {
ExpressionList<Order> where = Ebean.find(Order.class)
.where()
.gt("details.orderQty", 1)
.gt("details.unitPrice", 1)
.query().where();
DefaultExpressionList<?> exp = (DefaultExpressionList<?>)where;
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
List<SpiExpression> underlyingList = exp.getUnderlyingList();
assertEquals(underlyingList.size(), 2);
assertEquals(exp.allDocNestedPath, "details");
}
@Test
public void prepare_when_mixed() throws Exception {
ExpressionList<Order> where = Ebean.find(Order.class)
.where()
.gt("customer.id", 1)
.gt("details.orderQty", 1)
.gt("details.unitPrice", 1)
.query().where();
DefaultExpressionList<?> exp = (DefaultExpressionList<?>)where;
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
List<SpiExpression> underlyingList = exp.getUnderlyingList();
assertEquals(underlyingList.size(), 2);
assertNull(exp.allDocNestedPath);
DefaultExpressionList<?> second = (DefaultExpressionList<?>)underlyingList.get(1);
assertEquals(second.allDocNestedPath, "details");
}
@Test
public void prepare_when_nestedJunction() throws Exception {
ExpressionList<Order> where = Ebean.find(Order.class)
.where()
.not()
.gt("customer.id", 1)
.gt("details.orderQty", 1)
.gt("details.unitPrice", 1)
.query().where();
DefaultExpressionList<?> exp = (DefaultExpressionList<?>)where;
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
List<SpiExpression> underlyingList = exp.getUnderlyingList();
assertEquals(underlyingList.size(), 1);
assertNull(exp.allDocNestedPath);
JunctionExpression<?> junction = (JunctionExpression<?>)underlyingList.get(0);
List<SpiExpression> junctionUnderlying = junction.exprList.getUnderlyingList();
JunctionExpression<?> nestedNestedPath = (JunctionExpression)junctionUnderlying.get(1);
assertEquals(nestedNestedPath.exprList.allDocNestedPath, "details");
}
@Test
public void prepare_when_nestedMultiple() throws Exception {
ExpressionList<Order> where = Ebean.find(Order.class)
.where()
.isNotNull("shipments.shipTime")
.gt("details.orderQty", 1)
.gt("details.unitPrice", 1)
.query().where();
DefaultExpressionList<?> exp = (DefaultExpressionList<?>)where;
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
List<SpiExpression> underlyingList = exp.getUnderlyingList();
assertEquals(underlyingList.size(), 2);
assertNull(exp.allDocNestedPath);
DefaultExpressionList<?> shipExpr = (DefaultExpressionList<?>)underlyingList.get(0);
assertEquals(shipExpr.allDocNestedPath, "shipments");
DefaultExpressionList<?> detailsExpr = (DefaultExpressionList<?>)underlyingList.get(1);
assertEquals(detailsExpr.allDocNestedPath, "details");
}
@Test
public void prepare_when_manyMixed() throws Exception {
ExpressionList<Order> where = Ebean.find(Order.class)
.where()
.gt("customer.id", 1) // 0
.isNotNull("shipments.shipTime") // shipments 0
.isNotNull("status") // 1
.gt("details.orderQty", 1) // details 0
.isNotNull("orderDate") // 2
.gt("details.unitPrice", 1) // details 1
.query().where();
DefaultExpressionList<?> exp = (DefaultExpressionList<?>)where;
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
List<SpiExpression> underlyingList = exp.getUnderlyingList();
assertEquals(underlyingList.size(), 5);
assertNull(exp.allDocNestedPath);
DefaultExpressionList<?> shipExpr = (DefaultExpressionList<?>)underlyingList.get(3);
assertEquals(shipExpr.allDocNestedPath, "shipments");
DefaultExpressionList<?> detailsExpr = (DefaultExpressionList<?>)underlyingList.get(4);
assertEquals(detailsExpr.allDocNestedPath, "details");
}
}
@@ -0,0 +1,82 @@
package com.avaje.ebeaninternal.server.query;
import org.junit.Test;
import static org.junit.Assert.*;
public class SplitNameTest {
@Test
public void add() throws Exception {
assertEquals(SplitName.add("a","b"), "a.b");
assertEquals(SplitName.add("a","b.c"), "a.b.c");
}
@Test
public void count() throws Exception {
assertEquals(SplitName.count("a"), 0);
assertEquals(SplitName.count("a.b"), 1);
assertEquals(SplitName.count("a.b.c"), 2);
assertEquals(SplitName.count("a.b.c.foo"), 3);
}
@Test
public void parent() throws Exception {
assertNull(SplitName.parent("a"));
assertEquals(SplitName.parent("a.b"), "a");
assertEquals(SplitName.parent("a.b.c"), "a.b");
assertNull(SplitName.parent(null));
}
@Test
public void split() throws Exception {
String[] split = SplitName.split("a.b.c");
assertEquals(split[0], "a.b");
assertEquals(split[1], "c");
}
@Test
public void begin_when_one() throws Exception {
assertEquals(SplitName.begin("a"), "a");
}
@Test
public void begin_when_both() throws Exception {
assertEquals(SplitName.begin("a.b"), "a");
}
@Test
public void begin_when_multi() throws Exception {
assertEquals(SplitName.begin("a.b.c"), "a");
}
@Test
public void splitBegin_when_both() throws Exception {
String[] split = SplitName.splitBegin("a.b");
assertEquals(split[0], "a");
assertEquals(split[1], "b");
}
@Test
public void splitBegin_when_bothPlus() throws Exception {
String[] split = SplitName.splitBegin("a.b.c");
assertEquals(split[0], "a");
assertEquals(split[1], "b.c");
}
@Test
public void splitBegin_when_one() throws Exception {
String[] split = SplitName.splitBegin("a");
assertEquals(split[0], "a");
assertNull(split[1]);
}
}