mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Update tests for better cross database execution (ordering of results etc)
This commit is contained in:
@@ -7,6 +7,7 @@ import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.OrderBy;
|
||||
|
||||
@Entity
|
||||
public class SelfRefExample {
|
||||
@@ -20,6 +21,7 @@ public class SelfRefExample {
|
||||
@ManyToOne
|
||||
private SelfRefExample parent;
|
||||
|
||||
@OrderBy("id")
|
||||
@OneToMany(mappedBy = "parent")
|
||||
private List<SelfRefExample> children;
|
||||
|
||||
|
||||
@@ -2,9 +2,11 @@ package com.avaje.tests.model.selfref;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
@@ -14,10 +16,7 @@ public class TestSelfRefExample extends BaseTestCase {
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
if (Ebean.find(SelfRefExample.class).findRowCount() > 0) {
|
||||
// skip test when running multiple times (non memory only db testing)
|
||||
return;
|
||||
}
|
||||
Ebean.createSqlUpdate("delete from self_ref_example").execute();
|
||||
|
||||
SelfRefExample e1 = new SelfRefExample("test1", null);
|
||||
SelfRefExample e2 = new SelfRefExample("test1", e1);
|
||||
@@ -27,7 +26,7 @@ public class TestSelfRefExample extends BaseTestCase {
|
||||
SelfRefExample e6 = new SelfRefExample("test2", e2);
|
||||
SelfRefExample e7 = new SelfRefExample("test3", e3);
|
||||
SelfRefExample e8 = new SelfRefExample("test3", e7);
|
||||
|
||||
|
||||
Ebean.save(e1);
|
||||
Ebean.save(e2);
|
||||
Ebean.save(e3);
|
||||
@@ -36,29 +35,47 @@ public class TestSelfRefExample extends BaseTestCase {
|
||||
Ebean.save(e6);
|
||||
Ebean.save(e7);
|
||||
Ebean.save(e8);
|
||||
|
||||
Query<SelfRefExample> examples = Ebean.createQuery(SelfRefExample.class);
|
||||
List<SelfRefExample> list = examples.where().eq("name", "test1").findList();
|
||||
|
||||
Assert.assertEquals(2, list.size());
|
||||
|
||||
|
||||
Query<SelfRefExample> examples = Ebean.find(SelfRefExample.class).setLazyLoadBatchSize(1);
|
||||
List<SelfRefExample> list = examples.where().eq("name", "test1").order().asc("id").findList();
|
||||
|
||||
assertThat(list).hasSize(2);
|
||||
|
||||
//The first Example is e1. e2 is the first child of e1. e3 is the first child of e2.
|
||||
SelfRefExample e3Example = list.get(0).getChildren().get(0).getChildren().get(0);
|
||||
//ID should be 3, since this has to be e3.
|
||||
Assert.assertEquals(e3.getId(), e3Example.getId());
|
||||
|
||||
|
||||
//Now that we have e3, it should also have a child: e7.
|
||||
Assert.assertEquals(1, e3Example.getChildren().size());
|
||||
Assert.assertEquals(e7.getId(), e3Example.getChildren().get(0).getId());
|
||||
|
||||
SelfRefExample e1Fetched = list.get(0);
|
||||
SelfRefExample e2Fetched = list.get(1);
|
||||
|
||||
assertThat(e1Fetched.getId()).isEqualTo(e1.getId());
|
||||
assertThat(e2Fetched.getId()).isEqualTo(e2.getId());
|
||||
|
||||
// Now we can't guarantee the order of loaded children so use findChild
|
||||
|
||||
List<SelfRefExample> e1Children = e1Fetched.getChildren();
|
||||
SelfRefExample e2Searched = findChild(e1Children, e2.getId());
|
||||
assertThat(e2Fetched.getChildren()).hasSize(2);
|
||||
assertThat(e2Fetched.getChildren()).extracting("id").contains(e3.getId(), e6.getId());
|
||||
|
||||
|
||||
SelfRefExample e3Searched = findChild(e2Searched.getChildren(), e3.getId());
|
||||
assertThat(e3Searched).isNotNull();
|
||||
assertThat(e3Searched.getChildren()).extracting("id").contains(e7.getId());
|
||||
|
||||
// If we get all the items, you can see the structure goes down a fair bit further.
|
||||
Query<SelfRefExample> examples2 = Ebean.createQuery(SelfRefExample.class).orderBy("id asc");
|
||||
List<SelfRefExample> list2 = examples2.findList();
|
||||
|
||||
Assert.assertEquals(e1.getId(), list2.get(0).getId());
|
||||
Assert.assertEquals(e2.getId(), list2.get(0).getChildren().get(0).getId());
|
||||
Assert.assertEquals(e3.getId(), list2.get(0).getChildren().get(0).getChildren().get(0).getId());
|
||||
Assert.assertEquals(e7.getId(), list2.get(0).getChildren().get(0).getChildren().get(0).getChildren().get(0).getId());
|
||||
|
||||
assertEquals(e1.getId(), list2.get(0).getId());
|
||||
assertEquals(e2.getId(), list2.get(0).getChildren().get(0).getId());
|
||||
assertEquals(e3.getId(), list2.get(0).getChildren().get(0).getChildren().get(0).getId());
|
||||
assertEquals(e7.getId(), list2.get(0).getChildren().get(0).getChildren().get(0).getChildren().get(0).getId());
|
||||
}
|
||||
|
||||
private SelfRefExample findChild(List<SelfRefExample> e1Children, Long id) {
|
||||
for (SelfRefExample e1Child : e1Children) {
|
||||
if (e1Child.getId().equals(id)) {
|
||||
return e1Child;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
.columnMapping("r.name", "name").create();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setUseCache(false).setUseQueryCache(false);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ilike("name", "r%");
|
||||
|
||||
@@ -52,6 +53,7 @@ public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
.create();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setUseCache(false).setUseQueryCache(false);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
int initialRowCount = query.findRowCount();
|
||||
@@ -84,6 +86,7 @@ public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
.create();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setUseCache(false).setUseQueryCache(false);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
int initialRowCount = query.findRowCount();
|
||||
|
||||
Reference in New Issue
Block a user