如何在play-framework中运行测试时应用play-evolutions?

时间:2017-10-27 22:07:04

标签: scala playframework play-slick playframework-evolutions playframework-2.6

使用

在play框架中运行测试时,我遇到了进化问题
  • 用于scala的playframework v2.6.6
  • play-slick v3.0.2
  • play-slick-evolutions v3.0.2

测试如下:

class TestFooController extends PlaySpec with GuiceOneServerPerSuite {
  "foo endpoint should store some data" in {
    val wsClient = app.injector.instanceOf[WSClient]
    val url = s"http://localhost:$port/foo"
    val requestData = Json.obj("foo" -> "bar")
    val response = await(wsClient.url(url).post(requestData))
    response.status mustBe OK
  }
}

数据库配置如下所示:

slick.dbs.default.driver="slick.driver.H2Driver$"
slick.dbs.default.db.driver="org.h2.Driver"
slick.dbs.default.db.url="jdbc:h2:mem:play"

假设有一个创建表foos的进化脚本,这个脚本在开发模式下工作正常。

运行测试时会引发以下错误:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[JdbcSQLException: Table "foos" not found;

找不到表foos所以我认为数据库演变尚未应用。

然后我将数据库配置更改为在开发模式下使用的postgresql。

slick.dbs.default.driver = "slick.driver.PostgresDriver$"
slick.dbs.default.db.driver = "org.postgresql.Driver"
slick.dbs.default.db.url = "jdbc:postgresql://localhost:5432/foo-test"
slick.dbs.default.db.user = "user"
slick.dbs.default.db.password = "password"

使用这种配置,测试工作正常,数据存储在数据库中,因此数据库演进运行得很好。

现在的问题是,测试后数据库没有被清除。我想用干净的数据库运行每个测试套件。

总结一下。由于没有应用H2Db演变,应用postgresql演变但未清理。

即使在application.test.conf

中明确定义了这一点
play.evolutions.autoApply=true
play.evolutions.autoApplyDowns=true

我也试过

play.evolutions.db.default.autoApply=true
play.evolutions.db.default.autoApplyDowns=true

没效果。

然后我尝试通过以下方式手动执行此操作:

  def withManagedDatabase[T](block: Database => T): Unit = {
    val dbapi    = app.injector.instanceOf[DBApi]
    val database = dbapi.database("default")
    Evolutions.applyEvolutions(database)
    block(database)
    Evolutions.cleanupEvolutions(database)
  }

然后将测试更改为:

  "foo endpoint should store some data" in withManagedDatabase { _ =>
    ...
  }

对于H2数据库配置,它没有任何效果,抛出了无法找到表foos的相同错误。对于postgresql数据库配置,抛出了进化异常

play.api.db.evolutions.InconsistentDatabase: Database 'default' is in an inconsistent state![An evolution has not been applied properly. Please check the problem and resolve it manually before marking it as resolved.]

我希望在每个测试套件之后运行之前的进化运行和进化运行。如何实现这一目标?

2 个答案:

答案 0 :(得分:2)

您可以使用以下方法在每个套件之前应用演变,然后进行清理:

trait DatabaseSupport extends BeforeAndAfterAll {
  this: Suite with ServerProvider =>

  private lazy val db = app.injector.instanceOf[DBApi]

  override protected def beforeAll(): Unit = {
    super.beforeAll()
    initializeEvolutions(db.database("default"))
  }

  override protected def afterAll(): Unit = {
    cleanupEvolutions(db.database("default"))
    super.afterAll()
  }

  private def initializeEvolutions(database: Database):Unit = {
    Evolutions.cleanupEvolutions(database)
    Evolutions.applyEvolutions(database)
  }

  private def cleanupEvolutions(database: Database):Unit = {
    Evolutions.cleanupEvolutions(database)
  }

}

答案 1 :(得分:0)

这对我有用:

class DAOSpec extends PlaySpec with GuiceOneAppPerSuite {

  val dbUrl = sys.env.getOrElse("DATABASE_URL", "postgres://foo:password@localhost:5432/foo")

  val testConfig = Map("db.default.url" -> dbUrl)

  implicit override def fakeApplication() = new GuiceApplicationBuilder().configure(testConfig).build()

  lazy val database = app.injector.instanceOf[Database]
  lazy val dao = app.injector.instanceOf[DAO]

  "create" must {
    "work" in Evolutions.withEvolutions(database) {
      val foo = await(dao.create("foo"))
      foo.id must not be null
    }
  }

}