gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:18:55 25 4
gpt4 key购买 nike

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

  • playframework v2.6.6 for Scala
  • 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.]
    我希望在每个测试套件之前运行进化上升和进化下降运行。如何做到这一点?

    最佳答案

    您可以使用以下内容在每个套件之前应用演变并在之后进行清理:

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

    }

    关于scala - 在 play-framework 中运行测试时如何应用 play-evolutions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46984028/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com