I followed this link “Test the Model” part to write a test in Play
and Squeryl
. But I am using a named database "test" rather than "default". The following code does not work
import models.{AppDB, Bar}
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import org.squeryl.PrimitiveTypeMode.inTransaction
import play.api.test._
import play.api.test.Helpers._
class BarSpec extends FlatSpec with ShouldMatchers {
"A Bar" should "be creatable" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase("test"))) {
inTransaction {
val bar = AppDB.barTable insert Bar(Some("foo"))
bar.id should not equal(0)
}
}
}
}
I got an error saying:
[info] BarSpec:
[info] A Bar
[info] - should be creatable *** FAILED ***
[info] com.typesafe.config.ConfigException$BadPath: path parameter: Invalid path ' - could not find datasource for default': Token not allowed in path expression: '-' (you can double-quote this token if you really want it here)
[info] at com.typesafe.config.impl.Parser.parsePathExpression(Parser.java:881)
[info] at com.typesafe.config.impl.Parser.parsePath(Parser.java:921)
[info] at com.typesafe.config.impl.Path.newPath(Path.java:206)
[info] at com.typesafe.config.impl.SimpleConfig.hasPath(SimpleConfig.java:70)
[info] at play.api.Configuration.reportError(Configuration.scala:549)
[info] at play.api.db.BoneCPApi.play$api$db$BoneCPApi$$error(DB.scala:274)
[info] at play.api.db.BoneCPApi$$anonfun$getDataSource$5.apply(DB.scala:413)
[info] at play.api.db.BoneCPApi$$anonfun$getDataSource$5.apply(DB.scala:413)
[info] at scala.Option.getOrElse(Option.scala:108)
[info] at play.api.db.BoneCPApi.getDataSource(DB.scala:413)
[info] ...
[error] Failed: : Total 5, Failed 3, Errors 0, Passed 2, Skipped 0
[error] Failed tests:
[error] DataSourceTest
[error] BarSpec
java.lang.RuntimeException: Tests unsuccessful
I guess I cannot directly use inTransaction here. But what should I do to make the test case pass when I use non-"default" database names?
Thank you.
All you need is to setup connection for several databases for example:
package models
import org.squeryl.adapters.{H2Adapter}
import org.squeryl.Session
import play.api.Application
import play.api.db.DB
object TestConn {
def test_session(app : Application) = Session.create( DB.getConnection("test")(app), new H2Adapter)
}
And then in you BarSpec:
.....
"A Bar" should "be creatable" in {
running(FakeApplication()) {
transaction(TestConn.test_session(current)){
val bar = AppDB.barTable insert Bar(Some("foo"))
bar.id should not equal(0)
}
}
}
.....
Notice that you might have conf/evalutions/test directory with your database schema for these case.