我在Play上使用了ScalikeJDBC。我想将Specs2测试应用到内存数据库中。
import org.specs2.mutable.Specification
import org.specs2.specification.BeforeAfterAll
import play.api.db.{Database, Databases}
import play.api.db.evolutions.Evolutions
import scalikejdbc.ConnectionPool.DEFAULT_NAME
import scalikejdbc.{ConnectionPool, DataSourceConnectionPool}
import scalikejdbc.{AutoSession, _}
class PaymentRepoSpec extends Specification with BeforeAfterAll {
private var database: Option[Database] = None
def beforeAll(): Unit = {
database = Some(Databases.inMemory(
name = "payment-repo",
urlOptions = Map("MODE" -> "PostgreSQL", "DATABASE_TO_UPPER" -> "FALSE"),
config = Map()
))
database.foreach(Evolutions.applyEvolutions(_))
database.foreach(db => ConnectionPool.add(DEFAULT_NAME, new DataSourceConnectionPool(db.dataSource)))
}
def afterAll(): Unit = {
database.foreach(Evolutions.cleanupEvolutions(_))
database.foreach(_.shutdown())
}
"fruitcakes" should {
"be delicious" >> {
import scalikejdbc._
DB.readOnly { implicit s =>
sql"""select id from payments"""
.map(_.int(1)).list().apply().foreach(println)
}
ok
}
}
}
这失败了,因为尚未应用演变。
org.h2.jdbc.JdbcSQLException: Table "payments" not found; SQL statement:
select id from payments [42102-192]
application.conf 包含
modules.enabled += "scalikejdbc.PlayModule"
modules.disabled += "play.api.db.DBModule"
evolutions.default / 1.sql 包含
CREATE TABLE payments(
id SERIAL PRIMARY KEY,
source CHAR(56) NOT NULL,
destination CHAR(56) NOT NULL,
code VARCHAR(12) NOT NULL,
issuer CHAR(56),
units NUMERIC NOT NULL,
received TIMESTAMP NOT NULL,
scheduled TIMESTAMP NOT NULL,
submitted TIMESTAMP,
status VARCHAR(9) NOT NULL CHECK (status IN ('pending', 'submitted', 'failed', 'succeeded')),
op_result VARCHAR(64)
);
(这在运行应用程序时有效)
build.sbt 包括
libraryDependencies ++= Seq(
guice, evolutions, jdbc, specs2 % Test,
"io.github.synesso" %% "scala-stellar-sdk" % "0.5.1",
"com.nrinaudo" %% "kantan.csv-generic" % "0.4.0",
"com.h2database" % "h2" % "1.4.192",
"org.postgresql" % "postgresql" % "42.2.5",
"org.scalikejdbc" %% "scalikejdbc" % "3.3.0",
"org.scalikejdbc" %% "scalikejdbc-config" % "3.3.0",
"org.scalikejdbc" %% "scalikejdbc-play-initializer" % "2.6.0-scalikejdbc-3.3",
"org.webjars.npm" % "bulma" % "0.7.2",
"org.webjars" % "font-awesome" % "5.6.3",
"org.specs2" %% "specs2-scalacheck" % "4.3.6" % Test
)
如何获得针对ScalikeJDBC DB运行演变的测试?
答案 0 :(得分:0)
name
中的参数Databases.inMemory
必须与evolutions
下的文件夹匹配。
例如,如果演变在evolutions/default/*.sql
中,则必须调用Databases.inMemory(name="default", db)
。