Permalink
Browse files

Complete specs for MongoDB serializers.

Includes specs for pattern serialization, UUID serialization, and
DateTime serialization.
  • Loading branch information...
1 parent 9d575a5 commit 6ae2815099cf8eaca3e7c85964eee2c2b3025dd0 @Shadowfiend Shadowfiend committed Nov 13, 2012
Showing with 126 additions and 14 deletions.
  1. +126 −14 persistence/mongodb/src/test/scala/net/liftweb/mongodb/CustomSerializersSpec.scala
@@ -17,32 +17,77 @@
package net.liftweb
package mongodb
-import java.util.{Calendar, Date, TimeZone}
+import java.util.{Calendar, Date, TimeZone, UUID}
+import java.util.regex.Pattern
import org.bson.types.ObjectId
+import org.joda.time.{Instant, DateTime}
+
import org.specs2.mutable.Specification
package customserializersspecs {
/*
- * Date as String
+ * ObjectId as String
*/
- case class Person(_id: String, birthDate: Date)
+ case class Person(_id: String)
extends MongoDocument[Person]
{
def meta = Person
}
object Person extends MongoDocumentMeta[Person]
/*
+ * ObjectId as ObjectId
+ */
+ case class PersonWithObjectId(_id: ObjectId)
+ extends MongoDocument[PersonWithObjectId]
+ {
+ def meta = PersonWithObjectId
+ }
+ object PersonWithObjectId extends MongoDocumentMeta[PersonWithObjectId] {
+ override def formats = allFormats
+ }
+
+ /*
+ * Pattern as Pattern
+ */
+ case class PersonWithPattern(_id:ObjectId, pattern: Pattern) extends MongoDocument[PersonWithPattern] {
+ def meta = PersonWithPattern
+ }
+ object PersonWithPattern extends MongoDocumentMeta[PersonWithPattern] {
+ override def formats = allFormats
+ }
+
+ /*
* Date as Date
*/
- case class Person2(_id: ObjectId, birthDate: Date) extends MongoDocument[Person2] {
- def meta = Person2
+ case class PersonWithDate(_id: ObjectId, birthDate: Date) extends MongoDocument[PersonWithDate] {
+ def meta = PersonWithDate
+ }
+ object PersonWithDate extends MongoDocumentMeta[PersonWithDate] {
+ override def formats = allFormats
+ }
+
+ /*
+ * DateTime as DateTime
+ */
+ case class PersonWithDateTime(_id: ObjectId, birthDate: DateTime) extends MongoDocument[PersonWithDateTime] {
+ def meta = PersonWithDateTime
+ }
+ object PersonWithDateTime extends MongoDocumentMeta[PersonWithDateTime] {
+ override def formats = allFormats
}
- object Person2 extends MongoDocumentMeta[Person2] {
+
+ /*
+ * UUID as UUID
+ */
+ case class PersonWithUUID(_id: UUID) extends MongoDocument[PersonWithUUID] {
+ def meta = PersonWithUUID
+ }
+ object PersonWithUUID extends MongoDocumentMeta[PersonWithUUID] {
override def formats = allFormats
}
}
@@ -59,14 +104,11 @@ class CustomSerializersSpec extends Specification with MongoTestKit {
val utc = TimeZone.getTimeZone("UTC")
"CustomSerializers" should {
- "handle Date as String value" in {
+ "handle ObjectId as String value" in {
checkMongoIsRunning
// test data
- val bdjack = Calendar.getInstance
- bdjack.setTimeZone(utc)
- bdjack.setTimeInMillis(1288742280000L)
- val jack = Person(ObjectId.get.toString, bdjack.getTime)
+ val jack = Person(ObjectId.get.toString)
// save the Person document
jack.save
@@ -76,6 +118,59 @@ class CustomSerializersSpec extends Specification with MongoTestKit {
jack2.isDefined must_== true
jack2.toList map { j =>
j._id mustEqual jack._id
+ }
+ }
+
+ "handle ObjectId as ObjectId value using ObjectIdSerializer" in {
+ checkMongoIsRunning
+
+ // test data
+ val jack = PersonWithObjectId(ObjectId.get)
+
+ // save the PersonWithObjectId document
+ jack.save
+
+ // retrieve it and compare
+ val jack2 = PersonWithObjectId.find(jack._id)
+ jack2.isDefined must_== true
+ jack2.toList map { j =>
+ j._id mustEqual jack._id
+ }
+ }
+
+ "handle Pattern as Pattern value using PatternSerializer" in {
+ checkMongoIsRunning
+
+ // test data
+ val pattern = Pattern.compile("(?idmsux-idmsux)m(a)gi(?:ic)?[a-zA-Z]+boom")
+ val jack = PersonWithPattern(ObjectId.get, pattern)
+
+ // save the PersonWithPattern document
+ jack.save
+
+ // retrieve it and compare
+ val jack2 = PersonWithPattern.find(jack._id)
+ jack2.isDefined must_== true
+ jack2.toList map { j =>
+ j.pattern.pattern mustEqual jack.pattern.pattern
+ j.pattern.flags mustEqual jack.pattern.flags
+ }
+ }
+
+ "handle DateTime as DateTime value using DateTimeSerializer" in {
+ checkMongoIsRunning
+
+ // test data
+ val birthday = (new Instant(1288742280000L)).toDateTime
+ val jack = PersonWithDateTime(ObjectId.get, birthday)
+
+ // save the Person document
+ jack.save
+
+ // retrieve it and compare
+ val findJack = PersonWithDateTime.find(jack._id)
+ findJack.isDefined must_== true
+ findJack.toList map { j =>
j.birthDate mustEqual jack.birthDate
}
}
@@ -87,18 +182,35 @@ class CustomSerializersSpec extends Specification with MongoTestKit {
val bdjack = Calendar.getInstance
bdjack.setTimeZone(utc)
bdjack.setTimeInMillis(1288742280000L)
- val jack = Person2(ObjectId.get, bdjack.getTime)
+ val jack = PersonWithDate(ObjectId.get, bdjack.getTime)
// save the Person document
jack.save
// retrieve it and compare
- val findJack = Person2.find(jack._id)
+ val findJack = PersonWithDate.find(jack._id)
findJack.isDefined must_== true
findJack.toList map { j =>
- j._id mustEqual jack._id
j.birthDate mustEqual jack.birthDate
}
}
+
+ "handle UUID as UUID value using UUIDSerializer" in {
+ checkMongoIsRunning
+
+ // test data
+ val uuid = UUID.randomUUID
+ val jack = PersonWithUUID(uuid)
+
+ // save the Person document
+ jack.save
+
+ // retrieve it and compare
+ val findJack = PersonWithUUID.find(jack._id)
+ findJack.isDefined must_== true
+ findJack.toList map { j =>
+ j._id mustEqual jack._id
+ }
+ }
}
}

0 comments on commit 6ae2815

Please sign in to comment.