使用
在play框架中运行测试时,我遇到了进化问题测试如下:
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.]
我希望在每个测试套件之后运行之前的进化运行和进化运行。如何实现这一目标?
答案 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
}
}
}