mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Refactor move tests from ebean-core to ebean-test
This commit is contained in:
1 parent
0b00a14fd3
commit
eab56d323b
1650 files changed
+185
-147
No files matched your search
@@ -0,0 +1,207 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Expr;
|
||||
import io.ebean.FetchConfig;
|
||||
import io.ebean.Transaction;
|
||||
import org.tests.basic.MyTestDataSourcePoolListener;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestBasicLazy extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testQueries() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Order order = DB.find(Order.class).select("totalAmount").setMaxRows(1).order("id")
|
||||
.findOne();
|
||||
|
||||
assertNotNull(order);
|
||||
|
||||
Customer customer = order.getCustomer();
|
||||
assertNotNull(customer);
|
||||
assertNotNull(customer.getName());
|
||||
|
||||
Address address = customer.getBillingAddress();
|
||||
assertNotNull(address);
|
||||
assertNotNull(address.getCity());
|
||||
}
|
||||
|
||||
public void test_N1N() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
// safety check to see if our customer we are going to use for the test has
|
||||
// some contacts
|
||||
Customer c = DB.find(Customer.class).setId(1).findOne();
|
||||
assertNotNull(c.getContacts());
|
||||
assertTrue(!c.getContacts().isEmpty());
|
||||
|
||||
// start transaction so we have a "long running" persistence context
|
||||
Transaction tx = DB.beginTransaction();
|
||||
try {
|
||||
List<Order> order = DB.find(Order.class).where(Expr.eq("customer.id", 1)).findList();
|
||||
|
||||
assertNotNull(order);
|
||||
assertTrue(!order.isEmpty());
|
||||
|
||||
Customer customer = order.get(0).getCustomer();
|
||||
assertNotNull(customer);
|
||||
assertEquals(1, customer.getId().intValue());
|
||||
|
||||
// this should lazily fetch the contacts
|
||||
List<Contact> contacts = customer.getContacts();
|
||||
|
||||
assertNotNull(contacts);
|
||||
assertTrue(!contacts.isEmpty());
|
||||
} finally {
|
||||
tx.commit();
|
||||
}
|
||||
}
|
||||
|
||||
public void testRaceCondition_Simple() throws Throwable {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Order order = DB.find(Order.class).select("totalAmount").setMaxRows(1).order("id")
|
||||
.findOne();
|
||||
|
||||
assertNotNull(order);
|
||||
|
||||
final Customer customer = order.getCustomer();
|
||||
assertNotNull(customer);
|
||||
|
||||
assertTrue(DB.beanState(customer).isReference());
|
||||
|
||||
final Throwable throwables[] = new Throwable[2];
|
||||
Thread t1 = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
assertNotNull(customer.getName());
|
||||
} catch (Throwable e) {
|
||||
throwables[0] = e;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Thread t2 = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
assertNotNull(customer.getName());
|
||||
} catch (Throwable e) {
|
||||
throwables[1] = e;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
// prepare for race condition
|
||||
MyTestDataSourcePoolListener.SLEEP_AFTER_BORROW = 2000;
|
||||
|
||||
t1.start();
|
||||
t2.start();
|
||||
t1.join();
|
||||
t2.join();
|
||||
} finally {
|
||||
MyTestDataSourcePoolListener.SLEEP_AFTER_BORROW = 0;
|
||||
}
|
||||
|
||||
assertFalse(DB.beanState(customer).isReference());
|
||||
|
||||
if (throwables[0] != null) {
|
||||
throw throwables[0];
|
||||
}
|
||||
if (throwables[1] != null) {
|
||||
throw throwables[1];
|
||||
}
|
||||
}
|
||||
|
||||
private final AtomicBoolean mutex = new AtomicBoolean(false);
|
||||
private List<Order> orders;
|
||||
|
||||
private List<Throwable> exceptions = Collections.synchronizedList(new ArrayList<Throwable>());
|
||||
|
||||
private class FetchThread extends Thread {
|
||||
private int index;
|
||||
|
||||
private FetchThread(ThreadGroup tg, int index) {
|
||||
super(tg, "fetcher-" + index);
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
synchronized (mutex) {
|
||||
System.err.println("** WAIT **");
|
||||
try {
|
||||
while (!mutex.get()) {
|
||||
mutex.wait(100);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
System.err.println("** DO LAZY FETCH **");
|
||||
orders.get(index).getCustomer().getName();
|
||||
} catch (Throwable e) {
|
||||
exceptions.add(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testRaceCondition_Complex() throws Throwable {
|
||||
ResetBasicData.reset();
|
||||
|
||||
ThreadGroup tg = new ThreadGroup("fetchers");
|
||||
new FetchThread(tg, 0).start();
|
||||
new FetchThread(tg, 1).start();
|
||||
new FetchThread(tg, 2).start();
|
||||
new FetchThread(tg, 3).start();
|
||||
new FetchThread(tg, 0).start();
|
||||
new FetchThread(tg, 1).start();
|
||||
new FetchThread(tg, 2).start();
|
||||
new FetchThread(tg, 3).start();
|
||||
|
||||
orders = DB.find(Order.class).fetchLazy("customer").findList();
|
||||
assertTrue(orders.size() >= 4);
|
||||
|
||||
try {
|
||||
MyTestDataSourcePoolListener.SLEEP_AFTER_BORROW = 2000;
|
||||
|
||||
synchronized (mutex) {
|
||||
mutex.set(true);
|
||||
mutex.notifyAll();
|
||||
}
|
||||
|
||||
while (tg.activeCount() > 0) {
|
||||
Thread.sleep(100);
|
||||
}
|
||||
} finally {
|
||||
MyTestDataSourcePoolListener.SLEEP_AFTER_BORROW = 0;
|
||||
}
|
||||
|
||||
if (!exceptions.isEmpty()) {
|
||||
System.err.println("Seen Exceptions:");
|
||||
for (Throwable exception : exceptions) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
fail();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestBasicNavOnEmpty extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Customer c = new Customer();
|
||||
c.setName("HelloRob");
|
||||
|
||||
DB.save(c);
|
||||
|
||||
c = DB.find(Customer.class, c.getId());
|
||||
|
||||
List<Contact> contacts = c.getContacts();
|
||||
assertEquals(0, contacts.size());
|
||||
|
||||
// cleanup
|
||||
DB.delete(c);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheStatistics;
|
||||
import org.tests.model.basic.UUOne;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestBatchLazyWithCacheHits extends BaseTestCase {
|
||||
|
||||
private final ServerCache beanCache = server().cacheManager().beanCache(UUOne.class);
|
||||
|
||||
private UUOne insert(String name) {
|
||||
UUOne one = new UUOne();
|
||||
one.setName("testBLWCH" + name);
|
||||
DB.save(one);
|
||||
return one;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnCacheHit() {
|
||||
List<UUOne> inserted = insertData();
|
||||
clearCacheAndStatistics();
|
||||
|
||||
assertNotNull(DB.find(UUOne.class, inserted.get(1).getId()));
|
||||
// cache hit
|
||||
assertNotNull(DB.find(UUOne.class, inserted.get(1).getId()));
|
||||
assertBeanCacheHits(1);
|
||||
|
||||
assertNotNull(DB.find(UUOne.class).where().idEq(inserted.get(2).getId()).findOne());
|
||||
// cache hit
|
||||
assertNotNull(DB.find(UUOne.class).where().idEq(inserted.get(2).getId()).findOne());
|
||||
assertBeanCacheHits(1);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<UUOne> list = DB.find(UUOne.class)
|
||||
.select("id")
|
||||
.where().startsWith("name", "testBLWCH")
|
||||
.order("name")
|
||||
.findList();
|
||||
|
||||
// invoke lazy loading
|
||||
for (UUOne uuOne : list) {
|
||||
uuOne.getName();
|
||||
}
|
||||
list.get(0).getName();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertSql(sql.get(0)).contains("from uuone t0 where t0.name like ");
|
||||
platformAssertIn(sql.get(1), "from uuone t0 where t0.id");
|
||||
|
||||
// not lazy loading into bean cache
|
||||
final ServerCacheStatistics stats = beanCache.statistics(true);
|
||||
assertThat(stats.getHitCount()).isEqualTo(10);
|
||||
assertThat(stats.getSize()).isEqualTo(10);
|
||||
}
|
||||
|
||||
private void assertBeanCacheHits(int hits) {
|
||||
ServerCacheStatistics statistics = beanCache.statistics(true);
|
||||
assertEquals(statistics.getHitCount(), hits);
|
||||
}
|
||||
|
||||
private void clearCacheAndStatistics() {
|
||||
beanCache.clear();
|
||||
beanCache.statistics(true);
|
||||
}
|
||||
|
||||
private List<UUOne> insertData() {
|
||||
List<UUOne> inserted = new ArrayList<>();
|
||||
String[] names = "A,B,C,D,E,F,G,H,I,J".split(",");
|
||||
for (String name : names) {
|
||||
inserted.add(insert(name));
|
||||
}
|
||||
return inserted;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.UUOne;
|
||||
import org.tests.model.basic.UUTwo;
|
||||
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestBatchLazyWithDeleted extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testOnDeleted() {
|
||||
|
||||
DB.deleteAll(DB.find(UUTwo.class).findList());
|
||||
DB.deleteAll(DB.find(UUOne.class).findList());
|
||||
|
||||
UUOne oneA = new UUOne();
|
||||
oneA.setName("oneA");
|
||||
|
||||
UUOne oneB = new UUOne();
|
||||
oneB.setName("oneB");
|
||||
|
||||
UUTwo two = new UUTwo();
|
||||
two.setName("two-bld-A");
|
||||
two.setMaster(oneA);
|
||||
|
||||
UUTwo twoB = new UUTwo();
|
||||
twoB.setName("two-bld-B");
|
||||
twoB.setMaster(oneA);
|
||||
|
||||
UUTwo twoC = new UUTwo();
|
||||
twoC.setName("two-bld-C");
|
||||
twoC.setMaster(oneB);
|
||||
|
||||
DB.save(oneA);
|
||||
DB.save(oneB);
|
||||
DB.save(two);
|
||||
DB.save(twoB);
|
||||
DB.save(twoC);
|
||||
|
||||
List<UUTwo> list = DB.find(UUTwo.class)
|
||||
.fetchLazy("master")
|
||||
.where().startsWith("name", "two-bld-")
|
||||
.order("name")
|
||||
.findList();
|
||||
|
||||
// delete a bean that will be batch lazy loaded but
|
||||
// is NOT the bean that will invoke the lazy loading
|
||||
// (in this case it is the second bean in the list).
|
||||
int deletedCount = DB.delete(UUOne.class, oneB.getId());
|
||||
assertEquals(1, deletedCount);
|
||||
|
||||
for (UUTwo u : list) {
|
||||
u.getMaster();
|
||||
//BeanState beanState = DB.beanState(master);
|
||||
//assertTrue(beanState.isReference());
|
||||
}
|
||||
|
||||
// invoke lazy loading on the second 'master' bean
|
||||
// list.get(2).getMaster().getName();
|
||||
|
||||
// invoke lazy loading on the first 'master' bean
|
||||
list.get(0).getMaster().getName();
|
||||
|
||||
// this breaks with IllegalStateException
|
||||
// because the 2nd UUOne bean is still in 'reference' state
|
||||
// and has not had its lazyLoadProperty set so it fails as
|
||||
// it was removed from the "lazy load context" earlier
|
||||
try {
|
||||
list.get(2).getMaster().getName();
|
||||
assertTrue(false);
|
||||
} catch (EntityNotFoundException e) {
|
||||
// this bean was deleted and lazy loading failed
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.BeanState;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.bean.EntityBeanIntercept;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
public class TestBeanState extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> custs = DB.find(Customer.class).findList();
|
||||
|
||||
Customer customer = DB.find(Customer.class).setId(custs.get(0).getId()).select("name")
|
||||
.setUseCache(false)
|
||||
.findOne();
|
||||
|
||||
BeanState beanState = DB.beanState(customer);
|
||||
assertFalse(beanState.isNew());
|
||||
assertFalse(beanState.isDirty());
|
||||
assertFalse(beanState.isNewOrDirty());
|
||||
assertNotNull(beanState.loadedProps());
|
||||
|
||||
customer.setName("dirtyNameProp");
|
||||
assertTrue(beanState.isDirty());
|
||||
assertThat(beanState.changedProps()).containsOnly("name");
|
||||
|
||||
EntityBeanIntercept ebi = ((EntityBean) customer)._ebean_getIntercept();
|
||||
boolean[] dirtyProperties = ebi.getDirtyProperties();
|
||||
for (int i = 0; i < dirtyProperties.length; i++) {
|
||||
if (dirtyProperties[i]) {
|
||||
String dirtyPropertyName = ebi.getProperty(i);
|
||||
assertEquals("name", dirtyPropertyName);
|
||||
}
|
||||
}
|
||||
|
||||
customer.setStatus(Customer.Status.INACTIVE);
|
||||
|
||||
assertTrue(beanState.isDirty());
|
||||
assertThat(beanState.changedProps()).containsOnly("name", "status");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDisableLazyLoad_expect_lazyLoadingDisabled() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> custs = DB.find(Customer.class).order("id").findList();
|
||||
|
||||
Customer customer = DB.find(Customer.class)
|
||||
.setId(custs.get(0).getId())
|
||||
.select("id")
|
||||
.setUseCache(false)
|
||||
.findOne();
|
||||
|
||||
BeanState beanState = DB.beanState(customer);
|
||||
beanState.setDisableLazyLoad(true);
|
||||
assertNull(customer.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getChangedProps_when_setManyProperty() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Customer customer = DB.find(Customer.class).order("id").setMaxRows(1).findOne();
|
||||
|
||||
BeanState beanState = DB.beanState(customer);
|
||||
assertThat(beanState.changedProps()).isEmpty();
|
||||
|
||||
customer.setContacts(new ArrayList<>());
|
||||
assertThat(beanState.changedProps()).containsOnly("contacts");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getChangedProps_when_setManyProperty_onNewBean() {
|
||||
|
||||
Customer customer = new Customer();
|
||||
|
||||
BeanState beanState = DB.beanState(customer);
|
||||
assertThat(beanState.changedProps()).isEmpty();
|
||||
|
||||
// when new state, then loaded
|
||||
customer.setContacts(new ArrayList<>());
|
||||
assertThat(beanState.changedProps()).isEmpty();
|
||||
assertThat(beanState.loadedProps()).containsOnly("contacts");
|
||||
|
||||
// set loaded state, then marked as changed
|
||||
beanState.setLoaded();
|
||||
customer.setContacts(new ArrayList<>());
|
||||
assertThat(beanState.loadedProps()).containsOnly("contacts");
|
||||
assertThat(beanState.changedProps()).containsOnly("contacts");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readOnly_when_setManyProperty() {
|
||||
Customer customer = new Customer();
|
||||
customer.setContacts(new ArrayList<>());
|
||||
|
||||
BeanState beanState = DB.beanState(customer);
|
||||
beanState.setLoaded();
|
||||
beanState.setReadOnly(true);
|
||||
|
||||
// act, try to mutate read only bean
|
||||
assertThrows(IllegalStateException.class, () -> customer.setContacts(new ArrayList<>()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readOnly_when_setProperty() {
|
||||
Customer customer = new Customer();
|
||||
customer.setName("a");
|
||||
|
||||
BeanState beanState = DB.beanState(customer);
|
||||
beanState.setLoaded();
|
||||
beanState.setReadOnly(true);
|
||||
|
||||
// act, try to mutate read only bean
|
||||
assertThrows(IllegalStateException.class, () -> customer.setName("b"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.Order.Status;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestEmptyManyLazyLoad extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Customer c = DB.find(Customer.class).findList().get(0);
|
||||
|
||||
Order o = new Order();
|
||||
o.setCustomer(c);
|
||||
o.setStatus(Status.NEW);
|
||||
|
||||
DB.save(o);
|
||||
|
||||
Order o2 = DB.find(Order.class, o.getId());
|
||||
o2.getDetails().size();
|
||||
|
||||
// cleanup
|
||||
DB.delete(o2);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestLazyJoin extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testLazyOnNonLoaded() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status")
|
||||
.fetchLazy("customer", "name, status")
|
||||
.fetch("customer.contacts")
|
||||
.order().asc("id");
|
||||
|
||||
List<Order> list = query.findList();
|
||||
|
||||
Order order = list.get(0);
|
||||
Customer customer = order.getCustomer();
|
||||
|
||||
// this invokes lazy loading on a property that is
|
||||
// not one of the selected ones (name, status) ... and
|
||||
// therefore the lazy load query selects all properties
|
||||
// in the customer (not just name and status)
|
||||
Address billingAddress = customer.getBillingAddress();
|
||||
|
||||
assertNotNull(billingAddress);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestLazyJoin2 extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testLazyOnNonLoaded() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
// This will use 3 SQL queries to build this object graph
|
||||
List<Order> l0 = DB.find(Order.class).select("status, shipDate")
|
||||
|
||||
.fetchQuery("details", "orderQty, unitPrice")
|
||||
.fetch("details.product", "sku, name")
|
||||
|
||||
.fetchQuery("customer", "name")
|
||||
.fetch("customer.contacts", "firstName, lastName, mobile")
|
||||
.fetch("customer.shippingAddress", "line1, city").order().asc("id").findList();
|
||||
|
||||
Order o0 = l0.get(0);
|
||||
Customer c0 = o0.getCustomer();
|
||||
List<Contact> contacts = c0.getContacts();
|
||||
assertTrue(!contacts.isEmpty());
|
||||
|
||||
// query 1) find order (status, shipDate)
|
||||
// query 2) find orderDetail (quantity, price) join product (sku, name)
|
||||
// where order.id in (?,? ...)
|
||||
// query 3) find customer (name) join contacts (*) join shippingAddress (*)
|
||||
// where id in (?,?,?,?,?)
|
||||
|
||||
List<Order> orders = DB.find(Order.class)
|
||||
// .select("status")
|
||||
.fetchQuery("customer").order().asc("id").findList();
|
||||
// .join("customer.contacts");
|
||||
|
||||
// List<Order> list = query.findList();
|
||||
|
||||
Order order = orders.get(0);
|
||||
Customer customer = order.getCustomer();
|
||||
|
||||
// this invokes lazy loading on a property that is
|
||||
// not one of the selected ones (name, status) ... and
|
||||
// therefore the lazy load query selects all properties
|
||||
// in the customer (not just name and status)
|
||||
Address billingAddress = customer.getBillingAddress();
|
||||
|
||||
assertNotNull(billingAddress);
|
||||
|
||||
List<Order> list = DB.find(Order.class).fetchLazy("customer", "name")
|
||||
.fetch("customer.contacts", "contactName, phone, email").fetch("customer.shippingAddress")
|
||||
.where().eq("status", Order.Status.NEW).order().asc("id").findList();
|
||||
|
||||
Order order2 = list.get(0);
|
||||
Customer customer2 = order2.getCustomer();
|
||||
// customer2.getStatus();
|
||||
String name = customer2.getName();
|
||||
assertNotNull(name);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.FetchConfig;
|
||||
import io.ebean.TransactionalTestCase;
|
||||
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestLazyLoadEmptyCollection extends TransactionalTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Customer c = new Customer();
|
||||
c.setName("lazytest");
|
||||
|
||||
Contact con = new Contact("jim", "slim");
|
||||
c.addContact(con);
|
||||
|
||||
DB.save(c);
|
||||
|
||||
List<Customer> list = DB.find(Customer.class)
|
||||
.fetchQuery("contacts")
|
||||
.fetchQuery("contacts.notes")
|
||||
.findList();
|
||||
|
||||
for (Customer customer : list) {
|
||||
List<Contact> contacts = customer.getContacts();
|
||||
for (Contact contact : contacts) {
|
||||
contact.getNotes();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.UUOne;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestLazyLoadNonExistantBean extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testSimple() {
|
||||
|
||||
UUID uuid = UUID.randomUUID();
|
||||
UUOne one = DB.reference(UUOne.class, uuid);
|
||||
|
||||
try {
|
||||
// invoke lazy loading
|
||||
one.getName();
|
||||
assertTrue(false);
|
||||
} catch (EntityNotFoundException e) {
|
||||
// expecting this
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.UUOne;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestLazyLoadNonExistentBean extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testSimple() {
|
||||
|
||||
UUID uuid = UUID.randomUUID();
|
||||
UUOne one = DB.reference(UUOne.class, uuid);
|
||||
|
||||
try {
|
||||
// invoke lazy loading
|
||||
one.getName();
|
||||
assertTrue(false);
|
||||
} catch (EntityNotFoundException e) {
|
||||
// expecting this
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderDetail;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestQueryDisableLazyLoad extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void onAssocMany() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<Order> l0 = DB.find(Order.class)
|
||||
.setDisableLazyLoading(true)
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
|
||||
assertThat(l0).isNotEmpty();
|
||||
|
||||
Order order = l0.get(0);
|
||||
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
assertEquals(details.size(), 0);
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(1);
|
||||
|
||||
assertThat(trimSql(loggedSql.get(0), 2)).contains("select t0.id, t0.status, t0.order_date,");
|
||||
assertThat(loggedSql.get(0)).contains(" from o_order t0 ");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onAssocOne() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<Order> l0 = DB.find(Order.class)
|
||||
.setDisableLazyLoading(true)
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
|
||||
assertThat(l0).isNotEmpty();
|
||||
|
||||
Order order = l0.get(0);
|
||||
|
||||
// normally invokes lazy loading
|
||||
assertNull(order.getCustomer().getStatus());
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onAssocOne_when_partial() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<Order> l0 = DB.find(Order.class)
|
||||
.setDisableLazyLoading(true)
|
||||
.fetch("customer", "smallnote")
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
|
||||
assertThat(l0).isNotEmpty();
|
||||
|
||||
Order order = l0.get(0);
|
||||
|
||||
// normally invokes lazy loading
|
||||
assertNull(order.getCustomer().getStatus());
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.BeanState;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestQueryJoin extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
// Need to make sure the customer cache is cleared for the later
|
||||
// asserts on the lazy loading
|
||||
ServerCache custCache = DB.cacheManager().beanCache(Customer.class);
|
||||
custCache.clear();
|
||||
|
||||
Query<Order> query = DB.find(Order.class).select("status")
|
||||
.fetchLazy("customer", "name, status")
|
||||
.fetch("customer.contacts").order().asc("id");
|
||||
|
||||
List<Order> list = query.findList();
|
||||
|
||||
// list.get(0).getShipDate();
|
||||
|
||||
Order order = list.get(0);
|
||||
BeanState beanStateOrder = DB.beanState(order);
|
||||
assertNotNull(beanStateOrder.loadedProps());
|
||||
// assertTrue(beanStateOrder.getLoadedProps().contains("id"));
|
||||
assertThat(beanStateOrder.loadedProps()).contains("status", "shipments", "customer");
|
||||
|
||||
Customer customer = order.getCustomer();
|
||||
BeanState beanStateCustomer = DB.beanState(customer);
|
||||
assertTrue(beanStateCustomer.isReference());
|
||||
|
||||
customer.getName();
|
||||
assertNotNull(beanStateCustomer.loadedProps());
|
||||
assertThat(beanStateCustomer.loadedProps()).contains("name", "status");
|
||||
assertThat(beanStateCustomer.loadedProps()).doesNotContain("billingAddress");
|
||||
|
||||
customer.getName();
|
||||
|
||||
Address billingAddress = customer.getBillingAddress();
|
||||
billingAddress.getLine1();
|
||||
|
||||
assertFalse(list.isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.FetchConfig;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderDetail;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.tests.model.m2m.Role;
|
||||
import org.tests.model.m2m.Tenant;
|
||||
import org.tests.model.map.MpRole;
|
||||
import org.tests.model.map.MpUser;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestQueryJoinToAssocOne extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testQueryJoinOnFullyPopulatedParent() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// This will use 2 SQL queries to build this object graph
|
||||
List<Order> l0 = DB.find(Order.class)
|
||||
.fetchQuery("details", "orderQty, unitPrice")
|
||||
.fetch("details.product", "sku, name")
|
||||
.findList();
|
||||
|
||||
assertThat(l0).isNotEmpty();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(2);
|
||||
|
||||
String secondaryQuery = trimSql(loggedSql.get(1), 1);
|
||||
assertThat(secondaryQuery).contains("select t0.order_id, t0.id,");
|
||||
assertThat(secondaryQuery).contains(" from o_order_detail t0 left join o_product t1");
|
||||
platformAssertIn(secondaryQuery, " (t0.order_id)");
|
||||
assertThat(secondaryQuery).contains(" order by t0.order_id, t0.id");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testQueryJoinOnPartiallyPopulatedParent() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// This will use 2 SQL queries to build this object graph
|
||||
List<Order> l0 = DB.find(Order.class)
|
||||
.select("status, shipDate")
|
||||
.fetchQuery("details", "orderQty, unitPrice")
|
||||
.fetch("details.product", "sku, name")
|
||||
.findList();
|
||||
|
||||
assertThat(l0).isNotEmpty();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(2);
|
||||
|
||||
String secondaryQuery = trimSql(loggedSql.get(1), 1);
|
||||
assertThat(secondaryQuery).contains("select t0.order_id, t0.id,");
|
||||
assertThat(secondaryQuery).contains(" from o_order_detail t0 left join o_product t1");
|
||||
platformAssertIn(secondaryQuery, " (t0.order_id)");
|
||||
assertThat(secondaryQuery).contains(" order by t0.order_id, t0.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryJoinOnPartiallyPopulatedParent_withLazyLoadingDisabled() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// This will use 2 SQL queries to build this object graph
|
||||
List<Order> l0 = DB.find(Order.class)
|
||||
.setDisableLazyLoading(true)
|
||||
.select("status, shipDate")
|
||||
.fetchQuery("details", "orderQty, unitPrice")
|
||||
.fetch("details.product", "sku, name")
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
|
||||
assertThat(l0).isNotEmpty();
|
||||
|
||||
Order order = l0.get(0);
|
||||
// normally invokes lazy loading
|
||||
order.getOrderDate();
|
||||
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
OrderDetail orderDetail = details.get(0);
|
||||
// normally invokes lazy loading
|
||||
orderDetail.getShipQty();
|
||||
|
||||
// normally invokes lazy loading
|
||||
order.getShipments().size();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(2);
|
||||
|
||||
String secondaryQuery = trimSql(loggedSql.get(1), 1);
|
||||
assertThat(secondaryQuery).contains("select t0.order_id, t0.id,");
|
||||
assertThat(secondaryQuery).contains(" from o_order_detail t0 left join o_product t1");
|
||||
platformAssertIn(secondaryQuery, " (t0.order_id)");
|
||||
assertThat(secondaryQuery).contains(" order by t0.order_id, t0.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disableLazyLoading_when_oneToManyList() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// This will use 2 SQL queries to build this object graph
|
||||
List<Order> l0 = DB.find(Order.class)
|
||||
.setDisableLazyLoading(true)
|
||||
.select("status, shipDate")
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
|
||||
assertThat(l0).isNotEmpty();
|
||||
|
||||
Order order = l0.get(0);
|
||||
// try to invoke lazy loading on the bean
|
||||
assertThat(order.getCustomer()).isNull();
|
||||
assertThat(order.getCretime()).isNull();
|
||||
|
||||
// try to invoke lazy loading on the OneToMany ...
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
assertThat(details).isEmpty();
|
||||
assertThat(details.size()).isEqualTo(0);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disableLazyLoad_when_oneToManySet() {
|
||||
|
||||
Role role0 = new Role("r0");
|
||||
Role role1 = new Role("r1");
|
||||
DB.save(role0);
|
||||
DB.save(role1);
|
||||
|
||||
Tenant tenant = new Tenant("t0");
|
||||
tenant.getRoles().add(role0);
|
||||
tenant.getRoles().add(role1);
|
||||
|
||||
DB.save(tenant);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Tenant found = DB.find(Tenant.class)
|
||||
.setDisableLazyLoading(true)
|
||||
.select("name")
|
||||
.setId(tenant.getId())
|
||||
.findOne();
|
||||
|
||||
assertThat(found.getRoles().size()).isEqualTo(0);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disableLazyLoading_when_oneToManyMap() {
|
||||
|
||||
MpRole r0 = new MpRole();
|
||||
r0.setCode("disableLazyLoading");
|
||||
|
||||
MpUser u = new MpUser();
|
||||
u.setName("disableLazy");
|
||||
u.getRoles().put("disableLazyLoading", r0);
|
||||
DB.save(u);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
MpUser found = DB.find(MpUser.class)
|
||||
.setDisableLazyLoading(true)
|
||||
.select("name")
|
||||
.setId(u.getId())
|
||||
.findOne();
|
||||
|
||||
// normally invokes lazy loading
|
||||
assertThat(found.getRoles().size()).isEqualTo(0);
|
||||
|
||||
// only 1 query ... no lazy loading query
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
|
||||
DB.deleteAll(Arrays.asList(found, r0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOnPartiallyPopulatedParent_withLazyLoadingDisabled() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// This will use 2 SQL queries to build this object graph
|
||||
List<Order> l0 = DB.find(Order.class)
|
||||
.setDisableLazyLoading(true)
|
||||
.select("status, shipDate")
|
||||
.fetch("details", "orderQty, unitPrice")//, new FetchConfig().query())
|
||||
.fetch("details.product", "sku, name")
|
||||
.findList();
|
||||
|
||||
assertThat(l0).isNotEmpty();
|
||||
|
||||
Order order = l0.get(0);
|
||||
// normally invokes lazy loading
|
||||
order.getOrderDate();
|
||||
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
OrderDetail orderDetail = details.get(0);
|
||||
// normally invokes lazy loading
|
||||
orderDetail.getShipQty();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(1);
|
||||
|
||||
String originQuery = trimSql(loggedSql.get(0), 5);
|
||||
assertThat(originQuery).contains("select t0.id, t0.status, t0.ship_date, t1.id, t1.order_qty, t1.unit_price");
|
||||
assertThat(originQuery).contains(" from o_order t0 left join o_order_detail t1 ");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.FetchConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestSecondQueryNoRows extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Customer cnew = new Customer();
|
||||
cnew.setName("testSecQueryNoRows");
|
||||
|
||||
DB.save(cnew);
|
||||
|
||||
Customer c = DB.find(Customer.class)
|
||||
.setAutoTune(false)
|
||||
.setId(cnew.getId())
|
||||
.fetchQuery("contacts")
|
||||
.findOne();
|
||||
|
||||
assertNotNull(c);
|
||||
assertEquals(0, c.getContacts().size());
|
||||
|
||||
DB.delete(c);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
package org.tests.batchload;
|
||||
|
||||
import io.ebean.*;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestSecondaryQueries extends TransactionalTestCase {
|
||||
|
||||
@Test
|
||||
public void fetchQuery() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.find(Order.class)
|
||||
.select("status")
|
||||
.fetchQuery("customer", "name")
|
||||
.findList();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(2);
|
||||
assertThat(trimSql(sql.get(0), 2)).contains("select t0.id, t0.status, t0.kcustomer_id from o_order t0");
|
||||
assertThat(trimSql(sql.get(1), 2)).contains("select t0.id, t0.name from o_customer t0 where t0.id");
|
||||
platformAssertIn(sql.get(1), " where t0.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetchLazy() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
DB.cacheManager().clearAll();
|
||||
|
||||
List<Order> orders = DB.find(Order.class)
|
||||
.select("status")
|
||||
.fetchLazy("customer", "name")
|
||||
.setMaxRows(10)
|
||||
.setUseCache(false)
|
||||
.findList();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(1);
|
||||
if (isSqlServer()) {
|
||||
assertThat(trimSql(sql.get(0), 2)).contains("select top 10 t0.id, t0.status, t0.kcustomer_id from o_order t0");
|
||||
} else {
|
||||
assertThat(trimSql(sql.get(0), 2)).contains("select t0.id, t0.status, t0.kcustomer_id from o_order t0");
|
||||
}
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// invoke lazy loading
|
||||
for (Order order : orders) {
|
||||
order.getCustomer().getName();
|
||||
}
|
||||
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertThat(trimSql(sql.get(0), 1)).contains("select t0.id, t0.name from o_customer t0 where t0.id");
|
||||
platformAssertIn(sql.get(0), " where t0.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetchIterate() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
try (QueryIterator<Order> orders =
|
||||
DB.find(Order.class).select("status")
|
||||
.setMaxRows(10)
|
||||
.setUseCache(false)
|
||||
.findIterate()) {
|
||||
|
||||
while (orders.hasNext()) {
|
||||
orders.next(); // dummy read
|
||||
}
|
||||
}
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(1);
|
||||
if (isSqlServer()) {
|
||||
assertThat(trimSql(sql.get(0), 2)).contains("select top 10 t0.id, t0.status from o_order t0");
|
||||
} else {
|
||||
assertThat(trimSql(sql.get(0), 2)).contains("select t0.id, t0.status from o_order t0");
|
||||
}
|
||||
|
||||
}
|
||||
@Test
|
||||
public void testSecQueryOneToMany() {
|
||||
|
||||
Order testOrder = ResetBasicData.createOrderCustAndOrder("testSecQry10");
|
||||
Integer custId = testOrder.getCustomer().getId();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.select("name")
|
||||
.fetchQuery("contacts")
|
||||
.setId(custId);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Customer cust = query.findOne();
|
||||
assertNotNull(cust);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
String generatedSql = query.getGeneratedSql();
|
||||
assertThat(generatedSql).contains("from o_customer t0 where t0.id = ?");
|
||||
|
||||
assertEquals(2, sql.size());
|
||||
assertSql(sql.get(1)).contains("from contact t0 where (t0.customer_id) ");
|
||||
platformAssertIn(sql.get(1), " where (t0.customer_id)");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testManyToOneWithManyPlusOneToMany() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status")
|
||||
.fetchQuery("customer", "name, status")
|
||||
.fetch("customer.contacts")
|
||||
.fetchQuery("details")
|
||||
.where().eq("status", Order.Status.NEW)
|
||||
.query();
|
||||
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<Order> list = query.findList();
|
||||
assertTrue(!list.isEmpty());
|
||||
|
||||
for (Order order : list) {
|
||||
order.getCustomer().getStatus();
|
||||
}
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
String generatedSql = sqlOf(query, 2);
|
||||
//select t0.id c0, t0.status c1, t0.kcustomer_id c2 from o_order t0 where t0.status = ? ; --bind(NEW)
|
||||
assertEquals("select t0.id, t0.status, t0.kcustomer_id from o_order t0 where t0.status = ?", generatedSql);
|
||||
|
||||
|
||||
//List<SpiQuery<?>> secondaryQueries = spiQuery.getLoggedSecondaryQueries();
|
||||
assertEquals(3, sql.size());
|
||||
|
||||
//SpiQuery<?> custSecondaryQuery =;
|
||||
String custSecondarySql = sql.get(1);
|
||||
|
||||
// select t0.id c0, t0.name c1, t0.status c2,
|
||||
// t1.id c3, t1.first_name c4, t1.last_name c5, t1.phone c6, t1.mobile c7, t1.email c8, t1.cretime c9, t1.updtime c10, t1.customer_id c11, t1.group_id c12
|
||||
// from o_customer t0
|
||||
// left join contact t1 on t1.customer_id = t0.id
|
||||
// where t0.id = ? order by t0.id; --bind(1)
|
||||
|
||||
assertThat(custSecondarySql).contains("from o_customer t0 ");
|
||||
assertThat(custSecondarySql).contains("left join contact t1 on t1.customer_id = t0.id ");
|
||||
assertThat(custSecondarySql).contains("where t0.id ");
|
||||
|
||||
String ordSecondarySql = sql.get(2);
|
||||
|
||||
// select ...
|
||||
// from o_order_detail t0
|
||||
// where (t0.order_id) in (?,?,?,?,?) ; --bind(1,4,1,1,1)
|
||||
|
||||
assertThat(ordSecondarySql).contains(" from o_order_detail t0 where t0.id > 0 and (t0.order_id) ");
|
||||
platformAssertIn(ordSecondarySql, "and (t0.order_id)");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user