mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#660 - findPagedList() does not handle raw sql order by
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package com.avaje.ebean;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class PropertyTest {
|
||||
|
||||
@Test
|
||||
public void equals() throws Exception {
|
||||
|
||||
assertEquals(prop("foo", true), prop("foo", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diff_basic() throws Exception {
|
||||
|
||||
assertNotEquals(prop("foo", true), prop("bar", true));
|
||||
assertNotEquals(prop("foo", true), prop("foo", false));
|
||||
assertNotEquals(prop("foo", false), prop("foo", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diff_nulls() throws Exception {
|
||||
|
||||
assertEquals(prop("foo", true, "nulls", "high"), prop("foo", true, "nulls", "high"));
|
||||
assertEquals(prop("foo", true, "nulls", "low"), prop("foo", true, "nulls", "low"));
|
||||
|
||||
assertNotEquals(prop("foo", true), prop("foo", true, "nulls", "high"));
|
||||
assertNotEquals(prop("foo", true, "nulls", "high"), prop("foo", true));
|
||||
assertNotEquals(prop("foo", true, "nulls", "high"), prop("foo", true, "nulls", "low"));
|
||||
assertNotEquals(prop("foo", true, "nulls", "low"), prop("foo", true, "nulls", "high"));
|
||||
}
|
||||
|
||||
private OrderBy.Property prop(String name, boolean asc) {
|
||||
return new OrderBy.Property(name, asc, null, null);
|
||||
}
|
||||
|
||||
private OrderBy.Property prop(String name, boolean asc, String nulls, String highLow) {
|
||||
return new OrderBy.Property(name, asc, nulls, highLow);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.avaje.tests.rawsql;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -17,6 +18,8 @@ import com.avaje.ebean.RawSqlBuilder;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
@@ -107,4 +110,71 @@ public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPaging_with_existingRawSqlOrderBy_expect_id_appendToOrderBy() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse("select o.id, o.order_date, o.ship_date from o_order o order by o.ship_date desc nulls last")
|
||||
.columnMapping("o.id", "id")
|
||||
.columnMapping("o.order_date", "orderDate")
|
||||
.columnMapping("o.ship_date", "shipDate")
|
||||
.create();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class);
|
||||
query.setUseCache(false).setUseQueryCache(false);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
query.setMaxRows(100);
|
||||
query.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("order by o.ship_date desc nulls last, o.id limit 100");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPaging_when_setOrderBy_expect_id_appendToOrderBy() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse("select o.id, o.order_date, o.ship_date from o_order o order by o.ship_date desc nulls last")
|
||||
.columnMapping("o.id", "id")
|
||||
.columnMapping("o.order_date", "orderDate")
|
||||
.columnMapping("o.ship_date", "shipDate")
|
||||
.create();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class);
|
||||
query.setUseCache(false).setUseQueryCache(false);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
query.setMaxRows(100);
|
||||
query.order("coalesce(shipDate, now()) desc");
|
||||
query.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("order by coalesce(o.ship_date, now()) desc, o.id limit 100");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPaging_when_setOrderBy_containsId_expect_leaveAsIs() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse("select o.id, o.order_date, o.ship_date from o_order o order by o.ship_date desc nulls last")
|
||||
.columnMapping("o.id", "id")
|
||||
.columnMapping("o.order_date", "orderDate")
|
||||
.columnMapping("o.ship_date", "shipDate")
|
||||
.create();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class);
|
||||
query.setUseCache(false).setUseQueryCache(false);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
query.setMaxRows(100);
|
||||
query.order("id desc");
|
||||
PagedList<Order> pagedList = query.findPagedList();
|
||||
pagedList.getList();
|
||||
pagedList.getTotalRowCount();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("order by o.id desc limit 100");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,28 @@ public class TestOrderByParse extends BaseTestCase {
|
||||
assertFalse(o1.containsProperty("junk"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseNullsHigh() {
|
||||
|
||||
OrderBy<Object> o1 = new OrderBy<Object>("id desc nulls high");
|
||||
assertTrue(o1.getProperties().size() == 1);
|
||||
assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
|
||||
assertTrue(!o1.getProperties().get(0).isAscending());
|
||||
assertTrue(o1.toStringFormat().equals("id desc nulls high"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseNullsHigh_with_second() {
|
||||
|
||||
OrderBy<Object> o1 = new OrderBy<Object>("id desc nulls high, name");
|
||||
assertTrue(o1.getProperties().size() == 2);
|
||||
assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
|
||||
assertTrue(!o1.getProperties().get(0).isAscending());
|
||||
assertTrue(o1.toStringFormat().equals("id desc nulls high, name"));
|
||||
assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
|
||||
assertTrue(o1.getProperties().get(1).isAscending());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParsingTwo() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user