#919 - io.ebean package initial

This commit is contained in:
Rob Bygrave
2016-12-11 23:23:22 +13:00
parent 5821dc96b9
commit 971e2dc91b
2134 changed files with 10721 additions and 8652 deletions
@@ -0,0 +1,264 @@
package org.tests.softdelete;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.Query;
import io.ebean.SqlQuery;
import io.ebean.SqlRow;
import org.tests.model.softdelete.EBasicSDChild;
import org.tests.model.softdelete.EBasicSoftDelete;
import org.avaje.ebeantest.LoggedSqlCollector;
import org.junit.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestSoftDeleteBasic extends BaseTestCase {
@Test
public void test() {
EBasicSoftDelete bean = new EBasicSoftDelete();
bean.setName("one");
Ebean.save(bean);
Ebean.delete(bean);
SqlQuery sqlQuery = Ebean.createSqlQuery("select * from ebasic_soft_delete where id=?");
sqlQuery.setParameter(1, bean.getId());
SqlRow sqlRow = sqlQuery.findUnique();
assertThat(sqlRow).isNotNull();
EBasicSoftDelete findNormal = Ebean.find(EBasicSoftDelete.class)
.setId(bean.getId())
.findUnique();
assertThat(findNormal).isNull();
EBasicSoftDelete findInclude = Ebean.find(EBasicSoftDelete.class)
.setId(bean.getId())
.setIncludeSoftDeletes()
.findUnique();
assertThat(findInclude).isNotNull();
}
@Test
public void testFindSoftDeleted() {
EBasicSoftDelete bean = new EBasicSoftDelete();
bean.setName("softDelFetch");
Ebean.save(bean);
Ebean.delete(bean);
Query<EBasicSoftDelete> query = Ebean.find(EBasicSoftDelete.class)
.setIncludeSoftDeletes()
.where()
.eq("deleted", true)
.eq("name", "softDelFetch")
.query();
List<EBasicSoftDelete> list = query.findList();
assertThat(query.getGeneratedSql()).contains("deleted = ?");
assertThat(list).hasSize(1);
}
@Test
public void testDeleteById_and_findRowCount() {
EBasicSoftDelete bean = new EBasicSoftDelete();
bean.setName("two");
Ebean.save(bean);
int rowCountBefore = Ebean.find(EBasicSoftDelete.class).findCount();
Ebean.delete(EBasicSoftDelete.class, bean.getId());
// -- test .findRowCount()
LoggedSqlCollector.start();
int rowCountAfter = Ebean.find(EBasicSoftDelete.class).findCount();
List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(loggedSql).hasSize(1);
assertThat(loggedSql.get(0)).contains("where t0.deleted =");
assertThat(rowCountAfter).isEqualTo(rowCountBefore - 1);
// -- test includeSoftDeletes().findRowCount()
LoggedSqlCollector.start();
int rowCountFull = Ebean.find(EBasicSoftDelete.class).setIncludeSoftDeletes().findCount();
assertThat(rowCountFull).isGreaterThan(rowCountAfter);
loggedSql = LoggedSqlCollector.stop();
assertThat(loggedSql).hasSize(1);
assertThat(loggedSql.get(0)).doesNotContain("where coalesce(t0.deleted,false)=false");
}
@Test
public void testDeletePartial() {
EBasicSoftDelete bean = new EBasicSoftDelete();
bean.setName("partial");
Ebean.save(bean);
// partially loaded bean without deleted state loaded
EBasicSoftDelete partial = Ebean.find(EBasicSoftDelete.class)
.select("id")
.setId(bean.getId())
.findUnique();
LoggedSqlCollector.start();
Ebean.delete(partial);
// check lazy loading isn't invoked (deleted set to true without invoking lazy loading)
List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(loggedSql).hasSize(2);
assertThat(loggedSql.get(0)).contains("update ebasic_sdchild set deleted=");
assertThat(loggedSql.get(1)).contains("update ebasic_soft_delete set deleted=? where id=?");
}
@Test
public void testCascadeSaveDelete() {
EBasicSoftDelete bean = new EBasicSoftDelete();
bean.setName("cascadeOne");
bean.addChild("child1", 10);
bean.addChild("child2", 20);
bean.addChild("child3", 30);
bean.addNoSoftDeleteChild("nsd1", 101);
bean.addNoSoftDeleteChild("nsd2", 102);
Ebean.save(bean);
LoggedSqlCollector.start();
Ebean.delete(bean);
List<String> loggedSql = LoggedSqlCollector.stop();
// The children without SoftDelete are left as is (so no third statement)
assertThat(loggedSql).hasSize(2);
// first statement is a single bulk update of the children with SoftDelete
assertThat(loggedSql.get(0)).contains("update ebasic_sdchild set deleted=");
assertThat(loggedSql.get(0)).contains("where owner_id = ?");
// second statement is the top level bean
assertThat(loggedSql.get(1)).contains("update ebasic_soft_delete set version=?, deleted=? where id=? and version=?");
}
@Test
public void testFetch() {
EBasicSoftDelete bean = new EBasicSoftDelete();
bean.setName("cascadeOne");
bean.addChild("child1", 10);
bean.addChild("child2", 20);
bean.addChild("child3", 30);
bean.addNoSoftDeleteChild("nsd1", 101);
bean.addNoSoftDeleteChild("nsd2", 102);
Ebean.save(bean);
Ebean.delete(bean.getChildren().get(1));
LoggedSqlCollector.start();
Query<EBasicSoftDelete> query1 =
Ebean.find(EBasicSoftDelete.class)
.fetch("children")
.where().eq("id", bean.getId())
.query();
List<EBasicSoftDelete> fetch1 = query1.findList();
String generatedSql = query1.getGeneratedSql();
// first statement is a single bulk update of the children with SoftDelete
assertThat(generatedSql).contains("t0.deleted = ");
assertThat(generatedSql).contains("t1.deleted = ");
assertThat(fetch1.get(0).getChildren()).hasSize(2);
assertThat(fetch1.get(0).getNosdChildren()).hasSize(2);
// fetch again using lazy loading
EBasicSoftDelete fetchWithLazy = Ebean.find(EBasicSoftDelete.class, bean.getId());
assertThat(fetchWithLazy.getChildren()).hasSize(2);
// fetch includeSoftDeletes using lazy loading
EBasicSoftDelete fetchAllWithLazy =
Ebean.find(EBasicSoftDelete.class)
.setId(bean.getId())
.where()
.setIncludeSoftDeletes()
.findUnique();
assertThat(fetchAllWithLazy.getChildren()).hasSize(3);
}
@Test
public void testWhenAllChildrenSoftDeleted() {
EBasicSoftDelete bean = new EBasicSoftDelete();
bean.setName("softDelChildren");
bean.addChild("child1", 10);
bean.addChild("child2", 20);
Ebean.save(bean);
Ebean.deleteAll(bean.getChildren());
Query<EBasicSoftDelete> query = Ebean.find(EBasicSoftDelete.class)
.setId(bean.getId())
.fetch("children");
EBasicSoftDelete found = query.findUnique();
String generatedSql = sqlOf(query);
if (isPlatformBooleanNative()) {
assertThat(generatedSql).contains("left join ebasic_sdchild t1 on t1.owner_id = t0.id and t1.deleted = false");
assertThat(generatedSql).contains("t0.deleted = false");
} else {
assertThat(generatedSql).contains("left join ebasic_sdchild t1 on t1.owner_id = t0.id and t1.deleted = 0");
assertThat(generatedSql).contains("t0.deleted = 0");
}
assertThat(found).isNotNull();
assertThat(found.getChildren()).hasSize(0);
}
@Test
public void testFetchWithIncludeSoftDeletes() {
EBasicSoftDelete bean = new EBasicSoftDelete();
bean.setName("fetchWithInclude");
bean.addChild("child1", 91);
bean.addChild("child2", 92);
Ebean.save(bean);
EBasicSDChild child0 = bean.getChildren().get(0);
EBasicSDChild upd = new EBasicSDChild();
upd.setId(child0.getId());
upd.setDeleted(true);
Ebean.update(upd);
Query<EBasicSoftDelete> query = Ebean.find(EBasicSoftDelete.class)
.fetch("children")
.setIncludeSoftDeletes()
.where().eq("name", "fetchWithInclude")
.query();
List<EBasicSoftDelete> top = query.findList();
assertThat(top).hasSize(1);
assertThat(top.get(0).getChildren()).hasSize(2);
}
}
@@ -0,0 +1,108 @@
package org.tests.softdelete;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.Query;
import org.tests.model.softdelete.ESoftDelBook;
import org.tests.model.softdelete.ESoftDelUser;
import org.junit.Test;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
public class TestSoftDeleteBook extends BaseTestCase {
@Test
public void test() {
// Create users
ESoftDelUser user1 = new ESoftDelUser("user1");
Ebean.save(user1);
ESoftDelUser user2 = new ESoftDelUser("user2");
Ebean.save(user2);
ESoftDelUser user3 = new ESoftDelUser("user3");
Ebean.save(user3);
// Create books
ESoftDelBook book1 = new ESoftDelBook("book1");
book1.setLendBy(user1);
book1.setLendBys(Arrays.asList(user2, user3));
Ebean.save(book1);
ESoftDelBook book2 = new ESoftDelBook("book2");
book2.setLendBy(user2);
book2.setLendBys(Arrays.asList(user1, user3));
Ebean.save(book2);
// check if everything is stored correctly in DB
book1 = Ebean.find(ESoftDelBook.class).where().eq("bookTitle", "book1").findUnique();
book2 = Ebean.find(ESoftDelBook.class).where().eq("bookTitle", "book2").findUnique();
assertThat(book1.getLendBys().size()).isEqualTo(2);
assertThat(book2.getLendBys().size()).isEqualTo(2);
assertThat(book1.getLendBy().getUserName()).isEqualTo("user1");
assertThat(book2.getLendBy().getUserName()).isEqualTo("user2");
// delete user 1
Ebean.delete(user1);
// check if everything is still stored correctly in DB (softdeletes included)
book1 = Ebean.find(ESoftDelBook.class).where().eq("bookTitle", "book1").setIncludeSoftDeletes().findUnique();
book2 = Ebean.find(ESoftDelBook.class).where().eq("bookTitle", "book2").setIncludeSoftDeletes().findUnique();
assertThat(book1.getLendBys().size()).isEqualTo(2);
assertThat(book2.getLendBys().size()).isEqualTo(2);
assertThat(book1.getLendBy().getUserName()).isEqualTo("user1");
assertThat(book2.getLendBy().getUserName()).isEqualTo("user2");
// check without softdeletes included
book1 = Ebean.find(ESoftDelBook.class).where().eq("bookTitle", "book1").findUnique();
book2 = Ebean.find(ESoftDelBook.class).where().eq("bookTitle", "book2").findUnique();
assertThat(book1.getLendBys().size()).isEqualTo(2); // user2 & user3
assertThat(book2.getLendBys().size()).isEqualTo(1); // user1 (deleted) & user3
assertThat(book2.getLendBy().getUserName()).isEqualTo("user2");
// expected behaviour:
// book1.getLendBy() == null
// current behaviour:
// book1.getLendBy() is a "dead" object. nearly every operation on the
// user object leads into an EntityNotFoundException
// lendBy is not null (as FK key value set, lendBy is non null reference bean)
ESoftDelUser lendBy = book1.getLendBy();
assertThat(lendBy.isDeleted()).isTrue();
}
@Test
public void test_fetch_whenAllManySoftDeleted() {
// Create users
ESoftDelUser user1 = new ESoftDelUser("user1");
Ebean.save(user1);
ESoftDelUser user2 = new ESoftDelUser("user2");
Ebean.save(user2);
// Create books
ESoftDelBook book1 = new ESoftDelBook("book3");
book1.setLendBy(user1);
book1.setLendBys(Arrays.asList(user1, user2));
Ebean.save(book1);
Ebean.delete(user1);
Ebean.delete(user2);
Query<ESoftDelBook> query = Ebean.find(ESoftDelBook.class)
.setId(book1.getId())
.fetch("lendBys");
ESoftDelBook found = query.findUnique();
assertThat(found).isNotNull();
}
}
@@ -0,0 +1,43 @@
package org.tests.softdelete;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import org.tests.model.softdelete.ESoftDelRole;
import org.tests.model.softdelete.ESoftDelUser;
import org.avaje.ebeantest.LoggedSqlCollector;
import org.junit.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestSoftDeleteManyToMany extends BaseTestCase {
@Test
public void test() {
ESoftDelRole role1 = new ESoftDelRole("role1");
ESoftDelRole role2 = new ESoftDelRole("role2");
Ebean.save(role1);
Ebean.save(role2);
ESoftDelUser user1 = new ESoftDelUser("user1");
user1.getRoles().add(role1);
user1.getRoles().add(role2);
Ebean.save(user1);
LoggedSqlCollector.start();
Ebean.delete(user1);
List<String> loggedSql = LoggedSqlCollector.stop();
// No Delete from the relationship table
assertThat(loggedSql).hasSize(1);
assertThat(loggedSql.get(0)).contains("update esoft_del_user set version=?, deleted=? where id=? and version=?;");
}
}
@@ -0,0 +1,28 @@
package org.tests.softdelete;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import org.tests.model.softdelete.ESoftDelMid;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class TestSoftDeleteOptionalRelationship extends BaseTestCase {
@Test
public void testFindWhenNullRelationship() {
ESoftDelMid mid1 = new ESoftDelMid(null, "mid1");
Ebean.save(mid1);
ESoftDelMid bean = Ebean.find(ESoftDelMid.class)
.setId(mid1.getId())
.fetch("top")
.findUnique();
assertThat(bean).isNotNull();
assertThat(bean.getTop()).isNull();
}
}
@@ -0,0 +1,92 @@
package org.tests.softdelete;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.Transaction;
import io.ebean.PersistBatch;
import org.tests.model.softdelete.ESoftDelMid;
import org.tests.model.softdelete.ESoftDelTop;
import org.tests.model.softdelete.ESoftDelUp;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class TestSoftDeleteTop extends BaseTestCase {
@Test
public void testDeletePermanent() {
ESoftDelUp up1 = new ESoftDelUp("up1");
Ebean.save(up1);
Ebean.delete(up1);
Ebean.deletePermanent(up1);
}
@Test
public void testSoftDeleteJdbcBatch() {
ESoftDelUp up1 = new ESoftDelUp("upBatch1");
Ebean.save(up1);
ESoftDelUp up2 = new ESoftDelUp("upBatch2");
Ebean.save(up2);
Transaction transaction = Ebean.beginTransaction();
try {
transaction.setBatch(PersistBatch.ALL);
Ebean.delete(up1);
Ebean.delete(up2);
transaction.commit();
} finally {
transaction.end();
}
List<ESoftDelUp> list = new ArrayList<>();
list.add(up1);
list.add(up2);
Ebean.deleteAllPermanent(list);
}
@Test
public void testSoftDeleteAll() {
ESoftDelUp up1 = new ESoftDelUp("upBatchX");
ESoftDelUp up2 = new ESoftDelUp("upBatchY");
List<ESoftDelUp> list = new ArrayList<>();
list.add(up1);
list.add(up2);
// by default uses JDBC for the 'all' methods
Ebean.saveAll(list);
Ebean.deleteAll(list);
Ebean.deleteAllPermanent(list);
}
@Test
public void test() {
ESoftDelUp up1 = new ESoftDelUp("up1");
ESoftDelTop top1 = new ESoftDelTop("top1");
ESoftDelMid mid1 = top1.addMids("mid1");
mid1.addDown("down1");
mid1.addDown("down2");
mid1.setUp(up1);
ESoftDelMid mid2 = top1.addMids("mid2");
mid2.addDown("down3");
mid2.addDown("down4");
Ebean.save(top1);
Ebean.delete(top1);
Ebean.deletePermanent(top1);
}
}