jeudi 8 septembre 2016

Creating Implementation of DatabaseConfigProvider

I'm trying to test a Slick Play app without having to startup and run everything through a Play Application context (using H2). This is because I want to share H2 connections and do a lot of test DB prep before hand.

To do so, I need a DatabaseConfigProvider implementation. In this case, I need a JdbcProfile config. The trait I'm extending is:

trait DatabaseConfigProvider {
  def get[P <: BasicProfile]: DatabaseConfig[P]
}

However, I cannot get the signature of DatabaseConfigProvider's method to type check.

class TestDbProvider(config:DatabaseConfig[JdbcProfile]) extends DatabaseConfigProvider {
  def get[P <: BasicProfile]: DatabaseConfig[P] = config
}

The above implementation, and many other variants all fail.

[error] RepositoryTest.scala:78: type mismatch;
[error]  found   : slick.backend.DatabaseConfig[slick.driver.JdbcProfile]
[error]  required: slick.backend.DatabaseConfig[P]
[error]     def get[P <: BasicProfile]: DatabaseConfig[P] = config
[error]                                                     ^
[error] one error found
[error] (api/test:compileIncremental) Compilation failed

More bad variants:

// Fails
class TestDbProvider(config:DatabaseConfig[JdbcProfile]) extends DatabaseConfigProvider {
  def get[P <: BasicProfile]: DatabaseConfig[JdbcProfile] = config
}

// Fails
class TestDbProvider[P <: BasicProfile](config:DatabaseConfig[P]) extends DatabaseConfigProvider {
  def get[P]: DatabaseConfig[P] = config
}

// Fails
class TestDbProvider(config:DatabaseConfig[JdbcProfile]) extends DatabaseConfigProvider {
  def get[JdbcProfile]: DatabaseConfig[JdbcProfile] = config
}

What's the trick in Scala to make an implementation of DatabaseConfigProvider? Looking through Slick/Play's code base, there is no example of such a concrete implementation.

Aucun commentaire:

Enregistrer un commentaire