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,46 @@
|
||||
package org.tests.m2m;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.SqlRow;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.m2m.MnyA;
|
||||
import org.tests.model.m2m.MnyB;
|
||||
import org.tests.model.m2m.MnyC;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestM2MDeleteCascadeFromParent extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
DB.sqlUpdate("delete from mny_b_mny_c").execute();
|
||||
DB.sqlUpdate("delete from mny_c").execute();
|
||||
DB.sqlUpdate("delete from mny_b").execute();
|
||||
DB.sqlUpdate("delete from mny_a").execute();
|
||||
|
||||
MnyC c0 = new MnyC();
|
||||
c0.setName("c0");
|
||||
c0.save();
|
||||
|
||||
MnyA a0 = new MnyA();
|
||||
a0.setName("a0");
|
||||
a0.save();
|
||||
|
||||
MnyB b0 = new MnyB();
|
||||
b0.setName("b0");
|
||||
b0.setA(a0);
|
||||
b0.save();
|
||||
|
||||
c0.getBs().add(b0);
|
||||
DB.save(c0);
|
||||
|
||||
SqlRow row = DB.sqlQuery("select count(*) as count from mny_b_mny_c").findOne();
|
||||
assertEquals(Long.valueOf(1), row.getLong("count"));
|
||||
|
||||
a0.delete();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package org.tests.m2m;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.MnocRole;
|
||||
import org.tests.model.basic.MnocUser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestM2MDeleteNoCascade extends BaseTestCase {
|
||||
|
||||
private static MnocRole r0 = new MnocRole("r0");
|
||||
private static MnocRole r1 = new MnocRole("r1");
|
||||
private static MnocRole r2 = new MnocRole("r2");
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
DB.save(r0);
|
||||
DB.save(r1);
|
||||
DB.save(r2);
|
||||
}
|
||||
|
||||
@IgnorePlatform(Platform.NUODB)
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
MnocUser u0 = new MnocUser("usr0");
|
||||
u0.addValidRole(r0);
|
||||
u0.addValidRole(r1);
|
||||
|
||||
DB.save(u0);
|
||||
|
||||
MnocUser loadedUser = DB.find(MnocUser.class, u0.getUserId());
|
||||
List<MnocRole> validRoles = loadedUser.getValidRoles();
|
||||
assertThat(validRoles).hasSize(2);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
DB.delete(u0);
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertSql(sql.get(0)).contains("delete from mnoc_user_mnoc_role where mnoc_user_user_id = ?");
|
||||
assertSql(sql.get(1)).contains("delete from mnoc_user where user_id=? and version=?");
|
||||
|
||||
final MnocUser found = DB.find(MnocUser.class, u0.getUserId());
|
||||
assertThat(found).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
|
||||
MnocUser u0 = new MnocUser("usr1");
|
||||
u0.addValidRole(r0);
|
||||
u0.addValidRole(r1);
|
||||
|
||||
DB.save(u0);
|
||||
|
||||
List<MnocRole> roles = new ArrayList<>();
|
||||
roles.add(new MnocRole(r2));
|
||||
|
||||
final MnocUser user = DB.find(MnocUser.class, u0.getUserId());
|
||||
user.setUserName("usr1-mod");
|
||||
user.setValidRoles(roles);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
DB.update(user);
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(5);
|
||||
assertSql(sql.get(0)).contains("update mnoc_user set user_name=?, version=? where user_id=? and version=?");
|
||||
assertSql(sql.get(1)).contains("delete from mnoc_user_mnoc_role where mnoc_user_user_id = ?");
|
||||
assertSqlBind(sql.get(2));
|
||||
assertThat(sql.get(3)).contains("insert into mnoc_user_mnoc_role (mnoc_user_user_id, mnoc_role_role_id) values (?, ?)");
|
||||
assertSqlBind(sql.get(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteBatch() {
|
||||
|
||||
MnocUser u0 = new MnocUser("usr2a");
|
||||
u0.addValidRole(r0);
|
||||
u0.addValidRole(r1);
|
||||
|
||||
MnocUser u1 = new MnocUser("usr2b");
|
||||
u1.addValidRole(r0);
|
||||
u1.addValidRole(r1);
|
||||
|
||||
DB.save(u0);
|
||||
DB.save(u1);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
txn.setBatchMode(true);
|
||||
DB.delete(u0);
|
||||
DB.delete(u1);
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(8);
|
||||
assertSql(sql.get(0)).contains("delete from mnoc_user_mnoc_role where mnoc_user_user_id = ?");
|
||||
assertThat(sql.get(4)).contains("delete from mnoc_user_mnoc_role where mnoc_user_user_id = ?");
|
||||
assertSql(sql.get(2)).contains("delete from mnoc_user where user_id=? and version=?");
|
||||
assertThat(sql.get(6)).contains("delete from mnoc_user where user_id=? and version=?");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package org.tests.m2m;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.m2m.Permission;
|
||||
import org.tests.model.m2m.Role;
|
||||
import org.tests.model.m2m.Tenant;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestM2MDeleteObjectWithCascadeToM2m extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
DB.createUpdate(Permission.class, "delete from Permission").execute();
|
||||
DB.createUpdate(Tenant.class, "delete from Tenant").execute();
|
||||
DB.createUpdate(Role.class, "delete from Role").execute();
|
||||
|
||||
Tenant tenant1 = new Tenant("Tenant");
|
||||
DB.save(tenant1);
|
||||
|
||||
Permission p1 = new Permission("p1");
|
||||
Permission p2 = new Permission("p2");
|
||||
DB.save(p1);
|
||||
DB.save(p2);
|
||||
|
||||
Role role1 = new Role("RoleOne");
|
||||
role1.setTenant(tenant1);
|
||||
|
||||
Set<Permission> permissions = new HashSet<>();
|
||||
List<Permission> permsList = DB.find(Permission.class).findList();
|
||||
permissions.addAll(permsList);
|
||||
|
||||
role1.setPermissions(permissions);
|
||||
|
||||
DB.save(role1);
|
||||
|
||||
|
||||
List<Tenant> tenantList = DB.find(Tenant.class).fetch("roles").findList();
|
||||
List<Role> roleList = DB.find(Role.class).fetch("permissions").findList();
|
||||
List<Permission> permissionList = DB.find(Permission.class).fetch("roles").findList();
|
||||
|
||||
assertEquals(1, tenantList.size());
|
||||
assertEquals(2, permissionList.size());
|
||||
assertEquals(1, roleList.size());
|
||||
|
||||
DB.delete(tenant1);
|
||||
|
||||
List<Tenant> tenantList2 = DB.find(Tenant.class).fetch("roles").findList();
|
||||
List<Role> roleList2 = DB.find(Role.class).fetch("permissions").findList();
|
||||
List<Permission> permissionList2 = DB.find(Permission.class).fetch("roles").findList();
|
||||
|
||||
assertEquals(0, tenantList2.size());
|
||||
assertEquals(0, roleList2.size());
|
||||
assertEquals(2, permissionList2.size());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.tests.m2m;
|
||||
|
||||
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.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestM2MDeleteWithCascade extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
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);
|
||||
|
||||
List<MRole> roles = u0.getRoles();
|
||||
|
||||
// this will delete
|
||||
DB.delete(u0);
|
||||
|
||||
MUser notThere = DB.find(MUser.class, u0.getUserid());
|
||||
assertNull(notThere);
|
||||
|
||||
List<Object> roleIds = new ArrayList<>();
|
||||
Collections.addAll(roleIds, r0.getRoleid(), r1.getRoleid());
|
||||
|
||||
int rc = DB.find(MRole.class).where().idIn(roleIds).findCount();
|
||||
|
||||
assertEquals(2, rc);
|
||||
|
||||
DB.deleteAll(roles);
|
||||
|
||||
rc = DB.find(MRole.class).where().idIn(roleIds).findCount();
|
||||
|
||||
assertEquals(0, rc);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.tests.m2m;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.annotation.ForPlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebeantest.LoggedSql;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.m2m.Permission;
|
||||
import org.tests.model.m2m.Role;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestM2MDistinct_sqlServer extends BaseTestCase {
|
||||
|
||||
@ForPlatform(Platform.SQLSERVER)
|
||||
@Test
|
||||
public void testName() {
|
||||
|
||||
Permission perm = new Permission("TestPerm");
|
||||
DB.save(perm);
|
||||
LoggedSql.start();
|
||||
|
||||
List<Role> roles = DB.find(Role.class).where().eq("permissions", perm)
|
||||
.order().asc("name", "Latin1_General_CI_AS")
|
||||
.findList();
|
||||
|
||||
List<String> sqls = LoggedSql.stop();
|
||||
assertThat(sqls.get(0)).startsWith("select distinct t0.id, t0.name, t0.version, t0.tenant_id, t0.name collate Latin1_General_CI_AS "
|
||||
+ "from mt_role t0 join mt_role_permission u1z_ on u1z_.mt_role_id = t0.id "
|
||||
+ "join mt_permission u1 on u1.id = u1z_.mt_permission_id where u1.id = ? "
|
||||
+ "order by t0.name collate Latin1_General_CI_AS;");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.tests.m2m;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.MRole;
|
||||
import org.tests.model.basic.MUser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestM2MModifyTest extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
// Create 2 roles r0 and r1
|
||||
MRole r0 = new MRole("r0");
|
||||
MRole r1 = new MRole("r1");
|
||||
|
||||
// Save r1 and r2
|
||||
DB.save(r0);
|
||||
DB.save(r1);
|
||||
|
||||
// Create a new user
|
||||
MUser u0 = new MUser("usr0");
|
||||
u0.addRole(r0);
|
||||
u0.addRole(r1);
|
||||
|
||||
// Save the user
|
||||
DB.save(u0);
|
||||
|
||||
List<MRole> roles = u0.getRoles();
|
||||
|
||||
assertThat(roles).hasSize(2);
|
||||
|
||||
u0 = DB.find(MUser.class, u0.getUserid());
|
||||
|
||||
roles = u0.getRoles();
|
||||
assertThat(roles).hasSize(2);
|
||||
|
||||
roles.clear();
|
||||
roles.add(r0);
|
||||
roles.add(r1);
|
||||
roles.remove(r1);
|
||||
|
||||
DB.save(u0);
|
||||
|
||||
u0 = DB.find(MUser.class, u0.getUserid());
|
||||
|
||||
roles = u0.getRoles();
|
||||
assertThat(roles).hasSize(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.tests.m2m;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.m2m.MailBox;
|
||||
import org.tests.model.m2m.MailUser;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestM2MMultipleLists extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
MailUser u = new MailUser();
|
||||
u.setName("mailUser1");
|
||||
|
||||
MailBox m1 = new MailBox();
|
||||
m1.setName("mailBox1");
|
||||
|
||||
u.getInbox().add(m1);
|
||||
DB.save(u);
|
||||
|
||||
u = DB.find(MailUser.class, u.getId());
|
||||
|
||||
assertEquals(1, u.getInbox().size());
|
||||
assertEquals(0, u.getOutbox().size());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.tests.m2m;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.m2m.MnyTopic;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Added to test DDL generation for ManyToMany related back to itself.
|
||||
*/
|
||||
public class TestM2MSelfRelationship extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
DB.getDefault();
|
||||
|
||||
// Create 2 roles r0 and r1
|
||||
MnyTopic r0 = new MnyTopic("r0");
|
||||
MnyTopic r1 = new MnyTopic("r1");
|
||||
|
||||
// Save r1 and r2
|
||||
DB.save(r0);
|
||||
DB.save(r1);
|
||||
|
||||
r0.getSubTopics().add(r1);
|
||||
DB.save(r0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package org.tests.m2m;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.meta.MetaTimedMetric;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.m2m.Permission;
|
||||
import org.tests.model.m2m.Role;
|
||||
import org.tests.model.m2m.Tenant;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestM2mDeleteObject extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
resetAllMetrics();
|
||||
|
||||
DB.createUpdate(Permission.class, "delete from Permission").setLabel("deleteAllPermissions").execute();
|
||||
DB.createUpdate(Tenant.class, "delete from Tenant").execute();
|
||||
DB.createUpdate(Role.class, "delete from Role").execute();
|
||||
|
||||
List<MetaTimedMetric> sqlMetrics = sqlMetrics();
|
||||
assertThat(sqlMetrics).hasSize(1);
|
||||
assertThat(sqlMetrics.get(0).name()).isEqualTo("orm.update.deleteAllPermissions");
|
||||
|
||||
Tenant t = new Tenant("tenant");
|
||||
|
||||
DB.save(t);
|
||||
|
||||
Permission p1 = new Permission("p1");
|
||||
Permission p2 = new Permission("p2");
|
||||
|
||||
DB.save(p1);
|
||||
DB.save(p2);
|
||||
|
||||
Role role1 = new Role("role");
|
||||
role1.setTenant(t);
|
||||
|
||||
Set<Permission> permissions = new HashSet<>();
|
||||
List<Permission> permsList = DB.find(Permission.class).findList();
|
||||
permissions.addAll(permsList);
|
||||
|
||||
role1.setPermissions(permissions);
|
||||
|
||||
DB.save(role1);
|
||||
|
||||
List<Tenant> tenantList = DB.find(Tenant.class).fetch("roles").findList();
|
||||
List<Role> roleList = DB.find(Role.class).fetch("permissions").findList();
|
||||
List<Permission> permissionList = DB.find(Permission.class).fetch("roles").findList();
|
||||
|
||||
assertEquals(1, tenantList.size());
|
||||
assertEquals(2, permissionList.size());
|
||||
assertEquals(1, roleList.size());
|
||||
|
||||
DB.delete(role1);
|
||||
|
||||
List<Tenant> tenantList2 = DB.find(Tenant.class).fetch("roles").findList();
|
||||
List<Role> roleList2 = DB.find(Role.class).fetch("permissions").findList();
|
||||
List<Permission> permissionList2 = DB.find(Permission.class).fetch("roles").findList();
|
||||
|
||||
assertEquals(0, roleList2.size());
|
||||
assertEquals(1, tenantList2.size());
|
||||
assertEquals(2, permissionList2.size());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test_deleteCascading() {
|
||||
|
||||
Permission p1 = new Permission("p1");
|
||||
Permission p2 = new Permission("p2");
|
||||
DB.save(p1);
|
||||
DB.save(p2);
|
||||
|
||||
Role role1 = new Role("role");
|
||||
|
||||
Set<Permission> permissions = new HashSet<>();
|
||||
permissions.add(p2);
|
||||
role1.setPermissions(permissions);
|
||||
DB.save(role1);
|
||||
|
||||
// only deletes as not associated
|
||||
DB.delete(p1);
|
||||
// delete cascades
|
||||
DB.delete(role1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.tests.m2m.softdelete;
|
||||
|
||||
import io.ebean.annotation.SoftDelete;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class MsManyA {
|
||||
|
||||
@Id
|
||||
Long aid;
|
||||
|
||||
String name;
|
||||
|
||||
@SoftDelete
|
||||
boolean deleted;
|
||||
|
||||
/**
|
||||
* Name clash with M2M intersection table name.
|
||||
*/
|
||||
boolean ms_many_a_many_b;
|
||||
|
||||
/**
|
||||
* Name clash with other side table name.
|
||||
*/
|
||||
boolean ms_many_b;
|
||||
|
||||
@ManyToMany
|
||||
List<MsManyB> manybs;
|
||||
|
||||
public MsManyA(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getAid() {
|
||||
return aid;
|
||||
}
|
||||
|
||||
public void setAid(Long aid) {
|
||||
this.aid = aid;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<MsManyB> getManybs() {
|
||||
return manybs;
|
||||
}
|
||||
|
||||
public void setManybs(List<MsManyB> manybs) {
|
||||
this.manybs = manybs;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.tests.m2m.softdelete;
|
||||
|
||||
import io.ebean.annotation.SoftDelete;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class MsManyB {
|
||||
|
||||
@Id
|
||||
Long bid;
|
||||
|
||||
String name;
|
||||
|
||||
@SoftDelete
|
||||
boolean deleted;
|
||||
|
||||
@ManyToMany
|
||||
List<MsManyA> manyas;
|
||||
|
||||
public MsManyB(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package org.tests.m2m.softdelete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestM2MSoftDeleteExists extends BaseTestCase {
|
||||
|
||||
private List<MsManyA> as = new ArrayList<>();
|
||||
private List<MsManyB> bs = new ArrayList<>();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
setup();
|
||||
|
||||
Query<MsManyA> query = DB.find(MsManyA.class)
|
||||
.where().isNotEmpty("manybs")
|
||||
.order("aid").query();
|
||||
|
||||
List<MsManyA> list = query.findList();
|
||||
|
||||
if (isPlatformBooleanNative()) {
|
||||
assertThat(sqlOf(query)).contains("from ms_many_a t0 where exists (select 1 from ms_many_a_many_b x join ms_many_b x2 on x2.bid = x.ms_many_b_bid where x.ms_many_a_aid = t0.aid and x2.deleted = false) and t0.deleted = false");
|
||||
}
|
||||
assertThat(list).hasSize(3);
|
||||
assertThat(list.get(0).getName()).isEqualTo("a0");
|
||||
assertThat(list.get(1).getName()).isEqualTo("a1");
|
||||
assertThat(list.get(2).getName()).isEqualTo("a2");
|
||||
|
||||
testDeletedB0();
|
||||
|
||||
}
|
||||
|
||||
private void testDeletedB0() {
|
||||
|
||||
DB.delete(bs.get(0));
|
||||
|
||||
List<MsManyA> list = DB.find(MsManyA.class)
|
||||
.where().isNotEmpty("manybs")
|
||||
.order("aid")
|
||||
.findList();
|
||||
|
||||
assertThat(list).hasSize(2);
|
||||
assertThat(list.get(0).getName()).isEqualTo("a1");
|
||||
assertThat(list.get(1).getName()).isEqualTo("a2");
|
||||
|
||||
testDeletedB1();
|
||||
}
|
||||
|
||||
private void testDeletedB1() {
|
||||
|
||||
DB.delete(bs.get(1));
|
||||
|
||||
List<MsManyA> list = DB.find(MsManyA.class)
|
||||
.where().isNotEmpty("manybs")
|
||||
.order("aid")
|
||||
.findList();
|
||||
|
||||
assertThat(list).hasSize(1);
|
||||
assertThat(list.get(0).getName()).isEqualTo("a1");
|
||||
|
||||
testEndTotals();
|
||||
}
|
||||
|
||||
private void testEndTotals() {
|
||||
|
||||
assertEquals(5, DB.find(MsManyA.class).findCount());
|
||||
assertEquals(3, DB.find(MsManyB.class).findCount());
|
||||
assertEquals(5, DB.find(MsManyB.class).setIncludeSoftDeletes().findCount());
|
||||
}
|
||||
|
||||
private void setup() {
|
||||
clean();
|
||||
seed();
|
||||
}
|
||||
|
||||
private void clean() {
|
||||
|
||||
DB.sqlUpdate("delete from ms_many_a_many_b").execute();
|
||||
DB.sqlUpdate("delete from ms_many_a").execute();
|
||||
DB.sqlUpdate("delete from ms_many_b").execute();
|
||||
}
|
||||
|
||||
private void seed() {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
as.add(new MsManyA("a" + i));
|
||||
bs.add(new MsManyB("b" + i));
|
||||
}
|
||||
|
||||
DB.saveAll(as);
|
||||
DB.saveAll(bs);
|
||||
|
||||
as.get(0).getManybs().add(bs.get(0));
|
||||
as.get(1).getManybs().addAll(bs);
|
||||
as.get(2).getManybs().add(bs.get(1));
|
||||
|
||||
DB.save(as.get(0));
|
||||
DB.save(as.get(1));
|
||||
DB.save(as.get(2));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user