4

使用我的 Play 2.3.8/Slick 2.1 应用程序,我试图弄清楚如何在我的 application.conf 中设置多个数据库(MySQL 和 MySQL 模式下的内存数据库),其中一个被自动用于测试。我想我已经接近了,但它给了我这个错误:

 InconsistentDatabase: : Database 'test' 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.]  (Evolutions.scala:196)

我在那里看到了很多相关的问题/文档,但它要么已经过时,要么与我想要做的不完全匹配。

这是我的 application.conf 中的内容

db.default.driver="com.mysql.jdbc.Driver"
db.default.url="jdbc:mysql://localhost/maindb"
db.default.user="admin"
db.default.password="test"

applyEvolutions.default=true

db.test.driver=org.h2.Driver
db.test.url="jdbc:h2:mem:play;MODE=MYSQL"
db.test.user=sa
db.test.password=""

applyEvolutions.test=true

slick.default= "models.database.*"
slick.test= "models.database.*"

我意识到,为了让它工作,我可能需要将我的进化脚本复制到一个 conf/evolutions/test 目录中,所以我这样做了。我还想知道是否可以对其进行符号链接,以避免需要维护两个副本。

这是我的 Spec2 测试之一:

import org.specs2.mutable._
import org.specs2.runner._
import org.junit.runner._
import play.api.test._
import play.api.test.Helpers._
import org.specs2.execute.{Result, AsResult}
import play.api.db.slick._
import play.api.db.slick.Config.driver.simple._
import models.database.OwnersDatabase._

abstract class WithDbData extends WithApplication(FakeApplication(additionalConfiguration = inMemoryDatabase("test"))) {
  override def around[T: AsResult](callback: => T): Result = super.around {
    prepareDbWithData()
    callback
  }

  def prepareDbWithData() = DB("test").withSession {
    implicit session => owners ++= TestData.inputData
  }
}

@RunWith(classOf[JUnitRunner])
class OwnerControllerSpec extends Specification {

  "OwnerController" should {    

    "on owners return list of owners" in new WithDbData {
      val result = route(FakeRequest(GET, "/owners")).get

      status(result) must equalTo(OK)
      contentType(result) must beSome("application/json")
      contentAsJson(result) mustEqual TestData.expectedOwnersAll
    }

  }
}

这是用于填充表格的 TestData 类:

import models.Owner
import play.api.libs.json._

object TestData {
  val inputData = Seq(
    Owner("sam", "password", Some("Sam"), "test@example.com", None, "90210"),
    Owner("herb", "password", Some("Herb"), "test@example.com", None, "90210")
  )

  val expectedOwnersAll = Json.obj(
    "rows" -> JsArray(Seq(
      Json.obj("username" -> "sam", "password" -> "password", "name" -> "Sam", "email" -> "test@example.com", "pic_id" -> "", "zipcode" -> "90210"),
      Json.obj("username" -> "herb", "password" -> "password", "name" -> "Herb", "email" -> "test@example.com", "pic_id" -> "", "zipcode" -> "90210")
    ))
  )
}

怎么了?我是否在做任何被认为是不好的做法?

4

0 回答 0