我有一个表的简单插入和更新测试:
case class Player(id: UUID, name: String, email: String)
在模型对象中进行所有适当的设置以执行插入和查找。
运行以下测试:
class PlayerSpec extends Specification {
"Player" should {
val application = FakeApplication(additionalConfiguration = inMemoryDatabase("test"))
"insert a player" in {
running(application) {
val uuid = UUID.randomUUID()
Player.insert(Player(uuid, "Joe Person", "joe.player@someplace.mail")) must beEqualTo(1)
}
}
"Find a player by id" in {
running(application) {
val uuid = UUID.randomUUID()
Player.insert(Player(uuid, "Jane Person", "jane.player@someplace.mail"))
Player.findById(uuid) must beEqualTo(Some(Player(uuid, "Jane Person", "jane.player@someplace.mail")))
}
}
}
}
我在 Intellij 中得到一个绿色条(两个例子都通过了)。但是,当我在播放控制台中运行它时,我收到以下错误:
[info] PlayerSpec
[info]
[info] Player should
[info] + insert a player
[error] ! Find a player by id
[error] anon$1: Configuration error [Cannot connect to database [default]] Configuration.scala:258)
....
这是一个配置问题,还是我错过了某种 beforeEach 类型调用?
编辑
只需添加数据库代码,以获取更多参考
case class Player(id: UUID, name: String, email: String)
object Player {
private val fullPlayer = {
get[UUID]("player.id")(Utils.rowToUuid) ~
get[String]("player.name") ~
get[String]("player.email") map {
case id~name~email => Player(id, name, email)
}
}
def insert(player: Player) = {
DB.withConnection { implicit c =>
SQL(
"""
insert into player (id, name, email) values (
{id}, {name}, {email}
)
"""
).on(
'id -> player.id.toString,
'name -> player.name,
'email -> player.email
).executeUpdate()
}
}
def findById(id: UUID) : Option[Player] = {
DB.withConnection { implicit c =>
SQL("select * from player where id = {id}").on('id -> id).as(fullPlayer.singleOpt)
}
}
}
行到UUID:
object Utils {
implicit def rowToUuid: Column[UUID] = Column.nonNull {
(value, meta) =>
val MetaDataItem(qualified, nullable, clazz) = meta
value match {
case uuid: UUID => Right(uuid)
case _ => Left(TypeDoesNotMatch("Cannot convert " + value + ":" + value.asInstanceOf[AnyRef].getClass + " to UUID for column " + qualified))
}
}
}