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

https://github.com/derzzle/casbah · Scala · 110 lines · 68 code · 14 blank · 28 comment · 9 complexity · 13f33b9dfcdd7dc5731ed7811cc9eaf1 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 scalatest
  19. import com.mongodb.casbah.commons.Imports._
  20. import org.scalatest.matchers.Matcher
  21. import org.scalatest.matchers.MatchResult
  22. trait DBObjectMatchers {
  23. protected def someField(map: Option[DBObject], k: String) = if (k.indexOf('.') < 0) {
  24. map.getOrElse(MongoDBObject.empty).getAs[AnyRef](k)
  25. } else {
  26. map.getOrElse(MongoDBObject.empty).expand[AnyRef](k)
  27. }
  28. protected def field(map: DBObject, k: String) = if (k.indexOf('.') < 0) {
  29. map.getAs[AnyRef](k)
  30. } else {
  31. map.expand[AnyRef](k)
  32. }
  33. protected def listField(map: DBObject, k: String) = if (k.indexOf('.') < 0) {
  34. map.getAs[Seq[Any]](k)
  35. } else {
  36. map.expand[Seq[Any]](k)
  37. }
  38. val beDBObject = Matcher { left: AnyRef =>
  39. MatchResult(left.isInstanceOf[DBObject], left + " is a DBObject", left + " is not a DBObject")
  40. }
  41. def containSomeField(k: String) = Matcher { map: Option[DBObject] =>
  42. MatchResult(someField(map, k).isDefined, map.toString + " contains the key " + k, map.toString + " doesn't contain the key " + k)
  43. }
  44. /** matches if dbObject.contains(k) */
  45. def containField(k: String) = Matcher { map: DBObject =>
  46. MatchResult(field(map, k).isDefined, map.toString + " contains the key " + k, map.toString + " doesn't contain the key " + k)
  47. }
  48. /** matches if a Some(map) contains a pair (key, value) == (k, v)
  49. */
  50. def containSomeEntry[V](p: (String, V)) = Matcher { map: Option[DBObject] =>
  51. MatchResult(
  52. someField(map, p._1).exists(_ == p._2), // match only the value
  53. map.toString + " contain the pair " + p,
  54. map.toString + " doesn't contain the pair " + p
  55. )
  56. }
  57. /** Special version of "containEntry" that expects a list and then uses
  58. * "hasSameElements" on it.
  59. */
  60. def containListEntry(k: String, l: => Traversable[Any]) = Matcher { map: DBObject =>
  61. val objL = listField(map, k).getOrElse(Seq.empty[Any]).toSeq
  62. val _l = l.toSeq
  63. MatchResult(
  64. objL.sameElements(_l), // match only the value
  65. map.toString + " has the pair " + k,
  66. map.toString + " doesn't have the pair " + k
  67. )
  68. }
  69. /** matches if map contains a pair (key, value) == (k, v)
  70. */ // If a DBObject is a Map, can use contain already
  71. def containEntry[V](p: (String, V)) = Matcher { map: DBObject =>
  72. MatchResult(
  73. field(map, p._1).exists(_.equals(p._2)), // match only the value
  74. map.toString + " has the pair " + p,
  75. map.toString + "[" + field(map, p._1) + "] doesn't have the pair " + p + "[" + p._2 + "]"
  76. )
  77. }
  78. /** matches if Some(map) contains all the specified pairs
  79. * can expand dot notation to match specific sub-keys */
  80. def containSomeEntries[V](pairs: (String, V)*) = Matcher { map: Option[DBObject] =>
  81. MatchResult(
  82. pairs.forall(pair => someField(map, pair._1).exists(_ == pair._2) /* match only the value */ ),
  83. map.toString + " has the pairs " + pairs.mkString(", "),
  84. map.toString + " doesn't have the pairs " + pairs.mkString(", ")
  85. )
  86. }
  87. /** matches if map contains all the specified pairs
  88. * can expand dot notation to match specific sub-keys */
  89. def containEntries[V](pairs: (String, V)*) = Matcher { map: DBObject =>
  90. MatchResult(
  91. pairs.forall(pair => field(map, pair._1).exists(_ == pair._2) /* match only the value */ ),
  92. map.toString + " has the pairs " + pairs.mkString(", "),
  93. map.toString + " doesn't have the pairs " + pairs.mkString(", ")
  94. )
  95. }
  96. }