From 090093500091c8f9d597fd2040249f6c6ca8b724 Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Mon, 1 Mar 2021 09:05:25 +0530 Subject: [PATCH 1/4] Added tests for count --- .../CountQueryIntegrationTest.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java diff --git a/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java b/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java new file mode 100644 index 0000000000..b6f0a6a856 --- /dev/null +++ b/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java @@ -0,0 +1,46 @@ +package com.baeldung.jooq.introduction; + +import static com.baeldung.jooq.introduction.db.public_.tables.Author.AUTHOR; + +import org.jooq.DSLContext; +import org.jooq.impl.DSL; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +@ContextConfiguration(classes = PersistenceContextIntegrationTest.class) +@Transactional(transactionManager = "transactionManager") +@RunWith(SpringJUnit4ClassRunner.class) +public class CountQueryIntegrationTest { + + @Autowired + private DSLContext dsl; + + @Test + public void givenValidData_whenSimpleSelect_thenSucceed() { + int count = dsl.select().from(AUTHOR).execute(); + Assert.assertEquals(3, count); + } + + @Test + public void givenValidData_whenSelectCount_thenSucceed() { + int count = dsl.selectCount().from(AUTHOR).fetchOne(0, int.class); + Assert.assertEquals(3, count); + } + + @Test + public void givenValidData_whenCount_thenSucceed() { + int count = dsl.select(DSL.count()).from(AUTHOR).fetchOne(0, int.class); + Assert.assertEquals(3, count); + } + + @Test + public void givenValidData_whenFetchCount_thenSucceed() { + int count = dsl.fetchCount(DSL.selectFrom(AUTHOR)); + Assert.assertEquals(3, count); + } +} \ No newline at end of file From 2fff4d24c50ba03e1e6a2612c66f50e2389e9159 Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Sun, 7 Mar 2021 16:43:52 +0530 Subject: [PATCH 2/4] Added count queries --- .../introduction/CountQueryIntegrationTest.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java b/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java index b6f0a6a856..7dfa35eb73 100644 --- a/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java +++ b/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java @@ -22,25 +22,30 @@ public class CountQueryIntegrationTest { @Test public void givenValidData_whenSimpleSelect_thenSucceed() { - int count = dsl.select().from(AUTHOR).execute(); + int count = dsl.select().from(AUTHOR) + .execute(); Assert.assertEquals(3, count); } @Test public void givenValidData_whenSelectCount_thenSucceed() { - int count = dsl.selectCount().from(AUTHOR).fetchOne(0, int.class); + int count = dsl.selectCount().from(AUTHOR) + .where(AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan")) + .fetchOne(0, int.class); Assert.assertEquals(3, count); } @Test public void givenValidData_whenCount_thenSucceed() { - int count = dsl.select(DSL.count()).from(AUTHOR).fetchOne(0, int.class); - Assert.assertEquals(3, count); + int count = dsl.select(DSL.count()).from(AUTHOR) + .fetchOne(0, int.class); + Assert.assertEquals(1, count); } @Test public void givenValidData_whenFetchCount_thenSucceed() { - int count = dsl.fetchCount(DSL.selectFrom(AUTHOR)); - Assert.assertEquals(3, count); + int count = dsl.fetchCount(DSL.selectFrom(AUTHOR) + .where(AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan"))); + Assert.assertEquals(1, count); } } \ No newline at end of file From d010629fd54a69b3ff7b318e798b6b9bf0096979 Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Tue, 9 Mar 2021 20:23:15 +0530 Subject: [PATCH 3/4] Added changes to fix the tests --- .../CountQueryIntegrationTest.java | 61 ++++++++++++++++--- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java b/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java index 7dfa35eb73..4a21459d19 100644 --- a/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java +++ b/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java @@ -2,7 +2,13 @@ package com.baeldung.jooq.introduction; import static com.baeldung.jooq.introduction.db.public_.tables.Author.AUTHOR; +import java.util.ArrayList; +import java.util.List; + +import org.jooq.Condition; import org.jooq.DSLContext; +import org.jooq.Record2; +import org.jooq.Result; import org.jooq.impl.DSL; import org.junit.Assert; import org.junit.Test; @@ -22,8 +28,7 @@ public class CountQueryIntegrationTest { @Test public void givenValidData_whenSimpleSelect_thenSucceed() { - int count = dsl.select().from(AUTHOR) - .execute(); + int count = dsl.select().from(AUTHOR).execute(); Assert.assertEquals(3, count); } @@ -32,20 +37,62 @@ public class CountQueryIntegrationTest { int count = dsl.selectCount().from(AUTHOR) .where(AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan")) .fetchOne(0, int.class); - Assert.assertEquals(3, count); + Assert.assertEquals(1, count); } @Test public void givenValidData_whenCount_thenSucceed() { - int count = dsl.select(DSL.count()).from(AUTHOR) - .fetchOne(0, int.class); - Assert.assertEquals(1, count); + int count = dsl.select(DSL.count()) + .from(AUTHOR).fetchOne(0, int.class); + Assert.assertEquals(3, count); } @Test public void givenValidData_whenFetchCount_thenSucceed() { - int count = dsl.fetchCount(DSL.selectFrom(AUTHOR) + int count = dsl.fetchCount( + DSL.selectFrom(AUTHOR) .where(AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan"))); Assert.assertEquals(1, count); } + + @Test + public void givenValidData_whenFetchCountWithoutCondition_thenSucceed() { + int count = dsl.fetchCount( + DSL.selectFrom(AUTHOR)); + Assert.assertEquals(3, count); + } + + @Test + public void givenValidData_whenFetchCountWithSingleCondition_thenSucceed() { + int count = dsl.fetchCount(AUTHOR, AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan")); + Assert.assertEquals(1, count); + } + + @Test + public void givenValidData_whenFetchCountWithMultipleConditions_thenSucceed() { + Condition firstCond = AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan"); + Condition secondCond = AUTHOR.ID.notEqual(1); + List conditions = new ArrayList<>(); + conditions.add(firstCond); + conditions.add(secondCond); + int count = dsl.fetchCount(AUTHOR, conditions); + Assert.assertEquals(1, count); + } + + @Test + public void givenValidData_whenFetchCountWithConditionsInVarargs_thenSucceed() { + Condition firstCond = AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan"); + Condition secondCond = AUTHOR.ID.notEqual(1); + int count = dsl.fetchCount(AUTHOR, firstCond, secondCond); + Assert.assertEquals(1, count); + } + + @Test + public void givenValidData_whenCountwithGroupBy_thenSucceed() { + final Result> result = dsl.select(AUTHOR.FIRST_NAME, DSL.count()) + .from(AUTHOR).groupBy(AUTHOR.FIRST_NAME).fetch(); + Assert.assertEquals(3, result.size()); + Assert.assertEquals(result.get(0).get(0), "Bert"); + Assert.assertEquals(result.get(0).get(1), 1); + } } \ No newline at end of file From 0b16b1440babfff2a69506e67e4adde288e27053 Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Fri, 26 Mar 2021 21:03:08 +0530 Subject: [PATCH 4/4] Fixed review comments --- .../introduction/CountQueryIntegrationTest.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java b/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java index 4a21459d19..7edcc2cd4b 100644 --- a/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java +++ b/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java @@ -35,30 +35,28 @@ public class CountQueryIntegrationTest { @Test public void givenValidData_whenSelectCount_thenSucceed() { int count = dsl.selectCount().from(AUTHOR) - .where(AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan")) - .fetchOne(0, int.class); + .where(AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan")) + .fetchOne(0, int.class); Assert.assertEquals(1, count); } @Test public void givenValidData_whenCount_thenSucceed() { int count = dsl.select(DSL.count()) - .from(AUTHOR).fetchOne(0, int.class); + .from(AUTHOR).fetchOne(0, int.class); Assert.assertEquals(3, count); } @Test public void givenValidData_whenFetchCount_thenSucceed() { - int count = dsl.fetchCount( - DSL.selectFrom(AUTHOR) - .where(AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan"))); + int count = dsl.fetchCount(DSL.selectFrom(AUTHOR) + .where(AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan"))); Assert.assertEquals(1, count); } @Test public void givenValidData_whenFetchCountWithoutCondition_thenSucceed() { - int count = dsl.fetchCount( - DSL.selectFrom(AUTHOR)); + int count = dsl.fetchCount(DSL.selectFrom(AUTHOR)); Assert.assertEquals(3, count); } @@ -90,7 +88,7 @@ public class CountQueryIntegrationTest { @Test public void givenValidData_whenCountwithGroupBy_thenSucceed() { final Result> result = dsl.select(AUTHOR.FIRST_NAME, DSL.count()) - .from(AUTHOR).groupBy(AUTHOR.FIRST_NAME).fetch(); + .from(AUTHOR).groupBy(AUTHOR.FIRST_NAME).fetch(); Assert.assertEquals(3, result.size()); Assert.assertEquals(result.get(0).get(0), "Bert"); Assert.assertEquals(result.get(0).get(1), 1);