PageRenderTime 44ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/scala/CasbahSuite.scala

https://github.com/jwmatthews/scala_mongo_example
Scala | 84 lines | 63 code | 13 blank | 8 comment | 2 complexity | fac077b6209f02d3ccdbf3bcec60cbe4 MD5 | raw file
  1. import com.mongodb.casbah.Imports._
  2. import org.scalatest.{FunSuite, BeforeAndAfterAll, BeforeAndAfterEach}
  3. class CasbahSuite extends FunSuite with BeforeAndAfterEach with BeforeAndAfterAll {
  4. val hostname = "127.0.0.1"
  5. val port = 27017
  6. val dbName = "scala_mongo_example_casbahsuite"
  7. val collName = "widgets"
  8. var mongoConn: MongoConnection = null
  9. var mongoDB: MongoDB = null
  10. var mongoColl: MongoCollection = null
  11. override def beforeAll() {
  12. mongoConn = MongoConnection(hostname, port)
  13. mongoDB = mongoConn(dbName)
  14. }
  15. override def beforeEach() {
  16. mongoColl = mongoDB(collName)
  17. mongoColl.drop()
  18. mongoConn.setWriteConcern(WriteConcern.Safe)
  19. }
  20. test("unique index prevents duplicates") {
  21. // Add the unique index to the collection
  22. val index = MongoDBObject("id" -> 1)
  23. val options = MongoDBObject("unique" -> true)
  24. mongoColl.ensureIndex(index, options)
  25. val builder1 = MongoDBObject.newBuilder
  26. builder1 += "id" -> 1
  27. builder1 += "name" -> "item1"
  28. val item1 = builder1.result().asDBObject
  29. mongoColl += item1
  30. assert(mongoColl.find().count === 1)
  31. val builder2 = MongoDBObject.newBuilder
  32. builder2 += "id" -> 2
  33. builder2 += "name" -> "item2"
  34. val item2 = builder2.result().asDBObject
  35. mongoColl += item2
  36. assert(mongoColl.find().count === 2)
  37. // This should be a duplicate since it shares same "id" as 'item2'
  38. val item3 = MongoDBObject("id" -> 2, "name" -> "item2a")
  39. // Safe version throws an exception
  40. intercept[MongoException]{
  41. mongoColl += item3
  42. }
  43. assert(mongoColl.find().count === 2)
  44. // Default save won't save the object, yet no exception is thrown
  45. mongoConn.setWriteConcern(WriteConcern.None)
  46. mongoColl += item3
  47. assert(mongoColl.find().count === 2)
  48. mongoConn.setWriteConcern(WriteConcern.Safe)
  49. // Verify that the object with "id"->2 matches item2
  50. // Approach #1 using a map on the Option
  51. val found = mongoColl.findOne(MongoDBObject("id"->2))
  52. assert(found != None)
  53. found.map( item => assert(item.getAs[String]("name") === item2.getAs[String]("name")))
  54. // Approach #2 using a pattern match
  55. mongoColl.findOne(MongoDBObject("id"->2)) match {
  56. case None => assert(false == true)
  57. case Some(item) => assert(item.getAs[String]("name") === item2.getAs[String]("name"))
  58. }
  59. }
  60. test("simple write then read of an object") {
  61. assert(mongoColl.find().count === 0)
  62. val newObj = MongoDBObject(
  63. "foo" -> "bar",
  64. "x" -> "y",
  65. "pie" -> 3.14,
  66. "spam" -> "eggs")
  67. // Save the object to mongo
  68. mongoColl += newObj
  69. assert(mongoColl.find().count === 1)
  70. }
  71. }