Query DSL tests now pass
This commit is contained in:
parent
565ec43398
commit
d7902c9618
2 changed files with 299 additions and 277 deletions
|
|
@ -63,7 +63,19 @@
|
|||
(merge (empty-query) { :collection coll })))
|
||||
|
||||
(defn exec
|
||||
[{ :keys [^DBCollection collection query fields skip limit sort batch-size hint snapshot read-preference keywordize-fields options] :or { limit 0 batch-size 256 skip 0 } }]
|
||||
[{:keys [^DBCollection collection
|
||||
query
|
||||
fields
|
||||
skip
|
||||
limit
|
||||
sort
|
||||
batch-size
|
||||
hint
|
||||
snapshot
|
||||
read-preference
|
||||
keywordize-fields
|
||||
options]
|
||||
:or { limit 0 batch-size 256 skip 0 } }]
|
||||
(with-open [cursor (doto (.find collection (to-db-object query) (as-field-selector fields))
|
||||
(.limit limit)
|
||||
(.skip skip)
|
||||
|
|
@ -135,7 +147,7 @@
|
|||
[^DB db ^String coll & body]
|
||||
`(let [coll# ~coll
|
||||
db-coll# (if (string? coll#)
|
||||
(.getCollection db ^String coll#)
|
||||
(.getCollection ~db ^String coll#)
|
||||
coll#)
|
||||
query# (-> (empty-query db-coll#) ~@body)]
|
||||
(exec query#)))
|
||||
|
|
|
|||
|
|
@ -1,24 +1,34 @@
|
|||
(ns monger.test.querying-test
|
||||
(:refer-clojure :exclude [select find sort])
|
||||
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject ReadPreference]
|
||||
(:import [com.mongodb WriteResult WriteConcern DBObject ReadPreference]
|
||||
org.bson.types.ObjectId
|
||||
java.util.Date)
|
||||
(:require [monger core util]
|
||||
[monger.collection :as mgcol]
|
||||
(:require [monger.core :as mg]
|
||||
[monger.collection :as mc]
|
||||
monger.joda-time
|
||||
[monger.result :as mgres]
|
||||
[monger.test.helper :as helper]
|
||||
[clojure.test :refer :all]
|
||||
[monger.test.fixtures :refer :all]
|
||||
[monger.conversion :refer :all]
|
||||
[monger.query :refer :all]
|
||||
[monger.operators :refer :all]
|
||||
[clj-time.core :refer [date-time]]))
|
||||
|
||||
(helper/connect!)
|
||||
(let [conn (mg/connect)
|
||||
db (mg/get-db conn "monger-test")]
|
||||
|
||||
(use-fixtures :each purge-docs purge-things purge-locations
|
||||
purge-querying-docs)
|
||||
(defn purge-collections
|
||||
[f]
|
||||
(mc/remove db "docs")
|
||||
(mc/remove db "things")
|
||||
(mc/remove db "locations")
|
||||
(mc/remove db "querying_docs")
|
||||
(f)
|
||||
(mc/remove db "docs")
|
||||
(mc/remove db "things")
|
||||
(mc/remove db "locations")
|
||||
(mc/remove db "querying_docs"))
|
||||
|
||||
(use-fixtures :each purge-collections)
|
||||
|
||||
;;
|
||||
;; monger.collection/* finders ("low-level API")
|
||||
|
|
@ -30,9 +40,9 @@
|
|||
(let [coll "querying_docs"
|
||||
oid (ObjectId.)
|
||||
doc { :_id oid :title "Introducing Monger" }]
|
||||
(mgcol/insert coll doc)
|
||||
(is (= doc (mgcol/find-map-by-id coll oid)))
|
||||
(is (= doc (mgcol/find-one-as-map coll { :_id oid })))))
|
||||
(mc/insert db coll doc)
|
||||
(is (= doc (mc/find-map-by-id db coll oid)))
|
||||
(is (= doc (mc/find-one-as-map db coll { :_id oid })))))
|
||||
|
||||
|
||||
;; exact match over string field
|
||||
|
|
@ -40,9 +50,9 @@
|
|||
(deftest query-full-document-using-exact-matching-over-string-field
|
||||
(let [coll "querying_docs"
|
||||
doc { :title "monger" :language "Clojure" :_id (ObjectId.) }]
|
||||
(mgcol/insert coll doc)
|
||||
(is (= [doc] (mgcol/find-maps coll { :title "monger" })))
|
||||
(is (= doc (from-db-object (first (mgcol/find coll { :title "monger" })) true)))))
|
||||
(mc/insert db coll doc)
|
||||
(is (= [doc] (mc/find-maps db coll { :title "monger" })))
|
||||
(is (= doc (from-db-object (first (mc/find db coll { :title "monger" })) true)))))
|
||||
|
||||
|
||||
;; exact match over string field with limit
|
||||
|
|
@ -52,8 +62,8 @@
|
|||
doc1 { :title "monger" :language "Clojure" :_id (ObjectId.) }
|
||||
doc2 { :title "langohr" :language "Clojure" :_id (ObjectId.) }
|
||||
doc3 { :title "netty" :language "Java" :_id (ObjectId.) }
|
||||
_ (mgcol/insert-batch coll [doc1 doc2 doc3])
|
||||
result (with-collection coll
|
||||
_ (mc/insert-batch db coll [doc1 doc2 doc3])
|
||||
result (with-collection db coll
|
||||
(find { :title "monger" })
|
||||
(fields [:title, :language, :_id])
|
||||
(skip 0)
|
||||
|
|
@ -67,8 +77,8 @@
|
|||
doc1 { :title "lucene" :language "Java" :_id (ObjectId.) }
|
||||
doc2 { :title "joda-time" :language "Java" :_id (ObjectId.) }
|
||||
doc3 { :title "netty" :language "Java" :_id (ObjectId.) }
|
||||
_ (mgcol/insert-batch coll [doc1 doc2 doc3])
|
||||
result (with-collection coll
|
||||
_ (mc/insert-batch db coll [doc1 doc2 doc3])
|
||||
result (with-collection db coll
|
||||
(find { :language "Java" })
|
||||
(skip 1)
|
||||
(limit 2)
|
||||
|
|
@ -82,18 +92,18 @@
|
|||
doc2 { :a 1 :b 1 :c 4 :text "Blah " :_id (ObjectId.) }
|
||||
doc3 { :a 10 :b 3 :c 1 :text "Abc" :_id (ObjectId.) }
|
||||
doc4 { :a 10 :b 3 :c 3 :text "Abc" :_id (ObjectId.) }
|
||||
_ (mgcol/insert-batch coll [doc1 doc2 doc3 doc4])
|
||||
result1 (with-collection coll
|
||||
_ (mc/insert-batch db coll [doc1 doc2 doc3 doc4])
|
||||
result1 (with-collection db coll
|
||||
(find {})
|
||||
(limit 2)
|
||||
(fields [:a :b :c :text])
|
||||
(sort (sorted-map :a 1 :b 1 :text -1)))
|
||||
result2 (with-collection coll
|
||||
result2 (with-collection db coll
|
||||
(find {})
|
||||
(limit 2)
|
||||
(fields [:a :b :c :text])
|
||||
(sort (array-map :c 1 :text -1)))
|
||||
result3 (with-collection coll
|
||||
result3 (with-collection db coll
|
||||
(find {})
|
||||
(limit 2)
|
||||
(fields [:a :b :c :text])
|
||||
|
|
@ -110,8 +120,8 @@
|
|||
doc1 { :language "Clojure" :_id (ObjectId.) :inception_year 2006 }
|
||||
doc2 { :language "Java" :_id (ObjectId.) :inception_year 1992 }
|
||||
doc3 { :language "Scala" :_id (ObjectId.) :inception_year 2003 }
|
||||
_ (mgcol/insert-batch coll [doc1 doc2])
|
||||
lt-result (with-collection "querying_docs"
|
||||
_ (mc/insert-batch db coll [doc1 doc2])
|
||||
lt-result (with-collection db coll
|
||||
(find { :inception_year { $lt 2000 } })
|
||||
(limit 2))]
|
||||
(is (= [doc2] (vec lt-result)))))
|
||||
|
|
@ -123,8 +133,8 @@
|
|||
doc1 { :language "Clojure" :_id (ObjectId.) :inception_year (date-time 2006 1 1) }
|
||||
doc2 { :language "Java" :_id (ObjectId.) :inception_year (date-time 1992 1 2) }
|
||||
doc3 { :language "Scala" :_id (ObjectId.) :inception_year (date-time 2003 3 3) }
|
||||
_ (mgcol/insert-batch coll [doc1 doc2])
|
||||
lt-result (with-collection "querying_docs"
|
||||
_ (mc/insert-batch db coll [doc1 doc2])
|
||||
lt-result (with-collection db coll
|
||||
(find { :inception_year { $lt (date-time 2000 1 2) } })
|
||||
(limit 2))]
|
||||
(is (= (map :_id [doc2])
|
||||
|
|
@ -136,8 +146,8 @@
|
|||
doc1 { :language "Clojure" :_id (ObjectId.) :inception_year (date-time 2006 1 1) }
|
||||
doc2 { :language "Java" :_id (ObjectId.) :inception_year (date-time 1992 1 2) }
|
||||
doc3 { :language "Scala" :_id (ObjectId.) :inception_year (date-time 2003 3 3) }
|
||||
_ (mgcol/insert-batch coll [doc1 doc2 doc3])
|
||||
lt-result (with-collection "querying_docs"
|
||||
_ (mc/insert-batch db coll [doc1 doc2 doc3])
|
||||
lt-result (with-collection db coll
|
||||
(find { :inception_year { $gt (date-time 2000 1 2) $lte (date-time 2007 2 2) } })
|
||||
(sort { :inception_year 1 }))]
|
||||
(is (= (map :_id [doc3 doc1])
|
||||
|
|
@ -149,18 +159,18 @@
|
|||
doc1 { :language "Clojure" :_id (ObjectId.) :inception_year 2006 }
|
||||
doc2 { :language "Java" :_id (ObjectId.) :inception_year 1992 }
|
||||
doc3 { :language "Scala" :_id (ObjectId.) :inception_year 2003 }
|
||||
_ (mgcol/insert-batch coll [doc1 doc2 doc3])]
|
||||
_ (mc/insert-batch db coll [doc1 doc2 doc3])]
|
||||
(are [doc, result]
|
||||
(= doc, result)
|
||||
(doc2 (with-collection coll
|
||||
(doc2 (with-collection db coll
|
||||
(find { :inception_year { "$lt" 2000 } })))
|
||||
(doc2 (with-collection coll
|
||||
(doc2 (with-collection db coll
|
||||
(find { :inception_year { "$lte" 1992 } })))
|
||||
(doc1 (with-collection coll
|
||||
(doc1 (with-collection db coll
|
||||
(find { :inception_year { "$gt" 2002 } })
|
||||
(limit 1)
|
||||
(sort { :inception_year -1 })))
|
||||
(doc1 (with-collection coll
|
||||
(doc1 (with-collection db coll
|
||||
(find { :inception_year { "$gte" 2006 } }))))))
|
||||
|
||||
|
||||
|
|
@ -172,8 +182,8 @@
|
|||
srt (-> {}
|
||||
(limit 1)
|
||||
(sort { :inception_year -1 }))
|
||||
_ (mgcol/insert-batch coll [doc1 doc2 doc3])]
|
||||
(is (= [doc1] (with-collection coll
|
||||
_ (mc/insert-batch db coll [doc1 doc2 doc3])]
|
||||
(is (= [doc1] (with-collection db coll
|
||||
(find { :inception_year { "$gt" 2002 } })
|
||||
(merge srt))))))
|
||||
|
||||
|
|
@ -185,12 +195,12 @@
|
|||
doc1 { :_id (ObjectId.) :title "Clojure" :tags ["functional" "homoiconic" "syntax-oriented" "dsls" "concurrency features" "jvm"] }
|
||||
doc2 { :_id (ObjectId.) :title "Java" :tags ["object-oriented" "jvm"] }
|
||||
doc3 { :_id (ObjectId.) :title "Scala" :tags ["functional" "object-oriented" "dsls" "concurrency features" "jvm"] }
|
||||
- (mgcol/insert-batch coll [doc1 doc2 doc3])
|
||||
result1 (with-collection coll
|
||||
- (mc/insert-batch db coll [doc1 doc2 doc3])
|
||||
result1 (with-collection db coll
|
||||
(find { :tags { "$all" ["functional" "jvm" "homoiconic"] } }))
|
||||
result2 (with-collection coll
|
||||
result2 (with-collection db coll
|
||||
(find { :tags { "$all" ["functional" "native" "homoiconic"] } }))
|
||||
result3 (with-collection coll
|
||||
result3 (with-collection db coll
|
||||
(find { :tags { "$all" ["functional" "jvm" "dsls"] } })
|
||||
(sort { :title 1 }))]
|
||||
(is (= [doc1] result1))
|
||||
|
|
@ -205,9 +215,9 @@
|
|||
(let [coll "querying_docs"
|
||||
doc1 { :_id (ObjectId.) :published-by "Jill The Blogger" :draft false :title "X announces another Y" }
|
||||
doc2 { :_id (ObjectId.) :draft true :title "Z announces a Y competitor" }
|
||||
_ (mgcol/insert-batch coll [doc1 doc2])
|
||||
result1 (mgcol/find-one-as-map coll { :published-by { "$exists" true } })
|
||||
result2 (mgcol/find-one-as-map coll { :published-by { "$exists" false } })]
|
||||
_ (mc/insert-batch db coll [doc1 doc2])
|
||||
result1 (mc/find-one-as-map db coll { :published-by { "$exists" true } })
|
||||
result2 (mc/find-one-as-map db coll { :published-by { "$exists" false } })]
|
||||
(is (= doc1 result1))
|
||||
(is (= doc2 result2))))
|
||||
|
||||
|
|
@ -218,10 +228,10 @@
|
|||
doc1 { :_id (ObjectId.) :counter 25 }
|
||||
doc2 { :_id (ObjectId.) :counter 32 }
|
||||
doc3 { :_id (ObjectId.) :counter 63 }
|
||||
_ (mgcol/insert-batch coll [doc1 doc2 doc3])
|
||||
result1 (mgcol/find-one-as-map coll { :counter { "$mod" [10, 5] } })
|
||||
result2 (mgcol/find-one-as-map coll { :counter { "$mod" [10, 2] } })
|
||||
result3 (mgcol/find-one-as-map coll { :counter { "$mod" [11, 1] } })]
|
||||
_ (mc/insert-batch db coll [doc1 doc2 doc3])
|
||||
result1 (mc/find-one-as-map db coll { :counter { "$mod" [10, 5] } })
|
||||
result2 (mc/find-one-as-map db coll { :counter { "$mod" [10, 2] } })
|
||||
result3 (mc/find-one-as-map db coll { :counter { "$mod" [11, 1] } })]
|
||||
(is (= doc1 result1))
|
||||
(is (= doc2 result2))
|
||||
(is (empty? result3))))
|
||||
|
|
@ -233,9 +243,9 @@
|
|||
(let [coll "querying_docs"
|
||||
doc1 { :_id (ObjectId.) :counter 25 }
|
||||
doc2 { :_id (ObjectId.) :counter 32 }
|
||||
_ (mgcol/insert-batch coll [doc1 doc2])
|
||||
result1 (mgcol/find-one-as-map coll { :counter { "$ne" 25 } })
|
||||
result2 (mgcol/find-one-as-map coll { :counter { "$ne" 32 } })]
|
||||
_ (mc/insert-batch db coll [doc1 doc2])
|
||||
result1 (mc/find-one-as-map db coll { :counter { "$ne" 25 } })
|
||||
result2 (mc/find-one-as-map db coll { :counter { "$ne" 32 } })]
|
||||
(is (= doc2 result1))
|
||||
(is (= doc1 result2))))
|
||||
|
||||
|
|
@ -253,22 +263,22 @@
|
|||
doc5 { :_id (ObjectId.) :title "Groovy" :tags ["dynamic" "object-oriented" "dsls" "jvm"] }
|
||||
doc6 { :_id (ObjectId.) :title "OCaml" :tags ["functional" "static" "dsls"] }
|
||||
doc7 { :_id (ObjectId.) :title "Haskell" :tags ["functional" "static" "dsls" "concurrency features"] }
|
||||
- (mgcol/insert-batch coll [doc1 doc2 doc3 doc4 doc5 doc6 doc7])
|
||||
result1 (with-collection coll
|
||||
- (mc/insert-batch db coll [doc1 doc2 doc3 doc4 doc5 doc6 doc7])
|
||||
result1 (with-collection db coll
|
||||
(find {})
|
||||
(paginate :page 1 :per-page 3)
|
||||
(sort { :title 1 })
|
||||
(read-preference (ReadPreference/primary))
|
||||
(options com.mongodb.Bytes/QUERYOPTION_NOTIMEOUT))
|
||||
result2 (with-collection coll
|
||||
result2 (with-collection db coll
|
||||
(find {})
|
||||
(paginate :page 2 :per-page 3)
|
||||
(sort { :title 1 }))
|
||||
result3 (with-collection coll
|
||||
result3 (with-collection db coll
|
||||
(find {})
|
||||
(paginate :page 3 :per-page 3)
|
||||
(sort { :title 1 }))
|
||||
result4 (with-collection coll
|
||||
result4 (with-collection db coll
|
||||
(find {})
|
||||
(paginate :page 10 :per-page 3)
|
||||
(sort { :title 1 }))]
|
||||
|
|
@ -287,8 +297,8 @@
|
|||
tx-doc { :_id (ObjectId.) :name "Texas" :iso "TX" :population 25145561 :joined_in 1845 :capital "Austin" }
|
||||
top3 (partial-query (limit 3))
|
||||
by-population-desc (partial-query (sort { :population -1 }))
|
||||
_ (mgcol/insert-batch coll [ma-doc de-doc ny-doc ca-doc tx-doc])
|
||||
result (with-collection coll
|
||||
_ (mc/insert-batch db coll [ma-doc de-doc ny-doc ca-doc tx-doc])
|
||||
result (with-collection db coll
|
||||
(find {})
|
||||
(merge top3)
|
||||
(merge by-population-desc))]
|
||||
|
|
@ -303,8 +313,8 @@
|
|||
tx-doc { :_id (ObjectId.) :name "Texas" :iso "TX" :population 25145561 :joined_in 1845 :capital "Austin" }
|
||||
top3 (partial-query (limit 3))
|
||||
by-population-desc (partial-query (sort { :population -1 }))
|
||||
_ (mgcol/insert-batch coll [ma-doc de-doc ny-doc ca-doc tx-doc])
|
||||
result (with-collection coll
|
||||
_ (mc/insert-batch db coll [ma-doc de-doc ny-doc ca-doc tx-doc])
|
||||
result (with-collection db coll
|
||||
(find {})
|
||||
(merge top3)
|
||||
(merge by-population-desc)
|
||||
|
|
@ -312,4 +322,4 @@
|
|||
;; documents have fields as strings,
|
||||
;; not keywords
|
||||
(is (= (map #(% "name") result)
|
||||
(map #(% :name) [ca-doc tx-doc ny-doc])))))
|
||||
(map #(% :name) [ca-doc tx-doc ny-doc]))))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue