GrailsInAction / graina2

Source code for the 2nd edition of Grails in Action
90 stars 92 forks source link

MEAP v11, chapter 5, section 5.3.1 : fetch parameter oddities #41

Open marcpa00 opened 11 years ago

marcpa00 commented 11 years ago

When using def users = User.list(sort: 'loginId', order: 'asc', max: 5, fetch: [posts: 'eager']) the result list has 3 items whereas def users = User.list(sort: 'loginId', order: 'asc', max: 5) has 5.

I wonder if this is a bug in grails (version 2.2.3) ?

Here is a spec that should pass but where the tests using the fetch parameter are failing:

package com.grailsinaction

import grails.plugin.spock.IntegrationSpec

/**
 * Integration tests that should pass, but the one specifying fetch parameter to list() fails.
 */
class UserIntegrationCh531Spec extends IntegrationSpec {

    def setup() {
    }

    def cleanup() {
    }

    def "Static User list with sorting order"() {
        given: "User domain static list() method is used with parameters sort and order"
        def users = User.list(sort: 'id', order: 'asc')

        when: "a copy of the list is sorted by id"
        def copy = users.collect().sort { it.id }

        then: "both are equals"
        users == copy
    }

    def "Static User list with sorting order and eager fetch"() {
        given: "User domain static list() method is used with parameters sort, order and fetch"
        def users = User.list(sort: 'id', order: 'asc', fetch: [posts: 'eager'])

        when: "a copy of the list is sorted by id"
        def copy = users.collect().sort { it.id }

        then: "both are equals"
        users == copy
    }

    def "Static User list with max"() {
        given: "User domain static list() method used with a max parameter"
        def max = 4
        def users = User.list(max: max)

        when: "We count the results"
        def count = users.size()

        then: "it equals the max given"
        count == max
    }

    def "Static User list with max and eager fetch"() {
        given: "User domain static list() method used with a max parameter and fetch"
        def max = 5
        def users = User.list(max: max, fetch: [posts: 'eager'])

        when: "We count the results"
        def count = users.size()

        then: "it equals the max given"
        count == max
    }

}
pledbrook commented 11 years ago

I'll look into it.

marcpa00 commented 11 years ago

Note : I tried both with jdbc/hibernate logging enabled and disabled, but outcome is the same.

pledbrook commented 10 years ago

Odd. I have just tried this and it's working fine with Grails 2.2.3. I simply can't reproduce. I assume you're running against H2? I wonder if the version of the JDK is having an impact too. I've just tried with Java 6.

JoeCordingley commented 9 years ago

Same problem with grails 3.0.2, although I have a sort and when I remove that it gets the right amount.