PageRenderTime 31ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/ymasory/scala-corpus
Scala | 178 lines | 101 code | 53 blank | 24 comment | 0 complexity | c30725e490132fc5bb55d184f5d9edd2 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, AGPL-3.0, BSD-3-Clause, MIT
  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.commons.Logging
  25. import com.mongodb.casbah.commons.conversions.scala._
  26. import org.scala_tools.time.Imports._
  27. import org.specs._
  28. import org.specs.specification.PendingUntilFixed
  29. class CoreWrappersSpec extends Specification with PendingUntilFixed with Logging {
  30. "Casbah behavior between Scala and Java versions of Objects" should {
  31. shareVariables
  32. "provide working .asScala methods on the Java version of the objects" in {
  33. val javaConn = new com.mongodb.Mongo() // Java connection
  34. "Connection objects" in {
  35. javaConn must notBeNull
  36. val scalaConn = javaConn.asScala
  37. scalaConn must notBeNull
  38. scalaConn must haveSuperClass[com.mongodb.casbah.MongoConnection]
  39. scalaConn.underlying must beEqualTo(javaConn)
  40. }
  41. val javaDb = javaConn.getDB("test")
  42. "DB objects" in {
  43. javaDb must notBeNull
  44. val scalaDb = javaDb.asScala
  45. scalaDb must notBeNull
  46. scalaDb must haveSuperClass[com.mongodb.casbah.MongoDB]
  47. scalaDb.underlying must beEqualTo(javaDb)
  48. }
  49. val javaCollection = javaDb.getCollection("test")
  50. "Collection objects" in {
  51. javaCollection must notBeNull
  52. val scalaCollection = javaCollection.asScala
  53. scalaCollection must notBeNull
  54. scalaCollection must haveSuperClass[com.mongodb.casbah.MongoCollection]
  55. scalaCollection.underlying must beEqualTo(javaCollection)
  56. }
  57. }
  58. "be directly instantiable, with working apply methods" in {
  59. var conn: MongoConnection = null
  60. var db: MongoDB = null
  61. var coll: MongoCollection = null
  62. "MongoConnection" in {
  63. "direct instantiation" in {
  64. conn = MongoConnection()
  65. conn must notBeNull
  66. conn must haveSuperClass[com.mongodb.casbah.MongoConnection]
  67. conn.underlying must notBeNull
  68. conn.underlying must haveSuperClass[com.mongodb.Mongo]
  69. }
  70. "the apply method works" in {
  71. conn must notBeNull
  72. db = conn("test")
  73. db must notBeNull
  74. db must haveSuperClass[com.mongodb.casbah.MongoDB]
  75. db.underlying must notBeNull
  76. db.underlying must haveSuperClass[com.mongodb.DB]
  77. }
  78. }
  79. "MongoDB" in {
  80. "has a working apply method" in {
  81. db must notBeNull
  82. coll = db("collection.in")
  83. coll must notBeNull
  84. coll must haveSuperClass[com.mongodb.casbah.MongoCollection]
  85. coll.underlying must notBeNull
  86. coll.underlying must haveSuperClass[com.mongodb.DBCollection]
  87. }
  88. }
  89. }
  90. "Renaming a collection successfully tracks the rename in MongoCollection" in {
  91. val db = MongoConnection()("casbahTest")
  92. db("collection").drop()
  93. val coll = db("collectoin")
  94. coll.drop()
  95. coll.insert(MongoDBObject("foo" -> "bar"))
  96. coll must notBeNull
  97. coll must haveSuperClass[com.mongodb.casbah.MongoCollection]
  98. coll.name must beEqualTo("collectoin")
  99. val newColl = coll.rename("collection")
  100. newColl must notBeNull
  101. newColl must haveSuperClass[com.mongodb.casbah.MongoCollection]
  102. newColl.name must beEqualTo("collection")
  103. // no mutability in the old collection
  104. coll.name must beEqualTo("collectoin")
  105. // collection should be gone so rename fails
  106. newColl.rename("collection") must throwA[MongoException]
  107. }
  108. }
  109. "findOne operations" should {
  110. shareVariables()
  111. val db = MongoConnection()("casbahTest")
  112. "Not fail as reported by Max Afonov in SCALA-11" in {
  113. val coll = db("brand_new_coll_%d".format(System.currentTimeMillis))
  114. coll.insert(MongoDBObject("foo" -> "bar"))
  115. val basicFind = coll.find(MongoDBObject("foo" -> "bar"))
  116. basicFind must notBeNull
  117. basicFind must haveSize(1)
  118. val findOne = coll.findOne()
  119. findOne must beSomething
  120. val findOneMatch = coll.findOne(MongoDBObject("foo" -> "bar"))
  121. findOneMatch must beSomething
  122. }
  123. }
  124. }
  125. // vim: set ts=2 sw=2 sts=2 et: