apache / apache/grails-core

Criteria query unit test fails for composite id

Open
#10,162 2 comments 0 reactions 0 assignees View on GitHub
info: workaround available relates-to: grails-testing
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

### Steps to Reproduce
- Clone linked github repository and run unit tests
### Expected Behaviour

Given this domain class:

```

class PersistenceRolesToBeGranted implements Serializable {

String username

String authority

static constraints = {
username nullable: false, email: true
authority nullable: false
}

static mapping = {
id composite: ['username', 'authority'], generator: 'assigned'
version false
}
}

```

and service:

```
import grails.transaction.Transactional

@Transactional
class PersistenceRolesToBeGrantedGORMService {
List findAllAuthorityByEmail(String email) {
def c = PersistenceRolesToBeGranted.createCriteria()
c.list {
projections {
property('authority')
}
eq('username', email)
} as List
}
}

```

I expect this unit test to pass:

```
import grails.test.mixin.Mock
import grails.test.mixin.TestFor
import spock.lang.Ignore
import spock.lang.Specification

@TestFor(PersistenceRolesToBeGrantedGORMService)
@Mock([PersistenceRolesToBeGranted])
class PersistenceRolesToBeGrantedGORMServiceSpec extends Specification {

def "test findAllAuthorityByEmail"() {
given:
new PersistenceRolesToBeGranted(username: 'sergio.delamo@hola.com', authority: 'ROLE_ADMIN').save()

expect:
['ROLE_ADMIN'] == service.findAllAuthorityByEmail('sergio.delamo@hola.com')
}
}

```
### Actual Behaviour

The unit test fails. If I comment out the`composite` line on the domain class it passes
### Environment Information
- **Operating System**: OSX
- **Grails Version:** 3.1.11
- **JDK Version:** 1.8.0_45
### Example Application

[Github Repo](https://github.com/sdelamo/grails-issues-composite-id-fail-unit)

Contributor guide

Open the contributing guide

Research direction

Clone the linked example application and run its unit tests first. Inspect PersistenceRolesToBeGranted, PersistenceRolesToBeGrantedGORMService, and PersistenceRolesToBeGrantedGORMServiceSpec to isolate the composite-id criteria query failure. Done means the test passes while the composite mapping remains enabled.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend, databases, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.