/casbah-commons/src/main/scala/test/MongoDBSpecification.scala

https://github.com/derzzle/casbah · Scala · 150 lines · 95 code · 25 blank · 30 comment · 9 complexity · 19f61e0a0e854b3ba5d83338bd8a65bf MD5 · raw file

  1. /**
  2. * Copyright (c) 2010, 2011 10gen, Inc. <http://10gen.com>
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package com.mongodb.casbah.commons
  18. package test
  19. import com.mongodb.casbah.commons.Imports._
  20. import org.specs2.mutable._
  21. import org.specs2.data.Sized
  22. import org.specs2.matcher.{ Expectable, Matcher, MapMatchers }
  23. import org.specs2.matcher.Matchers._
  24. import com.mongodb.casbah.util.Logging
  25. import com.mongodb.casbah.commons.MongoDBObject
  26. import javax.management.remote.rmi._RMIConnection_Stub
  27. object `package` {
  28. }
  29. trait CasbahSpecification extends Specification with DBObjectMatchers with Logging {
  30. implicit val sizedOptDBObj = new Sized[Option[DBObject]] {
  31. def size(t: Option[DBObject]) = t.getOrElse(MongoDBObject.empty).size
  32. }
  33. implicit val sizedDBObj = new Sized[DBObject] {
  34. def size(t: DBObject) = t.size
  35. }
  36. implicit val sizedOptDBList = new Sized[Option[MongoDBList]] {
  37. def size(t: Option[MongoDBList]) = t.getOrElse(MongoDBList.empty).size
  38. }
  39. implicit val sizedDBList = new Sized[MongoDBList] {
  40. def size(t: MongoDBList) = t.size
  41. }
  42. }
  43. trait DBObjectMatchers extends DBObjectBaseMatchers
  44. trait DBObjectBaseMatchers extends Logging {
  45. protected def someField(map: Expectable[Option[DBObject]], k: String) = if (k.indexOf('.') < 0) {
  46. map.value.getOrElse(MongoDBObject.empty).getAs[AnyRef](k)
  47. } else {
  48. map.value.getOrElse(MongoDBObject.empty).expand[AnyRef](k)
  49. }
  50. protected def field(map: Expectable[DBObject], k: String) = if (k.indexOf('.') < 0) {
  51. map.value.getAs[AnyRef](k)
  52. } else {
  53. map.value.expand[AnyRef](k)
  54. }
  55. protected def listField(map: Expectable[DBObject], k: String) = if (k.indexOf('.') < 0) {
  56. map.value.getAs[Seq[Any]](k)
  57. } else {
  58. map.value.expand[Seq[Any]](k)
  59. }
  60. def beDBObject: Matcher[AnyRef] = ((_: AnyRef).isInstanceOf[DBObject], " is a DBObject", " is not a DBObject")
  61. def beMongoDBObject: Matcher[AnyRef] = ((_: AnyRef).isInstanceOf[MongoDBObject], " is a MongoDBObject", " is not a MongoDBObject")
  62. def beMongoDBList: Matcher[AnyRef] = ((_: AnyRef).isInstanceOf[MongoDBList], " is a MongoDBList", " is not a MongoDBList")
  63. def haveSomeField(k: String) = new Matcher[Option[DBObject]] {
  64. def apply[S <: Option[DBObject]](map: Expectable[S]) = {
  65. result(someField(map, k).isDefined, map.description + " has the key " + k, map.description + " doesn't have the key " + k, map)
  66. }
  67. }
  68. /** matches if dbObject.contains(k) */
  69. def haveField(k: String) = new Matcher[DBObject] {
  70. def apply[S <: DBObject](map: Expectable[S]) = {
  71. result(field(map, k).isDefined, map.description + " has the key " + k, map.description + " doesn't have the key " + k, map)
  72. }
  73. }
  74. /** matches if a Some(map) contains a pair (key, value) == (k, v)
  75. * Will expand out dot notation for matching.
  76. **/
  77. def haveSomeEntry[V](p: (String, V)) = new Matcher[Option[DBObject]] {
  78. def apply[S <: Option[DBObject]](map: Expectable[S]) = {
  79. result(someField(map, p._1).exists(_ == p._2), // match only the value
  80. map.description + " has the pair " + p, map.description + " doesn't have the pair " + p, map)
  81. }
  82. }
  83. /** Special version of "HaveEntry" that expects a list and then uses
  84. * "hasSameElements" on it.
  85. */
  86. def haveListEntry(k: String, l: => Traversable[Any]) = new Matcher[DBObject] {
  87. def apply[S <: DBObject](map: Expectable[S]) = {
  88. val objL = listField(map, k).getOrElse(Seq.empty[Any]).toSeq
  89. val _l = l.toSeq
  90. result(objL.sameElements(_l), // match only the value
  91. map.description + " has the pair " + k,
  92. map.description + " doesn't have the pair " + k,
  93. map)
  94. }
  95. }
  96. /** matches if map contains a pair (key, value) == (k, v)
  97. * Will expand out dot notation for matching.
  98. **/
  99. def haveEntry[V](p: (String, V)) = new Matcher[DBObject] {
  100. def apply[S <: DBObject](map: Expectable[S]) = {
  101. result(field(map, p._1).exists(_.equals(p._2)), // match only the value
  102. map.description + " has the pair " + p,
  103. map.description + "[" + field(map, p._1) + "] doesn't have the pair " + p + "[" + p._2 + "]",
  104. map)
  105. }
  106. }
  107. /** matches if Some(map) contains all the specified pairs
  108. * can expand dot notation to match specific sub-keys */
  109. def haveSomeEntries[V](pairs: (String, V)*) = new Matcher[Option[DBObject]] {
  110. def apply[S <: Option[DBObject]](map: Expectable[S]) = {
  111. result(pairs.forall(pair => someField(map, pair._1).exists(_ == pair._2) /* match only the value */ ),
  112. map.description + " has the pairs " + pairs.mkString(", "), map.description + " doesn't have the pairs " + pairs.mkString(", "), map)
  113. }
  114. }
  115. /** matches if map contains all the specified pairs
  116. * can expand dot notation to match specific sub-keys */
  117. def haveEntries[V](pairs: (String, V)*) = new Matcher[DBObject] {
  118. def apply[S <: DBObject](map: Expectable[S]) = {
  119. result(pairs.forall(pair => field(map, pair._1).exists(_ == pair._2) /* match only the value */ ),
  120. map.description + " has the pairs " + pairs.mkString(", "),
  121. map.description + " doesn't have the pairs " + pairs.mkString(", "),
  122. map)
  123. }
  124. }
  125. }