11package org.codehaus.groovy.grails.orm.hibernate
22
33import grails.gorm.DetachedCriteria
4+ import grails.persistence.Entity
45import spock.lang.Ignore
56import org.codehaus.groovy.control.MultipleCompilationErrorsException
67import spock.lang.Issue
@@ -11,9 +12,133 @@ import spock.lang.Issue
1112class WhereMethodSpec extends GormSpec {
1213 @Override
1314 List getDomainClasses () {
14- [Person , Pet ]
15+ [Face , Nose , Person , Pet ]
1516 }
1617
18+ @Issue (' GRAILS-8526' )
19+ def " Test association query with referenced arguments" () {
20+ given :" some people and pets"
21+ createPeopleWithPets()
22+
23+ when :" A query is built from arguments to a method"
24+ def q = getSomePets(name : " Ed" )
25+ def results = q. list()
26+
27+ then :" The results are correct"
28+ results. size() == 3
29+
30+ }
31+
32+ private getSomePets (args ) {
33+ Pet . where {
34+ owner. firstName == args. name
35+ }
36+ }
37+
38+
39+ @Issue (' GRAILS-8366' )
40+ def "Test calling method on RHS of collection size () query" () {
41+ given:" some people and pets"
42+ createPeopleWithPets()
43+
44+ when:" A query that inspects the size() of a collection and calls a method on the RHS is called"
45+ def query = Person.where {
46+ pets.size() == processPetSize(3)
47+ }
48+
49+ then:" The query returns the correct results"
50+ query.count() == 1
51+ }
52+ private processPetSize(int size) { size }
53+
54+ @Issue('GRAILS-9695')
55+ @Ignore // FIXME: http://jira.grails.org/browse/GRAILS-9695
56+ def " Test query with 3 level deep domain association" () {
57+ given:" create people and faces"
58+ createPeopleWithFaces()
59+
60+ when:" A query that uses criteria 2 levels deep is executed"
61+ def results = Nose.withCriteria {
62+ face { person { eq 'lastName', 'Simpson' } }
63+ }
64+
65+ then:" The results are correct"
66+ results.size() == 4
67+
68+ when:" A query that queries an association 2 levels deep is executed"
69+ def query = Nose.where {
70+ face.person.lastName == " Simpson "
71+ }
72+
73+ then:" The correct results are returned"
74+ query.count() == 4
75+
76+ when:" A query that queries an association 2 levels deep is executed via nesting"
77+ query = Nose.where {
78+ face { person { lastName == " Simpson " } }
79+ }
80+
81+ then:" The correct results are returned"
82+ query.count() == 4
83+ }
84+
85+ private createPeopleWithFaces() {
86+ final h = new Person(firstName: " Homer " , lastName: " Simpson " , age: 45)
87+ h.face = new Face(name: " Homer " , nose: new Nose(), person: h )
88+ h.save()
89+ final m = new Person(firstName: " Marge " , lastName: " Simpson " , age: 40)
90+ m.face = new Face(name: " Marge " , nose: new Nose(), person: m)
91+ m.save()
92+
93+ final b = new Person(firstName: " Bart " , lastName: " Simpson " , age: 9)
94+ b.face = new Face(name: " Bart " , nose: new Nose(hasFreckles: true), person: b)
95+ b.save()
96+
97+ final l = new Person(firstName: " Lisa " , lastName: " Simpson " , age: 7)
98+ l.face = new Face(name: " Lisa " , nose: new Nose(hasFreckles: true), person: l)
99+ l.save()
100+
101+ final ba = new Person(firstName: " Barney " , lastName: " Rubble " , age: 35)
102+ ba.face = new Face(name: " Barney " , nose: new Nose())
103+ ba.save()
104+
105+ assert Person.count() == 5
106+ assert Face.count() == 5
107+
108+ }
109+
110+ @Issue('GRAILS-9471')
111+ def " Test chaining where queries directly" () {
112+ given:" Some people"
113+ createPeople()
114+
115+ when:" 2 where queries are combined in a sequence"
116+ def results = Person.where { lastName == 'Simpson' }.where { firstName == 'Bart'}.list()
117+
118+ then:" The correct results are returned"
119+ results.size() == 1
120+ results[0].firstName == 'Bart'
121+ }
122+
123+ def " Test captured detached criteria instance" () {
124+ given:" people and pets"
125+ createPeopleWithPets()
126+
127+ when:" Another detached criteria variable is captured"
128+ def pets = Pet.where {
129+ name ==~ " J% "
130+ }
131+
132+ def owners = Person.where {
133+ pets.size() == 2
134+ }
135+
136+
137+ then:" The results are valid"
138+ owners.count() == 2
139+ }
140+
141+
17142 def " Test where query with join" () {
18143 given:" some people"
19144 createPeopleWithPets()
@@ -1078,3 +1203,31 @@ class CallMe {
10781203
10791204}
10801205
1206+
1207+ @Entity
1208+ class Face implements Serializable {
1209+ Long id
1210+ Long version
1211+ String name
1212+ Nose nose
1213+ Person person
1214+ static hasOne = [nose : Nose ]
1215+ static belongsTo = [person :Person ]
1216+
1217+ static constraints = {
1218+ person nullable :true
1219+ }
1220+ }
1221+
1222+ @Entity
1223+ class Nose implements Serializable {
1224+ Long id
1225+ Long version
1226+ boolean hasFreckles
1227+ Face face
1228+ static belongsTo = [face : Face ]
1229+
1230+ static mapping = {
1231+ face index :true
1232+ }
1233+ }
0 commit comments