Skip to content

Instantly share code, notes, and snippets.

@mattfeury
Created September 11, 2013 21:10
Show Gist options
  • Select an option

  • Save mattfeury/6529834 to your computer and use it in GitHub Desktop.

Select an option

Save mattfeury/6529834 to your computer and use it in GitHub Desktop.
NamedQueries with countDistinct projections do not function in TEST mode (tested on Grails 2.1.1, 2.2.4)
package org.example
class AccountInfo {
Integer type
String accountId
static mapping = {
version false
}
static constraints = {
accountId blank: false, size:1..20
}
static namedQueries = {
numAccountsWithType { type ->
eq 'type', type
projections {
countDistinct('accountId')
}
}
}
}
package org.example
import grails.test.mixin.*
import org.junit.*
/**
* See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
*/
@TestFor(AccountInfo)
@Mock([AccountInfo])
class AccountInfoTests {
void setUp() {
[
new AccountInfo(type: 1, accountId: "1234"),
new AccountInfo(type: 2, accountId: "1234"),
new AccountInfo(type: 1, accountId: "5678"),
new AccountInfo(type: 1, accountId: "7890"),
]*.save(failOnError: true)
}
void testAll() {
assert AccountInfo.findAll().size() == 4
}
void testEmbeddedProjectionCountDistinctWithGet() {
def c = AccountInfo.createCriteria()
assert c.get {
eq 'type', 1
projections {
countDistinct('accountId')
}
} == 3
}
// This doesn't work. But seems to work when in production
void testProjectionCountDistinctWithGet() {
assert AccountInfo.numAccountsWithType(1).get() == 3
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment