Skip to content

Commit 93a3048

Browse files
authored
Add getTotalCount() tests for sorting parameters (#1758)
* 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.
1 parent 250c4eb commit 93a3048

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

grails-datastore-gorm-tck/src/main/groovy/grails/gorm/tests/PagedResultSpec.groovy

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ package grails.gorm.tests
22

33
class PagedResultSpec extends GormDatastoreSpec {
44

5+
void "Test that a getTotalCount will return 0 on empty result from the list() method"() {
6+
when:"A query is executed that returns no results"
7+
def results = Person.list(max:1)
8+
9+
then:
10+
results.size() == 0
11+
results.totalCount == 0
12+
}
13+
514
void "Test that a paged result list is returned from the list() method with pagination params"() {
615
given:"Some people"
716
createPeople()
@@ -17,16 +26,32 @@ class PagedResultSpec extends GormDatastoreSpec {
1726
results.totalCount == 6
1827
}
1928
20-
void "Test that a getTotalCount will return 0 on empty result"() {
29+
void "Test that a paged result list is returned from the list() method with pagination and sorting params"() {
30+
given:"Some people"
31+
createPeople()
32+
33+
when:"The list method is used with pagination params"
34+
def results = Person.list(offset:2, max:2, sort:'firstName', order:'DESC')
35+
36+
then:"You get a paged result list back"
37+
results.getClass().simpleName == 'PagedResultList' // Grails/Hibernate has a custom class in different package
38+
results.size() == 2
39+
results[0].firstName == "Homer"
40+
results[1].firstName == "Fred"
41+
results.totalCount == 6
42+
}
43+
44+
void "Test that a getTotalCount will return 0 on empty result from the criteria"() {
2145
given:"Some people"
2246
createPeople()
2347
2448
when:"A query is executed that returns no results"
25-
def results = Person.createCriteria().list(max: 1) {
26-
eq 'lastName', 'NotFound'
49+
def results = Person.createCriteria().list(max: 1) {
50+
eq 'lastName', 'NotFound'
2751
}
28-
52+
2953
then:
54+
results.size() == 0
3055
results.totalCount == 0
3156
}
3257
@@ -47,6 +72,23 @@ class PagedResultSpec extends GormDatastoreSpec {
4772
results.totalCount == 4
4873
}
4974
75+
void "Test that a paged result list is returned from the critera with pagination and sorting params"() {
76+
given:"Some people"
77+
createPeople()
78+
79+
when:"The list method is used with pagination params"
80+
def results = Person.createCriteria().list(offset:1, max:2, sort:'firstName', order:'DESC') {
81+
eq 'lastName', 'Simpson'
82+
}
83+
84+
then:"You get a paged result list back"
85+
results.getClass().simpleName == 'PagedResultList' // Grails/Hibernate has a custom class in different package
86+
results.size() == 2
87+
results[0].firstName == "Lisa"
88+
results[1].firstName == "Homer"
89+
results.totalCount == 4
90+
}
91+
5092
protected void createPeople() {
5193
new Person(firstName: "Homer", lastName: "Simpson", age:45).save()
5294
new Person(firstName: "Marge", lastName: "Simpson", age:40).save()

0 commit comments

Comments
 (0)