Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Issue 984 - Empty objects don't deserialize properly in JObjectParser

  • Loading branch information...
commit a1f7ef45051d5957d97d48b688d4894de509adf8 1 parent 0b4163b
@eltimn eltimn authored
View
12 persistence/mongodb-record/src/test/scala/net/liftweb/mongodb/record/Fixtures.scala
@@ -160,7 +160,8 @@ object FieldTypeTestRecord extends FieldTypeTestRecord with MongoMetaRecord[Fiel
case class TypeTestJsonObject(
intField: Int,
- stringField: String
+ stringField: String,
+ mapField: Map[String, String]
) extends JsonObject[TypeTestJsonObject]
{
// TODO: Add more types
@@ -179,15 +180,12 @@ class MongoFieldTypeTestRecord private () extends MongoRecord[MongoFieldTypeTest
object mandatoryDateField extends DateField(this)
object legacyOptionalDateField extends DateField(this) { override def optional_? = true }
- object mandatoryDBRefField extends DBRefField[MongoFieldTypeTestRecord, DBRefTestRecord](this, DBRefTestRecord)
- object legacyOptionalDBRefField extends DBRefField[MongoFieldTypeTestRecord, DBRefTestRecord](this, DBRefTestRecord) { override def optional_? = true }
-
object mandatoryJsonObjectField extends JsonObjectField(this, TypeTestJsonObject) {
- def defaultValue = TypeTestJsonObject(0, "")
+ def defaultValue = TypeTestJsonObject(0, "", Map[String, String]())
}
object legacyOptionalJsonObjectField extends JsonObjectField(this, TypeTestJsonObject) {
override def optional_? = true
- def defaultValue = TypeTestJsonObject(0, "")
+ def defaultValue = TypeTestJsonObject(0, "", Map[String, String]())
}
object mandatoryObjectIdField extends ObjectIdField(this)
@@ -203,8 +201,6 @@ class MongoFieldTypeTestRecord private () extends MongoRecord[MongoFieldTypeTest
case that:MongoFieldTypeTestRecord =>
this.id.value == that.id.value &&
this.mandatoryDateField.value == that.mandatoryDateField.value &&
- //this.mandatoryDBRefField.value.getId == that.mandatoryDBRefField.value.getId &&
- //this.mandatoryDBRefField.value.getRef == that.mandatoryDBRefField.value.getRef &&
this.mandatoryJsonObjectField.value == that.mandatoryJsonObjectField.value &&
this.mandatoryObjectIdField.value == that.mandatoryObjectIdField.value &&
this.mandatoryPatternField.value.pattern == that.mandatoryPatternField.value.pattern &&
View
38 persistence/mongodb-record/src/test/scala/net/liftweb/mongodb/record/MongoFieldSpec.scala
@@ -29,6 +29,7 @@ import org.specs.Specification
import common._
import json.{Num => JsonNum, _}
+import JsonDSL._
import util.FieldError
import util.Helpers.randomString
import http.{LiftSession, S}
@@ -188,25 +189,18 @@ object MongoFieldSpec extends Specification("MongoField Specification") with Mon
)
}
- "DBRefField" should {
- checkMongoIsRunning
-
- if (isMongoRunning) { // Even if this gets skipped, the vals still get set.
- val rec = MongoFieldTypeTestRecord.createRecord
- val dbref = DBRefTestRecord.createRecord.getRef // This makes a call to MongoDB.use and needs a MongoDB connection.
- passBasicTests(dbref, rec.mandatoryDBRefField, rec.legacyOptionalDBRefField, false)
- }
- }
-
"JsonObjectField" should {
val rec = MongoFieldTypeTestRecord.createRecord
- val ttjo = TypeTestJsonObject(1, "jsonobj1")
+ val ttjo = TypeTestJsonObject(1, "jsonobj1", Map("x" -> "a"))
+ val json = ("intField" -> 1) ~ ("stringField" -> "jsonobj1") ~ ("mapField" -> (("x" -> "a")))
passBasicTests(ttjo, rec.mandatoryJsonObjectField, rec.legacyOptionalJsonObjectField)
passConversionTests(
ttjo,
rec.mandatoryJsonObjectField,
- JsObj(("intField", Num(1)), ("stringField", Str("jsonobj1"))),
- JObject(List(JField("intField", JInt(1)), JField("stringField", JString("jsonobj1")))),
+ new JsExp {
+ def toJsCmd = Printer.compact(render(json))
+ },
+ json,
Empty
)
}
@@ -303,19 +297,19 @@ object MongoFieldSpec extends Specification("MongoField Specification") with Mon
"MongoJsonObjectListField" should {
"function correctly" in {
val rec = ListTestRecord.createRecord
- val lst = List(TypeTestJsonObject(1, "jsonobj1"), TypeTestJsonObject(2, "jsonobj2"))
+ val lst = List(TypeTestJsonObject(1, "jsonobj1", Map("x" -> "1")), TypeTestJsonObject(2, "jsonobj2", Map("x" -> "2")))
+ val json = List(
+ ("intField" -> 1) ~ ("stringField" -> "jsonobj1") ~ ("mapField" -> (("x" -> "1"))),
+ ("intField" -> 2) ~ ("stringField" -> "jsonobj2") ~ ("mapField" -> (("x" -> "2")))
+ )
passBasicTests(lst, rec.mandatoryMongoJsonObjectListField, rec.legacyOptionalMongoJsonObjectListField)
passConversionTests(
lst,
rec.mandatoryMongoJsonObjectListField,
- JsArray(
- JsObj(("intField", Num(1)), ("stringField", Str("jsonobj1"))),
- JsObj(("intField", Num(2)), ("stringField", Str("jsonobj2")))
- ),
- JArray(List(
- JObject(List(JField("intField", JInt(1)), JField("stringField", JString("jsonobj1")))),
- JObject(List(JField("intField", JInt(2)), JField("stringField", JString("jsonobj2"))))
- )),
+ new JsExp {
+ def toJsCmd = Printer.compact(render(json))
+ },
+ json,
Empty
)
}
View
238 persistence/mongodb-record/src/test/scala/net/liftweb/mongodb/record/MongoRecordSpec.scala
@@ -25,7 +25,9 @@ import org.bson.types.ObjectId
import org.specs.Specification
import common._
+import http.js.JsExp
import json._
+import JsonDSL._
import net.liftweb.record.field.Countries
@@ -42,7 +44,7 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
val rec = MongoFieldTypeTestRecord.createRecord
val allExpectedFieldNames: List[String] = "_id" :: (for {
- typeName <- "Date DBRef JsonObject ObjectId Pattern UUID".split(" ")
+ typeName <- "Date JsonObject ObjectId Pattern UUID".split(" ")
flavor <- "mandatory legacyOptional".split(" ")
} yield flavor + typeName + "Field").toList
@@ -165,19 +167,6 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
"MongoRecord" should {
checkMongoIsRunning
- val ssr1 = SubSubRecord.createRecord.name("SubSubRecord1")
- val ssr2 = SubSubRecord.createRecord.name("SubSubRecord2")
-
- val sr1 = SubRecord.createRecord
- .name("SubRecord1")
- .subsub(ssr1)
- .subsublist(ssr1 :: ssr2 :: Nil)
- .slist("s1" :: "s2" :: Nil)
- .smap(Map("a" -> "s1", "b" -> "s2"))
- .pattern(Pattern.compile("^Mo", Pattern.CASE_INSENSITIVE))
-
- val sr2 = SubRecord.createRecord.name("SubRecord2")
-
val fttr = FieldTypeTestRecord.createRecord
//.mandatoryBinaryField()
.mandatoryBooleanField(false)
@@ -196,34 +185,82 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
val mfttr = MongoFieldTypeTestRecord.createRecord
.mandatoryDateField(new Date)
- .mandatoryJsonObjectField(TypeTestJsonObject(1, "jsonobj1"))
+ .mandatoryJsonObjectField(TypeTestJsonObject(1, "jsonobj1", Map("x" -> "1")))
.mandatoryObjectIdField(ObjectId.get)
.mandatoryPatternField(Pattern.compile("^Mo", Pattern.CASE_INSENSITIVE))
.mandatoryUUIDField(UUID.randomUUID)
- /* This causes problems if MongoDB is not running */
- if (isMongoRunning) {
- mfttr.mandatoryDBRefField(DBRefTestRecord.createRecord.getRef)
- }
+ val mfttrJson =
+ ("_id" -> ("$oid" -> mfttr.id.toString)) ~
+ ("mandatoryDateField" -> ("$dt" -> mfttr.meta.formats.dateFormat.format(mfttr.mandatoryDateField.value))) ~
+ ("legacyOptionalDateField" -> (None: Option[JObject])) ~
+ ("mandatoryJsonObjectField" -> (("intField" -> 1) ~ ("stringField" -> "jsonobj1") ~ ("mapField" -> ("x" -> "1")))) ~
+ ("legacyOptionalJsonObjectField" -> (("intField" -> 0) ~ ("stringField" -> "") ~ ("mapField" -> JObject(Nil)))) ~
+ ("mandatoryObjectIdField", ("$oid" -> mfttr.mandatoryObjectIdField.value.toString)) ~
+ ("legacyOptionalObjectIdField" -> (None: Option[JObject])) ~
+ ("mandatoryPatternField" -> (("$regex" -> mfttr.mandatoryPatternField.value.pattern) ~ ("$flags" -> mfttr.mandatoryPatternField.value.flags))) ~
+ ("legacyOptionalPatternField" -> (None: Option[JObject])) ~
+ ("mandatoryUUIDField" -> ("$uuid" -> mfttr.mandatoryUUIDField.value.toString)) ~
+ ("legacyOptionalUUIDField" -> (None: Option[JObject]))
val ltr = ListTestRecord.createRecord
.mandatoryStringListField(List("abc", "def", "ghi"))
.mandatoryIntListField(List(4, 5, 6))
- .mandatoryMongoJsonObjectListField(List(TypeTestJsonObject(1, "jsonobj1"), TypeTestJsonObject(2, "jsonobj2")))
+ .mandatoryMongoJsonObjectListField(List(TypeTestJsonObject(1, "jsonobj1", Map("x" -> "1")), TypeTestJsonObject(2, "jsonobj2", Map("x" -> "2"))))
.mongoCaseClassListField(List(MongoCaseClassTestObject(1,"str")))
+ val ltrJson =
+ ("_id" -> ("$uuid" -> ltr.id.toString)) ~
+ ("mandatoryStringListField" -> List("abc", "def", "ghi")) ~
+ ("legacyOptionalStringListField" -> List[String]()) ~
+ ("mandatoryIntListField" -> List(4, 5, 6)) ~
+ ("legacyOptionalIntListField" -> List[Int]()) ~
+ ("mandatoryMongoJsonObjectListField" -> List(
+ (("intField" -> 1) ~ ("stringField" -> "jsonobj1") ~ ("mapField" -> ("x" -> "1"))),
+ (("intField" -> 2) ~ ("stringField" -> "jsonobj2") ~ ("mapField" -> ("x" -> "2")))
+ )) ~
+ ("legacyOptionalMongoJsonObjectListField" -> List[JObject]()) ~
+ ("mongoCaseClassListField" -> List(
+ ("intField" -> 1) ~ ("stringField" -> "str")
+ ))
+
val mtr = MapTestRecord.createRecord
.mandatoryStringMapField(Map("a" -> "abc", "b" -> "def", "c" -> "ghi"))
.mandatoryIntMapField(Map("a" -> 4, "b" -> 5, "c" -> 6))
+ val mtrJson =
+ ("_id" -> mtr.id.toString) ~
+ ("mandatoryStringMapField" -> (
+ ("a" -> "abc") ~
+ ("b" -> "def") ~
+ ("c" -> "ghi")
+ )) ~
+ ("legacyOptionalStringMapField" -> JObject(Nil)) ~
+ ("mandatoryIntMapField" -> (
+ ("a" -> 4) ~
+ ("b" -> 5) ~
+ ("c" -> 6)
+ )) ~
+ ("legacyOptionalIntMapField" -> JObject(Nil))
+
+ // SubRecord
+ val ssr1 = SubSubRecord.createRecord.name("SubSubRecord1")
+ val ssr2 = SubSubRecord.createRecord.name("SubSubRecord2")
+
+ val sr1 = SubRecord.createRecord
+ .name("SubRecord1")
+ .subsub(ssr1)
+ .subsublist(ssr1 :: ssr2 :: Nil)
+ .slist("s1" :: "s2" :: Nil)
+ .smap(Map("a" -> "s1", "b" -> "s2"))
+ .pattern(Pattern.compile("^Mo", Pattern.CASE_INSENSITIVE))
+
+ val sr2 = SubRecord.createRecord.name("SubRecord2")
+
val srtr = SubRecordTestRecord.createRecord
.mandatoryBsonRecordField(sr1)
.mandatoryBsonRecordListField(List(sr1,sr2))
- val json = Printer.compact(render(mfttr.asJValue))
- val ljson = Printer.compact(render(ltr.asJValue))
- val mjson = Printer.compact(render(mtr.asJValue))
-
val sr1Json =
JObject(List(
JField("name", JString("SubRecord1")),
@@ -302,7 +339,7 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
FieldTypeTestRecord.find(fttr.id.value) must beEmpty
}
- "save and retrieve Mongo type fields" in {
+ "save and retrieve Mongo type fields with set values" in {
checkMongoIsRunning
mfttr.save
@@ -310,8 +347,6 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
val mfttrFromDb = MongoFieldTypeTestRecord.find(mfttr.id.value)
mfttrFromDb must notBeEmpty
mfttrFromDb foreach { tr =>
- tr.mandatoryDBRefField.value.getId mustEqual mfttr.mandatoryDBRefField.value.getId
- tr.mandatoryDBRefField.value.getRef mustEqual mfttr.mandatoryDBRefField.value.getRef
tr mustEqual mfttr
}
@@ -340,56 +375,54 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
}
}
+ "save and retrieve Mongo type fields with default values" in {
+ checkMongoIsRunning
+
+ val mfttrDef = MongoFieldTypeTestRecord.createRecord
+ mfttrDef.save
+
+ val mfttrFromDb = MongoFieldTypeTestRecord.find(mfttrDef.id.value)
+ mfttrFromDb must notBeEmpty
+ mfttrFromDb foreach { tr =>
+ tr mustEqual mfttrDef
+ }
+
+ val ltrDef = ListTestRecord.createRecord
+ ltrDef.save
+
+ val ltrFromDb = ListTestRecord.find(ltrDef.id.value)
+ ltrFromDb must notBeEmpty
+ ltrFromDb foreach { tr =>
+ tr mustEqual ltrDef
+ }
+
+ val mtrDef = MapTestRecord.createRecord
+ mtrDef.save
+
+ val mtrFromDb = MapTestRecord.find(mtrDef.id.value)
+ mtrFromDb must notBeEmpty
+ mtrFromDb foreach { tr =>
+ tr mustEqual mtrDef
+ }
+
+ val srtrDef = SubRecordTestRecord.createRecord
+ srtrDef.save
+
+ val srtrFromDb = SubRecordTestRecord.find(srtrDef.id.value)
+ srtrFromDb must notBeEmpty
+ srtrFromDb foreach { tr =>
+ tr mustEqual srtrDef
+ }
+ }
+
"convert Mongo type fields to JValue" in {
checkMongoIsRunning
- mfttr.asJValue mustEqual JObject(List(
- JField("_id", JObject(List(JField("$oid", JString(mfttr.id.toString))))),
- JField("mandatoryDateField", JObject(List(JField("$dt", JString(mfttr.meta.formats.dateFormat.format(mfttr.mandatoryDateField.value)))))),
- JField("legacyOptionalDateField", JNothing),
- JField("mandatoryDBRefField", JNothing),
- JField("legacyOptionalDBRefField", JNothing),
- JField("mandatoryJsonObjectField", JObject(List(JField("intField", JInt(1)), JField("stringField", JString("jsonobj1"))))),
- JField("legacyOptionalJsonObjectField", JObject(List(JField("intField", JInt(0)), JField("stringField", JString(""))))),
- JField("mandatoryObjectIdField", JObject(List(JField("$oid", JString(mfttr.mandatoryObjectIdField.value.toString))))),
- JField("legacyOptionalObjectIdField", JNothing),
- JField("mandatoryPatternField", JObject(List(JField("$regex", JString(mfttr.mandatoryPatternField.value.pattern)), JField("$flags", JInt(mfttr.mandatoryPatternField.value.flags))))),
- JField("legacyOptionalPatternField", JNothing),
- JField("mandatoryUUIDField", JObject(List(JField("$uuid", JString(mfttr.mandatoryUUIDField.value.toString))))),
- JField("legacyOptionalUUIDField", JNothing)
- ))
+ mfttr.asJValue mustEqual mfttrJson
- ltr.asJValue mustEqual JObject(List(
- JField("_id", JObject(List(JField("$uuid", JString(ltr.id.toString))))),
- JField("mandatoryStringListField", JArray(List(JString("abc"), JString("def"), JString("ghi")))),
- JField("legacyOptionalStringListField", JArray(List())),
- JField("mandatoryIntListField", JArray(List(JInt(4), JInt(5), JInt(6)))),
- JField("legacyOptionalIntListField", JArray(List())),
- JField("mandatoryMongoJsonObjectListField", JArray(List(
- JObject(List(JField("intField", JInt(1)), JField("stringField", JString("jsonobj1")))),
- JObject(List(JField("intField", JInt(2)), JField("stringField", JString("jsonobj2"))))
- ))),
- JField("legacyOptionalMongoJsonObjectListField", JArray(List())),
- JField("mongoCaseClassListField",JArray(List(
- JObject(List(JField("intField", JInt(1)), JField("stringField", JString("str"))))
- )))
- ))
+ ltr.asJValue mustEqual ltrJson
- mtr.asJValue mustEqual JObject(List(
- JField("_id", JString(mtr.id.toString)),
- JField("mandatoryStringMapField", JObject(List(
- JField("a", JString("abc")),
- JField("b", JString("def")),
- JField("c", JString("ghi"))
- ))),
- JField("legacyOptionalStringMapField", JObject(List())),
- JField("mandatoryIntMapField", JObject(List(
- JField("a", JInt(4)),
- JField("b", JInt(5)),
- JField("c", JInt(6))
- ))),
- JField("legacyOptionalIntMapField", JObject(List()))
- ))
+ mtr.asJValue mustEqual mtrJson
val srtrAsJValue = srtr.asJValue
srtrAsJValue \\ "_id" mustEqual srtrJson \\ "_id"
@@ -402,72 +435,35 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
"convert Mongo type fields to JsExp" in {
checkMongoIsRunning
- /*
- mfttr.asJsExp mustEqual JsObj(
- ("_id", JsObj(("$oid", Str(mfttr.id.toString)))),
- ("mandatoryDateField", JsObj(("$dt", Str(mfttr.meta.formats.dateFormat.format(mfttr.mandatoryDateField.value))))),
- ("legacyOptionalDateField", Str("null")),
- ("mandatoryDBRefField", Str("null")),
- ("legacyOptionalDBRefField", Str("null")),
- ("mandatoryJsonObjectField", JsObj(("intField", Num(1)), ("stringField", Str("jsonobj1")))),
- ("legacyOptionalJsonObjectField", JsObj(("intField", Num(0)), ("stringField", Str("")))),
- ("mandatoryObjectIdField", JsObj(("$oid", Str(mfttr.mandatoryObjectIdField.value.toString)))),
- ("legacyOptionalObjectIdField", Str("null")),
- ("mandatoryPatternField", JsObj(("$regex", Str(mfttr.mandatoryPatternField.value.pattern)), ("$flags", Num(mfttr.mandatoryPatternField.value.flags)))),
- ("legacyOptionalPatternField", Str("null")),
- ("mandatoryUUIDField", JsObj(("$uuid", Str(mfttr.mandatoryUUIDField.value.toString)))),
- ("legacyOptionalUUIDField", Str("null"))
- )*/
-
- /*
- ltr.asJsExp mustEqual JsObj(
- ("_id", JsObj(("$oid", Str(ltr.id.toString)))),
- ("mandatoryStringListField", JsArray(Str("abc"), Str("def"), Str("ghi"))),
- ("legacyOptionalStringListField", JsArray()),
- ("mandatoryIntListField", JsArray(Num(4), Num(5), Num(6))),
- ("legacyOptionalIntListField", JsArray()),
- ("mandatoryMongoJsonObjectListField", JsArray(
- JsObj(("intField", Num(1)), ("stringField", Str("jsonobj1"))),
- JsObj(("intField", Num(2)), ("stringField", Str("jsonobj2")))
- )),
- ("legacyOptionalMongoJsonObjectListField", JsArray())
- )
+ mfttr.asJsExp mustEqual new JsExp {
+ def toJsCmd = compact(render(mfttrJson))
+ }
- mtr.asJsExp mustEqual JsObj(
- ("_id", JsObj(("$oid", Str(mtr.id.toString)))),
- ("_id", JsObj(("$oid", Str(mtr.id.toString)))),
- ("mandatoryStringMapField", JsObj(
- ("a", Str("abc")),
- ("b", Str("def")),
- ("c", Str("ghi"))
- )),
- ("legacyOptionalStringMapField", JsObj()),
- ("mandatoryIntMapField", JsObj(
- ("a", Num(4)),
- ("b", Num(5)),
- ("c", Num(6))
- )),
- ("legacyOptionalIntMapField", JsObj())
- )*/
+ ltr.asJsExp mustEqual new JsExp {
+ def toJsCmd = compact(render(ltrJson))
+ }
+ mtr.asJsExp mustEqual new JsExp {
+ def toJsCmd = compact(render(mtrJson))
+ }
}
"get set from json string using lift-json parser" in {
checkMongoIsRunning
- val mfftrFromJson = MongoFieldTypeTestRecord.fromJsonString(json)
+ val mfftrFromJson = MongoFieldTypeTestRecord.fromJsonString(compact(render(mfttrJson)))
mfftrFromJson must notBeEmpty
mfftrFromJson foreach { tr =>
tr mustEqual mfttr
}
- val ltrFromJson = ListTestRecord.fromJsonString(ljson)
+ val ltrFromJson = ListTestRecord.fromJsonString(compact(render(ltrJson)))
ltrFromJson must notBeEmpty
ltrFromJson foreach { tr =>
tr mustEqual ltr
}
- val mtrFromJson = MapTestRecord.fromJsonString(mjson)
+ val mtrFromJson = MapTestRecord.fromJsonString(compact(render(mtrJson)))
mtrFromJson must notBeEmpty
mtrFromJson foreach { tr =>
tr mustEqual mtr
View
7 persistence/mongodb/src/main/scala/net/liftweb/mongodb/JObjectParser.scala
@@ -50,14 +50,11 @@ object JObjectParser {
case x if datetype_?(x.getClass) => datetype2jvalue(x)(formats)
case x if mongotype_?(x.getClass) => mongotype2jvalue(x)(formats)
case x: BasicDBList => JArray(x.toList.map( x => serialize(x, formats)))
- case x: BasicDBObject =>
+ case x: BasicDBObject => JObject(
x.keySet.toList.map { f =>
JField(f.toString, serialize(x.get(f.toString), formats))
}
- match {
- case Nil => JNothing
- case fields => JObject(fields)
- }
+ )
case x => {
JNothing
}
View
90 persistence/mongodb/src/test/scala/net/liftweb/mongodb/MongoDocumentSpec.scala
@@ -17,6 +17,7 @@
package net.liftweb
package mongodb
+import org.bson.types.ObjectId
import org.specs.Specification
import common._
@@ -25,7 +26,7 @@ import json.ext.JsonBoxSerializer
package mongodocumentspecs {
case class Primitives(
- _id: String,
+ _id: ObjectId,
s: String,
i: Int,
l: Long,
@@ -46,37 +47,58 @@ package mongodocumentspecs {
def meta = Primitives
}
- object Primitives extends MongoDocumentMeta[Primitives]
+ object Primitives extends MongoDocumentMeta[Primitives] {
+ override def formats = super.formats + new ObjectIdSerializer
+ }
case class NullTestEmbed(nul: String)
- case class NullTestDoc(_id: String, nul: String, ent: NullTestEmbed)
+ case class NullTestDoc(_id: ObjectId, nul: String, ent: NullTestEmbed)
extends MongoDocument[NullTestDoc] {
def meta = NullTestDoc
}
- object NullTestDoc extends MongoDocumentMeta[NullTestDoc]
+ object NullTestDoc extends MongoDocumentMeta[NullTestDoc] {
+ override def formats = super.formats + new ObjectIdSerializer
+ }
- case class OptionTestDoc(_id: String, optNone: Option[String],
+ case class OptionTestDoc(_id: ObjectId, optNone: Option[String],
optSome: Option[String])
extends MongoDocument[OptionTestDoc] {
def meta = OptionTestDoc
}
- object OptionTestDoc extends MongoDocumentMeta[OptionTestDoc]
+ object OptionTestDoc extends MongoDocumentMeta[OptionTestDoc] {
+ override def formats = super.formats + new ObjectIdSerializer
+ }
- case class BoxTestDoc(_id: String, boxEmpty: Box[String],
+ case class BoxTestDoc(_id: ObjectId, boxEmpty: Box[String],
boxFull: Box[String], boxFail: Box[String])
extends MongoDocument[BoxTestDoc] {
def meta = BoxTestDoc
}
object BoxTestDoc extends MongoDocumentMeta[BoxTestDoc] {
- override def formats = super.formats + new JsonBoxSerializer
+ override def formats = super.formats + new JsonBoxSerializer + new ObjectIdSerializer
+ }
+
+ case class MapTestDoc(_id: ObjectId, aMap: Map[String, String])
+ extends MongoDocument[MapTestDoc] {
+ def meta = MapTestDoc
+ }
+ object MapTestDoc extends MongoDocumentMeta[MapTestDoc] {
+ override def formats = super.formats + new ObjectIdSerializer
}
}
/**
- * System under specification for MongoDocument
+ * System specification for MongoDocument
*/
object MongoDocumentSpec extends Specification("MongoDocument Specification") with MongoTestKit {
+ def passSaveAndRetrieveTests(obj: MongoDocument[_], meta: MongoDocumentMeta[_]): Unit = {
+ obj.save
+ val objFromDb = meta.find(obj._id.asInstanceOf[ObjectId])
+ objFromDb.isDefined must_== true
+ objFromDb.get must_== obj
+ }
+
"MongoDocument" should {
"handle primitives" in {
@@ -84,7 +106,7 @@ object MongoDocumentSpec extends Specification("MongoDocument Specification") wi
import mongodocumentspecs._
val primitives = Primitives(
- "1",
+ ObjectId.get,
"This is a String",
123,
124L,
@@ -102,59 +124,43 @@ object MongoDocumentSpec extends Specification("MongoDocument Specification") wi
java.lang.Boolean.TRUE,
new java.lang.Short("135")
)
-
- primitives.save
-
- val pFromDb = Primitives.find(primitives._id)
-
- pFromDb.isDefined must_== true
-
- pFromDb.get must_== primitives
+ passSaveAndRetrieveTests(primitives, Primitives)
}
"handle null" in {
checkMongoIsRunning
import mongodocumentspecs._
- val ntd = NullTestDoc("1", null, NullTestEmbed(null))
-
- ntd.save
-
- val ntdFromDb = NullTestDoc.find(ntd._id)
-
- ntdFromDb.isDefined must_== true
-
- ntdFromDb.get must_== ntd
+ val ntd = NullTestDoc(ObjectId.get, null, NullTestEmbed(null))
+ passSaveAndRetrieveTests(ntd, NullTestDoc)
}
"handle Option" in {
checkMongoIsRunning
import mongodocumentspecs._
- val otd = OptionTestDoc("1", None, Some("Some String"))
-
- otd.save
-
- val otdFromDb = OptionTestDoc.find(otd._id)
-
- otdFromDb.isDefined must_== true
-
- otdFromDb.get must_== otd
+ val otd = OptionTestDoc(ObjectId.get, None, Some("Some String"))
+ passSaveAndRetrieveTests(otd, OptionTestDoc)
}
"handle Box using JsonBoxSerializer" in {
checkMongoIsRunning
import mongodocumentspecs._
- val btd = BoxTestDoc("1", Empty, Full("Full String"), Failure("This is a failure"))
-
- btd.save
+ val btd = BoxTestDoc(ObjectId.get, Empty, Full("Full String"), Failure("This is a failure"))
+ passSaveAndRetrieveTests(btd, BoxTestDoc)
+ }
- val btdFromDb = BoxTestDoc.find(btd._id)
+ "handle Maps properly" in {
+ checkMongoIsRunning
+ import mongodocumentspecs._
- btdFromDb.isDefined must_== true
+ val mtd = MapTestDoc(ObjectId.get, Map("x" -> "1"))
+ passSaveAndRetrieveTests(mtd, MapTestDoc)
- btdFromDb.get must_== btd
+ // empty map
+ val mtd2 = MapTestDoc(ObjectId.get, Map[String, String]())
+ passSaveAndRetrieveTests(mtd2, MapTestDoc)
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.