Skip to content

Nested criteria querying works incorrectly with mock domain objects #14617

Open

Description

@rmorrise

Task List

  • Steps to reproduce provided
  • Stacktrace (if present) provided
  • Example that reproduces the problem uploaded to Github
  • Full description of the issue provided (see below)

Steps to Reproduce

  1. Download the sample app:
    https://github.com/rmorrise/nested-query-mock-test

  2. Verify that the app works properly:
    grails run-app
    On the index page, a Grandchild object is rendered as JSON.

  3. Execute the unit tests:
    grails test-app
    The TestControllerSpec fails.

Expected Behaviour

The TestController.index() method should respond with a Grandchild object, causing the test to pass.

Actual Behaviour

TestController.index() responds with an empty list.

Note: The test will pass if all of the domain objects have the same ID value.

Environment Information

  • Operating System: Win 7 x64
  • Grails Version: 2.3.8 w/GORM 6.1.0.RELEASE
  • JDK Version: 1.8.0_121-b13
  • Container Version (If Applicable): N/A

Example Application

https://github.com/rmorrise/nested-query-mock-test

Activity

  1. jeffscottbrown commented on Apr 18, 2017

    @jeffscottbrown
    Contributor

    https://github.com/rmorrise/nested-query-mock-test/blob/d511e26e585fdca6f56c52a3dfbbd406228c854a/src/test/groovy/nested/query/mock/test/TestControllerSpec.groovy#L20-L32

        void setup() {
            def parent = new Parent(parentCode: 'P1')
            parent.id = 1
            parent.save(flush: true, failOnError: true)
    
            def child = new Child(code: 'C1', parent: parent)
            child.id = 2
            child.save(flush: true, failOnError: true)
    
            def grandChild = new Grandchild(grandChildCode: 'G1', child: child)
            grandChild.id = 3
            grandChild.save(flush: true, failOnError: true)
        }

    Why are you assigning values to the id properties in your test?

  2. rmorrise commented on Apr 18, 2017

    @rmorrise
    ContributorAuthor

    @jeffbrown Good question.

    In my real application code, the ID value is a custom value object which is always assigned by the app. The ID corresponds to the URI of the resource in my SPARQL datastore.

    I ran into this problem unit testing my app, so I created a new grails app (linked above) to reproduce the issue. Initially, I could not repro the issue in the new grails app.

    I spent some time walking through the new grails app and my real app side by side in the debugger. I saw the map query framework used for the mock domain objects was doing some things that didn't entirely make sense to me. It seemed to be mixing and matching IDs from the different types of domain objects into a single query result list. I wasn't sure if this was correct behavior or not, but I noticed that the map query framework does a lot of ID comparison when executing queries.

    In my unit test in the new app, there was only 1 instance of each domain object type, so all of the mocked domain objects involved in my test had a default ID of 1.

    I figured that this might be masking a bug in the framework, because of the ID comparison being done in the query. Sure enough, when I set the IDs to different values explicitly, the unit test I was looking at in the new test app stopped working.

    I don't have a thorough understanding the simple map query framework... but I am pretty sure that the current behavior is not correct. At the very least, if I am using a query syntax which is known not to be supported by simple map, it should throw an exception.

  3. jeffscottbrown commented on Apr 18, 2017

    @jeffscottbrown
    Contributor

    I think you may have misdiagnosed some of the behavior you are seeing. The ids don't all have to be the same in order for the query to work. You shouldn't be assigning values to the ids and I expect that is relevant. If you remove that code, your test will pass. You can verify that the ids being the same is not the issue by creating different numbers of instances of your domain classes in your setup method so that the ones you are testing with all have different ids (create and persist 7 Parent, 5 Child and 3 GrandChild, for example).

  4. jeffscottbrown commented on Apr 18, 2017

    @jeffscottbrown
    Contributor

    At the very least, if I am using a query syntax which is known not to be supported by simple map, it should throw an exception.

    As far as I can tell, the query syntax you are using is fine and does appear to work when the data is valid.

  5. rmorrise commented on Apr 18, 2017

    @rmorrise
    ContributorAuthor

    @jeffbrown If you run the app using run-app, you can see that the app is handling the query correctly. The data defined in Bootstrap is identical to the data defined in the test environment, including the ID assignment. The unit test behavior is inconsistent with the behavior of the in-memory DB.

    I don't understand why you are saying that data persisted with assigned IDs is not valid.

  6. jeffscottbrown commented on Apr 19, 2017

    @jeffscottbrown
    Contributor

    I don't understand why you are saying that data persisted with assigned IDs is not valid.

    It isn't valid to assign values to the id property unless the id generator is set to assigned. I haven't debugged to find out why that is causing a problem for your test, but it is an invalid thing to do.

  7. jeffscottbrown commented on Apr 19, 2017

    @jeffscottbrown
    Contributor

    Not only is it an invalid thing to do, there is no reason to do it.

  8. borinquenkid commented on Nov 15, 2026

    @borinquenkid
    Member

    Hibernate 5 and in-memory part in #17009 (stacked on #16931); the Hibernate 7 part follows on 8.0.x.

  9. borinquenkid commented on Nov 15, 2026

    @borinquenkid
    Member

    Hibernate 7 part in #17010 (stacked on #16930).

  10. borinquenkid commented on Nov 15, 2026

    @borinquenkid
    Member

    Neo4j part in #17011 (stacked on #16836).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions