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

http://github.com/mongodb/casbah · Scala · 93 lines · 53 code · 17 blank · 23 comment · 1 complexity · 499871134081ad45dfd8dc1de6046d32 MD5 · raw file

  1. /**
  2. * Copyright (c) 2010 MongoDB, Inc. <http://mongodb.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.test.core
  23. import java.io.IOException
  24. import scala.sys.process._
  25. import scala.collection.JavaConverters._
  26. import org.specs2.specification.Scope
  27. import com.mongodb.util.JSON
  28. import com.mongodb.casbah.Imports._
  29. class GroupSpec extends CasbahDBTestSpecification {
  30. "Casbah's Group Interfaces" should {
  31. "Work with a normal non-finalized Group statement" in new testData {
  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 = collection.group(key, cond, initial, reduce)
  37. result.size must beEqualTo(31)
  38. }
  39. // Test for CASBAH-37
  40. "Work with a trivial finalized Group statement" in new testData {
  41. val cond = MongoDBObject()
  42. val key = MongoDBObject("publicationYear" -> 1)
  43. val initial = MongoDBObject("count" -> 0)
  44. val reduce = "function(obj, prev) { prev.count++ }"
  45. val result = collection.group(key, cond, initial, reduce, "")
  46. result.size must beEqualTo(31)
  47. }
  48. "Work with a less-trivial finalized Group statement" in new testData {
  49. val cond = MongoDBObject()
  50. val key = MongoDBObject("publicationYear" -> 1)
  51. val initial = MongoDBObject("count" -> 0)
  52. val reduce = "function(obj, prev) { prev.count++; }"
  53. val finalise = "function(out) { out.avg_count = 3; }"
  54. val result = collection.group(key, cond, initial, reduce, finalise)
  55. result.forall(_.getOrElse("avg_count", 2) == 3)
  56. }
  57. }
  58. trait testData extends Scope {
  59. val jsonFile = "./casbah-core/src/test/resources/bookstore.json"
  60. database.dropDatabase()
  61. try {
  62. Seq("mongoimport", "-d", database.name, "-c", collection.name, "--drop", "--jsonArray", jsonFile).!!
  63. } catch {
  64. case ex: IOException => {
  65. val source = scala.io.Source.fromFile(jsonFile)
  66. val lines = source.mkString
  67. source.close()
  68. val rawDoc = JSON.parse(lines).asInstanceOf[BasicDBList]
  69. val docs = (for (doc <- rawDoc) yield doc.asInstanceOf[DBObject]).asJava
  70. collection.underlying.insert(docs)
  71. }
  72. }
  73. // Verify the treasury data is loaded or skip the test for now
  74. collection.count() must beGreaterThan(0)
  75. }
  76. }