jeudi 26 juillet 2018

Testing Scala Play with Slick and Evolutions

I would like to write a test of a slick DAO class. Each test should be isolated and database status should be refreshed between tests invocations. I am going to use Evolutions to perform necessary changes on the database itself.

My code is:

class FoundedItemsRepositoryTest extends CommonTest with GuiceOneAppPerSuite with ScalaFutures {
  implicit override lazy val app = new GuiceApplicationBuilder()
    .overrides(bind[Utils].to[UtilsMock])
    .overrides(bind[Mail].to[MailMock])
    .disable[Module]
    .in(Mode.Test)
    .build

  lazy val injector = app.injector
  lazy val databaseApi = injector.instanceOf[DBApi]

  override def withFixture(test: NoArgTest) = { // Define a shared fixture
    // Shared setup (run at beginning of each test)

    Evolutions.applyEvolutions(databaseApi.database("default"))

    try test()
    finally {
      // Shared cleanup (run at end of each test)
      Evolutions.cleanupEvolutions(databaseApi.database("default"))
    }
  }


  def foundedItemsRepository(implicit app: Application): FoundedItemsRepository = Application.instanceCache[FoundedItemsRepository].apply(app)

  "loadWebsites result" should " contain some records" in {
    whenReady(foundedItemsRepository.loadWebsites(w => true)) { res =>
      res.size should be > 0
    }
  }
}

My application.conf contains:

slick.dbs {
    default {
        profile="slick.jdbc.SQLiteProfile$"
        db.driver="org.sqlite.JDBC"
        db.url="jdbc:sqlite:/home/generic/harv.db"
        db.user=harv
        db.password="harv"
    }
}

The invocation ends with an error:

java.lang.IllegalArgumentException: Could not find database for default

I suppose that this related to that I have a slick configuration of slick.dbs.default.db.url instead of a plain jdbc form db.default.url.

How could I fix that?

Aucun commentaire:

Enregistrer un commentaire