#566 - Refactor internals - OrmQueryDetail, skip fetch path sort when empty

This commit is contained in:
Robin Bygrave
2016-02-17 16:47:38 +13:00
parent 27045baf44
commit 3d3948aa8b
2 changed files with 43 additions and 6 deletions
@@ -298,15 +298,18 @@ public class OrmQueryDetail implements Serializable {
fetch(new OrmQueryProperties(path, properties));
}
/**
* Sort the fetch paths into depth order adding any missing parent paths if necessary.
*/
public void sortFetchPaths(BeanDescriptor<?> d) {
LinkedHashMap<String, OrmQueryProperties> sorted = new LinkedHashMap<String, OrmQueryProperties>();
for (OrmQueryProperties p : fetchPaths.values()) {
sortFetchPaths(d, p, sorted);
if (!fetchPaths.isEmpty()) {
LinkedHashMap<String, OrmQueryProperties> sorted = new LinkedHashMap<String, OrmQueryProperties>();
for (OrmQueryProperties p : fetchPaths.values()) {
sortFetchPaths(d, p, sorted);
}
fetchPaths = sorted;
}
fetchPaths = sorted;
}
private void sortFetchPaths(BeanDescriptor<?> d, OrmQueryProperties p, LinkedHashMap<String, OrmQueryProperties> sorted) {
@@ -166,6 +166,40 @@ public class OrmQueryDetailTest extends BaseTestCase {
assertThat(detail.getChunk("customer.contacts", false).isQueryFetch()).isTrue();
}
@Test
public void sortFetchPaths_when_missingParent_expect_addsMissing() {
OrmQueryDetail detail = new OrmQueryDetail();
detail.fetch("customer.contacts", null, null);
detail.sortFetchPaths(orderDesc());
assertThat(detail.getFetchPaths()).containsExactly("customer","customer.contacts");
assertThat(detail.getChunk("customer",false).getIncluded()).containsExactly("id");
}
@Test
public void sortFetchPaths_when_outOfOrder_expect_correctOrder() {
OrmQueryDetail detail = new OrmQueryDetail();
detail.fetch("customer.contacts", "email", null);
detail.fetch("customer", "name", null);
detail.sortFetchPaths(orderDesc());
assertThat(detail.getFetchPaths()).containsExactly("customer","customer.contacts");
assertThat(detail.getChunk("customer",false).getIncluded()).containsExactly("name");
}
@Test
public void sortFetchPaths_when_empty_expect_stillEmpty() {
OrmQueryDetail detail = new OrmQueryDetail();
detail.sortFetchPaths(orderDesc());
assertThat(detail.getFetchPaths()).isEmpty();
}
BeanDescriptor<Order> orderDesc() {
return getBeanDescriptor(Order.class);
}