/casbah-core/src/test/scala/CoreWrappersSpec.scala

https://github.com/derzzle/casbah · Scala · 167 lines · 88 code · 48 blank · 31 comment · 1 complexity · 032c1fac0b9e6c00b70fce7bb6c0445d MD5 · raw file

  1. /**
  2. * Copyright (c) 2010 10gen, Inc. <http://10gen.com>
  3. * Copyright (c) 2009, 2010 Novus Partners, Inc. <http://novus.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * For questions and comments about this product, please see the project page at:
  18. *
  19. * http://github.com/mongodb/casbah
  20. *
  21. */
  22. package com.mongodb.casbah
  23. import com.mongodb.casbah.Imports._
  24. import com.mongodb.casbah.util.Logging
  25. import com.mongodb.casbah.commons.conversions.scala._
  26. import org.scala_tools.time.Imports._
  27. import com.mongodb.casbah.commons.test.CasbahSpecification
  28. class CoreWrappersSpec extends CasbahSpecification {
  29. "Casbah behavior between Scala and Java versions of Objects" should {
  30. "provide working .asScala methods on the Java version of the objects" in {
  31. val javaConn = new com.mongodb.Mongo() // Java connection
  32. "Connection objects" in {
  33. val scalaConn = javaConn.asScala
  34. scalaConn.underlying must beEqualTo(javaConn)
  35. }
  36. val javaDb = javaConn.getDB("test")
  37. "DB objects" in {
  38. val scalaDb = javaDb.asScala
  39. scalaDb.underlying must beEqualTo(javaDb)
  40. }
  41. val javaCollection = javaDb.getCollection("test")
  42. "Collection objects" in {
  43. val scalaCollection = javaCollection.asScala
  44. scalaCollection.underlying must beEqualTo(javaCollection)
  45. }
  46. }
  47. "be directly instantiable, with working apply methods" in {
  48. lazy val conn: MongoConnection = MongoConnection()
  49. lazy val db: MongoDB = conn("test")
  50. lazy val coll: MongoCollection = db("collection.in")
  51. "MongoConnection" in {
  52. "direct instantiation" in {
  53. conn.underlying must haveClass[com.mongodb.Mongo]
  54. }
  55. "the apply method works" in {
  56. db.underlying must haveSuperclass[com.mongodb.DB]
  57. }
  58. "MongoDB" in {
  59. "has a working apply method" in {
  60. coll.underlying must haveSuperclass[com.mongodb.DBCollection]
  61. }
  62. }
  63. }
  64. }
  65. "Renaming a collection successfully tracks the rename in MongoCollection" in {
  66. val db = MongoConnection()("casbahTest")
  67. db("collection").drop()
  68. val coll = db("collectoin")
  69. coll.drop()
  70. coll.insert(MongoDBObject("foo" -> "bar"))
  71. coll must beAnInstanceOf[com.mongodb.casbah.MongoCollection]
  72. coll.name must beEqualTo("collectoin")
  73. val newColl = coll.rename("collection")
  74. newColl must beAnInstanceOf[com.mongodb.casbah.MongoCollection]
  75. newColl.name must beEqualTo("collection")
  76. // no mutability in the old collection
  77. coll.name must beEqualTo("collectoin")
  78. // collection should be gone so rename fails
  79. newColl.rename("collection") must throwA[MongoException]
  80. }
  81. }
  82. "findOne operations" should {
  83. val db = MongoConnection()("casbahTest")
  84. "Not fail as reported by Max Afonov in SCALA-11" in {
  85. val coll = db("brand_new_coll_%d".format(System.currentTimeMillis))
  86. coll.insert(MongoDBObject("foo" -> "bar"))
  87. val basicFind = coll.find(MongoDBObject("foo" -> "bar"))
  88. basicFind must haveSize(1)
  89. val findOne = coll.findOne()
  90. findOne must beSome
  91. val findOneMatch = coll.findOne(MongoDBObject("foo" -> "bar"))
  92. findOneMatch must beSome
  93. }
  94. }
  95. "Cursor Operations" should {
  96. import scala.util.Random
  97. val db = MongoConnection()("casbahTest")
  98. val coll = db("test_coll_%d".format(System.currentTimeMillis))
  99. for (i <- 1 to 100)
  100. coll += MongoDBObject("foo" -> "bar", "x" -> Random.nextDouble())
  101. val first5 = coll.find(MongoDBObject("foo" -> "bar")) limit 5
  102. "Behave in chains" in {
  103. //"For loops for idiomatic cleanness" in {
  104. //// todo - add limit, skip etc on COLLECTION for cleaner chains like this?
  105. //val items = for (x <- coll.find(MongoDBObject("foo" -> "bar")) skip 5 limit 20) yield x
  106. //items must haveSize(20)
  107. //// TODO - Matchers that support freaking cursors, etc
  108. //[>items must haveSameElementsAs(first5).not<]
  109. //}
  110. "Chain operations must return the proper *subtype*" in {
  111. val cur = coll.find(MongoDBObject("foo" -> "bar")) skip 5
  112. cur must beAnInstanceOf[MongoCursor]
  113. val cur2 = coll.find(MongoDBObject("foo" -> "bar")) limit 25 skip 12
  114. cur2 must beAnInstanceOf[MongoCursor]
  115. }
  116. }
  117. }
  118. }
  119. // vim: set ts=2 sw=2 sts=2 et: