Use clojure.test/are and namespace aliases

This commit is contained in:
Michael S. Klishin 2011-10-11 21:04:21 +04:00
parent c48c87fd2f
commit a7264122e3

View file

@ -333,43 +333,40 @@
;; more sophisticated examples ;; more sophisticated examples
(deftest find-with-conditional-operators-comparison (deftest find-with-conditional-operators-comparison
(let [collection "libraries"] (let [collection "libraries"]
(monger.collection/insert-batch collection [{ :language "Clojure", :name "monger" :users 1} (mgcol/insert-batch collection [{ :language "Clojure", :name "monger" :users 1}
{ :language "Clojure", :name "langohr" :users 5 } { :language "Clojure", :name "langohr" :users 5 }
{ :language "Clojure", :name "incanter" :users 15 } { :language "Clojure", :name "incanter" :users 15 }
{ :language "Scala", :name "akka" :users 150}]) { :language "Scala", :name "akka" :users 150}])
(are [a b] (= a (.count (mgcol/find collection b)))
(is (= 2 (.count (monger.collection/find collection { :users { "$gt" 10 }})))) 2 { :users { "$gt" 10 }}
(is (= 3 (.count (monger.collection/find collection { :users { "$gte" 5 }})))) 3 { :users { "$gte" 5 }}
(is (= 2 (.count (monger.collection/find collection { :users { "$lt" 10 }})))) 2 { :users { "$lt" 10 }}
(is (= 2 (.count (monger.collection/find collection { :users { "$lte" 5 }})))) 2 { :users { "$lte" 5 }}
(is (= 1 (.count (monger.collection/find collection { :users { "$gt" 10 "$lt" 150 }})))) 1 { :users { "$gt" 10 "$lt" 150 }})))
))
(deftest find-on-embedded-arrays (deftest find-on-embedded-arrays
(let [collection "libraries"] (let [collection "libraries"]
(monger.collection/insert-batch collection [{ :language "Clojure", :tags [ "functional" ] } (mgcol/insert-batch collection [{ :language "Clojure", :tags [ "functional" ] }
{ :language "Scala", :tags [ "functional" "object-oriented" ] } { :language "Scala", :tags [ "functional" "object-oriented" ] }
{ :language "Ruby", :tags [ "object-oriented" "dynamic" ] }]) { :language "Ruby", :tags [ "object-oriented" "dynamic" ] }])
(is (= "Scala" (:language (first (monger.collection/find-maps collection { :tags { "$all" [ "functional" "object-oriented" ] } } ))))) (is (= "Scala" (:language (first (mgcol/find-maps collection { :tags { "$all" [ "functional" "object-oriented" ] } } )))))
(is (= 3 (.count (monger.collection/find-maps collection { :tags { "$in" [ "functional" "object-oriented" ] } } )))))) (is (= 3 (.count (mgcol/find-maps collection { :tags { "$in" [ "functional" "object-oriented" ] } } ))))))
(deftest find-with-conditional-operators-on-embedded-documents (deftest find-with-conditional-operators-on-embedded-documents
(let [collection "people"] (let [collection "people"]
(monger.collection/insert-batch collection [{ :name "Bob", :comments [ { :text "Nice!" :rating 1 } (mgcol/insert-batch collection [{ :name "Bob", :comments [ { :text "Nice!" :rating 1 }
{ :text "Love it" :rating 4 } { :text "Love it" :rating 4 }
{ :text "What?":rating -5 } ] } { :text "What?":rating -5 } ] }
{ :name "Alice", :comments [ { :text "Yeah" :rating 2 } { :name "Alice", :comments [ { :text "Yeah" :rating 2 }
{ :text "Doh" :rating 1 } { :text "Doh" :rating 1 }
{ :text "Agreed" :rating 3 } { :text "Agreed" :rating 3 }
] } ]) ] } ])
(are [a b] (= a (.count (mgcol/find collection b)))
(is (= 1 (.count (monger.collection/find collection { :comments { "$elemMatch" { :text "Nice!" :rating { "$gte" 1 } } } })))) 1 { :comments { "$elemMatch" { :text "Nice!" :rating { "$gte" 1 } } } }
2 { "comments.rating" 1 }
(is (= 2 (.count (monger.collection/find collection { "comments.rating" 1 } )))) 1 { "comments.rating" { "$gt" 3 } })))
(is (= 1 (.count (monger.collection/find collection { "comments.rating" { "$gt" 3 } }))))))
;; ;;
;; update, save ;; update, save