gpt4 book ai didi

scala - 数据库自动创建 -> 找不到参数 session 的隐式值

转载 作者:行者123 更新时间:2023-12-04 00:12:17 24 4
gpt4 key购买 nike

我是 scala 的新手(长期的 java 开发人员)。我试图理解隐式,我认为我了解基础知识,但我不明白为什么它找不到隐式值 session。我尝试使用尽可能多的信息来描述我的问题。

我关注了以下博文:http://blog.websudos.com/2015/04/04/a-series-on-phantom-part-1-getting-started-with-phantom/

一切顺利/编译正常,直到我想测试它。出现以下错误:

Error:(15, 46) could not find implicit value for parameter session: com.datastax.driver.core.Session
Await.result(Database.autocreate().future(), 5.seconds)
^
Error:(20, 48) could not find implicit value for parameter session: com.datastax.driver.core.Session
Await.result(Database.autotruncate().future(), 5.seconds)
^

当我执行下面的测试类时:

import org.joda.time.DateTime
import org.scalatest.{BeforeAndAfterAll, FlatSpec}

import scala.concurrent.Await
import scala.concurrent.duration._

class DatabaseTest extends FlatSpec with BeforeAndAfterAll{
override def beforeAll(): Unit = {
super.beforeAll()
Await.result(Database.autocreate().future(), 5.seconds)
}

override def afterAll(): Unit = {
super.afterAll()
Await.result(Database.autotruncate().future(), 5.seconds)
}

"A TwitterMessage" should "be stored in cassandra" in {
val twitterMessageBefore = TwitterMessage(1L, DateTime.now, "This is a message", "me", "none")

Database.twitterMessages.store(
twitterMessageBefore
)

val twitterMessageAfter:Option[TwitterMessage] = Await.result(Database.twitterMessages.getById(1L), 5.seconds)

assert(twitterMessageAfter.isDefined, "No entry was found regarding the id.")

assert(twitterMessageAfter.get equals twitterMessageBefore)
}
}

下面我把自己写的其他类也抄过来了:

TwitterMessages.scala

import com.websudos.phantom.dsl._

import scala.concurrent.Future

case class TwitterMessage (
id: Long,
timestamp: DateTime,
msg: String,
user: String,
category: String
)

sealed class TwitterMessages extends CassandraTable[ConcreteTwitterMessages, TwitterMessage]{
object id extends LongColumn(this) with PartitionKey[Long]
object timestamp extends DateTimeColumn(this)
object msg extends StringColumn(this)
object user extends StringColumn(this)
object category extends StringColumn(this)

def fromRow(row: Row): TwitterMessage = {
TwitterMessage(
id(row),
timestamp(row),
msg(row),
user(row),
category(row)
)
}
}

abstract class ConcreteTwitterMessages extends TwitterMessages with RootConnector{
def store(twitterMessage: TwitterMessage): Future[ResultSet] = {
insert.value(_.id, twitterMessage.id)
.value(_.timestamp, twitterMessage.timestamp)
.value(_.msg, twitterMessage.msg)
.value(_.user, twitterMessage.user)
.value(_.category, twitterMessage.category)
.consistencyLevel_=(ConsistencyLevel.ALL)
.future()
}

def getById(id: Long): Future[Option[TwitterMessage]] = {
select.where(_.id eqs id).one()
}
}

Database.scala

import com.websudos.phantom.connectors.{ContactPoint, KeySpaceDef}

object Defaults {
val connector = ContactPoint.local.keySpace("twitter")
}

class Database(val keyspace:KeySpaceDef) extends com.websudos.phantom.db.DatabaseImpl(keyspace){
object twitterMessages extends ConcreteTwitterMessages with keyspace.Connector
}

object Database extends Database(Defaults.connector)

最佳答案

要专门解决您的问题,您所要做的就是将连接器混入测试套件中。这个是我的,因为我忘记用这些信息更新博客文章。

class DatabaseTest extends FlatSpec with BeforeAndAfterAll
with Defaults.connector.Connector

关于scala - 数据库自动创建 -> 找不到参数 session 的隐式值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32736368/

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