I'm working on a Grails plugin with it's own domain class. My integration tests worked fine until I started using a named data source. Now, when I run my tests the fixture data gets duplicated in the database with each call to fixtureLoader.load()
in my tests.
Here is an example of my domain object, fixture, etc:
Program.groovy:
package mydomain
class Program {
String name
String code
static mapping = {
datasource 'myData'
}
String toString() {
"$name ($code)"
}
}
programData.groovy:
import mydomain.*
fixture {
currentProg1(Program, name:'Program Name', code:'44')
}
My data source is configured like so:
test {
dataSource_myData {
dbCreate = "create-drop"
url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
And my tests are like this:
package mydomain
import grails.plugin.spock.IntegrationSpec
class ProgramSpec extends IntegrationSpec {
def fixtureLoader
def "test current program list"() {
given:
def loader = fixtureLoader.load("programData")
when:
List results = Program.list()
then:
assert results.size() == 1
}
def "test toString"() {
given:
def loader = fixtureLoader.load("programData")
when:
def testCase = loader.currentProg1
then:
assert testCase.toString() == "Program Name (44)"
}
}
When I run the tests I get:
Failure: test current program list(com.sg.contract.guide.ProgramSpec)
Condition not satisfied:
results.size() == 1
| | |
| 2 false
[Program Name (44), Program Name (44)]
If I comment out the second test it works fine. But running both tests causes the fixture data to get inserted into the DB twice.
If I change my data source and remove the named source (change dataSource_myData
to dataSource
) and remove the datasource configuration from mapping of my domain class it also works.
I don't know why having a named datasource causes fixture data to either get inserted twice or not get cleaned up after each test. Any ideas?
It looks like a bug. I've created ticket, please vote for it http://jira.grails.org/browse/GPFIXTURES-28