Port many monger.test.querying tests to use new monger.query DSL
This commit is contained in:
parent
568efa4efc
commit
9b5eee13e0
1 changed files with 37 additions and 30 deletions
|
|
@ -39,7 +39,7 @@
|
||||||
|
|
||||||
;; exact match over string field
|
;; exact match over string field
|
||||||
|
|
||||||
(deftest query-full-document-with-find-maps-using-exact-matching-over-string-field
|
(deftest query-full-document-using-exact-matching-over-string-field
|
||||||
(let [coll "docs"
|
(let [coll "docs"
|
||||||
doc { :title "monger" :language "Clojure" :_id (ObjectId.) }]
|
doc { :title "monger" :language "Clojure" :_id (ObjectId.) }]
|
||||||
(mgcol/insert coll doc)
|
(mgcol/insert coll doc)
|
||||||
|
|
@ -49,18 +49,22 @@
|
||||||
|
|
||||||
;; exact match over string field with limit
|
;; exact match over string field with limit
|
||||||
|
|
||||||
(deftest query-full-document-with-find-maps-using-exact-matching-over-string-with-field-with-limit
|
(deftest query-full-document-using-exact-matching-over-string-with-field-with-limit
|
||||||
(let [coll "docs"
|
(let [coll "docs"
|
||||||
doc1 { :title "monger" :language "Clojure" :_id (ObjectId.) }
|
doc1 { :title "monger" :language "Clojure" :_id (ObjectId.) }
|
||||||
doc2 { :title "langohr" :language "Clojure" :_id (ObjectId.) }
|
doc2 { :title "langohr" :language "Clojure" :_id (ObjectId.) }
|
||||||
doc3 { :title "netty" :language "Java" :_id (ObjectId.) }
|
doc3 { :title "netty" :language "Java" :_id (ObjectId.) }
|
||||||
_ (mgcol/insert-batch coll [doc1 doc2 doc3])
|
_ (mgcol/insert-batch coll [doc1 doc2 doc3])
|
||||||
result (mgcol/find-maps coll { :title "monger" } {} 0 1)]
|
result (with-collection coll
|
||||||
|
(find { :title "monger" })
|
||||||
|
(fields [:title, :language, :_id])
|
||||||
|
(skip 0)
|
||||||
|
(limit 1))]
|
||||||
(is (= 1 (count result)))
|
(is (= 1 (count result)))
|
||||||
(is (= [doc1] result))))
|
(is (= [doc1] result))))
|
||||||
|
|
||||||
|
|
||||||
(deftest query-full-document-with-find-maps-using-exact-matching-over-string-field-with-limit-and-offset
|
(deftest query-full-document-using-exact-matching-over-string-field-with-limit-and-offset
|
||||||
(let [coll "docs"
|
(let [coll "docs"
|
||||||
doc1 { :title "lucene" :language "Java" :_id (ObjectId.) }
|
doc1 { :title "lucene" :language "Java" :_id (ObjectId.) }
|
||||||
doc2 { :title "joda-time" :language "Java" :_id (ObjectId.) }
|
doc2 { :title "joda-time" :language "Java" :_id (ObjectId.) }
|
||||||
|
|
@ -68,16 +72,18 @@
|
||||||
_ (mgcol/insert-batch coll [doc1 doc2 doc3])
|
_ (mgcol/insert-batch coll [doc1 doc2 doc3])
|
||||||
;; this example is indeed ugly, we need to come up with a DSL similar to what Casbah has
|
;; this example is indeed ugly, we need to come up with a DSL similar to what Casbah has
|
||||||
;; to make this easy to read and write. MK.
|
;; to make this easy to read and write. MK.
|
||||||
result (from-db-object (seq (.sort (mgcol/find coll { :language "Java" } {} 1 2)
|
result (with-collection coll
|
||||||
(to-db-object { :title 1 })))
|
(find { :language "Java" })
|
||||||
true)]
|
(skip 1)
|
||||||
|
(limit 2)
|
||||||
|
(sort { :title 1 }))]
|
||||||
(is (= 2 (count result)))
|
(is (= 2 (count result)))
|
||||||
(is (= [doc1 doc3] result))))
|
(is (= [doc1 doc3] result))))
|
||||||
|
|
||||||
|
|
||||||
;; < ($lt), <= ($lte), > ($gt), >= ($gte)
|
;; < ($lt), <= ($lte), > ($gt), >= ($gte)
|
||||||
|
|
||||||
(deftest query-using-dsl-and-$lt-operator-1
|
(deftest query-using-dsl-and-$lt-operator
|
||||||
(let [coll "docs"
|
(let [coll "docs"
|
||||||
doc1 { :language "Clojure" :_id (ObjectId.) :inception_year 2006 }
|
doc1 { :language "Clojure" :_id (ObjectId.) :inception_year 2006 }
|
||||||
doc2 { :language "Java" :_id (ObjectId.) :inception_year 1992 }
|
doc2 { :language "Java" :_id (ObjectId.) :inception_year 1992 }
|
||||||
|
|
@ -88,28 +94,24 @@
|
||||||
(limit 2))]
|
(limit 2))]
|
||||||
(is (= [doc2] lt-result))))
|
(is (= [doc2] lt-result))))
|
||||||
|
|
||||||
(deftest query-using-dsl-and-$lt-operator-2
|
|
||||||
|
|
||||||
|
(deftest query-using-$gt-$lt-$gte-$lte-operators
|
||||||
(let [coll "docs"
|
(let [coll "docs"
|
||||||
doc1 { :language "Clojure" :_id (ObjectId.) :inception_year 2006 }
|
doc1 { :language "Clojure" :_id (ObjectId.) :inception_year 2006 }
|
||||||
doc2 { :language "Java" :_id (ObjectId.) :inception_year 1992 }
|
doc2 { :language "Java" :_id (ObjectId.) :inception_year 1992 }
|
||||||
doc3 { :language "Scala" :_id (ObjectId.) :inception_year 2003 }
|
doc3 { :language "Scala" :_id (ObjectId.) :inception_year 2003 }
|
||||||
_ (mgcol/insert-batch coll [doc1 doc2])
|
_ (mgcol/insert-batch coll [doc1 doc2])
|
||||||
lt-result (with-collection "docs"
|
lt-result (with-collection coll
|
||||||
(find { :inception_year { $lt 2000 } }))]
|
(find { :inception_year { "$lt" 2000 } }))
|
||||||
(is (= [doc2] lt-result))))
|
lte-result (with-collection coll
|
||||||
|
(find { :inception_year { "$lte" 1992 } }))
|
||||||
|
gt-result (with-collection coll
|
||||||
|
(find { :inception_year { "$gt" 2002 } })
|
||||||
(deftest query-with-find-maps-using-$lt-operator
|
(limit 1)
|
||||||
(let [coll "docs"
|
(sort { :inception_year -1 }))
|
||||||
doc1 { :language "Clojure" :_id (ObjectId.) :inception_year 2006 }
|
gte-result (with-collection coll
|
||||||
doc2 { :language "Java" :_id (ObjectId.) :inception_year 1992 }
|
(find { :inception_year { "$gte" 2006 } }))]
|
||||||
doc3 { :language "Scala" :_id (ObjectId.) :inception_year 2003 }
|
|
||||||
_ (mgcol/insert-batch coll [doc1 doc2])
|
|
||||||
lt-result (mgcol/find-maps coll { :inception_year { "$lt" 2000 } })
|
|
||||||
lte-result (mgcol/find-maps coll { :inception_year { "$lte" 1992 } })
|
|
||||||
gt-result (mgcol/find-maps coll { :inception_year { "$gt" 2005 } })
|
|
||||||
gte-result (mgcol/find-maps coll { :inception_year { "$gte" 2006 } })]
|
|
||||||
(is (= [doc2] lt-result))
|
(is (= [doc2] lt-result))
|
||||||
(is (= [doc2] lte-result))
|
(is (= [doc2] lte-result))
|
||||||
(is (= [doc1] gt-result))
|
(is (= [doc1] gt-result))
|
||||||
|
|
@ -118,18 +120,23 @@
|
||||||
|
|
||||||
;; $all
|
;; $all
|
||||||
|
|
||||||
(deftest query-with-find-maps-using-$all
|
(deftest query-with-using-$all
|
||||||
(let [coll "docs"
|
(let [coll "docs"
|
||||||
doc1 { :_id (ObjectId.) :title "Clojure" :tags ["functional" "homoiconic" "syntax-oriented" "dsls" "concurrency features" "jvm"] }
|
doc1 { :_id (ObjectId.) :title "Clojure" :tags ["functional" "homoiconic" "syntax-oriented" "dsls" "concurrency features" "jvm"] }
|
||||||
doc2 { :_id (ObjectId.) :title "Java" :tags ["object-oriented" "jvm"] }
|
doc2 { :_id (ObjectId.) :title "Java" :tags ["object-oriented" "jvm"] }
|
||||||
doc3 { :_id (ObjectId.) :title "Scala" :tags ["functional" "object-oriented" "dsls" "concurrency features" "jvm"] }
|
doc3 { :_id (ObjectId.) :title "Scala" :tags ["functional" "object-oriented" "dsls" "concurrency features" "jvm"] }
|
||||||
- (mgcol/insert-batch coll [doc1 doc2 doc3])
|
- (mgcol/insert-batch coll [doc1 doc2 doc3])
|
||||||
result1 (mgcol/find-maps coll { :tags { "$all" ["functional" "jvm" "homoiconic"] } })
|
result1 (with-collection coll
|
||||||
result2 (mgcol/find-maps coll { :tags { "$all" ["functional" "native" "homoiconic"] } })
|
(find { :tags { "$all" ["functional" "jvm" "homoiconic"] } }))
|
||||||
result3 (mgcol/find-maps coll { :tags { "$all" ["functional" "jvm" "dsls"] } })]
|
result2 (with-collection coll
|
||||||
|
(find { :tags { "$all" ["functional" "native" "homoiconic"] } }))
|
||||||
|
result3 (with-collection coll
|
||||||
|
(find { :tags { "$all" ["functional" "jvm" "dsls"] } })
|
||||||
|
(sort { :title 1 }))]
|
||||||
(is (= [doc1] result1))
|
(is (= [doc1] result1))
|
||||||
(is (empty? result2))
|
(is (empty? result2))
|
||||||
(is (= 2 (count result3)))))
|
(is (= 2 (count result3)))
|
||||||
|
(is (= doc1 (first result3)))))
|
||||||
|
|
||||||
|
|
||||||
;; $exists
|
;; $exists
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue