Permalink
Browse files

Adds the tests for the FromReader classes

  • Loading branch information...
fedefernandez committed Oct 23, 2017
1 parent bd02435 commit c4ed44a04eb0f30bf8871a0286fb0faf8626e0c8
@@ -104,21 +104,6 @@ package object codecs {
byteBuffer => byteBuffer.getShort(byteBuffer.position())
}
implicit def optionalByteBufferCodec[T](
implicit codec: ByteBufferCodec[T]): ByteBufferCodec[Option[T]] =
new ByteBufferCodec[Option[T]] {
override def deserialize[M[_]](bytes: ByteBuffer)(
implicit E: MonadError[M, Throwable]): M[Option[T]] =
Option(bytes) map (b => E.map(codec.deserialize(b))(Option(_))) getOrElse E.pure(None)
override def serialize[M[_]](value: Option[T])(
implicit E: MonadError[M, Throwable]): M[ByteBuffer] =
value
.map(codec.serialize(_)(E))
.getOrElse(E.raiseError(new IllegalArgumentException("Option not supported")))
}
implicit def byteBufferCodec[T](
implicit tc: TypeCodec[T],
pv: ProtocolVersion): ByteBufferCodec[T] = new ByteBufferCodec[T] {
@@ -17,31 +17,25 @@
package freestyle.cassandra
package query.mapper
import java.nio.ByteBuffer
import cats.MonadError
import freestyle.cassandra.codecs.ByteBufferCodec
import freestyle.cassandra.query.Printer
import freestyle.cassandra.query._
import shapeless._
import shapeless.labelled.{FieldBuilder, FieldType}
import shapeless.{::, HList, Witness}
trait ByteBufferReader {
def read[M[_]](name: String)(implicit ME: MonadError[M, Throwable]): M[ByteBuffer]
}
trait FromReader[A] {
def apply[M[_]](reader: ByteBufferReader)(implicit ME: MonadError[M, Throwable]): M[A]
}
trait GenericFromRow {
trait GenericFromReader {
implicit val hnilFromRow: FromReader[HNil] = new FromReader[HNil] {
implicit val hnilFromReader: FromReader[HNil] = new FromReader[HNil] {
override def apply[M[_]](reader: ByteBufferReader)(
implicit ME: MonadError[M, Throwable]): M[HNil] = ME.pure(HNil)
}
implicit def hconsFromRow[K <: Symbol, V, L <: HList](
implicit def hconsFromReader[K <: Symbol, V, L <: HList](
implicit
witness: Witness.Aux[K],
codec: ByteBufferCodec[V],
@@ -59,7 +53,7 @@ trait GenericFromRow {
}
}
implicit def productFromRow[A, L <: HList](
implicit def productFromReader[A, L <: HList](
implicit
gen: LabelledGeneric.Aux[A, L],
grL: FromReader[L]): FromReader[A] =
@@ -69,38 +63,4 @@ trait GenericFromRow {
}
}
object GenericFromRow extends GenericFromRow
//object Test extends App {
//
// import com.datastax.driver.core.{ProtocolVersion, TypeCodec}
// import freestyle.cassandra.query.mapper.ByteBufferToField._
//
// object GenericFromRow extends GenericFromRow
// import GenericFromRow._
//
// case class User(name: String, age: Int)
//
// implicit val protocolVersion: ProtocolVersion = ProtocolVersion.V4
// implicit val stringTypeCodec: TypeCodec[String] = TypeCodec.ascii()
//
// implicit val printer: Printer = ByteBufferToField.identityPrinter
//
// val fromReader: FromReader[User] = implicitly[FromReader[User]]
//
// val nameByteBuffer = stringTypeCodec.serialize("Username", protocolVersion)
// val ageByteBuffer = TypeCodec.cint().serialize(34, protocolVersion)
//
// val reader = new ByteBufferReader() {
// override def read[M[_]](name: String)(implicit ME: MonadError[M, Throwable]): M[ByteBuffer] =
// name match {
// case "name" => ME.pure(nameByteBuffer)
// case "age" => ME.pure(ageByteBuffer)
// }
// }
//
// import cats.instances.try_._
// val result = fromReader[Try](reader)
//
// println(result)
//}
object GenericFromReader extends GenericFromReader
@@ -16,8 +16,16 @@
package freestyle.cassandra
import java.nio.ByteBuffer
import cats.MonadError
package object query {
trait ByteBufferReader {
def read[M[_]](name: String)(implicit ME: MonadError[M, Throwable]): M[ByteBuffer]
}
trait Printer {
def print(name: String): String
}
@@ -0,0 +1,109 @@
/*
* Copyright 2017 47 Degrees, LLC. <http://www.47deg.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package freestyle.cassandra
package query.mapper
import java.nio.ByteBuffer
import cats.MonadError
import com.datastax.driver.core.{ProtocolVersion, TypeCodec}
import freestyle.cassandra.query._
import org.scalacheck.Prop._
import org.scalatest.prop.Checkers
import org.scalatest.{Matchers, WordSpec}
import org.scalacheck.ScalacheckShapeless._
import scala.util.{Failure, Success, Try}
class ByteBufferToFieldSpec extends WordSpec with Matchers with Checkers {
case class User(name: String, age: Int)
implicit val stringTypeCodec: TypeCodec[String] = TypeCodec.varchar()
implicit val protocolVersion: ProtocolVersion = ProtocolVersion.V3
implicit val printer: Printer = identityPrinter
import GenericFromReader._
val fromReader: FromReader[User] = implicitly[FromReader[User]]
"fromReader" should {
import cats.instances.try_._
"return the right value when the reader return a success response" in {
check {
forAll { user: User =>
val reader = new ByteBufferReader() {
override def read[M[_]](name: String)(
implicit ME: MonadError[M, Throwable]): M[ByteBuffer] =
name match {
case "name" => ME.pure(stringTypeCodec.serialize(user.name, protocolVersion))
case "age" => ME.pure(TypeCodec.cint().serialize(user.age, protocolVersion))
}
}
fromReader[Try](reader) == Success(user)
}
}
}
"return the failure when the reader fails returning the ByteBuffer for the 'name' field" in {
check {
forAll { user: User =>
val exception = new RuntimeException("Test Exception")
val reader = new ByteBufferReader() {
override def read[M[_]](name: String)(
implicit ME: MonadError[M, Throwable]): M[ByteBuffer] =
name match {
case "name" => ME.raiseError(exception)
case "age" => ME.pure(TypeCodec.cint().serialize(user.age, protocolVersion))
}
}
fromReader[Try](reader) == Failure(exception)
}
}
}
"return the failure when the reader fails returning the ByteBuffer for the 'age' field" in {
check {
forAll { user: User =>
val exception = new RuntimeException("Test Exception")
val reader = new ByteBufferReader() {
override def read[M[_]](name: String)(
implicit ME: MonadError[M, Throwable]): M[ByteBuffer] =
name match {
case "name" => ME.pure(stringTypeCodec.serialize(user.name, protocolVersion))
case "age" => ME.raiseError(exception)
}
}
fromReader[Try](reader) == Failure(exception)
}
}
}
}
}

0 comments on commit c4ed44a

Please sign in to comment.