mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#566 - Refactor internals - OrmQueryDetailProperties with final FetchConfig
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
package com.avaje.ebean;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class FetchConfigTest {
|
||||
|
||||
@Test
|
||||
public void testLazy() throws Exception {
|
||||
|
||||
FetchConfig config = new FetchConfig().lazy();
|
||||
|
||||
assertThat(config.getLazyBatchSize()).isEqualTo(0);
|
||||
assertThat(config.getQueryBatchSize()).isEqualTo(-1);
|
||||
assertThat(config.isQueryAll()).isEqualTo(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLazy_withParameter() throws Exception {
|
||||
|
||||
FetchConfig config = new FetchConfig().lazy(50);
|
||||
|
||||
assertThat(config.getLazyBatchSize()).isEqualTo(50);
|
||||
assertThat(config.getQueryBatchSize()).isEqualTo(-1);
|
||||
assertThat(config.isQueryAll()).isEqualTo(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuery() throws Exception {
|
||||
|
||||
FetchConfig config = new FetchConfig().query();
|
||||
|
||||
assertThat(config.getLazyBatchSize()).isEqualTo(-1);
|
||||
assertThat(config.getQueryBatchSize()).isEqualTo(0);
|
||||
assertThat(config.isQueryAll()).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuery_withParameter() throws Exception {
|
||||
|
||||
FetchConfig config = new FetchConfig().query(50);
|
||||
|
||||
assertThat(config.getLazyBatchSize()).isEqualTo(-1);
|
||||
assertThat(config.getQueryBatchSize()).isEqualTo(50);
|
||||
assertThat(config.isQueryAll()).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryFirst() throws Exception {
|
||||
|
||||
FetchConfig config = new FetchConfig().queryFirst(50);
|
||||
|
||||
assertThat(config.getLazyBatchSize()).isEqualTo(-1);
|
||||
assertThat(config.getQueryBatchSize()).isEqualTo(50);
|
||||
assertThat(config.isQueryAll()).isEqualTo(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryAndLazy_withParameters() throws Exception {
|
||||
|
||||
FetchConfig config = new FetchConfig().query(50).lazy(10);
|
||||
|
||||
assertThat(config.getLazyBatchSize()).isEqualTo(10);
|
||||
assertThat(config.getQueryBatchSize()).isEqualTo(50);
|
||||
assertThat(config.isQueryAll()).isEqualTo(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryAndLazy() throws Exception {
|
||||
|
||||
FetchConfig config = new FetchConfig().query(50).lazy();
|
||||
|
||||
assertThat(config.getLazyBatchSize()).isEqualTo(0);
|
||||
assertThat(config.getQueryBatchSize()).isEqualTo(50);
|
||||
assertThat(config.isQueryAll()).isEqualTo(false);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEquals_when_noOptions() throws Exception {
|
||||
|
||||
assertSame(new FetchConfig(), new FetchConfig());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals_when_query_50_lazy_40() throws Exception {
|
||||
|
||||
assertSame(new FetchConfig().query(50).lazy(40), new FetchConfig().query(50).lazy(40));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals_when_query_50_lazy() throws Exception {
|
||||
|
||||
assertSame(new FetchConfig().query(50).lazy(), new FetchConfig().query(50).lazy());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals_when_query_50() throws Exception {
|
||||
|
||||
assertSame(new FetchConfig().query(50), new FetchConfig().query(50));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals_when_queryFirst_50_lazy_40() throws Exception {
|
||||
|
||||
assertSame(new FetchConfig().queryFirst(50).lazy(40), new FetchConfig().queryFirst(50).lazy(40));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals_when_queryFirst_50_lazy() throws Exception {
|
||||
|
||||
assertSame(new FetchConfig().queryFirst(50).lazy(), new FetchConfig().queryFirst(50).lazy());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals_when_queryFirst_50() throws Exception {
|
||||
|
||||
assertSame(new FetchConfig().queryFirst(50), new FetchConfig().queryFirst(50));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotEquals_when_query_50() throws Exception {
|
||||
|
||||
assertDifferent(new FetchConfig().query(50), new FetchConfig().query(40));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotEquals_when_query_50_lazy() throws Exception {
|
||||
|
||||
assertDifferent(new FetchConfig().query(50), new FetchConfig().query(50).lazy());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotEquals_when_query_50_lazy_40() throws Exception {
|
||||
|
||||
assertDifferent(new FetchConfig().query(50), new FetchConfig().query(50).lazy(40));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotEquals_when_queryFirst_50() throws Exception {
|
||||
|
||||
assertDifferent(new FetchConfig().queryFirst(50), new FetchConfig().queryFirst(40));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotEquals_when_queryFirst_50_lazy() throws Exception {
|
||||
|
||||
assertDifferent(new FetchConfig().queryFirst(50), new FetchConfig().queryFirst(50).lazy());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotEquals_when_queryFirst_50_lazy_40() throws Exception {
|
||||
|
||||
assertDifferent(new FetchConfig().queryFirst(50), new FetchConfig().queryFirst(50).lazy(40));
|
||||
}
|
||||
|
||||
void assertDifferent(FetchConfig v1, FetchConfig v2) {
|
||||
assertThat(v1).isNotEqualTo(v2);
|
||||
assertThat(v1.hashCode()).isNotEqualTo(v2.hashCode());
|
||||
}
|
||||
|
||||
|
||||
void assertSame(FetchConfig v1, FetchConfig v2) {
|
||||
assertThat(v1).isEqualTo(v2);
|
||||
assertThat(v1.hashCode()).isEqualTo(v2.hashCode());
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.avaje.ebeaninternal.server.querydefn;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -7,7 +10,7 @@ import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class OrmQueryDetailTest {
|
||||
public class OrmQueryDetailTest extends BaseTestCase {
|
||||
|
||||
OrmQueryDetail parse(String query) {
|
||||
return new OrmQueryDetailParser(query).parse();
|
||||
@@ -115,4 +118,56 @@ public class OrmQueryDetailTest {
|
||||
assertThat(detail.getFetchPaths()).containsExactly("details", "customer", "details.product");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void markQueryJoins_when_allowOne_expect_stillFetchJoin() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.fetch("details", null, null);
|
||||
|
||||
detail.markQueryJoins(orderDesc(), null, true);
|
||||
|
||||
assertThat(detail.getChunk("details", false).isQueryFetch()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void markQueryJoins_when_allowNone_expect_queryJoin() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.fetch("details", null, null);
|
||||
|
||||
detail.markQueryJoins(orderDesc(), null, false);
|
||||
|
||||
assertThat(detail.getChunk("details", false).isQueryFetch()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void markQueryJoins_when_allowOneButSecond_expect_queryJoin() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.fetch("details", null, null);
|
||||
detail.fetch("customer.contacts", null, null);
|
||||
|
||||
detail.markQueryJoins(orderDesc(), null, true);
|
||||
|
||||
assertThat(detail.getChunk("details", false).isQueryFetch()).isFalse();
|
||||
assertThat(detail.getChunk("customer.contacts", false).isQueryFetch()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void markQueryJoins_when_allowNone_expect_bothQueryJoin() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.fetch("details", null, null);
|
||||
detail.fetch("customer.contacts", null, null);
|
||||
|
||||
detail.markQueryJoins(orderDesc(), null, false);
|
||||
|
||||
assertThat(detail.getChunk("details", false).isQueryFetch()).isTrue();
|
||||
assertThat(detail.getChunk("customer.contacts", false).isQueryFetch()).isTrue();
|
||||
}
|
||||
|
||||
BeanDescriptor<Order> orderDesc() {
|
||||
return getBeanDescriptor(Order.class);
|
||||
}
|
||||
|
||||
}
|
||||
+9
-10
@@ -74,7 +74,7 @@ public class OrmQueryPropertiesParserTest {
|
||||
public void when_hasLazy() throws Exception {
|
||||
|
||||
OrmQueryPropertiesParser.Response res = OrmQueryPropertiesParser.parse("+lazy");
|
||||
assertThat(res.lazyFetchBatch).isEqualTo(0);
|
||||
assertThat(res.fetchConfig.getLazyBatchSize()).isEqualTo(0);
|
||||
assertThat(res.included).isNull();
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ public class OrmQueryPropertiesParserTest {
|
||||
public void when_hasLazyValue() throws Exception {
|
||||
|
||||
OrmQueryPropertiesParser.Response res = OrmQueryPropertiesParser.parse("+lazy(20)");
|
||||
assertThat(res.lazyFetchBatch).isEqualTo(20);
|
||||
assertThat(res.fetchConfig.getLazyBatchSize()).isEqualTo(20);
|
||||
assertThat(res.included).isNull();
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ public class OrmQueryPropertiesParserTest {
|
||||
public void when_hasLazyValue_last() throws Exception {
|
||||
|
||||
OrmQueryPropertiesParser.Response res = OrmQueryPropertiesParser.parse("name,+lazy(20)");
|
||||
assertThat(res.lazyFetchBatch).isEqualTo(20);
|
||||
assertThat(res.fetchConfig.getLazyBatchSize()).isEqualTo(20);
|
||||
assertThat(res.included).containsExactly("name");
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ public class OrmQueryPropertiesParserTest {
|
||||
public void when_hasLazyValue_first() throws Exception {
|
||||
|
||||
OrmQueryPropertiesParser.Response res = OrmQueryPropertiesParser.parse("+lazy(20),id,name");
|
||||
assertThat(res.lazyFetchBatch).isEqualTo(20);
|
||||
assertThat(res.fetchConfig.getLazyBatchSize()).isEqualTo(20);
|
||||
assertThat(res.included).containsExactly("id", "name");
|
||||
}
|
||||
|
||||
@@ -106,17 +106,16 @@ public class OrmQueryPropertiesParserTest {
|
||||
public void when_allProperties() throws Exception {
|
||||
|
||||
OrmQueryPropertiesParser.Response res = OrmQueryPropertiesParser.parse("+query(4),+lazy(5)");
|
||||
assertThat(res.lazyFetchBatch).isEqualTo(5);
|
||||
assertThat(res.fetchConfig.getLazyBatchSize()).isEqualTo(5);
|
||||
assertThat(res.included).isNull();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void when_everything_set() throws Exception {
|
||||
|
||||
OrmQueryPropertiesParser.Response res = OrmQueryPropertiesParser.parse("id, name +readonly +lazy(20) +query(30) +cache");
|
||||
assertThat(res.lazyFetchBatch).isEqualTo(20);
|
||||
assertThat(res.queryFetchBatch).isEqualTo(30);
|
||||
assertThat(res.fetchConfig.getLazyBatchSize()).isEqualTo(20);
|
||||
assertThat(res.fetchConfig.getQueryBatchSize()).isEqualTo(30);
|
||||
assertThat(res.readOnly).isTrue();
|
||||
assertThat(res.cache).isTrue();
|
||||
assertThat(res.included).containsExactly("id", "name");
|
||||
@@ -125,8 +124,8 @@ public class OrmQueryPropertiesParserTest {
|
||||
private void assertAllDefaults(OrmQueryPropertiesParser.Response res) {
|
||||
assertThat(res.cache).isFalse();
|
||||
assertThat(res.readOnly).isFalse();
|
||||
assertThat(res.lazyFetchBatch).isEqualTo(-1);
|
||||
assertThat(res.queryFetchBatch).isEqualTo(-1);
|
||||
assertThat(res.fetchConfig.getLazyBatchSize()).isEqualTo(-1);
|
||||
assertThat(res.fetchConfig.getQueryBatchSize()).isEqualTo(-1);
|
||||
assertThat(res.included).isNull();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user