grails-data-mapping icon indicating copy to clipboard operation
grails-data-mapping copied to clipboard

GORM query does not return expected results during unit testing

Open TeyimPila opened this issue 6 years ago • 0 comments

Thanks for reporting an issue for GORM, please review the task list below before submitting the issue.

WARNING: Your issue report will be closed if the issue report is incomplete and does not include an example. Make sure the below tasks not completed!

NOTE: If you are unsure about something and the issue is more of a question a better place to ask questions is on Stack Overflow (http://stackoverflow.com/tags/grails) or Slack (http://slack-signup.grails.org). DO NOT use the issue tracker to ask questions.

Task List

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

Steps to Reproduce

  1. Create 5 linked domain classes (A, B, C, D, E), such that: A hasMany B, B hasMany C, C hasMany D, D hasMany E
  2. Create a service class method to query for E as follows:
List<E> listEMethod(A instanceOfA){
    List<E> listE = E.where {
            (isSomeAttribute == true) && (d.c.b.a == instanceOfA)
    }.list()
    return listE
}
  1. Write a unite test to test listE as follows
 void "Test listE method"() {
        given:
        A instanceOfA = new A(name: 'iName').save(flush: true, failOnError: true)
        B instanceOfB = new B(a: instanceOfA).save(flush: true, failOnError: true)
        C instanceOfC = new C(b: instanceOfB).save(flush: true, failOnError: true)
        D instanceOfD = new D(c: instanceOfC).save(flush: true, failOnError: true)
        
        10.times {
            new E(d: instanceOfD, isSomeAttribute: true ).save(flush: true, failOnError: true)
        }

        when:
        List<D> sampleList = E.findAllByisSomeAttribute(true)

        then:
        sampleList.size() == 10 **// This passes**
        sampleList.every { it.d.c.b.a == instanceOfA } **//This passes**

        when:
        List<E> result = service.listEMethod(instanceOfA)

        then:
        result.size() == 10 //This fails
    }

Expected Behaviour

The last assertion should pass

void "Test listE method"() {
       given:
       A instanceOfA = new A(name: 'iName').save(flush: true, failOnError: true)
       B instanceOfB = new B(a: instanceOfA).save(flush: true, failOnError: true)
       C instanceOfC = new C(b: instanceOfB).save(flush: true, failOnError: true)
       D instanceOfD = new D(c: instanceOfC).save(flush: true, failOnError: true)
       
       10.times {
           new E(d: instanceOfD, isSomeAttribute: true ).save(flush: true, failOnError: true)
       }

       when:
       List<D> sampleList = E.findAllByisSomeAttribute(true)

       then:
       sampleList.size() == 10 **// This passes**
       sampleList.every { it.d.c.b.a == instanceOfA } **//This passes**

       when:
       List<E> result = service.listEMethod(instanceOfA)

       then:
       result.size() == 10 //This Should pass
   }

Actual Behaviour

See steps to reproduce above

Environment Information

  • Operating System: MacOS Sierra
  • GORM Version: 6.1.9.RELEASE
  • Grails Version (if using Grails): 3.3.6
  • JDK Version: TODO

Example Application

TeyimPila avatar Dec 17 '18 11:12 TeyimPila