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,69 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.converstation.Conversation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestFetchPreference extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void fetchPreference_overrideOrder() {
|
||||
|
||||
Query<Conversation> query = DB.find(Conversation.class)
|
||||
// FetchPreference overrides so participants is joined and messages query joined
|
||||
.fetch("messages")
|
||||
.fetch("participants");
|
||||
|
||||
query.findList();
|
||||
String sql = sqlOf(query, 1);
|
||||
assertThat(sql).contains(" from c_conversation t0 left join c_participation t1 ");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void fetchPreference_sr() {
|
||||
|
||||
Query<Conversation> query = DB.find(Conversation.class)
|
||||
// without FetchPreference this ToMany would be our first ToMany join
|
||||
// and participants would be query joined
|
||||
.fetch("group.users")
|
||||
.fetch("messages")
|
||||
.fetch("participants");
|
||||
|
||||
query.findList();
|
||||
String sql = sqlOf(query, 1);
|
||||
|
||||
// join to group (the ToOne part only) and participants (our preferred ToMany path)
|
||||
assertThat(sql).contains(" from c_conversation t0 left join c_group t1 on t1.id = t0.group_id left join c_participation t2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetchPreference_inOrder() {
|
||||
|
||||
Query<Conversation> query = DB.find(Conversation.class)
|
||||
.fetch("participants")
|
||||
.fetch("messages");
|
||||
|
||||
query.findList();
|
||||
String sql = sqlOf(query, 1);
|
||||
assertThat(sql).contains(" from c_conversation t0 left join c_participation t1 ");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetchQuery_onlyOneJoinCandidate() {
|
||||
|
||||
Query<Conversation> query = DB.find(Conversation.class)
|
||||
.fetch("messages")
|
||||
.fetchQuery("participants");
|
||||
|
||||
query.findList();
|
||||
String sql = sqlOf(query, 1);
|
||||
|
||||
// participants is explicitly a "query join" so we can join to message
|
||||
assertThat(sql).contains(" from c_conversation t0 left join c_message t1 ");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.Truck;
|
||||
import org.tests.model.basic.Vehicle;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestFindIdsWithInheritance extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testQuery() {
|
||||
|
||||
Truck truck = new Truck();
|
||||
truck.setLicenseNumber("TK123");
|
||||
|
||||
DB.save(truck);
|
||||
|
||||
List<Integer> ids = DB.find(Vehicle.class).findIds();
|
||||
assertNotNull(ids);
|
||||
|
||||
DB.delete(truck);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.tests.model.basic.EBasic;
|
||||
import com.sun.management.HotSpotDiagnosticMXBean;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
import java.io.File;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@SuppressWarnings("restriction")
|
||||
public class TestFindIterateHeapDump extends BaseTestCase {
|
||||
|
||||
// All that heap dumping code from :
|
||||
// https://blogs.oracle.com/sundararajan/entry/programmatically_dumping_heap_from_java
|
||||
private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
|
||||
|
||||
/**
|
||||
* field to store the hotspot diagnostic MBean
|
||||
*/
|
||||
private static volatile HotSpotDiagnosticMXBean hotspotMBean;
|
||||
|
||||
/**
|
||||
* Ignore this test generally - run it manually against MySQL specifically for Issue #56.
|
||||
*/
|
||||
@Disabled
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
// Transaction transaction = server.beginTransaction();
|
||||
// try {
|
||||
// transaction.setBatchMode(true);
|
||||
// transaction.setBatchSize(20);
|
||||
// for (int i = 0; i < 20000; i++) {
|
||||
// EBasic dumbModel = new EBasic();
|
||||
// dumbModel.setName("Goodbye now");
|
||||
// server.save(dumbModel);
|
||||
// }
|
||||
// transaction.commit();
|
||||
//
|
||||
// } finally {
|
||||
// transaction.end();
|
||||
// }
|
||||
//
|
||||
// if (true) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// Intentionally not iterating through the iterator to
|
||||
|
||||
final AtomicInteger counter = new AtomicInteger();
|
||||
|
||||
server.find(EBasic.class)
|
||||
.findEach(bean -> {
|
||||
|
||||
int count = counter.incrementAndGet();
|
||||
if (count == 1) {
|
||||
dumpHeap("heap-dump13-initial.snapshot", true);
|
||||
}
|
||||
});
|
||||
|
||||
// try {
|
||||
// while (iterate.hasNext()) {
|
||||
// EBasic eBasic = iterate.next();
|
||||
// eBasic.getDescription();
|
||||
// }
|
||||
// } finally {
|
||||
// iterate.close();
|
||||
// }
|
||||
|
||||
String fileName = "heap-dump13.snapshot";
|
||||
|
||||
File file = new File(fileName);
|
||||
if (file.exists())
|
||||
file.delete();
|
||||
|
||||
dumpHeap(fileName, true);
|
||||
}
|
||||
|
||||
static void dumpHeap(String fileName, boolean live) {
|
||||
// initialize hotspot diagnostic MBean
|
||||
initHotspotMBean();
|
||||
try {
|
||||
hotspotMBean.dumpHeap(fileName, live);
|
||||
} catch (RuntimeException re) {
|
||||
throw re;
|
||||
} catch (Exception exp) {
|
||||
throw new RuntimeException(exp);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* initialize the hotspot diagnostic MBean field
|
||||
*/
|
||||
private static void initHotspotMBean() {
|
||||
if (hotspotMBean == null) {
|
||||
synchronized (TestFindIterateHeapDump.class) {
|
||||
if (hotspotMBean == null) {
|
||||
hotspotMBean = getHotspotMBean();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get the hotspot diagnostic MBean from the ManagementFactory.
|
||||
*/
|
||||
private static HotSpotDiagnosticMXBean getHotspotMBean() {
|
||||
try {
|
||||
final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
|
||||
return ManagementFactory.newPlatformMXBeanProxy(server, HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
|
||||
} catch (RuntimeException re) {
|
||||
throw re;
|
||||
} catch (Exception exp) {
|
||||
throw new RuntimeException(exp);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.BeanState;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestFindPartialWithConstructorSetFields extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Order> list = DB.find(Order.class)
|
||||
.setLazyLoadBatchSize(100)
|
||||
.setUseCache(false)
|
||||
.select("shipDate")
|
||||
.findList();
|
||||
|
||||
assertTrue(list.size() > 2);
|
||||
|
||||
// assert first bean is partially loaded
|
||||
Order order0 = list.get(0);
|
||||
BeanState beanState = DB.beanState(order0);
|
||||
Set<String> loadedProps = beanState.loadedProps();
|
||||
assertTrue(loadedProps.contains("shipDate"));
|
||||
assertFalse(loadedProps.contains("status"));
|
||||
assertFalse(loadedProps.contains("orderDate"));
|
||||
|
||||
// invoke lazy loading
|
||||
order0.getStatus();
|
||||
order0.getOrderDate();
|
||||
|
||||
// assert that these beans are fully loaded
|
||||
for (int i = 1; i < list.size(); i++) {
|
||||
Order order1 = list.get(1);
|
||||
beanState = DB.beanState(order1);
|
||||
loadedProps = beanState.loadedProps();
|
||||
assertNull(loadedProps);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.TransactionalTestCase;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.MNonEnum;
|
||||
import org.tests.model.basic.MNonUpdPropEntity;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestFindWhereUsingEnumerated extends TransactionalTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
MNonUpdPropEntity e = new MNonUpdPropEntity();
|
||||
e.setNonEnum(MNonEnum.END);
|
||||
e.setName("TheNonEnumIsEnd");
|
||||
e.setNote("note");
|
||||
|
||||
DB.save(e);
|
||||
|
||||
MNonUpdPropEntity unique = DB.find(MNonUpdPropEntity.class).where()
|
||||
.eq("nonEnum", MNonEnum.END)
|
||||
.eq("name", "TheNonEnumIsEnd")
|
||||
.findOne();
|
||||
|
||||
assertThat(unique).isNotNull();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.ExpressionList;
|
||||
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.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestFormulaWithFindCount extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testFindCount() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
|
||||
ExpressionList<Order> ex = server.find(Order.class).select("id, status ,totalAmount").where().gt("totalAmount", 1d);
|
||||
List<Order> list = ex.findList();
|
||||
|
||||
for (Order order : list) {
|
||||
Double amount = order.getTotalAmount();
|
||||
assertNotNull(amount);
|
||||
}
|
||||
|
||||
ExpressionList<Order> expressionList = server.find(Order.class).where().gt("totalAmount", 1d);
|
||||
int rowCount = expressionList.findCount();
|
||||
assertEquals(list.size(), rowCount);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.TransactionalTestCase;
|
||||
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestLikeEscaping extends TransactionalTestCase {
|
||||
|
||||
|
||||
@Test
|
||||
public void testLikeEscaping() {
|
||||
// Mysql, PgSql, H2 special chars: "_" "%"
|
||||
// MsSql special chars: "_" "%" "[" AND different Quoting!
|
||||
DB.save(ResetBasicData.createCustomer("Paul % Percentage", "*Star", "[none]", 0, null));
|
||||
DB.save(ResetBasicData.createCustomer("(none)", "More * Star", "[none]", 0, null));
|
||||
|
||||
DB.save(ResetBasicData.createCustomer("Paul %% Doublepercentage", "|Pipeway", "[other]", 1, null));
|
||||
DB.save(ResetBasicData.createCustomer("_Udo Underscore", "|Pipeway", "[other]", 1, null));
|
||||
|
||||
DB.save(ResetBasicData.createCustomer("Bodo \\ backslash", "\\BS", "[other]", 1, null));
|
||||
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().contains("name", "Paul %%").findCount()
|
||||
).isEqualTo(1);
|
||||
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().contains("name", "o \\ b").findCount()
|
||||
).isEqualTo(1);
|
||||
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().contains("name", "o \\\\ b").findCount()
|
||||
).isEqualTo(0);
|
||||
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().startsWith("name", "_").findCount()
|
||||
).isEqualTo(1);
|
||||
|
||||
if (isPlatformCaseSensitive()) {
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().startsWith("name", "_u").findCount()
|
||||
).isEqualTo(0);
|
||||
}
|
||||
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().istartsWith("name", "_U").findCount()
|
||||
).isEqualTo(1);
|
||||
|
||||
if (isPlatformCaseSensitive()) {
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().startsWith("shippingAddress.line1", "|p").findCount()
|
||||
).isEqualTo(0);
|
||||
}
|
||||
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().startsWith("shippingAddress.line1", "|P").findCount()
|
||||
).isEqualTo(2);
|
||||
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().startsWith("shippingAddress.line1", "\\B").findCount()
|
||||
).isEqualTo(1);
|
||||
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().endsWith("billingAddress.line1", "]").findCount()
|
||||
).isEqualTo(5);
|
||||
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().endsWith("billingAddress.line1", "[none]").findCount()
|
||||
).isEqualTo(2);
|
||||
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().startsWith("billingAddress.line1", "[none]").findCount()
|
||||
).isEqualTo(2);
|
||||
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().contains("billingAddress.line1", "[none]").findCount()
|
||||
).isEqualTo(2);
|
||||
|
||||
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().contains("shippingAddress.line1", "*").findCount()
|
||||
).isEqualTo(2);
|
||||
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().startsWith("shippingAddress.line1", "*").findCount()
|
||||
).isEqualTo(1);
|
||||
|
||||
assertThat(DB.find(Customer.class)
|
||||
.where().endsWith("shippingAddress.line1", "*").findCount()
|
||||
).isEqualTo(0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.api.SpiQuery;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderDetail;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestManyLazyLoadingQuery extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer) DB.getDefault();
|
||||
|
||||
BeanDescriptor<Order> descOrder = server.descriptor(Order.class);
|
||||
BeanPropertyAssocMany<?> beanProperty = (BeanPropertyAssocMany<?>) descOrder.beanProperty("details");
|
||||
|
||||
List<Object> parentIds = new ArrayList<>();
|
||||
parentIds.add(1);
|
||||
|
||||
|
||||
List<Order> orders =
|
||||
DB.find(Order.class)
|
||||
.where().lt("id", 4)
|
||||
.findList();
|
||||
|
||||
for (Order order : orders) {
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
System.out.println(details.size());
|
||||
}
|
||||
|
||||
|
||||
// start transaction to keep PC going to lazy query
|
||||
DB.beginTransaction();
|
||||
try {
|
||||
DB.find(Order.class, 1);
|
||||
|
||||
SpiQuery<?> query0 = (SpiQuery<?>) DB.find(OrderDetail.class);
|
||||
|
||||
query0.setLazyLoadForParents(beanProperty);
|
||||
|
||||
beanProperty.addWhereParentIdIn(query0, parentIds, false);
|
||||
|
||||
query0.findList();
|
||||
assertThat(query0.getGeneratedSql()).contains(" from o_order_detail t0 where (t0.order_id) ");
|
||||
platformAssertIn(query0.getGeneratedSql(), "where (t0.order_id)");
|
||||
|
||||
} finally {
|
||||
DB.endTransaction();
|
||||
}
|
||||
|
||||
List<OrderDetail> details = DB.find(OrderDetail.class)
|
||||
.where().eq("order.id", 1)
|
||||
.findList();
|
||||
|
||||
for (OrderDetail orderDetail : details) {
|
||||
System.out.println(orderDetail);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.MRole;
|
||||
import org.tests.model.basic.MUser;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestManyToManyLazyLoading extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
createData();
|
||||
|
||||
List<MUser> users = DB.find(MUser.class).findList();
|
||||
|
||||
for (MUser user : users) {
|
||||
List<MRole> roles = user.getRoles();
|
||||
roles.size();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void createData() {
|
||||
MRole r0 = new MRole("r0");
|
||||
MRole r1 = new MRole("r1");
|
||||
|
||||
DB.save(r0);
|
||||
DB.save(r1);
|
||||
|
||||
MUser u0 = new MUser("usr0");
|
||||
u0.addRole(r0);
|
||||
u0.addRole(r1);
|
||||
|
||||
DB.save(u0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.map.MpRole;
|
||||
import org.tests.model.map.MpUser;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
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 TestOneToManyAsMap extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
MpUser u = new MpUser();
|
||||
DB.save(u);
|
||||
|
||||
MpUser u2 = DB.find(MpUser.class, u.getId());
|
||||
assertNotNull(u2);
|
||||
|
||||
u2.setName("Charlie Brown");
|
||||
MpRole ourl = new MpRole();
|
||||
ourl.setOrganizationId(47L);
|
||||
u2.getRoles().put("one", ourl);
|
||||
DB.save(u2);
|
||||
|
||||
MpUser u3 = DB.find(MpUser.class, u.getId());
|
||||
assertEquals("Charlie Brown", u3.getName());
|
||||
|
||||
Map<String, MpRole> listMap = u3.getRoles();
|
||||
assertEquals(1, listMap.size());
|
||||
|
||||
MpRole mpRole = listMap.get("one");
|
||||
assertNotNull(mpRole);
|
||||
assertEquals(Long.valueOf(47L), mpRole.getOrganizationId());
|
||||
assertEquals("one", mpRole.getCode());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void json() {
|
||||
|
||||
Map<String,MpRole> roles = new LinkedHashMap<>();
|
||||
roles.put("r1", newRole(1L, "r1"));
|
||||
roles.put("r2", newRole(2L, "r2"));
|
||||
|
||||
MpUser u = new MpUser();
|
||||
u.setName("myName");
|
||||
u.setRoles(roles);
|
||||
|
||||
final String asJson = DB.json().toJson(u);
|
||||
|
||||
assertThat(asJson).isEqualTo("{\"name\":\"myName\",\"roles\":[{\"id\":1,\"code\":\"r1\"},{\"id\":2,\"code\":\"r2\"}]}");
|
||||
|
||||
final MpUser bean = DB.json().toBean(MpUser.class, asJson);
|
||||
assertThat(bean.getName()).isEqualTo("myName");
|
||||
final Map<String, MpRole> beanRoles = bean.getRoles();
|
||||
assertThat(beanRoles.get("r1").getId()).isEqualTo(1L);
|
||||
assertThat(beanRoles.get("r1").getCode()).isEqualTo("r1");
|
||||
assertThat(beanRoles.get("r2").getId()).isEqualTo(2L);
|
||||
assertThat(beanRoles.get("r2").getCode()).isEqualTo("r2");
|
||||
}
|
||||
|
||||
private MpRole newRole(Long id, String code) {
|
||||
MpRole role = new MpRole();
|
||||
role.setId(id);
|
||||
role.setCode(code);
|
||||
return role;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.converstation.Conversation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestQueryConversationRowCount extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
//"find conversation where club = :clubId and ( ( isPublic = :false and participants.user.id = :userId ) or isPublic = :true ) order by createdAt desc ";
|
||||
//"find conversation where groupId = :groupId and ( ( open = :false and participants.user.id = :userId ) or open = :true ) order by whenCreated desc ";
|
||||
|
||||
Long groupId = 1L;
|
||||
Long userId = 1L;
|
||||
|
||||
Query<Conversation> query = DB.find(Conversation.class)
|
||||
.where().eq("group.id", groupId)
|
||||
.disjunction()
|
||||
.conjunction()
|
||||
.eq("open", false).eq("participants.user.id", userId)
|
||||
.endJunction()
|
||||
.eq("open", true)
|
||||
.endJunction()
|
||||
.order("whenCreated desc").query();
|
||||
|
||||
query.findList();
|
||||
String generatedSql = sqlOf(query, 1);
|
||||
|
||||
// select distinct t0.id c0, t0.title c1, t0.open c2, t0.version c3, t0.when_created c4, t0.when_updated c5, t0.group_id c6, t0.when_created
|
||||
// from c_conversation t0
|
||||
// left join c_participation u1 on u1.conversation_id = t0.id
|
||||
// where t0.group_id = ? and ((t0.open = ? and u1.user_id = ? ) or t0.open = ? )
|
||||
// order by t0.when_created desc;
|
||||
|
||||
if (isPostgres()) {
|
||||
assertThat(generatedSql).contains("select distinct on (t0.when_created, t0.id) t0.id, t0.title, t0.isopen");
|
||||
|
||||
} else {
|
||||
assertThat(generatedSql).contains("select distinct t0.id, t0.title, t0.isopen");
|
||||
}
|
||||
assertThat(generatedSql).contains("left join c_participation u1 on u1.conversation_id = t0.id");
|
||||
assertThat(generatedSql).contains("where t0.group_id = ? and ((t0.isopen = ? and u1.user_id = ?) or t0.isopen = ?)");
|
||||
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
query.findCount();
|
||||
|
||||
// select count(*) from (
|
||||
// select distinct t0.id c0
|
||||
// from c_conversation t0
|
||||
// left join c_participation u1 on u1.conversation_id = t0.id
|
||||
// where t0.group_id = ? and ((t0.open = ? and u1.user_id = ? ) or t0.open = ? )
|
||||
// ); --bind(1,true,1,true)
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertEquals(1, loggedSql.size());
|
||||
|
||||
String countSql = trimSql(loggedSql.get(0), 0);
|
||||
assertThat(countSql).contains("select count(*) from ( select distinct t0.id from c_conversation t0 left join c_participation u1 on u1.conversation_id = t0.id where t0.group_id = ? and ((t0.isopen = ? and u1.user_id = ?) or t0.isopen = ?))");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.bean.EntityBeanIntercept;
|
||||
import org.tests.model.basic.Customer;
|
||||
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 TestQueryDistinct extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.setDistinct(true)
|
||||
.select("name");
|
||||
|
||||
List<Customer> customers = query.findList();
|
||||
|
||||
String generatedSql = sqlOf(query);
|
||||
assertThat(generatedSql).contains("select distinct t0.name from o_customer t0");
|
||||
|
||||
for (Customer customer : customers) {
|
||||
|
||||
EntityBeanIntercept ebi = ((EntityBean) customer)._ebean_getIntercept();
|
||||
assertTrue(ebi.isDisableLazyLoad());
|
||||
assertNull(ebi.getPersistenceContext());
|
||||
|
||||
// lazy loading disabled
|
||||
assertNull(customer.getId());
|
||||
assertNull(customer.getAnniversary());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_onWhere() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.where().setDistinct(true)
|
||||
.select("name");
|
||||
|
||||
query.findList();
|
||||
|
||||
String generatedSql = sqlOf(query);
|
||||
assertThat(generatedSql).contains("select distinct t0.name from o_customer t0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDistinctStatus() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.setDistinct(true)
|
||||
.select("status")
|
||||
.where().isNotNull("status").query();
|
||||
|
||||
List<Customer> customers = query.findList();
|
||||
|
||||
String generatedSql = sqlOf(query);
|
||||
assertThat(generatedSql).contains("select distinct t0.status from o_customer t0");
|
||||
|
||||
for (Customer customer : customers) {
|
||||
|
||||
assertNotNull(customer.getStatus());
|
||||
|
||||
// lazy loading disabled
|
||||
assertNull(customer.getId());
|
||||
assertNull(customer.getAnniversary());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPagingQuery_expect_doesNotAddOrderBy() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.setMaxRows(10)
|
||||
.setDistinct(true)
|
||||
.select("name");
|
||||
|
||||
query.findList();
|
||||
|
||||
if (isH2() || isPostgres()) {
|
||||
String generatedSql = sqlOf(query);
|
||||
assertThat(generatedSql).contains("select distinct t0.name from o_customer t0 limit 10");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.tests.model.basic.Order;
|
||||
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;
|
||||
|
||||
public class TestQueryRawExpressionMany extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Integer quantity = 1;
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.where().raw("details.orderQty = ?", quantity)
|
||||
.query();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
query.findCount();
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(trimSql(sql.get(0), 1)).contains("select count(*) from ( select distinct t0.id from o_order t0 left join o_order_detail t1 on t1.order_id = t0.id and t1.id > 0 where t1.order_qty = ?)");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
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;
|
||||
|
||||
public class TestQueryRowCountWithMany extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Long productId = 1L;
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.fetch("details")
|
||||
.where().eq("details.product.id", productId)
|
||||
.order("cretime asc").query();
|
||||
|
||||
List<Order> list = query.findList();
|
||||
|
||||
// select distinct t0.id c0, t0.status c1, t0.order_date c2, t0.ship_date c3, t2.name c4, t0.cretime c5, t0.updtime c6, t0.kcustomer_id c7,
|
||||
// t1.id c8, t1.order_qty c9, t1.ship_qty c10, t1.unit_price c11, t1.cretime c12, t1.updtime c13, t1.order_id c14, t1.product_id c15, t0.cretime, t0.id, t1.id, t1.order_qty, t1.cretime
|
||||
// from o_order t0
|
||||
// join o_customer t2 on t2.id = t0.kcustomer_id
|
||||
// left join o_order_detail t1 on t1.order_id = t0.id
|
||||
// join o_order_detail u1 on u1.order_id = t0.id
|
||||
// where t1.id > 0 and u1.product_id = ?
|
||||
// order by t0.cretime, t0.id, t1.id asc, t1.order_qty asc, t1.cretime desc; --bind(1)
|
||||
|
||||
String generatedSql = sqlOf(query, 1);
|
||||
if (isPostgres()) {
|
||||
assertThat(generatedSql).contains("select distinct on (t0.cretime, t0.id, t1.id, t1.order_qty, t1.cretime) t0.id, t0.status,"); // need the distinct
|
||||
|
||||
} else {
|
||||
assertThat(generatedSql).contains("select distinct t0.id, t0.status,"); // need the distinct
|
||||
}
|
||||
assertThat(generatedSql).contains("left join o_order_detail t1 on t1.order_id = t0.id"); //fetch join
|
||||
assertThat(generatedSql).contains("join o_order_detail u1 on u1.order_id = t0.id"); //predicate join
|
||||
assertThat(generatedSql).contains(" u1.product_id = ?"); // u1 as predicate alias
|
||||
assertThat(generatedSql).contains(" order by t0.cretime");
|
||||
|
||||
|
||||
int rowCount = query.findCount();
|
||||
|
||||
// select count(*) from o_order t0
|
||||
// left join o_order_detail t1 on t1.order_id = t0.id
|
||||
// where t1.product_id = ? ; --bind(1)
|
||||
|
||||
// select count(*) from (
|
||||
// select distinct t0.id c0 from o_order t0 join o_order_detail u1 on u1.order_id = t0.id where
|
||||
// u1.product_id = ?
|
||||
// ); --bind(1)
|
||||
|
||||
List<String> sqlLogged = LoggedSqlCollector.stop();
|
||||
|
||||
assertEquals(list.size(), rowCount);
|
||||
assertEquals(2, sqlLogged.size());
|
||||
assertThat(trimSql(sqlLogged.get(1), 1)).contains(
|
||||
"select count(*) from ( select distinct t0.id from o_order t0 join o_order_detail u1 on u1.order_id = t0.id and u1.id > 0 where u1.product_id = ?)");
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testWithFirstRowsMaxRows() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Long productId = 1L;
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.fetch("details")
|
||||
.where().eq("details.product.id", productId)
|
||||
.setFirstRow(2)
|
||||
.setMaxRows(20)
|
||||
.orderBy("cretime asc")
|
||||
.query();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
query.findCount();
|
||||
|
||||
List<String> sqlLogged = LoggedSqlCollector.stop();
|
||||
|
||||
assertEquals(1, sqlLogged.size());
|
||||
assertThat(trimSql(sqlLogged.get(0), 1)).contains("select count(*) from ( select distinct t0.id from o_order t0 join o_order_detail u1 on u1.order_id = t0.id and u1.id > 0 where u1.product_id = ?)");
|
||||
|
||||
query.findList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,799 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.CountDistinctOrder;
|
||||
import io.ebean.CountedValue;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.inherit.ChildA;
|
||||
import org.tests.inherit.Data;
|
||||
import org.tests.inherit.EUncle;
|
||||
import org.tests.lazyforeignkeys.MainEntity;
|
||||
import org.tests.lazyforeignkeys.MainEntityRelation;
|
||||
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.tests.model.basic.VwCustomer;
|
||||
import org.tests.o2m.OmBasicParent;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestQuerySingleAttribute extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void findSingleAttributesTwoToMany() {
|
||||
ResetBasicData.reset();
|
||||
// Query without ors with equals causing joins, only one Customer with name Rob exists
|
||||
Query<Customer> query0 = DB.find(Customer.class)
|
||||
.select("name")
|
||||
.setCountDistinct(CountDistinctOrder.COUNT_DESC_ATTR_ASC)
|
||||
.where()
|
||||
.eq("name", "Rob")
|
||||
.query();
|
||||
|
||||
CountedValue<String> robs0 = (CountedValue<String>) query0.findSingleAttributeList().get(0);
|
||||
assertThat(robs0.getValue()).isEqualTo("Rob");
|
||||
assertThat(robs0.getCount()).isEqualTo(1);
|
||||
|
||||
// Query with or with equals causing joins
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.select("name")
|
||||
.setCountDistinct(CountDistinctOrder.COUNT_DESC_ATTR_ASC)
|
||||
.where()
|
||||
.eq("name", "Rob")
|
||||
.or()
|
||||
.eq("orders.status", Order.Status.NEW)
|
||||
.eq("contacts.firstName", "Fred1")
|
||||
.endOr()
|
||||
.query();
|
||||
|
||||
CountedValue<String> robs = (CountedValue<String>) query.findSingleAttributeList().get(0);
|
||||
assertThat(robs.getValue()).isEqualTo("Rob");
|
||||
// only one Customer named rob exists, but 7 is returned for the amount of Customers named Rob
|
||||
assertThat(robs.getCount()).isEqualTo(1);
|
||||
|
||||
assertThat(sqlOf(query)).contains("select r1.attribute_, count(*) " +
|
||||
"from (select distinct t0.id, t0.name as attribute_ " +
|
||||
"from o_customer t0 left join contact u1 on u1.customer_id = t0.id left join o_order u2 on u2.kcustomer_id = t0.id and u2.order_date is not null " +
|
||||
"where t0.name = ? and (u2.status = ? or u1.first_name = ?)) r1 " +
|
||||
"group by r1.attribute_ " +
|
||||
"order by count(*) desc, r1.attribute_");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exampleUsage() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<String> names =
|
||||
DB.find(Customer.class)
|
||||
.setDistinct(true)
|
||||
.select("name")
|
||||
.where().eq("status", Customer.Status.NEW).startsWith("name", "R")
|
||||
.order().asc("name")
|
||||
.setMaxRows(100)
|
||||
.findSingleAttributeList();
|
||||
|
||||
assertThat(names).isNotNull();
|
||||
for (String name : names) {
|
||||
assertThat(name).startsWith("R");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exampleUsage_otherType() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Date> dates =
|
||||
DB.find(Customer.class)
|
||||
.setDistinct(true)
|
||||
.select("anniversary")
|
||||
.where().isNotNull("anniversary")
|
||||
.order().asc("anniversary")
|
||||
.findSingleAttributeList();
|
||||
|
||||
assertThat(dates).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withOrderBy() {
|
||||
|
||||
Query<Customer> query =
|
||||
DB.find(Customer.class)
|
||||
.setDistinct(true)
|
||||
.select("name")
|
||||
.where().eq("status", Customer.Status.NEW)
|
||||
.orderBy().asc("name");
|
||||
|
||||
query.findSingleAttributeList();
|
||||
assertThat(sqlOf(query)).contains("select distinct t0.name from o_customer t0 where t0.status = ? order by t0.name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basic() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class).select("name");
|
||||
|
||||
List<String> names = query.findSingleAttributeList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select t0.name from o_customer t0");
|
||||
assertThat(names).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleAttribute() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.select("name")
|
||||
.where().eq("id", 1).query();
|
||||
|
||||
String name = query.findSingleAttribute();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select t0.name from o_customer t0");
|
||||
assertThat(name).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleAttributeList_with_join_column() {
|
||||
ResetBasicData.reset();
|
||||
Query<MainEntityRelation> query = DB.find(MainEntityRelation.class)
|
||||
.fetch("entity1", "attr1")
|
||||
.setDistinct(true)
|
||||
.setCountDistinct(CountDistinctOrder.COUNT_DESC_ATTR_ASC)
|
||||
.where().query();
|
||||
|
||||
List<CountedValue<String>> attr1list = query.findSingleAttributeList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select r1.attribute_, count(*)"
|
||||
+ " from (select distinct t0.id, t0.id1, t1.attr1 as attribute_ from main_entity_relation t0 left join main_entity t1 on t1.id = t0.id1) r1"
|
||||
+ " group by r1.attribute_"
|
||||
+ " order by count(*) desc, r1.attribute_"); // sub-query select clause includes t0.id1
|
||||
assertThat(attr1list).isNotNull();
|
||||
assertThat(attr1list).hasSize(2);
|
||||
assertThat(attr1list.get(0).getValue()).isEqualTo("a1");
|
||||
assertThat(attr1list.get(0).getCount()).isEqualTo(2l);
|
||||
assertThat(attr1list.get(1).getValue()).isEqualTo("a2");
|
||||
assertThat(attr1list.get(1).getCount()).isEqualTo(1l);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleAttributesVariousSelection1() {
|
||||
Query<MainEntityRelation> query = DB.find(MainEntityRelation.class)
|
||||
.fetch("entity1", "attr1")
|
||||
.setCountDistinct(CountDistinctOrder.COUNT_DESC_ATTR_ASC)
|
||||
.where().query();
|
||||
query.findSingleAttributeList();
|
||||
assertThat(sqlOf(query)).contains("select r1.attribute_, count(*)"
|
||||
+ " from (select t0.id1, t1.attr1 as attribute_ from main_entity_relation t0 left join main_entity t1 on t1.id = t0.id1) r1"
|
||||
+ " group by r1.attribute_"
|
||||
+ " order by count(*) desc, r1.attribute_"); // sub-query select clause includes t0.id1
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleAttributesVariousSelection2() {
|
||||
Query<MainEntityRelation> query = DB.find(MainEntityRelation.class)
|
||||
.select("attr1")
|
||||
.setCountDistinct(CountDistinctOrder.COUNT_DESC_ATTR_ASC)
|
||||
.where().query();
|
||||
query.findSingleAttributeList();
|
||||
assertThat(sqlOf(query)).contains("select r1.attribute_, count(*)"
|
||||
+ " from (select t0.attr1 as attribute_ from main_entity_relation t0) r1"
|
||||
+ " group by r1.attribute_"
|
||||
+ " order by count(*) desc, r1.attribute_");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleAttributesVariousSelection3() {
|
||||
Query<MainEntityRelation> query = DB.find(MainEntityRelation.class)
|
||||
.select("id")
|
||||
.setCountDistinct(CountDistinctOrder.COUNT_DESC_ATTR_ASC)
|
||||
.where().query();
|
||||
query.findSingleAttributeList();
|
||||
assertThat(sqlOf(query)).contains("select r1.attribute_, count(*)"
|
||||
+ " from (select t0.id as attribute_ from main_entity_relation t0) r1"
|
||||
+ " group by r1.attribute_"
|
||||
+ " order by count(*) desc, r1.attribute_");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleAttributesVariousSelection4() {
|
||||
Query<MainEntityRelation> query = DB.find(MainEntityRelation.class)
|
||||
.fetch("entity1", "id")
|
||||
.setCountDistinct(CountDistinctOrder.COUNT_DESC_ATTR_ASC)
|
||||
.where().query();
|
||||
query.findSingleAttributeList();
|
||||
assertThat(sqlOf(query)).contains("select r1.attribute_, count(*)"
|
||||
+ " from (select t0.id1, t1.id as attribute_ from main_entity_relation t0 left join main_entity t1 on t1.id = t0.id1) r1"
|
||||
+ " group by r1.attribute_"
|
||||
+ " order by count(*) desc, r1.attribute_"); // sub-query select clause includes t0.id1,
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleAttributesVariousSelection5() {
|
||||
Query<OmBasicParent> query = DB.find(OmBasicParent.class)
|
||||
.fetch("children", "name")
|
||||
.setCountDistinct(CountDistinctOrder.COUNT_DESC_ATTR_ASC)
|
||||
.where().query();
|
||||
query.findSingleAttributeList();
|
||||
assertThat(sqlOf(query)).contains("select r1.attribute_, count(*)"
|
||||
+ " from (select t1.name as attribute_ from om_basic_parent t0 left join om_basic_child t1 on t1.parent_id = t0.id) r1 "
|
||||
+ "group by r1.attribute_ order by count(*) desc, r1.attribute_"); // sub-query select clause includes t1.id,
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleAttribute_with_aggregate() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.select("max(name)")
|
||||
.where().gt("id", 1).query();
|
||||
|
||||
String name = query.findSingleAttribute();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select max(t0.name) from o_customer t0");
|
||||
assertThat(name).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleAttribute_viaExpression() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String name = DB.find(Customer.class)
|
||||
.select("name")
|
||||
.where().eq("id", 1)
|
||||
.findSingleAttribute();
|
||||
|
||||
assertThat(name).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctAndWhere() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.setDistinct(true)
|
||||
.select("name")
|
||||
.where().eq("status", Customer.Status.NEW)
|
||||
.query();
|
||||
|
||||
List<String> names = query.findSingleAttributeList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select distinct t0.name from o_customer t0 where t0.status = ?");
|
||||
assertThat(names).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctWhereWithJoin() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.setDistinct(true)
|
||||
.select("name")
|
||||
.where().eq("status", Customer.Status.NEW)
|
||||
.istartsWith("billingAddress.city", "auck")
|
||||
.query();
|
||||
|
||||
List<String> names = query.findSingleAttributeList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select distinct t0.name from o_customer t0 left join o_address t1 on t1.id = t0.billing_address_id where t0.status = ? and lower(t1.city) like ");
|
||||
assertThat(names).isNotNull();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void queryPlan_expect_differentPlans() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class).select("name");
|
||||
query.findSingleAttributeList();
|
||||
assertThat(sqlOf(query)).contains("select t0.name from o_customer t0");
|
||||
|
||||
Query<Customer> query2 = DB.find(Customer.class).select("name");
|
||||
query2.findList();
|
||||
assertThat(sqlOf(query2, 1)).contains("select t0.id, t0.name from o_customer t0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctOnIdProperty() {
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.setDistinct(true)
|
||||
.select("id")
|
||||
.setMaxRows(100);
|
||||
|
||||
List<String> ids = query.findSingleAttributeList();
|
||||
if (isSqlServer()) {
|
||||
assertThat(sqlOf(query)).contains("select distinct top 100 t0.id from o_customer t0");
|
||||
} else if (isOracle()) {
|
||||
assertThat(sqlOf(query)).contains("select distinct t0.id from o_customer t0 fetch next 100 rows only");
|
||||
} else {
|
||||
assertThat(sqlOf(query)).contains("select distinct t0.id from o_customer t0 limit 100");
|
||||
}
|
||||
assertThat(ids).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctWithFetch() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.setDistinct(true)
|
||||
.fetch("billingAddress", "city");
|
||||
|
||||
List<String> cities = query.findSingleAttributeList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select distinct t1.city from o_customer t0 left join o_address t1 on t1.id = t0.billing_address_id");
|
||||
assertThat(cities).contains("Auckland").containsNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctWithCascadedFetch() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Contact> query = DB.find(Contact.class)
|
||||
.setDistinct(true)
|
||||
.fetch("customer.billingAddress", "city");
|
||||
|
||||
List<String> cities = query.findSingleAttributeList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select distinct t2.city from contact t0 join o_customer t1 on t1.id = t0.customer_id left join o_address t2 on t2.id = t1.billing_address_id");
|
||||
assertThat(cities).contains("Auckland").containsNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctSelectOnInheritedBean() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<ChildA> query = DB.find(ChildA.class)
|
||||
.setDistinct(true)
|
||||
.select("more");
|
||||
|
||||
query.findSingleAttributeList();
|
||||
assertThat(sqlOf(query)).contains("select distinct t0.more from rawinherit_parent t0 where t0.type = 'A'");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctFetchManyToOneInheritedBean() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<EUncle> query = DB.find(EUncle.class)
|
||||
.setDistinct(true)
|
||||
.fetch("parent", "more");
|
||||
|
||||
query.findSingleAttributeList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select distinct t1.more from rawinherit_uncle t0 join rawinherit_parent t1 on t1.id = t0.parent_id");
|
||||
|
||||
}
|
||||
|
||||
// hmm - same problem when not using distinct
|
||||
@Test
|
||||
public void findSingleOnIdProperty() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.select("id")
|
||||
.setMaxRows(100);
|
||||
|
||||
List<String> ids = query.findSingleAttributeList();
|
||||
if (isSqlServer()) {
|
||||
assertThat(sqlOf(query)).contains("select top 100 t0.id from o_customer t0");
|
||||
} else if (isOracle()) {
|
||||
assertThat(sqlOf(query)).contains("fetch next 100 rows only");
|
||||
} else {
|
||||
assertThat(sqlOf(query)).contains("select t0.id from o_customer t0 limit 100");
|
||||
}
|
||||
assertThat(ids).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleWithFetch() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.fetch("billingAddress", "city");
|
||||
|
||||
List<String> cities = query.findSingleAttributeList();
|
||||
|
||||
assertThat(cities).contains("Auckland").containsNull();
|
||||
assertThat(sqlOf(query)).contains("select t1.city from o_customer t0 left join o_address t1 on t1.id = t0.billing_address_id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleWithFetchOnView() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<VwCustomer> query = DB.find(VwCustomer.class)
|
||||
.fetch("billingAddress", "city");
|
||||
|
||||
List<String> cities = query.findSingleAttributeList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select t1.city from o_customer t0 left join o_address t1 on t1.id = t0.billing_address_id");
|
||||
assertThat(cities).contains("Auckland").containsNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleSelectOnInheritedBean() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<ChildA> query = DB.find(ChildA.class)
|
||||
.select("more");
|
||||
|
||||
query.findSingleAttributeList();
|
||||
assertThat(sqlOf(query)).contains("select t0.more from rawinherit_parent t0 where t0.type = 'A'");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleFetchManyToOneInheritedBean() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<EUncle> query = DB.find(EUncle.class)
|
||||
.fetch("parent", "more");
|
||||
|
||||
query.findSingleAttributeList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select t1.more from rawinherit_uncle t0 join rawinherit_parent t1 on t1.id = t0.parent_id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleFetchManyToOneInheritedBean_viaEbeanServer() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<EUncle> query = DB.find(EUncle.class)
|
||||
.fetch("parent", "more");
|
||||
|
||||
query.findSingleAttributeList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select t1.more from rawinherit_uncle t0 join rawinherit_parent t1 on t1.id = t0.parent_id");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled //don't know if ebean can handle this on many to many, as this means that the cartesian product is generated
|
||||
public void distinctFetchManyToManyInheritedBean() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Data> query = DB.find(Data.class)
|
||||
.setDistinct(true)
|
||||
.fetch("parents", "more")
|
||||
.setMaxRows(100);
|
||||
|
||||
query.findSingleAttributeList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select distinct t0.more from rawinherit_data t0 "
|
||||
+ "join rawinherit_parent_rawinherit_data t1 on t0.id = t1.rawinherit_data_id "
|
||||
+ "join parent t2 on t1.rawinherit_parent_id = t2.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctWithOrderByPk() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Contact> query = DB.find(Contact.class)
|
||||
.setDistinct(true)
|
||||
.select("customer")
|
||||
.order().desc("customer");
|
||||
|
||||
query.findSingleAttributeList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select distinct t0.customer_id from contact t0 order by t0.customer_id desc");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctWithOrderByPkWithId() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Contact> query = DB.find(Contact.class)
|
||||
.setDistinct(true)
|
||||
.select("customer.id")
|
||||
.order().desc("customer.id");
|
||||
|
||||
query.findSingleAttributeList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select distinct t0.customer_id from contact t0 order by t0.customer_id desc");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctWithCascadedFetchOrderByPk() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Contact> query = DB.find(Contact.class)
|
||||
.setDistinct(true)
|
||||
.fetch("customer", "billingAddress")
|
||||
.order().desc("customer.billingAddress");
|
||||
|
||||
query.findSingleAttributeList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 join o_customer t1 on t1.id = t0.customer_id order by t1.billing_address_id desc");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctWithOrderByPkAndQuery() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Contact> query = DB.find(Contact.class)
|
||||
.setDistinct(true)
|
||||
.fetch("customer", "billingAddress")
|
||||
.where().eq("customer.billingAddress.city", "Auckland")
|
||||
.order().desc("customer.billingAddress.id");
|
||||
|
||||
List<Integer> ids = query.findSingleAttributeList();
|
||||
assertThat(ids).isNotEmpty();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 "
|
||||
+ "join o_customer t1 on t1.id = t0.customer_id "
|
||||
+ "left join o_address t2 on t2.id = t1.billing_address_id "
|
||||
+ "where t2.city = ? "
|
||||
+ "order by t1.billing_address_id desc");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctWithCascadedFetchOrderByPkAndQuery() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Contact> query = DB.find(Contact.class)
|
||||
.setDistinct(true)
|
||||
.fetch("customer", "billingAddress")
|
||||
.where().eq("customer.billingAddress.city", "Auckland")
|
||||
.order().desc("customer.billingAddress.id");
|
||||
|
||||
List<Short> ids = query.findSingleAttributeList();
|
||||
assertThat(ids).isNotEmpty();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 "
|
||||
+ "join o_customer t1 on t1.id = t0.customer_id "
|
||||
+ "left join o_address t2 on t2.id = t1.billing_address_id "
|
||||
+ "where t2.city = ? "
|
||||
+ "order by t1.billing_address_id desc");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctWithCascadedFetchOrderByPkAndQuery3() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Contact> query = DB.find(Contact.class)
|
||||
.setDistinct(true)
|
||||
.fetch("customer", "billingAddress")
|
||||
.where().eq("customer.billingAddress.city", "Auckland")
|
||||
.order().desc("customer.billingAddress.id");
|
||||
|
||||
List<Integer> ids = query.findSingleAttributeList();
|
||||
assertThat(ids).isNotEmpty();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 "
|
||||
+ "join o_customer t1 on t1.id = t0.customer_id "
|
||||
+ "left join o_address t2 on t2.id = t1.billing_address_id "
|
||||
+ "where t2.city = ? "
|
||||
+ "order by t1.billing_address");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctWithCascadedFetchOrderByPkAndQuery2() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Contact> query = DB.find(Contact.class)
|
||||
.setDistinct(true)
|
||||
.fetch("customer", "billingAddress")
|
||||
.where().eq("customer.shippingAddress.city", "Auckland") // query on shippingAddress
|
||||
.order().desc("customer.billingAddress.id");
|
||||
|
||||
List<Short> ids = query.findSingleAttributeList();
|
||||
assertThat(ids).isNotEmpty();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 "
|
||||
+ "join o_customer t1 on t1.id = t0.customer_id "
|
||||
+ "left join o_address t2 on t2.id = t1.shipping_address_id "
|
||||
+ "where t2.city = ? "
|
||||
+ "order by t1.billing_address_id desc");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctWithCascadedFetchCount() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
|
||||
int count = DB.find(Contact.class).findCount();
|
||||
assertThat(count).isGreaterThanOrEqualTo(12);
|
||||
|
||||
Query<Contact> query = DB.find(Contact.class)
|
||||
.select("firstName");
|
||||
|
||||
List<CountedValue<Object>> list1 = query
|
||||
.setCountDistinct(CountDistinctOrder.ATTR_ASC)
|
||||
|
||||
.findSingleAttributeList();
|
||||
assertThat(sqlOf(query)).contains("select r1.attribute_, count(*) from ("
|
||||
+ "select t0.first_name as attribute_ from contact t0"
|
||||
+ ") r1 group by r1.attribute_ order by r1.attribute_");
|
||||
assertThat(list1.get(0)).isInstanceOf(CountedValue.class);
|
||||
// FIXME: These asserts will fail, because other tests will interfere. see #1298
|
||||
//assertThat(list1.toString()).isEqualTo("[3: Bugs1, 1: Fiona, 3: Fred1, 1: Jack, 3: Jim1, 1: Tracy]");
|
||||
|
||||
|
||||
query = DB.find(Contact.class).select("firstName");
|
||||
list1 = query
|
||||
.setCountDistinct(CountDistinctOrder.ATTR_DESC)
|
||||
.findSingleAttributeList();
|
||||
assertThat(list1.get(0)).isInstanceOf(CountedValue.class);
|
||||
//assertThat(list1.toString()).isEqualTo("[1: Tracy, 3: Jim1, 1: Jack, 3: Fred1, 1: Fiona, 3: Bugs1]");
|
||||
|
||||
query = DB.find(Contact.class).select("firstName");
|
||||
list1 = query
|
||||
.setCountDistinct(CountDistinctOrder.COUNT_ASC_ATTR_DESC)
|
||||
.findSingleAttributeList();
|
||||
assertThat(list1.get(0)).isInstanceOf(CountedValue.class);
|
||||
//assertThat(list1.toString()).isEqualTo("[1: Tracy, 1: Jack, 1: Fiona, 3: Jim1, 3: Fred1, 3: Bugs1]");
|
||||
|
||||
query = DB.find(Contact.class).fetch("customer.shippingAddress", "line1");//("firstName")
|
||||
List<CountedValue<Object>> list2 = query
|
||||
.setCountDistinct(CountDistinctOrder.ATTR_ASC)
|
||||
.findSingleAttributeList();
|
||||
assertThat(sqlOf(query)).contains("select r1.attribute_, count(*) from ("
|
||||
+ "select t2.line_1 as attribute_ "
|
||||
+ "from contact t0 join o_customer t1 on t1.id = t0.customer_id "
|
||||
+ "left join o_address t2 on t2.id = t1.shipping_address_id"
|
||||
+ ") r1 group by r1.attribute_ order by r1.attribute_");
|
||||
assertThat(list2.get(0)).isInstanceOf(CountedValue.class);
|
||||
//assertThat(list2.toString()).isEqualTo("[1: null, 3: 1 Banana St, 5: 12 Apple St, 3: 15 Kumera Way]");
|
||||
|
||||
|
||||
query = DB.find(Contact.class).select("firstName")
|
||||
.where().eq("customer.shippingAddress.line1", "12 Apple St").query();
|
||||
List<CountedValue<Object>> list3 = query
|
||||
.setCountDistinct(CountDistinctOrder.ATTR_ASC)
|
||||
.findSingleAttributeList();
|
||||
assertThat(sqlOf(query)).contains("select r1.attribute_, count(*) from ("
|
||||
+ "select t0.first_name as attribute_ from contact t0 "
|
||||
+ "join o_customer t1 on t1.id = t0.customer_id "
|
||||
+ "left join o_address t2 on t2.id = t1.shipping_address_id where t2.line_1 = ?"
|
||||
+ ") r1 group by r1.attribute_ order by r1.attribute_");
|
||||
assertThat(list3.get(0)).isInstanceOf(CountedValue.class);
|
||||
//assertThat(list3.toString()).isEqualTo("[1: Bugs1, 1: Fiona, 1: Fred1, 1: Jim1, 1: Tracy]");
|
||||
|
||||
|
||||
query = DB.find(Contact.class).fetch("customer.billingAddress", "line1")
|
||||
.where().or()
|
||||
.ne("customer.shippingAddress.line1", "12 Apple St")
|
||||
.isNull("customer.shippingAddress.line1")
|
||||
.endOr().query();
|
||||
List<CountedValue<Object>> list4 = query
|
||||
.setCountDistinct(CountDistinctOrder.ATTR_ASC)
|
||||
.findSingleAttributeList();
|
||||
assertThat(sqlOf(query)).contains("select r1.attribute_, count(*) from ("
|
||||
+ "select t2.line_1 as attribute_ "
|
||||
+ "from contact t0 join o_customer t1 on t1.id = t0.customer_id "
|
||||
+ "left join o_address t2 on t2.id = t1.billing_address_id "
|
||||
+ "left join o_address t3 on t3.id = t1.shipping_address_id "
|
||||
+ "where (t3.line_1 <> ? or t3.line_1 is null)"
|
||||
+ ") r1 group by r1.attribute_ order by r1.attribute_");
|
||||
assertThat(list4.get(0)).isInstanceOf(CountedValue.class);
|
||||
//assertThat(list4.toString()).isEqualTo("[1: null, 3: Bos town, 3: P.O.Box 1234]");
|
||||
|
||||
|
||||
// Test Limiter for MSSQL
|
||||
query = DB.find(Contact.class).fetch("customer.billingAddress", "line1").setFirstRow(1).setMaxRows(2);
|
||||
List<CountedValue<Object>> list5 = query
|
||||
.where().isNotNull("customer.billingAddress.line1").query()
|
||||
.setCountDistinct(CountDistinctOrder.ATTR_DESC)
|
||||
.findSingleAttributeList();
|
||||
|
||||
if (isOracle()) {
|
||||
assertSql(query).contains("select r1.attribute_, count(*) from (select t2.line_1 as attribute_ from contact t0 join o_customer t1 on t1.id = t0.customer_id left join o_address t2 on t2.id = t1.billing_address_id where t2.line_1 is not null) r1 group by r1.attribute_ order by r1.attribute_ desc offset 1 rows fetch next 2 rows only");
|
||||
} else {
|
||||
assertSql(query).contains("select r1.attribute_, count(*) from ("
|
||||
+ "select t2.line_1 as attribute_ from contact t0 "
|
||||
+ "join o_customer t1 on t1.id = t0.customer_id "
|
||||
+ "left join o_address t2 on t2.id = t1.billing_address_id "
|
||||
+ "where t2.line_1 is not null"
|
||||
+ ") r1 group by r1.attribute_ order by r1.attribute_ desc ");
|
||||
}
|
||||
if (isSqlServer()) {
|
||||
assertThat(sqlOf(query)).endsWith(" fetch next 2 rows only");
|
||||
} else if (isDb2()) {
|
||||
assertSql(query).endsWith("FETCH FIRST 2 ROWS ONLY");
|
||||
} else if (isOracle()) {
|
||||
assertSql(query).contains(" offset 1 rows fetch next 2 rows only");
|
||||
} else {
|
||||
assertThat(sqlOf(query)).endsWith(" limit 2 offset 1");
|
||||
}
|
||||
assertThat(list5.get(0)).isInstanceOf(CountedValue.class);
|
||||
//assertThat(list5.toString()).isEqualTo("[3: P.O.Box 1234, 3: Bos town]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void example() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<CountedValue<Order.Status>> orderStatusCount =
|
||||
|
||||
DB.find(Order.class)
|
||||
.select("status")
|
||||
.where()
|
||||
.gt("orderDate", LocalDate.now().minusMonths(3))
|
||||
.setCountDistinct(CountDistinctOrder.COUNT_DESC_ATTR_ASC)
|
||||
.findSingleAttributeList();
|
||||
|
||||
for (CountedValue<Order.Status> entry : orderStatusCount) {
|
||||
System.out.println(" count:" + entry.getCount()+" orderStatus:" + entry.getValue() );
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
MainEntity e1 = new MainEntity();
|
||||
e1.setId("1");
|
||||
e1.setAttr1("a1");
|
||||
DB.save(e1);
|
||||
|
||||
MainEntity e2 = new MainEntity();
|
||||
e2.setId("2");
|
||||
e2.setAttr1("a2");
|
||||
DB.save(e2);
|
||||
|
||||
MainEntity e3 = new MainEntity();
|
||||
e3.setId("3");
|
||||
e3.setAttr1("a1");
|
||||
DB.save(e3);
|
||||
|
||||
MainEntityRelation rel = new MainEntityRelation();
|
||||
rel.setEntity1(e1);
|
||||
rel.setEntity2(e1);
|
||||
DB.save(rel);
|
||||
|
||||
rel = new MainEntityRelation();
|
||||
rel.setEntity1(e2);
|
||||
rel.setEntity2(e2);
|
||||
DB.save(rel);
|
||||
|
||||
rel = new MainEntityRelation();
|
||||
rel.setEntity1(e3);
|
||||
rel.setEntity2(e3);
|
||||
DB.save(rel);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void cleanup() {
|
||||
DB.find(MainEntityRelation.class).delete();
|
||||
DB.find(MainEntity.class).delete();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.selfref.SelfParent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestSelfParent extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
if (DB.find(SelfParent.class).findCount() > 0) {
|
||||
// only run once
|
||||
return;
|
||||
}
|
||||
|
||||
SelfParent root = new SelfParent("root", null);
|
||||
SelfParent child1 = new SelfParent("child1", root);
|
||||
SelfParent child11 = new SelfParent("child11", child1);
|
||||
SelfParent child111 = new SelfParent("child111", child11);
|
||||
SelfParent child12 = new SelfParent("child12", child1);
|
||||
|
||||
SelfParent child2 = new SelfParent("child2", root);
|
||||
SelfParent child21 = new SelfParent("child21", child2);
|
||||
SelfParent child22 = new SelfParent("child22", child2);
|
||||
|
||||
DB.save(root);
|
||||
DB.save(child1);
|
||||
DB.save(child11);
|
||||
DB.save(child111);
|
||||
DB.save(child12);
|
||||
DB.save(child2);
|
||||
DB.save(child21);
|
||||
DB.save(child22);
|
||||
|
||||
List<SelfParent> roots = DB.find(SelfParent.class).where().eq("parent", null).findList();
|
||||
|
||||
assertEquals(1, roots.size());
|
||||
|
||||
printNode(roots.get(0));
|
||||
}
|
||||
|
||||
public static void printNode(SelfParent o) {
|
||||
//System.out.println(o.getName());
|
||||
for (SelfParent c : o.getChildren()) {
|
||||
printNode(c);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Expression;
|
||||
import io.ebean.Query;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestSubQueryBinding extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
Query<Customer> someCustomerIds = server.find(Customer.class).select("id").where().lt("id", 5).query();
|
||||
|
||||
Expression someCustIdsExpression = server.expressionFactory().in("id", someCustomerIds);
|
||||
|
||||
Query<Customer> query = server.find(Customer.class).where().like("name", "Rob%").not(someCustIdsExpression).query();
|
||||
|
||||
query.findList();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
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.assertTrue;
|
||||
|
||||
public class TestWhereAnnotation extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void fetchEager_inFirstQuery() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.find(Customer.class)
|
||||
.fetch("orders")
|
||||
.findList();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
|
||||
assertEquals(1, loggedSql.size());
|
||||
|
||||
String sql = loggedSql.get(0);
|
||||
assertTrue(sql.contains("t1.order_date is not null"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetchLazy_inLazyLoadQuery() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
DB.cacheManager().clearAll();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<Customer> customers = DB.find(Customer.class)
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
|
||||
List<Order> orders = customers.get(0).getOrders();
|
||||
assertThat(orders.size()).isGreaterThan(0);
|
||||
orders.get(0).getStatus();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(loggedSql).hasSize(2);
|
||||
assertThat(loggedSql.get(1)).contains("t0.order_date is not null");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package org.tests.query.other;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.EBasic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestWhereLikeWithSlash extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testWithSlash() {
|
||||
|
||||
EBasic basic = new EBasic("slash\\monkey");
|
||||
DB.save(basic);
|
||||
|
||||
Query<EBasic> query = DB.find(EBasic.class).where().startsWith("name", "slash\\mon").query();
|
||||
List<EBasic> list = query.findList();
|
||||
|
||||
assertEquals(1, list.size());
|
||||
|
||||
Query<EBasic> query1 = DB.find(EBasic.class).where().like("name", "slash\\mon%").query();
|
||||
List<EBasic> list1 = query1.findList();
|
||||
|
||||
if (!isMySql() && !isNuoDb() && !isMariaDB()) {
|
||||
// For mysql this assert depends on no_backslash_escapes setting so we won't assert here
|
||||
// Still good on Postgres which was the original issue
|
||||
assertEquals(1, list1.size());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithPipe() {
|
||||
|
||||
EBasic basic = new EBasic("bash|monkey");
|
||||
DB.save(basic);
|
||||
|
||||
Query<EBasic> query = DB.find(EBasic.class).where().startsWith("name", "bash|mo").query();
|
||||
List<EBasic> list = query.findList();
|
||||
|
||||
assertEquals(1, list.size());
|
||||
|
||||
Query<EBasic> query1 = DB.find(EBasic.class).where().like("name", "bash|mon%").query();
|
||||
List<EBasic> list1 = query1.findList();
|
||||
|
||||
// This doesn't work in the latest version of H2 so disable for now.
|
||||
// Still good on Postgres which was the original issue
|
||||
assertEquals(1, list1.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithOpenSquare() {
|
||||
|
||||
EBasic basic = new EBasic("mash[monkey");
|
||||
DB.save(basic);
|
||||
|
||||
Query<EBasic> query = DB.find(EBasic.class).where().startsWith("name", "mash[mo").query();
|
||||
List<EBasic> list = query.findList();
|
||||
|
||||
assertEquals(1, list.size());
|
||||
|
||||
List<EBasic> list1;
|
||||
if (isSqlServer()) {
|
||||
list1 = DB.find(EBasic.class).where().like("name", "mash[[]mon%").query().findList();
|
||||
|
||||
} else {
|
||||
list1 = DB.find(EBasic.class).where().like("name", "mash[mon%").query().findList();
|
||||
}
|
||||
|
||||
// This doesn't work in the latest version of H2 so disable for now.
|
||||
// Still good on Postgres which was the original issue
|
||||
assertEquals(1, list1.size());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user