数据库自动创建 - >找不到参数会话

时间:2015-09-23 09:55:19

标签: scala phantom-dsl

我是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)

1 个答案:

答案 0 :(得分:4)

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

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