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

https://github.com/derzzle/casbah · Scala · 82 lines · 45 code · 14 blank · 23 comment · 1 complexity · f277cb49081a6baa725693356f297e36 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 GroupSpec extends CasbahSpecification {
  29. "Casbah's Group Interfaces" should {
  30. implicit val mongoDB = MongoConnection()("casbahIntegration")
  31. "Work with a normal non-finalized Group statement" in {
  32. val cond = MongoDBObject()
  33. val key = MongoDBObject("publicationYear" -> 1)
  34. val initial = MongoDBObject("count" -> 0)
  35. val reduce = "function(obj, prev) { prev.count++ }"
  36. val result = mongoDB("books").group(key, cond, initial, reduce)
  37. println(result)
  38. success
  39. }
  40. // Test for SCALA-37
  41. "Work with a trivial finalized Group statement" in {
  42. val cond = MongoDBObject()
  43. val key = MongoDBObject("publicationYear" -> 1)
  44. val initial = MongoDBObject("count" -> 0)
  45. val reduce = "function(obj, prev) { prev.count++ }"
  46. val result = mongoDB("books").group(key, cond, initial, reduce, "")
  47. println(result)
  48. success
  49. }
  50. "Work with a less-trivial finalized Group statement" in {
  51. val cond = MongoDBObject()
  52. val key = MongoDBObject("publicationYear" -> 1)
  53. val initial = MongoDBObject("count" -> 0)
  54. val reduce = "function(obj, prev) { prev.count++; }"
  55. val finalize = "function(out) { out.avg_count = 3; }"
  56. val result = mongoDB("books").group(key, cond, initial, reduce, finalize)
  57. result.forall(_.getOrElse("avg_count", 2) == 3)
  58. }
  59. "Use a default Group statement that changes nothing" in {
  60. val cond = MongoDBObject()
  61. val key = MongoDBObject("publicationYear" -> 1)
  62. val r1 = mongoDB("books").group(key, cond)
  63. val r2 = mongoDB("books").group(key, cond, "function(o,p) { }")
  64. r1 must haveTheSameElementsAs(r2)
  65. }
  66. }
  67. }
  68. // vim: set ts=2 sw=2 sts=2 et: