From 0df9ad7c753fe74605d7fe5fd373608843a6a631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scheibe?= Date: Sun, 1 Oct 2023 18:14:18 +0200 Subject: [PATCH 1/2] Add getTotalCount() test on empty result for list() method This is for consistency with the test for the criteria. --- .../grails/gorm/tests/PagedResultSpec.groovy | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/grails-datastore-gorm-tck/src/main/groovy/grails/gorm/tests/PagedResultSpec.groovy b/grails-datastore-gorm-tck/src/main/groovy/grails/gorm/tests/PagedResultSpec.groovy index 7f6dd52d432..1f534fa4e66 100644 --- a/grails-datastore-gorm-tck/src/main/groovy/grails/gorm/tests/PagedResultSpec.groovy +++ b/grails-datastore-gorm-tck/src/main/groovy/grails/gorm/tests/PagedResultSpec.groovy @@ -2,6 +2,15 @@ package grails.gorm.tests class PagedResultSpec extends GormDatastoreSpec { + void "Test that a getTotalCount will return 0 on empty result from the list() method"() { + when:"A query is executed that returns no results" + def results = Person.list(max:1) + + then: + results.size() == 0 + results.totalCount == 0 + } + void "Test that a paged result list is returned from the list() method with pagination params"() { given:"Some people" createPeople() @@ -17,16 +26,17 @@ class PagedResultSpec extends GormDatastoreSpec { results.totalCount == 6 } - void "Test that a getTotalCount will return 0 on empty result"() { + void "Test that a getTotalCount will return 0 on empty result from the criteria"() { given:"Some people" createPeople() when:"A query is executed that returns no results" - def results = Person.createCriteria().list(max: 1) { - eq 'lastName', 'NotFound' + def results = Person.createCriteria().list(max: 1) { + eq 'lastName', 'NotFound' } - + then: + results.size() == 0 results.totalCount == 0 } From d4dfb7e42caffc9c1e4b95638fb8cb4baf69ee82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scheibe?= Date: Sun, 1 Oct 2023 18:14:24 +0200 Subject: [PATCH 2/2] Add getTotalCount() tests for sorting parameters This is to check that a getTotalCount() implementation should not have sorting in the generated query. Otherwise, depending on the database and its strictness configuration, the query might fail. --- .../grails/gorm/tests/PagedResultSpec.groovy | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/grails-datastore-gorm-tck/src/main/groovy/grails/gorm/tests/PagedResultSpec.groovy b/grails-datastore-gorm-tck/src/main/groovy/grails/gorm/tests/PagedResultSpec.groovy index 1f534fa4e66..237dfd63b4f 100644 --- a/grails-datastore-gorm-tck/src/main/groovy/grails/gorm/tests/PagedResultSpec.groovy +++ b/grails-datastore-gorm-tck/src/main/groovy/grails/gorm/tests/PagedResultSpec.groovy @@ -26,6 +26,21 @@ class PagedResultSpec extends GormDatastoreSpec { results.totalCount == 6 } + void "Test that a paged result list is returned from the list() method with pagination and sorting params"() { + given:"Some people" + createPeople() + + when:"The list method is used with pagination params" + def results = Person.list(offset:2, max:2, sort:'firstName', order:'DESC') + + then:"You get a paged result list back" + results.getClass().simpleName == 'PagedResultList' // Grails/Hibernate has a custom class in different package + results.size() == 2 + results[0].firstName == "Homer" + results[1].firstName == "Fred" + results.totalCount == 6 + } + void "Test that a getTotalCount will return 0 on empty result from the criteria"() { given:"Some people" createPeople() @@ -57,6 +72,23 @@ class PagedResultSpec extends GormDatastoreSpec { results.totalCount == 4 } + void "Test that a paged result list is returned from the critera with pagination and sorting params"() { + given:"Some people" + createPeople() + + when:"The list method is used with pagination params" + def results = Person.createCriteria().list(offset:1, max:2, sort:'firstName', order:'DESC') { + eq 'lastName', 'Simpson' + } + + then:"You get a paged result list back" + results.getClass().simpleName == 'PagedResultList' // Grails/Hibernate has a custom class in different package + results.size() == 2 + results[0].firstName == "Lisa" + results[1].firstName == "Homer" + results.totalCount == 4 + } + protected void createPeople() { new Person(firstName: "Homer", lastName: "Simpson", age:45).save() new Person(firstName: "Marge", lastName: "Simpson", age:40).save()