Querying hasMany relation fails in unit test
- Dominant language
- Groovy
- Stars
- 2.9k
- Forks
- 975
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 92
Description
### Expected Behaviour
Querying hasMany relation should work with e.g.
`Parent.findAllByChildrenIsEmpty()`
This, however, does not work in unit test but fine in integration test.
### Actual Behaviour
In unit test it can be done with e.g.
`Parent.findByChildrenIsNull()`
Which clearly is an error, and it fails in integration test as expected.
### Example
Domain classes:
`class Parent {
static hasMany = [children: Child]
}`
`class Child {
Parent parent
}
`
Unit test:
```
@TestFor(Parent)
class ParentUnitSpec extends Specification {
void "find parents w/o children works not"() {
when:
new Parent().save(flush: true)
then:
Parent.all.every { !it.children }
and: 'should fail'
Parent.findByChildrenIsNull() //<-- no error
and: 'should NOT fail'
Parent.findByChildrenIsEmpty() //<-- fails
Parent.findAllByChildrenIsEmpty() //<-- fails
Parent.withCriteria { isEmpty("children") } //<-- fails
}
}
```
### Environment Information
- **Operating System**: Ubuntu 16.10
- **Grails Version:** 3.2.8 / 3.2.7 / 3.2.2
- **JDK Version:** 8
- **Container Version (If Applicable):** N/A
### Example Application
The attached example illustrates it clearly; with both unit and integration test to verify.
[has-many-query-fails-unit-test-bug-report-07042017.zip](https://github.com/grails/grails-core/files/905052/has-many-query-fails-unit-test-bug-report-07042017.zip)
Contributor guide
Research direction
Start with the attached example application and the ParentUnitSpec reproduction, comparing its unit test with the integration test. Trace the hasMany mapping and the dynamic finder and criteria calls for children. Done means the empty-relation queries behave consistently in unit and integration tests, including findByChildrenIsNull failing as expected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy
- Domain
- backend, databases, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100