Skip to content

Instantly share code, notes, and snippets.

@fmpwizard
Last active August 18, 2016 13:40
Show Gist options
  • Save fmpwizard/a0320e031eb13db372df4d76d21a127f to your computer and use it in GitHub Desktop.
Save fmpwizard/a0320e031eb13db372df4d76d21a127f to your computer and use it in GitHub Desktop.
[info] ObjectIdExtractSpec:
[info] Extract
[info] - should return same ObjectId if I don't have an implicit ObjectIdStringSerializer in scope and pass format directly *** FAILED ***
[info] 57b5ba2c21e60e8ef31e77a0 was not equal to "57b5b4a321e65efe0b7b5fd5" (ObjectIdExtractSpec.scala:15)
[info] - should return same ObjectId if I don't have an implicit ObjectIdStringSerializer in scope *** FAILED ***
[info] 57b5ba2c21e60e8ef31e77a1 was not equal to "57b5b4a321e65efe0b7b5fd5" (ObjectIdExtractSpec.scala:21)
[info] - should return same ObjectId if I do have an implicit ObjectIdStringSerializer in scope
[info] Run completed in 310 milliseconds.
[info] Total number of tests run: 3
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 2, canceled 0, ignored 0, pending 0
[info] *** 2 TESTS FAILED ***
class ObjectIdStringSerializer extends Serializer[ObjectId] {
private val ObjectIdClass = classOf[ObjectId]
def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), ObjectId] = {
case (TypeInfo(ObjectIdClass, _), json) => json match {
case JString(s) if (ObjectId.isValid(s)) =>
new ObjectId(s)
case x => throw new MappingException("Can't convert " + x + " to ObjectId")
}
}
def serialize(implicit formats: Formats): PartialFunction[Any, JValue] = {
case x: ObjectId => JString(x.toString)
}
}
package code
import code.lib.ObjectIdStringSerializer
import code.testkit._
import net.liftweb.json._
import org.scalatest.Inspectors
import scala.reflect.Manifest
class ObjectIdExtractSpec extends BaseWordSpec with Inspectors {
"Extract" should {
"return same ObjectId if I don't have an implicit ObjectIdStringSerializer in scope and pass format directly" in {
val format = DefaultFormats
val j = JString("57b5b4a321e65efe0b7b5fd5")
Extraction.extract[org.bson.types.ObjectId](j)(format, Manifest.classType(classOf[org.bson.types.ObjectId])) should be("57b5b4a321e65efe0b7b5fd5")
}
"return same ObjectId if I don't have an implicit ObjectIdStringSerializer in scope" in {
implicit val format = DefaultFormats
val j = JString("57b5b4a321e65efe0b7b5fd5")
j.extract[org.bson.types.ObjectId] should be("57b5b4a321e65efe0b7b5fd5")
}
"return same ObjectId if I do have an implicit ObjectIdStringSerializer in scope" in {
implicit val format = DefaultFormats + new ObjectIdStringSerializer
val j = JString("57b5b4a321e65efe0b7b5fd5")
j.extract[org.bson.types.ObjectId] should be("57b5b4a321e65efe0b7b5fd5")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment