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,522 @@
|
||||
package org.tests.softdelete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.SqlQuery;
|
||||
import io.ebean.SqlRow;
|
||||
import io.ebean.Transaction;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.softdelete.EBasicNoSDChild;
|
||||
import org.tests.model.softdelete.EBasicSDChild;
|
||||
import org.tests.model.softdelete.EBasicSoftDelete;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestSoftDeleteBasic extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testCascadeSaveDelete_other() {
|
||||
|
||||
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);
|
||||
|
||||
DB.save(bean);
|
||||
|
||||
assertThat(bean.getVersion()).isEqualTo(1);
|
||||
assertThat(bean.getChildren().get(0).getVersion()).isEqualTo(1);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.delete(bean);
|
||||
|
||||
// List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
//
|
||||
// assertEquals(new Long(2), bean.getVersion());
|
||||
// assertEquals(new Long(2), bean.getChildren().get(0).getVersion()); // Fails with 1
|
||||
//
|
||||
// // 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 findSingleAttribute() {
|
||||
|
||||
EBasicSoftDelete bean = new EBasicSoftDelete();
|
||||
bean.setName("findSingleAttribute");
|
||||
DB.save(bean);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
final String name0 = DB.find(EBasicSoftDelete.class)
|
||||
.select("name")
|
||||
.where().eq("name", "findSingleAttribute")
|
||||
.findSingleAttribute();
|
||||
|
||||
List<String> sql0 = LoggedSqlCollector.current();
|
||||
assertThat(sql0.get(0)).contains("where t0.name = ? and t0.deleted =");
|
||||
assertThat(name0).isEqualTo("findSingleAttribute");
|
||||
|
||||
// now soft delete the bean
|
||||
DB.delete(bean);
|
||||
List<String> sqlUpdate = LoggedSqlCollector.current();
|
||||
assertThat(sqlUpdate.get(0)).contains("update ebasic_sdchild set");
|
||||
|
||||
// use setIncludeSoftDeletes
|
||||
final String name1 = DB.find(EBasicSoftDelete.class)
|
||||
.select("name")
|
||||
.where().eq("name", "findSingleAttribute")
|
||||
.setIncludeSoftDeletes()
|
||||
.findSingleAttribute();
|
||||
|
||||
List<String> sql1 = LoggedSqlCollector.current();
|
||||
assertThat(sql1.get(0)).doesNotContain(" and t0.deleted =");
|
||||
assertThat(name1).isEqualTo("findSingleAttribute");
|
||||
|
||||
|
||||
// not using setIncludeSoftDeletes, so don't find it
|
||||
final String name2 = DB.find(EBasicSoftDelete.class)
|
||||
.select("name")
|
||||
.where().eq("name", "findSingleAttribute")
|
||||
.findSingleAttribute();
|
||||
|
||||
List<String> sql2 = LoggedSqlCollector.stop();
|
||||
assertThat(sql2.get(0)).contains(" and t0.deleted =");
|
||||
assertThat(name2).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindIdsWhenIncludeSoftDeletedChlld() {
|
||||
|
||||
EBasicSoftDelete bean = new EBasicSoftDelete();
|
||||
bean.setName("softDelChildren");
|
||||
bean.addChild("child1", 10);
|
||||
bean.addChild("child2", 20);
|
||||
bean.addChild("child3", 30);
|
||||
|
||||
DB.save(bean);
|
||||
DB.delete(bean.getChildren().get(0));
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<Object> ids = DB.find(EBasicSDChild.class).where().eq("owner", bean).findIds();
|
||||
assertThat(ids).hasSize(2);
|
||||
|
||||
List<EBasicSDChild> beans = DB.find(EBasicSDChild.class).where().eq("owner", bean).findList();
|
||||
assertThat(beans).hasSize(2);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(2);
|
||||
assertSql(sql.get(0)).contains("from ebasic_sdchild t0 where t0.owner_id = ? and t0.deleted = ");
|
||||
assertSql(sql.get(1)).contains("from ebasic_sdchild t0 where t0.owner_id = ? and t0.deleted = ");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EBasicSoftDelete bean = new EBasicSoftDelete();
|
||||
bean.setName("one");
|
||||
DB.save(bean);
|
||||
|
||||
DB.delete(bean);
|
||||
|
||||
SqlQuery sqlQuery = DB.sqlQuery("select * from ebasic_soft_delete where id=?");
|
||||
sqlQuery.setParameter(bean.getId());
|
||||
SqlRow sqlRow = sqlQuery.findOne();
|
||||
assertThat(sqlRow).isNotNull();
|
||||
|
||||
EBasicSoftDelete findNormal = DB.find(EBasicSoftDelete.class)
|
||||
.setId(bean.getId())
|
||||
.findOne();
|
||||
|
||||
assertThat(findNormal).isNull();
|
||||
|
||||
EBasicSoftDelete findInclude = DB.find(EBasicSoftDelete.class)
|
||||
.setId(bean.getId())
|
||||
.setIncludeSoftDeletes()
|
||||
.findOne();
|
||||
|
||||
assertThat(findInclude).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindSoftDeleted() {
|
||||
|
||||
EBasicSoftDelete bean = new EBasicSoftDelete();
|
||||
bean.setName("softDelFetch");
|
||||
DB.save(bean);
|
||||
|
||||
DB.delete(bean);
|
||||
|
||||
Query<EBasicSoftDelete> query = DB.find(EBasicSoftDelete.class)
|
||||
.setIncludeSoftDeletes()
|
||||
.where()
|
||||
.eq("deleted", true)
|
||||
.eq("name", "softDelFetch")
|
||||
.query();
|
||||
|
||||
List<EBasicSoftDelete> list = query.findList();
|
||||
assertSql(query).contains("deleted = ?");
|
||||
assertThat(list).hasSize(1);
|
||||
|
||||
// Cleanup created entity
|
||||
query.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindChild_joinParent() {
|
||||
EBasicSoftDelete bean = new EBasicSoftDelete();
|
||||
bean.setName("softDelParent_withChild");
|
||||
bean.addChild("child1", 10);
|
||||
|
||||
DB.save(bean);
|
||||
|
||||
Query<EBasicSDChild> query = DB.find(EBasicSDChild.class)
|
||||
.where()
|
||||
.eq("owner.name", "softDelParent_withChild")
|
||||
.query();
|
||||
|
||||
List<EBasicSDChild> list = query.findList();
|
||||
assertSql(query).contains("join ebasic_soft_delete t1 on t1.id = t0.owner_id and t1.deleted =");
|
||||
assertThat(list).hasSize(1);
|
||||
|
||||
// Cleanup created entity
|
||||
DB.deletePermanent(bean);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindSoftDeletedList() {
|
||||
EBasicSoftDelete bean = new EBasicSoftDelete();
|
||||
bean.setName("softDelFetch");
|
||||
DB.save(bean);
|
||||
|
||||
EBasicSDChild bean2 = new EBasicSDChild(bean, "softDelFetchus", 1L);
|
||||
DB.save(bean2);
|
||||
DB.delete(bean2);
|
||||
|
||||
EBasicSDChild child = DB
|
||||
.find(EBasicSDChild.class)
|
||||
.setIncludeSoftDeletes()
|
||||
.where()
|
||||
.idEq(bean2.getId())
|
||||
.findOne();
|
||||
assertThat(child).isNotNull();
|
||||
assertThat(child.getOwner().getChildren().size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNotFindSoftDeleted() {
|
||||
EBasicSoftDelete bean = new EBasicSoftDelete();
|
||||
bean.setName("Shouldn't be found with child expression when child is deleted");
|
||||
DB.save(bean);
|
||||
final EBasicSDChild child = new EBasicSDChild(bean, "Delete me, don't find me", 1L);
|
||||
DB.save(child);
|
||||
DB.delete(child);
|
||||
|
||||
Query<EBasicSoftDelete> query = DB.find(EBasicSoftDelete.class)
|
||||
.where()
|
||||
.eq("id", bean.getId())
|
||||
.eq("children.amount", 1L)
|
||||
.query();
|
||||
|
||||
List<EBasicSoftDelete> list = query.findList();
|
||||
if (isMySql() || isMariaDB() || isSqlServer()) {
|
||||
assertSql(query).contains("t0.deleted = 0");
|
||||
assertSql(query).contains("u1.deleted = 0");
|
||||
} else {
|
||||
assertSql(query).contains("t0.deleted = false");
|
||||
// Make sure that query includes that the child mustn't've been deleted
|
||||
assertSql(query).contains("u1.deleted = false");
|
||||
}
|
||||
assertThat(list).hasSize(0);
|
||||
|
||||
// Cleanup created entity
|
||||
DB.deleteAllPermanent(singletonList(child));
|
||||
DB.deleteAllPermanent(singletonList(bean));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotFindSoftDeletedMultilevel() {
|
||||
EBasicSoftDelete bean = new EBasicSoftDelete();
|
||||
bean.setName("Shouldn't be found with child expression when child is deleted");
|
||||
DB.save(bean);
|
||||
final EBasicSDChild child = new EBasicSDChild(bean, "Delete me, don't find me", 1L);
|
||||
DB.save(child);
|
||||
DB.delete(child);
|
||||
|
||||
EBasicNoSDChild secondChild = new EBasicNoSDChild(bean, "Never deleted", 2L);
|
||||
DB.save(secondChild);
|
||||
|
||||
|
||||
Query<EBasicNoSDChild> query = DB.find(EBasicNoSDChild.class)
|
||||
.where()
|
||||
.eq("id", bean.getId())
|
||||
.eq("owner.children.amount", 1L)
|
||||
.query();
|
||||
|
||||
List<EBasicNoSDChild> list = query.findList();
|
||||
// Make sure that query includes that the child mustn't've been deleted
|
||||
if (isMySql() || isMariaDB() || isSqlServer()) {
|
||||
assertSql(query).contains("u1.deleted = 0");
|
||||
assertSql(query).contains("u2.deleted = 0");
|
||||
} else {
|
||||
assertSql(query).contains("u1.deleted = false");
|
||||
assertSql(query).contains("u2.deleted = false");
|
||||
}
|
||||
assertThat(list).hasSize(0);
|
||||
|
||||
// Cleanup created entity
|
||||
DB.deleteAllPermanent(Arrays.asList(child, secondChild));
|
||||
DB.deleteAllPermanent(singletonList(bean));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotFindSoftDeletedMultilevel_with_setIncludeSoftDeletes() {
|
||||
EBasicSoftDelete bean = new EBasicSoftDelete();
|
||||
bean.setName("Found with child expression");
|
||||
DB.save(bean);
|
||||
final EBasicSDChild child = new EBasicSDChild(bean, "SoftDelete me", 1L);
|
||||
DB.save(child);
|
||||
DB.delete(child);
|
||||
|
||||
EBasicNoSDChild secondChild = new EBasicNoSDChild(bean, "Never deleted", 2L);
|
||||
DB.save(secondChild);
|
||||
|
||||
|
||||
Query<EBasicNoSDChild> query = DB.find(EBasicNoSDChild.class)
|
||||
.setIncludeSoftDeletes()
|
||||
.where()
|
||||
.eq("id", bean.getId())
|
||||
.eq("owner.children.amount", 1L)
|
||||
.query();
|
||||
|
||||
List<EBasicNoSDChild> list = query.findList();
|
||||
// Make sure that query not not includes the soft delete join predicates
|
||||
assertSql(query).doesNotContain("u1.deleted = false");
|
||||
assertSql(query).doesNotContain("u2.deleted = false");
|
||||
assertThat(list).hasSize(1);
|
||||
|
||||
// Cleanup created entity
|
||||
DB.deleteAllPermanent(Arrays.asList(child, secondChild));
|
||||
DB.deleteAllPermanent(singletonList(bean));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteById_and_findCount() {
|
||||
|
||||
EBasicSoftDelete bean = new EBasicSoftDelete();
|
||||
bean.setName("two");
|
||||
DB.save(bean);
|
||||
|
||||
int rowCountBefore = DB.find(EBasicSoftDelete.class).findCount();
|
||||
|
||||
DB.delete(EBasicSoftDelete.class, bean.getId());
|
||||
|
||||
|
||||
// -- test .findCount()
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
int rowCountAfter = DB.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().findCount()
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
int rowCountFull = DB.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");
|
||||
DB.save(bean);
|
||||
|
||||
// partially loaded bean without deleted state loaded
|
||||
EBasicSoftDelete partial = DB.find(EBasicSoftDelete.class)
|
||||
.select("id")
|
||||
.setId(bean.getId())
|
||||
.findOne();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
DB.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);
|
||||
|
||||
DB.save(bean);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.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);
|
||||
|
||||
DB.save(bean);
|
||||
|
||||
DB.delete(bean.getChildren().get(1));
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Query<EBasicSoftDelete> query1 =
|
||||
DB.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 = DB.find(EBasicSoftDelete.class, bean.getId());
|
||||
assertThat(fetchWithLazy.getChildren()).hasSize(2);
|
||||
|
||||
// fetch includeSoftDeletes using lazy loading
|
||||
EBasicSoftDelete fetchAllWithLazy =
|
||||
DB.find(EBasicSoftDelete.class)
|
||||
.setId(bean.getId())
|
||||
.where()
|
||||
.setIncludeSoftDeletes()
|
||||
.findOne();
|
||||
|
||||
assertThat(fetchAllWithLazy.getChildren()).hasSize(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWhenAllChildrenSoftDeleted() {
|
||||
|
||||
EBasicSoftDelete bean = new EBasicSoftDelete();
|
||||
bean.setName("softDelChildren");
|
||||
bean.addChild("child1", 10);
|
||||
bean.addChild("child2", 20);
|
||||
|
||||
DB.save(bean);
|
||||
DB.deleteAll(bean.getChildren());
|
||||
|
||||
Query<EBasicSoftDelete> query = DB.find(EBasicSoftDelete.class)
|
||||
.setId(bean.getId())
|
||||
.fetch("children");
|
||||
|
||||
EBasicSoftDelete found = query.findOne();
|
||||
|
||||
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() {
|
||||
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
EBasicSoftDelete bean = new EBasicSoftDelete();
|
||||
bean.setName("fetchWithInclude");
|
||||
bean.addChild("child1", 91);
|
||||
bean.addChild("child2", 92);
|
||||
|
||||
DB.save(bean);
|
||||
|
||||
EBasicSDChild child0 = bean.getChildren().get(0);
|
||||
|
||||
EBasicSDChild upd = new EBasicSDChild();
|
||||
upd.setId(child0.getId());
|
||||
upd.setDeleted(true);
|
||||
DB.update(upd);
|
||||
|
||||
Query<EBasicSoftDelete> query = DB.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.DB;
|
||||
import io.ebean.Query;
|
||||
import org.tests.model.softdelete.ESoftDelBook;
|
||||
import org.tests.model.softdelete.ESoftDelUser;
|
||||
import org.junit.jupiter.api.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");
|
||||
DB.save(user1);
|
||||
|
||||
ESoftDelUser user2 = new ESoftDelUser("user2");
|
||||
DB.save(user2);
|
||||
|
||||
ESoftDelUser user3 = new ESoftDelUser("user3");
|
||||
DB.save(user3);
|
||||
|
||||
// Create books
|
||||
ESoftDelBook book1 = new ESoftDelBook("book1");
|
||||
book1.setLendBy(user1);
|
||||
book1.setLendBys(Arrays.asList(user2, user3));
|
||||
DB.save(book1);
|
||||
|
||||
ESoftDelBook book2 = new ESoftDelBook("book2");
|
||||
book2.setLendBy(user2);
|
||||
book2.setLendBys(Arrays.asList(user1, user3));
|
||||
DB.save(book2);
|
||||
|
||||
// check if everything is stored correctly in DB
|
||||
book1 = DB.find(ESoftDelBook.class).where().eq("bookTitle", "book1").findOne();
|
||||
book2 = DB.find(ESoftDelBook.class).where().eq("bookTitle", "book2").findOne();
|
||||
|
||||
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
|
||||
DB.delete(user1);
|
||||
|
||||
// check if everything is still stored correctly in DB (softdeletes included)
|
||||
book1 = DB.find(ESoftDelBook.class).where().eq("bookTitle", "book1").setIncludeSoftDeletes().findOne();
|
||||
book2 = DB.find(ESoftDelBook.class).where().eq("bookTitle", "book2").setIncludeSoftDeletes().findOne();
|
||||
|
||||
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 = DB.find(ESoftDelBook.class).where().eq("bookTitle", "book1").findOne();
|
||||
book2 = DB.find(ESoftDelBook.class).where().eq("bookTitle", "book2").findOne();
|
||||
|
||||
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");
|
||||
DB.save(user1);
|
||||
|
||||
ESoftDelUser user2 = new ESoftDelUser("user2");
|
||||
DB.save(user2);
|
||||
|
||||
// Create books
|
||||
ESoftDelBook book1 = new ESoftDelBook("book3");
|
||||
book1.setLendBy(user1);
|
||||
book1.setLendBys(Arrays.asList(user1, user2));
|
||||
DB.save(book1);
|
||||
|
||||
DB.delete(user1);
|
||||
DB.delete(user2);
|
||||
|
||||
Query<ESoftDelBook> query = DB.find(ESoftDelBook.class)
|
||||
.setId(book1.getId())
|
||||
.fetch("lendBys");
|
||||
|
||||
ESoftDelBook found = query.findOne();
|
||||
|
||||
assertThat(found).isNotNull();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.tests.softdelete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.softdelete.ESoftDelRole;
|
||||
import org.tests.model.softdelete.ESoftDelUser;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.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");
|
||||
|
||||
DB.save(role1);
|
||||
DB.save(role2);
|
||||
|
||||
ESoftDelUser user1 = new ESoftDelUser("user1");
|
||||
user1.getRoles().add(role1);
|
||||
user1.getRoles().add(role2);
|
||||
|
||||
DB.save(user1);
|
||||
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
DB.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,70 @@
|
||||
package org.tests.softdelete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DB;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.tests.model.softdelete.ESoftDelMid;
|
||||
import io.ebean.Finder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.softdelete.ESoftDelY;
|
||||
import org.tests.model.softdelete.ESoftDelZ;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestSoftDeleteOptionalRelationship extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testFindWhenNullRelationship() {
|
||||
|
||||
ESoftDelMid mid1 = new ESoftDelMid(null, "mid1");
|
||||
DB.save(mid1);
|
||||
|
||||
ESoftDelMid bean = DB.find(ESoftDelMid.class)
|
||||
.setId(mid1.getId())
|
||||
.fetch("top")
|
||||
.findOne();
|
||||
|
||||
assertThat(bean).isNotNull();
|
||||
assertThat(bean.getTop()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindNullWhenMultiple() {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
{
|
||||
ESoftDelZ z = new ESoftDelZ();
|
||||
z.setUuid(uuid);
|
||||
DB.save(z);
|
||||
|
||||
ESoftDelY y = new ESoftDelY();
|
||||
y.setOrganization(z);
|
||||
y.setX(null);
|
||||
DB.save(y);
|
||||
}
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Finder<Long, ESoftDelY> finder = new Finder<>(ESoftDelY.class);
|
||||
ESoftDelY bean = finder
|
||||
.query()
|
||||
.where()
|
||||
.eq("organization.uuid", uuid)
|
||||
.isNull("x")
|
||||
.findOne();
|
||||
|
||||
assertThat(bean).isNotNull();
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
if (isSqlServer() || isMySql() || isMariaDB()) {
|
||||
assertThat(sql.get(0)).contains("left join esoft_del_z t1 on t1.id = t0.organization_id and t1.deleted = 0 where");
|
||||
} else {
|
||||
assertThat(sql.get(0)).contains("left join esoft_del_z t1 on t1.id = t0.organization_id and t1.deleted = false where");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.tests.softdelete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.softdelete.ESoftDelOneA;
|
||||
import org.tests.model.softdelete.ESoftDelOneB;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestSoftDeleteOtoCascade extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ESoftDelOneA a = new ESoftDelOneA("a1");
|
||||
ESoftDelOneB b = new ESoftDelOneB("b1");
|
||||
|
||||
a.setOneb(b);
|
||||
b.setOnea(a);
|
||||
|
||||
DB.save(a);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// delete doesn't cascade to ESoftDelOneB as this is a
|
||||
// soft delete and ESoftDelOneB doesn't have soft delete flag
|
||||
DB.delete(a);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertSql(sql.get(0)).contains("update esoft_del_one_a set deleted=?, version=? where id=? and version=?");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.tests.softdelete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebeantest.LoggedSql;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.softdelete.EsdDetail;
|
||||
import org.tests.model.softdelete.EsdMaster;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestSoftDeleteStatelessUpdate extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EsdMaster master = new EsdMaster("m1");
|
||||
master.getDetails().add(new EsdDetail("d1"));
|
||||
master.getDetails().add(new EsdDetail("d2"));
|
||||
master.getDetails().add(new EsdDetail("d3"));
|
||||
|
||||
DB.save(master);
|
||||
|
||||
EsdMaster upd = new EsdMaster("m1-modified");
|
||||
upd.setId(master.getId());
|
||||
|
||||
EsdDetail d1 = new EsdDetail("d1");
|
||||
d1.setId(master.getDetails().get(0).getId());
|
||||
upd.getDetails().add(d1);
|
||||
|
||||
EsdDetail d3 = new EsdDetail("d3-mod");
|
||||
d3.setId(master.getDetails().get(2).getId());
|
||||
upd.getDetails().add(d3);
|
||||
|
||||
|
||||
LoggedSql.start();
|
||||
|
||||
DB.update(upd);
|
||||
|
||||
List<String> sql = LoggedSql.collect();
|
||||
assertThat(sql).hasSize(6);
|
||||
assertSql(sql.get(0)).contains("update esd_master set name=? where id=?");
|
||||
if (isPlatformBooleanNative()) {
|
||||
assertSql(sql.get(1)).contains("update esd_detail set deleted=true where master_id = ? and not");
|
||||
}
|
||||
|
||||
EsdMaster fetchedWithSoftDeletes = DB.find(EsdMaster.class)
|
||||
.setId(master.getId())
|
||||
.setIncludeSoftDeletes()
|
||||
.fetch("details")
|
||||
.findOne();
|
||||
|
||||
assertThat(fetchedWithSoftDeletes.getDetails()).hasSize(3);
|
||||
|
||||
sql = LoggedSql.collect();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertSql(sql.get(0)).contains("left join esd_detail t1 on t1.master_id = t0.id where t0.id = ?");
|
||||
|
||||
EsdMaster fetchedWithOutSoftDeletes = DB.find(EsdMaster.class)
|
||||
.setId(master.getId())
|
||||
.fetch("details")
|
||||
.findOne();
|
||||
|
||||
assertThat(fetchedWithOutSoftDeletes.getDetails()).hasSize(2);
|
||||
|
||||
sql = LoggedSql.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
if (isPlatformBooleanNative()) {
|
||||
assertSql(sql.get(0)).contains("left join esd_detail t1 on t1.master_id = t0.id and t1.deleted = false where t0.id = ?");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package org.tests.softdelete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.Transaction;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.softdelete.ESoftDelMid;
|
||||
import org.tests.model.softdelete.ESoftDelTop;
|
||||
import org.tests.model.softdelete.ESoftDelUp;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestSoftDeleteTop extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testDeletePermanent() {
|
||||
|
||||
ESoftDelUp up1 = new ESoftDelUp("up1");
|
||||
DB.save(up1);
|
||||
|
||||
DB.delete(up1);
|
||||
DB.deletePermanent(up1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSoftDeleteJdbcBatch() {
|
||||
|
||||
ESoftDelUp up1 = new ESoftDelUp("upBatch1");
|
||||
DB.save(up1);
|
||||
ESoftDelUp up2 = new ESoftDelUp("upBatch2");
|
||||
DB.save(up2);
|
||||
|
||||
Transaction transaction = DB.beginTransaction();
|
||||
try {
|
||||
transaction.setBatchMode(true);
|
||||
DB.delete(up1);
|
||||
DB.delete(up2);
|
||||
transaction.commit();
|
||||
} finally {
|
||||
transaction.end();
|
||||
}
|
||||
|
||||
List<ESoftDelUp> list = new ArrayList<>();
|
||||
list.add(up1);
|
||||
list.add(up2);
|
||||
|
||||
DB.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
|
||||
DB.saveAll(list);
|
||||
DB.deleteAll(list);
|
||||
DB.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");
|
||||
|
||||
DB.save(top1);
|
||||
|
||||
DB.delete(top1);
|
||||
|
||||
DB.deletePermanent(top1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWhereNull() {
|
||||
|
||||
ESoftDelTop top1 = new ESoftDelTop("top1");
|
||||
top1.addMids("mid1");
|
||||
top1.addMids("mid2");
|
||||
|
||||
DB.save(top1);
|
||||
|
||||
Query<ESoftDelTop> query = DB.find(ESoftDelTop.class)
|
||||
.where().isEmpty("mids")
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("where not exists (select 1 from esoft_del_mid x where x.top_id = t0.id and x.deleted =");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user