Skip to content

Commit

Permalink
Issue 1118 - Make MongoMetaRecord.useColl as public and move up to Mo…
Browse files Browse the repository at this point in the history
…ngoMeta
  • Loading branch information
Tim Nelson committed Nov 4, 2011
1 parent ddf1cb8 commit 993026c
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 65 deletions.
Expand Up @@ -124,8 +124,8 @@ trait BsonMetaRecord[BaseRecord <: BsonRecord[BaseRecord]] extends MetaRecord[Ba
* using setFromAny passing it the DBObject returned from Mongo.
*
* @param inst - the record that will be populated
* @param obj - The DBObject
* @return Box[BaseRecord]
* @param dbo - The DBObject
* @return Unit
*/
def setFieldsFromDBObject(inst: BaseRecord, dbo: DBObject): Unit = {
for (k <- dbo.keySet; field <- inst.fieldByName(k.toString)) {
Expand Down
Expand Up @@ -63,9 +63,6 @@ trait MongoMetaRecord[BaseRecord <: MongoRecord[BaseRecord]]
true
}

protected def useColl[T](f: DBCollection => T) =
MongoDB.useCollection(mongoIdentifier, collectionName)(f)

def bulkDelete_!!(qry: DBObject): Unit = {
useColl(coll =>
coll.remove(qry)
Expand Down Expand Up @@ -276,7 +273,7 @@ trait MongoMetaRecord[BaseRecord <: MongoRecord[BaseRecord]]
* Update records with a JObject query
*/
def update(qry: JObject, newbr: BaseRecord, opts: UpdateOption*) {
MongoDB.use(mongoIdentifier) ( db =>
useDb ( db =>
update(qry, newbr, db, opts :_*)
)
}
Expand Down
Expand Up @@ -27,7 +27,7 @@ import org.specs.Specification

import common._
import json.{Num => _, _}
import JsonDSL._
import BsonDSL._
import util.Helpers.randomString
import http.{LiftSession, S}
import http.js.JE._
Expand Down
Expand Up @@ -31,6 +31,7 @@ import JsonDSL._

import net.liftweb.record.field.Countries

import com.mongodb._

/**
* Systems under specification for MongoRecord.
Expand Down Expand Up @@ -560,6 +561,63 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
rftr.mandatoryLongRefListField.objs mustEqual List(btr)
}

"use defaultValue when field is not present in the database" in {
checkMongoIsRunning

val missingFieldDocId = ObjectId.get

// create a dbobject with no fields manually
val builder = BasicDBObjectBuilder.start
.add("_id", missingFieldDocId)

FieldTypeTestRecord.useColl { coll => coll.save(builder.get) }

val recFromDb = FieldTypeTestRecord.find(missingFieldDocId)

recFromDb must notBeEmpty

recFromDb foreach { r =>
r.mandatoryBooleanField.is must_== false
r.legacyOptionalBooleanField
r.optionalBooleanField.is must beEmpty
r.mandatoryCountryField.is must_== Countries.C1
r.legacyOptionalCountryField.valueBox must beEmpty
r.optionalCountryField.is must beEmpty
r.mandatoryDecimalField.is must_== 0.00
r.legacyOptionalDecimalField.valueBox must beEmpty
r.optionalDecimalField.is must beEmpty
r.mandatoryDoubleField.is must_== 0d
r.legacyOptionalDoubleField.valueBox must beEmpty
r.optionalDoubleField.is must beEmpty
r.mandatoryEmailField.is must_== ""
r.legacyOptionalEmailField.valueBox must beEmpty
r.optionalEmailField.is must beEmpty
r.mandatoryEnumField.is must_== MyTestEnum.ONE
r.legacyOptionalEnumField.valueBox must beEmpty
r.optionalEnumField.is must beEmpty
r.mandatoryIntField.is must_== 0
r.legacyOptionalIntField.valueBox must beEmpty
r.optionalIntField.is must beEmpty
r.mandatoryLocaleField.is must_== "en_US"
r.legacyOptionalLocaleField.valueBox must beEmpty
r.optionalLocaleField.is must beEmpty
r.mandatoryLongField.is must_== 0L
r.legacyOptionalLongField.valueBox must beEmpty
r.optionalLongField.is must beEmpty
r.mandatoryPostalCodeField.is must_== ""
r.legacyOptionalPostalCodeField.valueBox must beEmpty
r.optionalPostalCodeField.is must beEmpty
r.mandatoryStringField.is must_== ""
r.legacyOptionalStringField.valueBox must beEmpty
r.optionalStringField.is must beEmpty
r.mandatoryTextareaField.is must_== ""
r.legacyOptionalTextareaField.valueBox must beEmpty
r.optionalTextareaField.is must beEmpty
r.mandatoryTimeZoneField.is must_== "America/Chicago"
r.legacyOptionalTimeZoneField.valueBox must beEmpty
r.optionalTimeZoneField.is must beEmpty
}
}
}
}

Expand Up @@ -19,7 +19,7 @@ import org.bson.types.ObjectId
import json.{DefaultFormats, Formats}
import json.JsonAST.JObject

import com.mongodb.{BasicDBObject, DB, DBObject}
import com.mongodb.{BasicDBObject, DB, DBCollection, DBObject}

trait JsonFormats {
// override this for custom Formats
Expand Down Expand Up @@ -64,23 +64,26 @@ trait MongoMeta[BaseDocument] extends JsonFormats {
// override this to specify a MongoIdentifier for this MongoDocument type
def mongoIdentifier: MongoIdentifier = DefaultMongoIdentifier

/*
* Use the collection associated with this Meta.
*/
def useColl[T](f: DBCollection => T) =
MongoDB.useCollection(mongoIdentifier, collectionName)(f)

/*
* Use the db associated with this Meta.
*/
def useDb[T](f: DB => T) = MongoDB.use(mongoIdentifier)(f)

/*
* Count all documents
*/
def count: Long = {
MongoDB.useCollection(mongoIdentifier, collectionName) ( coll =>
coll.getCount
)
}
def count: Long = useColl { coll => coll.getCount }

/*
* Count documents by DBObject query
*/
def count(qry: DBObject):Long = {
MongoDB.useCollection(mongoIdentifier, collectionName) ( coll =>
coll.getCount(qry)
)
}
def count(qry: DBObject):Long = useColl { coll => coll.getCount(qry) }

/*
* Count documents by JObject query
Expand All @@ -90,20 +93,14 @@ trait MongoMeta[BaseDocument] extends JsonFormats {
/*
* Count distinct records on a given field
*/
def countDistinct(key: String, query: DBObject): Long = {
MongoDB.useCollection(mongoIdentifier, collectionName) ( coll =>
coll.distinct(key, query).size
)
}
def countDistinct(key: String, query: DBObject): Long =
useColl { coll => coll.distinct(key, query).size }

/*
* Delete documents by a DBObject query
*/
def delete(qry: DBObject) {
MongoDB.useCollection(mongoIdentifier, collectionName) ( coll =>
coll.remove(qry)
)
}
def delete(qry: DBObject): Unit =
useColl { coll => coll.remove(qry) }

// delete a document
def delete(k: String, v: Any) {
Expand All @@ -116,45 +113,35 @@ trait MongoMeta[BaseDocument] extends JsonFormats {
/*
* Delete documents by a JObject query
*/
def delete(qry: JObject) {
delete(JObjectParser.parse(qry))
}
def delete(qry: JObject): Unit = delete(JObjectParser.parse(qry))

/* drop this document collection */
def drop {
MongoDB.useCollection(mongoIdentifier, collectionName) ( coll =>
coll.drop
)
}
def drop: Unit = useColl { coll => coll.drop }

/*
* Ensure an index exists
*/
def ensureIndex(keys: JObject) {
MongoDB.useCollection(mongoIdentifier, collectionName) ( coll => {
coll.ensureIndex(JObjectParser.parse(keys))
})
}
def ensureIndex(keys: JObject): Unit =
useColl { coll => coll.ensureIndex(JObjectParser.parse(keys)) }

/*
* Ensure an index exists and make unique
*/
def ensureIndex(keys: JObject, unique: Boolean) {
def ensureIndex(keys: JObject, unique: Boolean): Unit = {
val options = new BasicDBObject
if (unique) options.put("unique", true)
MongoDB.useCollection(mongoIdentifier, collectionName) ( coll => {
useColl { coll =>
coll.ensureIndex(JObjectParser.parse(keys), options)
})
}
}

/*
* Ensure an index exists with options
*/
def ensureIndex(keys: JObject, opts: JObject) {
MongoDB.useCollection(mongoIdentifier, collectionName) ( coll => {
def ensureIndex(keys: JObject, opts: JObject): Unit =
useColl { coll =>
coll.ensureIndex(JObjectParser.parse(keys), JObjectParser.parse(opts))
})
}
}

/*
* Update document with a DBObject query using the given Mongo instance.
Expand Down Expand Up @@ -185,9 +172,7 @@ trait MongoMeta[BaseDocument] extends JsonFormats {
* Update document with a JObject query.
*/
def update(qry: JObject, newobj: JObject, opts: UpdateOption*) {
MongoDB.use(mongoIdentifier) ( db => {
update(qry, newobj, db, opts :_*)
})
useDb { db => update(qry, newobj, db, opts :_*) }
}
}

Expand Down
Expand Up @@ -17,6 +17,8 @@
package net.liftweb
package mongodb

import BsonDSL._

import java.util.{Calendar, Date, UUID}
import java.util.regex.Pattern

Expand All @@ -27,7 +29,6 @@ import org.specs.Specification

import json.DefaultFormats
import json.JsonParser._
import json.JsonDSL._


package mongotestdocs {
Expand Down Expand Up @@ -175,7 +176,7 @@ package mongotestdocs {
/**
* Systems under specification for MongoDocumentExamples.
*/
object MongoDocumentExamplesSpec extends Specification("MongoDocumentExamples Specification") with MongoTestKit {
class MongoDocumentExamplesSpec extends Specification("MongoDocumentExamples Specification") with MongoTestKit {
import mongotestdocs._

override def dbName = "lift_mongodocumentexamples"
Expand All @@ -200,16 +201,14 @@ object MongoDocumentExamplesSpec extends Specification("MongoDocumentExamples Sp
p mustEqual pFromDb.get

// retrieve it using a Json query
def pFromDbViaJson = SimplePerson.find(("_id" -> ("$oid" -> p._id.toString)))
def pFromDbViaJson = SimplePerson.find(("_id" -> pid))

pFromDbViaJson.isDefined must_== true

p mustEqual pFromDbViaJson.get

// modify and save the person
// with scala 2.8 you can use the copy function to do this
// val p3 = p.copy(name = "Tim3")
val p2 = SimplePerson(p._id, "Timm", 27)
val p2 = p.copy(name="Timm", age=27)
p2.save
pFromDb.isDefined must_== true
p2 must_== pFromDb.get
Expand All @@ -220,16 +219,16 @@ object MongoDocumentExamplesSpec extends Specification("MongoDocumentExamples Sp

all.isEmpty must_== false

if (!debug) {
all.size must_== 1
all.head must_== p2
all.size must_== 1
all.head must_== p2

// delete it
p2.delete
// delete it
p2.delete

pFromDb.isEmpty must_== true
pFromDbViaJson.isEmpty must_== true
pFromDb.isEmpty must_== true
pFromDbViaJson.isEmpty must_== true

if (!debug) {
SimplePerson.drop
}
}
Expand Down
Expand Up @@ -250,7 +250,7 @@ trait AbstractScreen extends Factory {
stuff)

/**
* Add a validtion (the wacky symbols are supposed to look like a check mark)
* Add a validation (the wacky symbols are supposed to look like a check mark)
*/
def ^/(f: T => List[FieldError]): FieldBuilder[T] =
new FieldBuilder[T](name, default,
Expand Down

0 comments on commit 993026c

Please sign in to comment.