apache / apache/grails-core

GORM query does not return expected results during unit testing

Open
#14,535 0 comments 0 reactions 0 assignees View on GitHub
relates-to: gorm
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

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 listEMethod(A instanceOfA){
List listE = E.where {
(isSomeAttribute == true) && (d.c.b.a == instanceOfA)
}.list()
return listE
}
```
3. 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 sampleList = E.findAllByisSomeAttribute(true)

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

when:
List 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 sampleList = E.findAllByisSomeAttribute(true)

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

when:
List 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

- TODO: [Sample Application](https://github.com/TeyimPila/sampleGrailsApp)

Contributor guide

Open the contributing guide

Research direction

Start with the service.listEMethod entry point and the E.where query shown in the reproduction, then compare it with E.findAllByisSomeAttribute(true) in the unit test. Done means the chained association query returns 10 E records for the supplied A instance, matching the direct query.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.