#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 11:29:38 +12:00
parent ec4f655312
commit 47829f3985
6 changed files with 179 additions and 80 deletions
@@ -50,6 +50,7 @@ public class TestAddOrderByWithFirstRowsMaxRows extends BaseTestCase {
List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(loggedSql).hasSize(1);
assertThat(loggedSql.get(0)).contains("order by t0.id");
}
@@ -72,6 +73,25 @@ public class TestAddOrderByWithFirstRowsMaxRows extends BaseTestCase {
assertThat(loggedSql.get(0)).contains("order by t0.id");
}
@Test
public void test_maxRows1() {
ResetBasicData.reset();
LoggedSqlCollector.start();
// maxRows 1 with no first rows means Ebean does not automatically
// add the order by id to the query
Ebean.find(Order.class)
.setMaxRows(1)
.findList();
List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(loggedSql).hasSize(1);
assertThat(loggedSql.get(0)).doesNotContain("order by t0.id");
}
@Test
public void test_pagingOne() {
@@ -107,4 +127,42 @@ public class TestAddOrderByWithFirstRowsMaxRows extends BaseTestCase {
assertThat(loggedSql).hasSize(1);
assertThat(loggedSql.get(0)).contains("order by t0.id");
}
@Test
public void test_pagingAppendToExistingOrderBy() {
ResetBasicData.reset();
LoggedSqlCollector.start();
Ebean.find(Order.class)
.order().asc("orderDate")
.findPagedList(0, 10)
.getList();
List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(loggedSql).hasSize(1);
assertThat(loggedSql.get(0)).contains("order by t0.order_date, t0.id");
}
@Test
public void test_pagingExistingOrderByWithId() {
ResetBasicData.reset();
LoggedSqlCollector.start();
Ebean.find(Order.class)
.order().asc("orderDate")
.order().desc("id")
.findPagedList(0, 10)
.getList();
List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(loggedSql).hasSize(1);
assertThat(loggedSql.get(0)).contains("order by t0.order_date, t0.id desc");
}
}
@@ -1,10 +1,12 @@
package com.avaje.tests.unitinternal;
import org.junit.Assert;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.OrderBy;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Test the OrderBy object and especially its parsing.
@@ -15,105 +17,109 @@ public class TestOrderByParse extends BaseTestCase {
public void testParsingOne() {
OrderBy<Object> o1 = new OrderBy<Object>("id");
Assert.assertTrue(o1.getProperties().size() == 1);
Assert.assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
Assert.assertTrue(o1.getProperties().get(0).isAscending());
Assert.assertTrue(o1.toStringFormat().equals("id"));
assertTrue(o1.getProperties().size() == 1);
assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
assertTrue(o1.getProperties().get(0).isAscending());
assertTrue(o1.toStringFormat().equals("id"));
o1 = new OrderBy<Object>("id asc");
Assert.assertTrue(o1.getProperties().size() == 1);
Assert.assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
Assert.assertTrue(o1.getProperties().get(0).isAscending());
Assert.assertTrue(o1.toStringFormat().equals("id"));
assertTrue(o1.getProperties().size() == 1);
assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
assertTrue(o1.getProperties().get(0).isAscending());
assertTrue(o1.toStringFormat().equals("id"));
o1 = new OrderBy<Object>("id desc");
Assert.assertTrue(o1.getProperties().size() == 1);
Assert.assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
Assert.assertTrue(!o1.getProperties().get(0).isAscending());
Assert.assertTrue(o1.toStringFormat().equals("id desc"));
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"));
o1 = new OrderBy<Object>(" id asc ");
Assert.assertTrue(o1.getProperties().size() == 1);
Assert.assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
Assert.assertTrue(o1.getProperties().get(0).isAscending());
Assert.assertTrue(o1.toStringFormat().equals("id"));
assertTrue(o1.getProperties().size() == 1);
assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
assertTrue(o1.getProperties().get(0).isAscending());
assertTrue(o1.toStringFormat().equals("id"));
assertTrue(o1.containsProperty("id"));
assertFalse(o1.containsProperty("junk"));
}
@Test
public void testParsingTwo() {
OrderBy<?> o1 = new OrderBy<Object>("id,name");
Assert.assertTrue(o1.getProperties().size() == 2);
Assert.assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
Assert.assertTrue(o1.getProperties().get(0).isAscending());
Assert.assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
Assert.assertTrue(o1.getProperties().get(1).isAscending());
Assert.assertEquals("id, name", o1.toStringFormat());
assertTrue(o1.getProperties().size() == 2);
assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
assertTrue(o1.getProperties().get(0).isAscending());
assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
assertTrue(o1.getProperties().get(1).isAscending());
assertEquals("id, name", o1.toStringFormat());
o1 = new OrderBy<Object>(" id , name ");
Assert.assertTrue(o1.getProperties().size() == 2);
Assert.assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
Assert.assertTrue(o1.getProperties().get(0).isAscending());
Assert.assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
Assert.assertTrue(o1.getProperties().get(1).isAscending());
Assert.assertEquals("id, name", o1.toStringFormat());
assertTrue(o1.getProperties().size() == 2);
assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
assertTrue(o1.getProperties().get(0).isAscending());
assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
assertTrue(o1.getProperties().get(1).isAscending());
assertEquals("id, name", o1.toStringFormat());
o1 = new OrderBy<Object>(" id desc , name desc ");
Assert.assertTrue(o1.getProperties().size() == 2);
Assert.assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
Assert.assertTrue(!o1.getProperties().get(0).isAscending());
Assert.assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
Assert.assertTrue(!o1.getProperties().get(1).isAscending());
Assert.assertEquals("id desc, name desc", o1.toStringFormat());
assertTrue(o1.getProperties().size() == 2);
assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
assertTrue(!o1.getProperties().get(0).isAscending());
assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
assertTrue(!o1.getProperties().get(1).isAscending());
assertEquals("id desc, name desc", o1.toStringFormat());
o1 = new OrderBy<Object>(" id ascending, name asc");
Assert.assertTrue(o1.getProperties().size() == 2);
Assert.assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
Assert.assertTrue(o1.getProperties().get(0).isAscending());
Assert.assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
Assert.assertTrue(o1.getProperties().get(1).isAscending());
Assert.assertEquals("id, name", o1.toStringFormat());
assertTrue(o1.getProperties().size() == 2);
assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
assertTrue(o1.getProperties().get(0).isAscending());
assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
assertTrue(o1.getProperties().get(1).isAscending());
assertEquals("id, name", o1.toStringFormat());
}
@Test
public void testAddMethods() {
OrderBy<?> o1 = new OrderBy<Object>();
o1.asc("id");
o1.asc("name");
Assert.assertTrue(o1.getProperties().size() == 2);
Assert.assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
Assert.assertTrue(o1.getProperties().get(0).isAscending());
Assert.assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
Assert.assertTrue(o1.getProperties().get(1).isAscending());
Assert.assertEquals("id, name", o1.toStringFormat());
assertTrue(o1.getProperties().size() == 2);
assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
assertTrue(o1.getProperties().get(0).isAscending());
assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
assertTrue(o1.getProperties().get(1).isAscending());
assertEquals("id, name", o1.toStringFormat());
o1 = new OrderBy<Object>();
o1.desc("id");
o1.desc("name");
Assert.assertTrue(o1.getProperties().size() == 2);
Assert.assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
Assert.assertTrue(!o1.getProperties().get(0).isAscending());
Assert.assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
Assert.assertTrue(!o1.getProperties().get(1).isAscending());
Assert.assertEquals("id desc, name desc", o1.toStringFormat());
assertTrue(o1.getProperties().size() == 2);
assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
assertTrue(!o1.getProperties().get(0).isAscending());
assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
assertTrue(!o1.getProperties().get(1).isAscending());
assertEquals("id desc, name desc", o1.toStringFormat());
o1.reverse();
Assert.assertTrue(o1.getProperties().size() == 2);
Assert.assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
Assert.assertTrue(o1.getProperties().get(0).isAscending());
Assert.assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
Assert.assertTrue(o1.getProperties().get(1).isAscending());
Assert.assertEquals("id, name", o1.toStringFormat());
assertTrue(o1.getProperties().size() == 2);
assertTrue(o1.getProperties().get(0).getProperty().equals("id"));
assertTrue(o1.getProperties().get(0).isAscending());
assertTrue(o1.getProperties().get(1).getProperty().equals("name"));
assertTrue(o1.getProperties().get(1).isAscending());
assertEquals("id, name", o1.toStringFormat());
OrderBy<?> copy = o1.copy();
Assert.assertTrue(copy != o1);
Assert.assertTrue(copy.getProperties().size() == 2);
Assert.assertTrue(copy.getProperties().get(0).getProperty().equals("id"));
Assert.assertTrue(copy.getProperties().get(0).isAscending());
Assert.assertTrue(copy.getProperties().get(1).getProperty().equals("name"));
Assert.assertTrue(copy.getProperties().get(1).isAscending());
Assert.assertEquals("id, name", copy.toStringFormat());
assertTrue(copy != o1);
assertTrue(copy.getProperties().size() == 2);
assertTrue(copy.getProperties().get(0).getProperty().equals("id"));
assertTrue(copy.getProperties().get(0).isAscending());
assertTrue(copy.getProperties().get(1).getProperty().equals("name"));
assertTrue(copy.getProperties().get(1).isAscending());
assertEquals("id, name", copy.toStringFormat());
}
}