scalascalatestscalikejdbc

ScalikeJDBC won't connect to NamedDB for DSL queries in ScalaTest test cases


I'm having a heck of a time using a test database for my ScalaTest test cases, as shown in the documentation examples.

I have a default database, and a testdb database, and my Spec looks like

class JobSpec extends FlatSpec with AutoRollback {
  DBsWithEnv("test").setup('testdb)
  override def db = NamedDB('testdb).toDB

  override def fixture(implicit session:DBSession) = {
    User.insert("test_user")
  }

  it should "create a new user" in { implicit session: DBSession =>
    User.sqlFind("test_user") //succeeds
    User.dslFind("test_user") //fails
  }
}

It seems that my queries using the sql work, but the ones using the dsl do not. The DSL queries error, trying to access the 'default database, but the sql queries correctly use the 'testdb database. Here's the error

Connection pool is not yet initialized.(name:'default)
java.lang.IllegalStateException: Connection pool is not yet initialized.(name:'default)

Here's the User class

case class User(name: String)
object User extends SQLSyntaxSupport[User] {
  def apply(u: SyntaxProvider[User])(rs: WrappedResultSet) = apply(u.resultName)(rs)
  def apply(j: ResultName[User])(rs: WrappedResultSet) = new User(rs.get(u.name))

  override val tableName = "users"
  val u = User.syntax("u")

  def dslFind(name: String)(implicit session: DBSession) = 
    withSQL {
      select.from(User as u).where.eq(u.name, name)
    }.map(User(u)).single().apply()
  def sqlFind(name: String)(implicit session: DBSession) = 
    sql""" select (name) from users where name = $name;"""
      .map(rs => new User(rs.string(1)).single().apply()
}

Anyone know why it is trying to use the default database instead of the testdb, when calling DSL-created queries? Thanks!


Solution

  • You need to override your SQLSyntaxSupport like this:

    override val connectionPoolName = 'testdb
    

    The reason for that is that SQLSyntaxSupport fetches columns from JDBC metadata when accessing for the first time.

    http://scalikejdbc.org/documentation/sql-interpolation.html

    If you prefer to avoid automatically accessing metadata, override columns or use autoColumns macros.

    https://github.com/scalikejdbc/scalikejdbc/blob/2.2.8/scalikejdbc-syntax-support-macro/src/test/scala/foo/AutoSpec.scala#L20