10

在使用游戏框架中运行测试时,我遇到了进化问题

  • scala 的 playframework v2.6.6
  • 玩滑 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找不到table的错误。对于 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.]

我希望在每个测试套件之前运行进化上升,并在每个测试套件之后运行进化下降。如何做到这一点?

4

2 回答 2

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)
  }

}

于 2020-02-06T18:58:34.173 回答
1

这对我有用:

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
    }
  }

}
于 2017-12-08T20:27:17.893 回答