From fbac95718274fe123f7e826deba9cde70f700578 Mon Sep 17 00:00:00 2001 From: Juri Skrobko Date: Wed, 23 Oct 2024 11:45:44 +0200 Subject: [PATCH 1/4] add failing tests --- .../org/tests/m2m/TestM2MQueryAndIds.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java diff --git a/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java b/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java new file mode 100644 index 0000000000..ee1de0dfb0 --- /dev/null +++ b/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java @@ -0,0 +1,106 @@ +package org.tests.m2m; + + +import io.ebean.DB; +import io.ebean.Query; +import io.ebean.annotation.Platform; +import io.ebean.xtest.BaseTestCase; +import io.ebean.xtest.ForPlatform; +import org.junit.jupiter.api.BeforeEach; +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.List; +import java.util.Set; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Foconis Analytics GmbH + */ +public class TestM2MQueryAndIds extends BaseTestCase { + + @BeforeEach + public void beforeEach() { + DB.find(Role.class).delete(); + DB.find(Permission.class).delete(); + DB.find(Tenant.class).delete(); + + Tenant tenant = new Tenant("TestTenant"); + DB.save(tenant); + + Permission perm = new Permission("TestPerm"); + DB.save(perm); + + Role role = new Role("TestRole"); + role.getPermissions().add(perm); + role.setTenant(tenant); + DB.save(role); + } + + @ForPlatform(Platform.H2) + @Test + public void testM2MWithId() { + Query sq1 = DB.find(Permission.class) + .alias("sq1") + .where() + .eq("name", "TestPerm") + .raw("(sq1.id = permissions.id)") + .query(); + + Query query = DB.find(Role.class).where().exists(sq1).query(); + List models = query.findList(); + assertThat(models).hasSize(1); + assertThat(query.getGeneratedSql()).isEqualTo("select distinct t0.id, t0.name, t0.version, t0.tenant_id from mt_role t0 left join mt_role_permission t1z_ on t1z_.mt_role_id = t0.id left join mt_permission t1 on t1.id = t1z_.mt_permission_id where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t1.id))"); + } + + @ForPlatform(Platform.H2) + @Test + public void testM2MWithoutId() { + Query sq1 = DB.find(Permission.class) + .alias("sq1") + .where() + .eq("name", "TestPerm") + .raw("(sq1.id = permissions)") + .query(); + + Query query = DB.find(Role.class).where().exists(sq1).query(); + List models = query.findList(); + assertThat(models).hasSize(1); + // select t0.id, t0.name, t0.version, t0.tenant_id from mt_role t0 where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t0.[*]null)) + assertThat(query.getGeneratedSql()).isEqualTo("select distinct t0.id, t0.name, t0.version, t0.tenant_id from mt_role t0 left join mt_role_permission t1z_ on t1z_.mt_role_id = t0.id where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t1z_.mt_permission_id))"); + } + + @ForPlatform(Platform.H2) + @Test + public void testO2MWithId() { + Query sq1 = DB.find(Tenant.class) + .alias("sq1") + .where() + .eq("name", "TestTenant") + .raw("(sq1.id = tenant.id)") + .query(); + + Query query = DB.find(Role.class).where().exists(sq1).query(); + List models = query.findList(); + assertThat(models).hasSize(1); + assertThat(query.getGeneratedSql()).isEqualTo("select t0.id, t0.name, t0.version, t0.tenant_id from mt_role t0 where exists (select 1 from mt_tenant sq1 where sq1.name = ? and (sq1.id = t0.tenant_id))"); + } + + @ForPlatform(Platform.H2) + @Test + public void testO2MWithoutId() { + Query sq1 = DB.find(Tenant.class) + .alias("sq1") + .where() + .eq("name", "TestTenant") + .raw("(sq1.id = tenant)") + .query(); + + Query query = DB.find(Role.class).where().exists(sq1).query(); + List models = query.findList(); + assertThat(models).hasSize(1); + assertThat(query.getGeneratedSql()).isEqualTo("select t0.id, t0.name, t0.version, t0.tenant_id from mt_role t0 where exists (select 1 from mt_tenant sq1 where sq1.name = ? and (sq1.id = t0.tenant_id))"); + } +} From 1ea6722ab5edd28710c65b723b45907768a876d5 Mon Sep 17 00:00:00 2001 From: Juri Skrobko Date: Tue, 29 Oct 2024 14:26:12 +0100 Subject: [PATCH 2/4] changes --- .../org/tests/m2m/TestM2MQueryAndIds.java | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java b/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java index ee1de0dfb0..2a1a6176c5 100644 --- a/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java +++ b/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java @@ -6,7 +6,11 @@ import io.ebean.annotation.Platform; import io.ebean.xtest.BaseTestCase; import io.ebean.xtest.ForPlatform; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.tests.model.m2m.Permission; import org.tests.model.m2m.Role; @@ -16,17 +20,10 @@ import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; -/** - * @author Foconis Analytics GmbH - */ public class TestM2MQueryAndIds extends BaseTestCase { - @BeforeEach - public void beforeEach() { - DB.find(Role.class).delete(); - DB.find(Permission.class).delete(); - DB.find(Tenant.class).delete(); - + @BeforeAll + public static void beforeAll() throws Exception { Tenant tenant = new Tenant("TestTenant"); DB.save(tenant); @@ -39,6 +36,13 @@ public void beforeEach() { DB.save(role); } + @AfterAll + public static void afterAll() { + DB.find(Role.class).delete(); + DB.find(Permission.class).delete(); + DB.find(Tenant.class).delete(); + } + @ForPlatform(Platform.H2) @Test public void testM2MWithId() { @@ -49,10 +53,10 @@ public void testM2MWithId() { .raw("(sq1.id = permissions.id)") .query(); - Query query = DB.find(Role.class).where().exists(sq1).query(); + Query query = DB.find(Role.class).select("id").where().exists(sq1).query(); List models = query.findList(); assertThat(models).hasSize(1); - assertThat(query.getGeneratedSql()).isEqualTo("select distinct t0.id, t0.name, t0.version, t0.tenant_id from mt_role t0 left join mt_role_permission t1z_ on t1z_.mt_role_id = t0.id left join mt_permission t1 on t1.id = t1z_.mt_permission_id where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t1.id))"); + assertThat(query.getGeneratedSql()).isEqualTo("select distinct t0.id from mt_role t0 left join mt_role_permission t1z_ on t1z_.mt_role_id = t0.id left join mt_permission t1 on t1.id = t1z_.mt_permission_id where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t1.id))"); } @ForPlatform(Platform.H2) @@ -65,11 +69,11 @@ public void testM2MWithoutId() { .raw("(sq1.id = permissions)") .query(); - Query query = DB.find(Role.class).where().exists(sq1).query(); + Query query = DB.find(Role.class).select("id").where().exists(sq1).query(); List models = query.findList(); assertThat(models).hasSize(1); - // select t0.id, t0.name, t0.version, t0.tenant_id from mt_role t0 where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t0.[*]null)) - assertThat(query.getGeneratedSql()).isEqualTo("select distinct t0.id, t0.name, t0.version, t0.tenant_id from mt_role t0 left join mt_role_permission t1z_ on t1z_.mt_role_id = t0.id where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t1z_.mt_permission_id))"); + // query from testO2MWith...Id(): select t0.id from mt_role t0 where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t0.tenant_id)) + assertThat(query.getGeneratedSql()).isEqualTo("select distinct t0.id from mt_role t0 left join mt_role_permission t1z_ on t1z_.mt_role_id = t0.id where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t1z_.mt_permission_id))"); } @ForPlatform(Platform.H2) @@ -82,10 +86,10 @@ public void testO2MWithId() { .raw("(sq1.id = tenant.id)") .query(); - Query query = DB.find(Role.class).where().exists(sq1).query(); + Query query = DB.find(Role.class).select("id").where().exists(sq1).query(); List models = query.findList(); assertThat(models).hasSize(1); - assertThat(query.getGeneratedSql()).isEqualTo("select t0.id, t0.name, t0.version, t0.tenant_id from mt_role t0 where exists (select 1 from mt_tenant sq1 where sq1.name = ? and (sq1.id = t0.tenant_id))"); + assertThat(query.getGeneratedSql()).isEqualTo("select t0.id from mt_role t0 where exists (select 1 from mt_tenant sq1 where sq1.name = ? and (sq1.id = t0.tenant_id))"); } @ForPlatform(Platform.H2) @@ -98,9 +102,9 @@ public void testO2MWithoutId() { .raw("(sq1.id = tenant)") .query(); - Query query = DB.find(Role.class).where().exists(sq1).query(); + Query query = DB.find(Role.class).select("id").where().exists(sq1).query(); List models = query.findList(); assertThat(models).hasSize(1); - assertThat(query.getGeneratedSql()).isEqualTo("select t0.id, t0.name, t0.version, t0.tenant_id from mt_role t0 where exists (select 1 from mt_tenant sq1 where sq1.name = ? and (sq1.id = t0.tenant_id))"); + assertThat(query.getGeneratedSql()).isEqualTo("select t0.id from mt_role t0 where exists (select 1 from mt_tenant sq1 where sq1.name = ? and (sq1.id = t0.tenant_id))"); } } From b6395769b3892d87e5198641b6f5319d7f08bda4 Mon Sep 17 00:00:00 2001 From: Juri Skrobko Date: Tue, 29 Oct 2024 14:52:30 +0100 Subject: [PATCH 3/4] assert changes --- ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java b/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java index 2a1a6176c5..75c4845ae7 100644 --- a/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java +++ b/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java @@ -56,7 +56,8 @@ public void testM2MWithId() { Query query = DB.find(Role.class).select("id").where().exists(sq1).query(); List models = query.findList(); assertThat(models).hasSize(1); - assertThat(query.getGeneratedSql()).isEqualTo("select distinct t0.id from mt_role t0 left join mt_role_permission t1z_ on t1z_.mt_role_id = t0.id left join mt_permission t1 on t1.id = t1z_.mt_permission_id where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t1.id))"); + assertThat(query.getGeneratedSql()).isEqualTo("select distinct t0.id from mt_role t0 left join mt_role_permission t1z_ on t1z_.mt_role_id = t0.id where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t1z_.mt_permission_id))"); +// assertThat(query.getGeneratedSql()).isEqualTo("select distinct t0.id from mt_role t0 left join mt_role_permission t1z_ on t1z_.mt_role_id = t0.id left join mt_permission t1 on t1.id = t1z_.mt_permission_id where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t1.id))"); } @ForPlatform(Platform.H2) From bc0dfc8b0552731e1f8dc829742e78c9ba6a0d74 Mon Sep 17 00:00:00 2001 From: Juri Skrobko Date: Thu, 31 Oct 2024 11:36:15 +0100 Subject: [PATCH 4/4] additional tests --- .../org/tests/m2m/TestM2MQueryAndIds.java | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java b/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java index 75c4845ae7..cc338e0f2a 100644 --- a/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java +++ b/ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java @@ -80,6 +80,38 @@ public void testM2MWithoutId() { @ForPlatform(Platform.H2) @Test public void testO2MWithId() { + Query sq1 = DB.find(Role.class) + .alias("sq1") + .where() + .eq("name", "TestRole") + .raw("(sq1.id = roles.id)") + .query(); + + Query query = DB.find(Tenant.class).select("id").where().exists(sq1).query(); + List models = query.findList(); + assertThat(models).hasSize(1); + assertThat(query.getGeneratedSql()).isEqualTo("select distinct t0.id from mt_tenant t0 left join mt_role t1 on t1.tenant_id = t0.id where exists (select 1 from mt_role sq1 where sq1.name = ? and (sq1.id = t1.id))"); + } + + @ForPlatform(Platform.H2) + @Test + public void testO2MWithoutId() { + Query sq1 = DB.find(Role.class) + .alias("sq1") + .where() + .eq("name", "TestRole") + .raw("(sq1.id = roles)") + .query(); + + Query query = DB.find(Tenant.class).select("id").where().exists(sq1).query(); + List models = query.findList(); + assertThat(models).hasSize(1); + assertThat(query.getGeneratedSql()).isEqualTo("select t0.id from mt_tenant t0 where exists (select 1 from mt_role sq1 where sq1.name = ? and (sq1.id = t0.roles_id))"); + } + + @ForPlatform(Platform.H2) + @Test + public void testM2OWithId() { Query sq1 = DB.find(Tenant.class) .alias("sq1") .where() @@ -95,7 +127,7 @@ public void testO2MWithId() { @ForPlatform(Platform.H2) @Test - public void testO2MWithoutId() { + public void testM2OWithoutId() { Query sq1 = DB.find(Tenant.class) .alias("sq1") .where()