Skip to content

Instantly share code, notes, and snippets.

@krsmes
Created February 10, 2014 14:28
Show Gist options
  • Save krsmes/8916849 to your computer and use it in GitHub Desktop.
Save krsmes/8916849 to your computer and use it in GitHub Desktop.
// TRYING TO FIGURE OUT WHAT I'M DOING WRONG
/*
DOMAINS:
class Type {
String code
}
class Parent {
String name
static hasMany = [types: Type]
static namedQueries = {
queryNames { name, type ->
eq 'name', name
types { eq 'code', type }
}
}
}
class ChildOne extends Parent {
Boolean value
static constraints = { value nullable: true }
}
class ChildTwo extends Parent {
String value
static constraints = { value nullable: true }
}
*/
package ksmith3
import grails.test.mixin.Mock
import spock.lang.Specification
@Mock([Type,Parent,ChildOne,ChildTwo])
class ParentSpec extends Specification {
def type1
def type2
def setup() {
type1 = new Type(code: '1').save()
type2 = new Type(code: '2').save()
new ChildOne(name: 'A', types: [type1]).save()
new ChildTwo(name: 'A', types: [type2]).save()
new ChildTwo(name: 'A', types: [type1, type2]).save()
new ChildOne(name: 'B', types: [type1]).save()
new ChildOne(name: 'B', types: [type2]).save()
}
def "queryNames namedQuery"() {
expect: Parent.queryNames('A', '1').list().size() == 2
}
def "findAllByName"() {
expect: Parent.findAllByName('A').size() == 3
}
def "findAllByTypes"() {
expect: Parent.findAllByTypes([type1] as Set<Type>).size() == 2
}
def "findAllByTypesInList"() {
expect: Parent.findAllByTypesInList([type1]).size() == 3
}
def "findAllByNameAndTypesInList"() {
expect: Parent.findAllByNameAndTypesInList('A', [type1]).size() == 2
}
}
/*
Using Grails 2.2.1 and running this unit test produced the following results...
queryNames namedQuery:
Parent.queryNames('A', '1').list().size() == 2
| | | |
| [] 0 false
org.grails.datastore.gorm.query.NamedCriteriaProxy@1aff2b36
findAllByName: passes
findAllByTypes: passes
findAllByTypesInList:
No signature of method: ksmith3.Parent.findAllByTypesInList() is applicable for argument types: (java.util.ArrayList) values: [[ksmith3.Type : 1]]
findAllByNameAndTypesInList
No signature of method: ksmith3.Parent.findAllByNameAndTypesInList() is applicable for argument types: (java.lang.String, java.util.ArrayList) values: [A, [ksmith3.Type : 1]]
*/
/*
Using Grails 2.2.1 and converting this test to an IntegrationSpec(getting rid of the @Mock) produced the following results...
queryNames namedQuery:
Parent.queryNames('A', '1').list().size() == 2
| | | |
| [] 0 false
org.grails.datastore.gorm.query.NamedCriteriaProxy@1aff2b36
findAllByName: passes
findAllByTypes:
Caused by: org.h2.jdbc.JdbcSQLException: Parameter "#1" is not set; SQL statement:
select this_.id as id0_0_, this_.version as version0_0_, this_.name as name0_0_, this_.value as value0_0_, this_.class as class0_0_ from parent this_ where this_.id=? [90012-164]
findAllByTypesInList:
Caused by: org.h2.jdbc.JdbcSQLException: Parameter "#1" is not set; SQL statement:
select this_.id as id0_0_, this_.version as version0_0_, this_.name as name0_0_, this_.value as value0_0_, this_.class as class0_0_ from parent this_ where this_.id in (?) [90012-164]
findAllByNameAndTypesInList:
Caused by: org.h2.jdbc.JdbcSQLException: Parameter "#2" is not set; SQL statement:
select this_.id as id0_0_, this_.version as version0_0_, this_.name as name0_0_, this_.value as value0_0_, this_.class as class0_0_ from parent this_ where this_.name=? and this_.id in (?) [90012-164]
*/
// -------------------------------------------------------------------------------------------------
/*
If in the setup I use the Parent class instead of the Child classes then the namedQuery test passes.
*/
import grails.test.mixin.Mock
import spock.lang.Specification
@Mock([Type,Parent])
class Parent2Spec extends Specification {
def type1
def type2
def setup() {
type1 = new Type(code: '1').save()
type2 = new Type(code: '2').save()
new Parent(name: 'A', types: [type1]).save()
new Parent(name: 'A', types: [type2]).save()
new Parent(name: 'A', types: [type1, type2]).save()
new Parent(name: 'B', types: [type1]).save()
new Parent(name: 'B', types: [type2]).save()
}
def "queryNames namedQuery"() {
expect: Parent.queryNames('A', '1').list().size() == 2
}
def "findAllByName"() {
expect: Parent.findAllByName('A').size() == 3
}
def "findAllByTypes"() {
expect: Parent.findAllByTypes([type1] as Set<Type>).size() == 2
}
def "findAllByTypesInList"() {
expect: Parent.findAllByTypesInList([type1]).size() == 3
}
def "findAllByNameAndTypesInList"() {
expect: Parent.findAllByNameAndTypesInList('A', [type1]).size() == 2
}
}
/*
Using Grails 2.2.1 and running this unit test produced the following results...
queryNames namedQuery: passes
findAllByName: passes
findAllByTypes: passes
findAllByTypesInList:
No signature of method: ksmith3.Parent.findAllByTypesInList() is applicable for argument types: (java.util.ArrayList) values: [[ksmith3.Type : 1]]
findAllByNameAndTypesInList
No signature of method: ksmith3.Parent.findAllByNameAndTypesInList() is applicable for argument types: (java.lang.String, java.util.ArrayList) values: [A, [ksmith3.Type : 1]]
*/
@krsmes
Copy link
Author

krsmes commented Feb 10, 2014

I don't even really care about the InList issue, the namedQuery not working with child types is the problem I'm trying to solve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment