Skip to content

Commit

Permalink
Add getTotalCount() tests for sorting parameters (#1758)
Browse files Browse the repository at this point in the history
* Add getTotalCount() test on empty result for list() method

This is for consistency with the test for the criteria.

* 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.
  • Loading branch information
darxriggs authored Oct 5, 2023
1 parent 250c4eb commit 93a3048
Showing 1 changed file with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -17,16 +26,32 @@ class PagedResultSpec extends GormDatastoreSpec {
results.totalCount == 6
}
void "Test that a getTotalCount will return 0 on empty result"() {
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()
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
}
Expand All @@ -47,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()
Expand Down

0 comments on commit 93a3048

Please sign in to comment.