Permalink
Browse files

Minor improvements

  • Loading branch information...
1 parent df43fa1 commit 2b913a56a1e78c57149de6b913557806b3375883 @eltimn eltimn committed Aug 1, 2012
@@ -44,10 +44,7 @@ class MongoFieldSpec extends MongoTestSpecification {
import fixtures._
val session = new LiftSession("", randomString(20), Empty)
- /*def inLiftSession(a: =>Any) = S.initIfUninitted(session) { a }
- new SpecContext {
- aroundExpectations(inLiftSession(_))
- }*/
+
def passBasicTests[A](
example: A,
mandatory: MandatoryTypedField[A],
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2012 WorldWide Conferencing, LLC
+ *
+ * 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 net.liftweb
+package mongodb
+package record
+
+/**
+ * Systems under specification for MongoRecord.
+ */
+class MongoRecordFieldSpec extends MongoTestSpecification {
+ import fixtures._
+
+ "MongoRecord field introspection" should {
+ val rec = MongoFieldTypeTestRecord.createRecord
+ val allExpectedFieldNames: List[String] = "_id" :: "mandatoryMongoCaseClassField" ::
+ (for {
+ typeName <- "Date JsonObject ObjectId Pattern UUID".split(" ")
+ flavor <- "mandatory legacyOptional".split(" ")
+ } yield flavor + typeName + "Field").toList
+
+ "introspect only the expected fields" in {
+ rec.fields().map(_.name).filterNot(allExpectedFieldNames.contains(_)) must_== Nil
+ }
+
+ "correctly look up fields by name" in {
+ for (name <- allExpectedFieldNames) {
+ rec.fieldByName(name).isDefined must beTrue
+ }
+ success
+ }
+
+ "not look up fields by bogus names" in {
+ for (name <- allExpectedFieldNames) {
+ rec.fieldByName("x" + name + "y").isDefined must beFalse
+ }
+ success
+ }
+ }
+}
+
@@ -0,0 +1,126 @@
+/*
+ * Copyright 2012 WorldWide Conferencing, LLC
+ *
+ * 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 net.liftweb
+package mongodb
+package record
+
+import org.specs2.execute.Result
+
+/**
+ * Systems under specification for MongoRecord.
+ */
+class MongoRecordLifecycleSpec extends MongoTestSpecification {
+ import fixtures._
+ "MongoRecordLifecycle callbacks" should {
+ def testOneHarness(scope: String, f: LifecycleTestRecord => HarnessedLifecycleCallbacks): Result = {
+ ("be called before validation when specified at " + scope) in {
+ val rec = LifecycleTestRecord.createRecord
+ var triggered = false
+ f(rec).beforeValidationHarness = () => triggered = true
+ rec.foreachCallback(_.beforeValidation)
+ triggered must_== true
+ }
+
+ ("be called after validation when specified at " + scope) in {
+ val rec = LifecycleTestRecord.createRecord
+ var triggered = false
+ f(rec).afterValidationHarness = () => triggered = true
+ rec.foreachCallback(_.afterValidation)
+ triggered must_== true
+ }
+
+ ("be called around validate when specified at " + scope) in {
+ val rec = LifecycleTestRecord.createRecord
+ var triggeredBefore = false
+ var triggeredAfter = false
+ f(rec).beforeValidationHarness = () => triggeredBefore = true
+ f(rec).afterValidationHarness = () => triggeredAfter = true
+ rec.validate must_== Nil
+ triggeredBefore must_== true
+ triggeredAfter must_== true
+ }
+
+ ("be called before save when specified at " + scope) in {
+ val rec = LifecycleTestRecord.createRecord
+ var triggered = false
+ f(rec).beforeSaveHarness = () => triggered = true
+ rec.foreachCallback(_.beforeSave)
+ triggered must_== true
+ }
+
+ ("be called before create when specified at " + scope) in {
+ val rec = LifecycleTestRecord.createRecord
+ var triggered = false
+ f(rec).beforeCreateHarness = () => triggered = true
+ rec.foreachCallback(_.beforeCreate)
+ triggered must_== true
+ }
+
+ ("be called before update when specified at " + scope) in {
+ val rec = LifecycleTestRecord.createRecord
+ var triggered = false
+ f(rec).beforeUpdateHarness = () => triggered = true
+ rec.foreachCallback(_.beforeUpdate)
+ triggered must_== true
+ }
+
+ ("be called after save when specified at " + scope) in {
+ val rec = LifecycleTestRecord.createRecord
+ var triggered = false
+ f(rec).afterSaveHarness = () => triggered = true
+ rec.foreachCallback(_.afterSave)
+ triggered must_== true
+ }
+
+ ("be called after create when specified at " + scope) in {
+ val rec = LifecycleTestRecord.createRecord
+ var triggered = false
+ f(rec).afterCreateHarness = () => triggered = true
+ rec.foreachCallback(_.afterCreate)
+ triggered must_== true
+ }
+
+ ("be called after update when specified at " + scope) in {
+ val rec = LifecycleTestRecord.createRecord
+ var triggered = false
+ f(rec).afterUpdateHarness = () => triggered = true
+ rec.foreachCallback(_.afterUpdate)
+ triggered must_== true
+ }
+
+ ("be called before delete when specified at " + scope) in {
+ val rec = LifecycleTestRecord.createRecord
+ var triggered = false
+ f(rec).beforeDeleteHarness = () => triggered = true
+ rec.foreachCallback(_.beforeDelete)
+ triggered must_== true
+ }
+
+ ("be called after delete when specified at " + scope) in {
+ val rec = LifecycleTestRecord.createRecord
+ var triggered = false
+ f(rec).afterDeleteHarness = () => triggered = true
+ rec.foreachCallback(_.afterDelete)
+ triggered must_== true
+ }
+ success
+ }
+
+ testOneHarness("the field level", rec => rec.stringFieldWithCallbacks: HarnessedLifecycleCallbacks)
+ }
+}
+
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2010-2011 WorldWide Conferencing, LLC
+ *
+ * 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 net.liftweb
+package mongodb
+package record
+
+import org.specs2.mutable.Specification
+import org.specs2.specification.{Fragments, Step}
+
+import com.mongodb._
+
+trait MongoTestSpecification extends Specification {
+
+ override def map(fs: => Fragments) = Step(doBeforeSpec) ^ fs ^ Step(doAfterSpec)
+
+ def dbName = "lift_record_"+this.getClass.getName
+ .replace("$", "")
+ .replace("net.liftweb.mongodb.record.", "")
+ .replace(".", "_")
+ .toLowerCase
+
+ val defaultHost = MongoHost("127.0.0.1", 27017)
+
+ // If you need more than one db, override this
+ def dbs: List[(MongoIdentifier, MongoHost, String)] =
+ List((DefaultMongoIdentifier, defaultHost, dbName))
+
+ def debug = false
+
+ def doBeforeSpec: Unit = {
+ // define the dbs
+ dbs foreach { dbtuple =>
+ MongoDB.defineDb(dbtuple._1, MongoAddress(dbtuple._2, dbtuple._3))
+ }
+ }
+
+ def isMongoRunning: Boolean =
+ try {
+ if (dbs.length < 1)
+ false
+ else {
+ dbs foreach { dbtuple =>
+ MongoDB.use(dbtuple._1) ( db => { db.getLastError } )
+ }
+ true
+ }
+ } catch {
+ case _ => false
+ }
+
+ def checkMongoIsRunning = isMongoRunning must beEqualTo(true).orSkip("MongoDB is not running")
+
+ def doAfterSpec: Unit = {
+ if (!debug && isMongoRunning) {
+ // drop the databases
+ dbs foreach { dbtuple =>
+ MongoDB.use(dbtuple._1) { db => db.dropDatabase }
+ }
+ }
+
+ // clear the mongo instances
+ MongoDB.close
+ }
+}
+
Oops, something went wrong.

0 comments on commit 2b913a5

Please sign in to comment.