#410 - Require findPagedList() query to specify an order by clause - throw exception if no order by supplied

This commit is contained in:
Robin Bygrave
2015-09-14 10:39:41 +12:00
parent 04fda0a6b4
commit ec4f655312
4 changed files with 134 additions and 3 deletions
@@ -0,0 +1,110 @@
package com.avaje.tests.query;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.PagedList;
import com.avaje.tests.model.basic.Order;
import com.avaje.tests.model.basic.ResetBasicData;
import org.avaje.ebeantest.LoggedSqlCollector;
import org.junit.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Ebean adds order by clause if none provided when using both first rows and max rows.
*/
public class TestAddOrderByWithFirstRowsMaxRows extends BaseTestCase {
@Test
public void test_firstRows() {
ResetBasicData.reset();
LoggedSqlCollector.start();
Ebean.find(Order.class)
.setFirstRow(3)
.orderBy().asc("id")
.findList();
List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(loggedSql).hasSize(1);
assertThat(loggedSql.get(0)).contains("order by t0.id");
}
@Test
public void test_maxRows() {
ResetBasicData.reset();
LoggedSqlCollector.start();
Ebean.find(Order.class)
.setMaxRows(10)
.findList();
List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(loggedSql).hasSize(1);
}
@Test
public void test_firstRowsMaxRows() {
ResetBasicData.reset();
LoggedSqlCollector.start();
Ebean.find(Order.class)
.setFirstRow(3)
.setMaxRows(10)
.orderBy().asc("id")
.findList();
List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(loggedSql).hasSize(1);
assertThat(loggedSql.get(0)).contains("order by t0.id");
}
@Test
public void test_pagingOne() {
ResetBasicData.reset();
LoggedSqlCollector.start();
PagedList<Order> pagedList =
Ebean.find(Order.class)
.findPagedList(0, 10);
pagedList.getList();
List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(loggedSql).hasSize(1);
assertThat(loggedSql.get(0)).contains("order by t0.id");
}
@Test
public void test_pagingTwo() {
ResetBasicData.reset();
LoggedSqlCollector.start();
Ebean.find(Order.class)
.findPagedList(1, 10)
.getList();
List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(loggedSql).hasSize(1);
assertThat(loggedSql.get(0)).contains("order by t0.id");
}
}