PageRenderTime 25ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/CasbahDSL/src/test/scala/test.scala

http://github.com/ochafik/Scalaxy
Scala | 91 lines | 65 code | 22 blank | 4 comment | 9 complexity | e1b980e016c8d48dcf199e23e014d02f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import org.junit._
  2. import org.junit.Assert._
  3. import com.mongodb.casbah.Imports._
  4. import com.mongodb.casbah.query.dsl._
  5. import scalaxy.casbah._
  6. class QueryTest {
  7. def sameQuery(expected: DBObject, actual: DBObject) =
  8. assertEquals(expected, actual)
  9. @Test
  10. def testQuery {
  11. sameQuery(
  12. $and("blah" $lt 10, "foo" $eq "bar"),
  13. query(d => d.blah < 10 && d.foo == "bar"))
  14. sameQuery(
  15. "a.b.c" $eq 10,
  16. query(_.a.b.c == 10))
  17. sameQuery(
  18. "blah" $mod (2, 1),
  19. query(d => d.blah % 2 == 1))
  20. sameQuery(
  21. $or("blah" $exists true, "foo" $exists false),
  22. query(d => d.contains("blah") || !d.contains("foo")))
  23. sameQuery(
  24. $or("blah" $mod (2, 1), "foo" $gt "bar"),
  25. query(d => d.blah % 2 == 1 || d.foo > d.bar))
  26. sameQuery(
  27. "foo" $lt "bar",
  28. query(_.foo < "bar"))
  29. sameQuery(
  30. $inc("foo" -> 10),
  31. update(_.foo += 10))
  32. sameQuery(
  33. $set("a" -> 10, "b" -> 20),
  34. update(d => d.set(a = 10, b = 20)))
  35. sameQuery(
  36. $set("a" -> 10),
  37. update(d => d.a = 10))
  38. sameQuery(
  39. $setOnInsert("a" -> 10, "b" -> 20),
  40. update(d => d.setOnInsert(a = 10, b = 20)))
  41. // sameQuery(
  42. // $set("a" -> 10, "" -> 20),
  43. // update(d => d.set(a = 10, 20)))
  44. sameQuery(
  45. $rename("a" -> "blah", "b" -> "hooh"),
  46. update(d => d.rename(a = "blah", b = "hooh")))
  47. sameQuery(
  48. "a".$type[BasicDBList],
  49. update(d => d.a.isInstanceOf[BasicDBList]))
  50. sameQuery(
  51. $or("foo" $lt "bar", "foo" $size 4),
  52. query(d => d.foo < "bar" || d.foo.size == 4))
  53. }
  54. @Ignore
  55. @Test
  56. def testConnection {
  57. // val client = MongoClient()
  58. def makeEntry(word: String) =
  59. $set ( (word + "_").iterator.mkString(".") -> word )
  60. val dict = MongoConnection("localhost")("appdb")("dict")
  61. val index = MongoDBObject("_id" -> "_index")
  62. dict.update(index, makeEntry("applaud"), true, false)
  63. dict.update(index, makeEntry("apple"), true, false)
  64. dict.update(index, makeEntry("bad"), true, false)
  65. dict.update(index, makeEntry("banana"), true, false)
  66. val entry = dict.findOne(index ++ ("a.p.p" $exists true))
  67. println(entry)
  68. }
  69. }