本文整理汇总了Scala中com.github.dnvriend.TestSpec类的典型用法代码示例。如果您正苦于以下问题:Scala TestSpec类的具体用法?Scala TestSpec怎么用?Scala TestSpec使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TestSpec类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Foo
//设置package包名称以及导入依赖的类
package com.github.dnvriend.pickle.deserialization
import com.github.dnvriend.TestSpec
import org.typelevel.scalatest.DisjunctionMatchers
import upickle.default._
import scala.util.Try
final case class Foo(foo: Int)
final case class Bar(bar: String)
final case class Baz(baz: Option[String])
final case class Qux(qux: Option[Long])
class DeserializerTest extends TestSpec with DisjunctionMatchers {
def fromJson(str: String): Option[Any] = {
(
Try(read[Foo](str)) orElse
Try(read[Bar](str)) orElse
Try(read[Baz](str)) orElse
Try(read[Qux](str))
).toOption
}
val logOfMessages: String = List(
write(Foo(1)), write(Bar("bar")), write(Baz(Option("baz"))), write(Qux(Option(1))), write(Foo(2)), write(Baz(None))
).mkString("\n")
it should "read log of messages" in {
logOfMessages.split("\n").toList.flatMap(fromJson) shouldBe List(Foo(1), Bar("bar"), Baz(Some("baz")), Qux(Some(1)), Foo(2), Baz(None))
}
}
示例2: Foo
//设置package包名称以及导入依赖的类
package com.github.dnvriend.play.deserialization
import com.github.dnvriend.TestSpec
import org.typelevel.scalatest.DisjunctionMatchers
import play.api.libs.json._
import scala.util.Try
object Foo {
implicit val format = Json.format[Foo]
}
final case class Foo(foo: Int)
object Bar {
implicit val format = Json.format[Bar]
}
final case class Bar(bar: String)
object Baz {
implicit val format = Json.format[Baz]
}
final case class Baz(baz: Option[String])
object Qux {
implicit val format = Json.format[Qux]
}
final case class Qux(qux: Option[Long])
class PlayDeserializerTest extends TestSpec with DisjunctionMatchers {
def toJson[A: Format](a: A) = Json.toJson(a).toString
def fromJson(json: JsValue): Option[Any] = {
(
Try(json.as[Foo]) orElse
Try(json.as[Bar]) orElse
Try(json.as[Baz]) orElse
Try(json.as[Qux])
).map { e => println(s"$json => $e"); e }.toOption
}
val logOfMessages: String =
List(
toJson(Foo(1)),
toJson(Bar("bar")),
toJson(Baz(Option("baz"))),
toJson(Qux(Option(1))),
toJson(Foo(2)),
toJson(Baz(None))
).mkString("\n")
ignore should "read log of messages" in {
// good to know that matching on a function with orElse can give false positives
logOfMessages
.split("\n")
.toList
.map(Json.parse)
.flatMap(fromJson) shouldBe List(Foo(1), Bar("bar"), Baz(Some("baz")), Qux(Some(1)), Foo(2), Baz(None))
}
}
示例3: ProducerConsumerTest
//设置package包名称以及导入依赖的类
package com.github.dnvriend.kafka
import akka.kafka.scaladsl.{Consumer, Producer}
import akka.kafka.scaladsl.Producer.Message
import akka.stream.scaladsl.Source
import com.github.dnvriend.TestSpec
import org.apache.kafka.clients.producer.ProducerRecord
class ProducerConsumerTest extends TestSpec {
it should "produce and consume messages" in {
Source(1 to 10000)
.map(_.toString)
.map(elem => Message(new ProducerRecord[Array[Byte], String]("topic1", elem), elem))
.via(Producer.flow(producerSettings))
.map { result =>
val record = result.message.record
println(s"${record.topic}/${record.partition} ${result.offset}: ${record.value} (${result.message.passThrough})")
result
}
.runForeach(_ => ()).toTry should be a 'success
Consumer.plainSource(consumerSettings.withClientId("client1"))
.take(1000)
.runForeach(println).toTry should be a 'success
}
}
示例4: ConnectionTest
//设置package包名称以及导入依赖的类
package com.github.dnvriend.connections
import com.github.dnvriend.TestSpec
import doobie.imports._
import scalaz._, Scalaz._
import scalaz.concurrent.Task
class ConnectionTest extends TestSpec {
it should "0" in {
val actions: ConnectionIO[Int] = 42.point[ConnectionIO]
val task = actions.transact(xa)
task.run shouldBe 42
}
it should "1" in {
val actions: ConnectionIO[Int] = sql"select 42".query[Int].unique
val task2 = actions.transact(xa)
task2.run shouldBe 42
}
it should "2" in {
val actions: ConnectionIO[(Int, Double)] =
for {
a ? sql"select 42".query[Int].unique
b ? sql"select random()".query[Double].unique
} yield (a, b)
actions.transact(xa).run should matchPattern {
case (x: Int, y: Double) ?
}
}
it should "3" in {
val actions: ConnectionIO[(Int, Double)] = {
val a = sql"select 42".query[Int].unique
val b = sql"select random()".query[Double].unique
(a |@| b).tupled
}
actions.transact(xa).run should matchPattern {
case (x: Int, y: Double) ?
}
}
it should "4" in {
val actions: ConnectionIO[(Int, Double)] = {
val a = sql"select 42".query[Int].unique
val b = sql"select random()".query[Double].unique
(a |@| b).tupled
}
actions.replicateM(2).transact(xa).run should matchPattern {
case (a: Int, b: Double) :: (c: Int, d: Double) :: Nil ?
}
}
}
示例5: UserRepositoryTest
//设置package包名称以及导入依赖的类
package com.github.dnvriend.scaliketests
import com.github.dnvriend.{ TestSpec, UserTableRow }
class UserRepositoryTest extends TestSpec {
it should "user repo should be empty" in {
userRepository.allUsers.futureValue shouldBe empty
}
it should "initialize with users" in {
userRepository.init.toTry should be a 'success
userRepository.allUsers.futureValue should not be empty
}
it should "find by id" in {
userRepository.init.toTry should be a 'success
val users = userRepository.allUsers.futureValue
users should not be empty
val userId = userRepository.allUsers.futureValue.head.id.value
userRepository.findById(userId).futureValue.value shouldBe UserTableRow(Option(5), "Bill", "Gates")
}
}