Merge branch 'master' into query-dsl

This commit is contained in:
Michael S. Klishin 2011-11-09 12:44:56 +04:00
commit 99c46c4860
2 changed files with 60 additions and 31 deletions

27
src/monger/operators.clj Normal file
View file

@ -0,0 +1,27 @@
(ns monger.operators)
(defmacro defoperator
[operator]
(let [op# (str operator)
op-sym# (symbol op#)]
`(def ~op-sym# (str ~op#))))
(defoperator $gt)
(defoperator $inc)
(defoperator $set)
(defoperator $unset)
(defoperator $lt)
(defoperator $lte)
(defoperator $all)
(defoperator $in)
(defoperator $set)
(defoperator $unset)
(defoperator $inc)
(defoperator $push)
(defoperator $pushAll)
(defoperator $addToSet)
(defoperator $pop)
(defoperator $pull)
(defoperator $pullAll)
(defoperator $rename)

View file

@ -3,14 +3,16 @@
(ns monger.test.atomic-modifiers (ns monger.test.atomic-modifiers
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject CommandResult$CommandFailure] (:import [com.mongodb WriteResult WriteConcern DBCursor DBObject CommandResult$CommandFailure]
[org.bson.types ObjectId] [org.bson.types ObjectId]
[java.util Date]) [java.util Date]
)
(:require [monger core util] (:require [monger core util]
[monger.collection :as mgcol] [monger.collection :as mgcol]
[monger.result :as mgres]) [monger.result :as mgres])
(:use [clojure.test] (:use [clojure.test]
[monger.operators]
[monger.test.fixtures])) [monger.test.fixtures]))
(defn purge-scores-collection (defn purge-scores-collection
[f] [f]
(purge-collection "scores" f)) (purge-collection "scores" f))
@ -29,7 +31,7 @@
(let [coll "scores" (let [coll "scores"
oid (ObjectId.)] oid (ObjectId.)]
(mgcol/insert coll { :_id oid :username "l33r0y" :score 100 }) (mgcol/insert coll { :_id oid :username "l33r0y" :score 100 })
(mgcol/update coll { :_id oid } { "$inc" { :score 20 } }) (mgcol/update coll { :_id oid } { $inc { :score 20 } })
(is (= 120 (:score (mgcol/find-map-by-id coll oid)))))) (is (= 120 (:score (mgcol/find-map-by-id coll oid))))))
@ -37,7 +39,7 @@
(let [coll "scores" (let [coll "scores"
oid (ObjectId.)] oid (ObjectId.)]
(mgcol/insert coll { :_id oid :username "l33r0y" }) (mgcol/insert coll { :_id oid :username "l33r0y" })
(mgcol/update coll { :_id oid } { "$inc" { :score 30 } }) (mgcol/update coll { :_id oid } { $inc { :score 30 } })
(is (= 30 (:score (mgcol/find-map-by-id coll oid)))))) (is (= 30 (:score (mgcol/find-map-by-id coll oid))))))
@ -45,7 +47,7 @@
(let [coll "scores" (let [coll "scores"
oid (ObjectId.)] oid (ObjectId.)]
(mgcol/insert coll { :_id oid :username "l33r0y" :score 100 :bonus 0 }) (mgcol/insert coll { :_id oid :username "l33r0y" :score 100 :bonus 0 })
(mgcol/update coll { :_id oid } { "$inc" { :score 20 :bonus 10 } }) (mgcol/update coll { :_id oid } {$inc { :score 20 :bonus 10 } })
(is (= { :_id oid :score 120 :bonus 10 :username "l33r0y" } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :score 120 :bonus 10 :username "l33r0y" } (mgcol/find-map-by-id coll oid)))))
@ -53,7 +55,7 @@
(let [coll "scores" (let [coll "scores"
oid (ObjectId.)] oid (ObjectId.)]
(mgcol/insert coll { :_id oid :username "l33r0y" :score 100 }) (mgcol/insert coll { :_id oid :username "l33r0y" :score 100 })
(mgcol/update coll { :_id oid } { "$inc" { :score 20 :bonus 10 } }) (mgcol/update coll { :_id oid } { $inc { :score 20 :bonus 10 } })
(is (= { :_id oid :score 120 :bonus 10 :username "l33r0y" } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :score 120 :bonus 10 :username "l33r0y" } (mgcol/find-map-by-id coll oid)))))
@ -66,21 +68,21 @@
(let [coll "things" (let [coll "things"
oid (ObjectId.)] oid (ObjectId.)]
(mgcol/insert coll { :_id oid :weight 10.0 }) (mgcol/insert coll { :_id oid :weight 10.0 })
(mgcol/update coll { :_id oid } { "$set" { :weight 20.5 } }) (mgcol/update coll { :_id oid } { $set { :weight 20.5 } })
(is (= 20.5 (:weight (mgcol/find-map-by-id coll oid [:weight])))))) (is (= 20.5 (:weight (mgcol/find-map-by-id coll oid [:weight]))))))
(deftest set-a-single-non-existing-field-using-$set-modifier (deftest set-a-single-non-existing-field-using-$set-modifier
(let [coll "things" (let [coll "things"
oid (ObjectId.)] oid (ObjectId.)]
(mgcol/insert coll { :_id oid :weight 10.0 }) (mgcol/insert coll { :_id oid :weight 10.0 })
(mgcol/update coll { :_id oid } { "$set" { :height 17.2 } }) (mgcol/update coll { :_id oid } { $set { :height 17.2 } })
(is (= 17.2 (:height (mgcol/find-map-by-id coll oid [:height])))))) (is (= 17.2 (:height (mgcol/find-map-by-id coll oid [:height]))))))
(deftest update-multiple-existing-fields-using-$set-modifier (deftest update-multiple-existing-fields-using-$set-modifier
(let [coll "things" (let [coll "things"
oid (ObjectId.)] oid (ObjectId.)]
(mgcol/insert coll { :_id oid :weight 10.0 :height 15.2 }) (mgcol/insert coll { :_id oid :weight 10.0 :height 15.2 })
(mgcol/update coll { :_id oid } { "$set" { :weight 20.5 :height 25.6 } }) (mgcol/update coll { :_id oid } { $set { :weight 20.5 :height 25.6 } })
(is (= { :_id oid :weight 20.5 :height 25.6 } (mgcol/find-map-by-id coll oid [:weight]))))) (is (= { :_id oid :weight 20.5 :height 25.6 } (mgcol/find-map-by-id coll oid [:weight])))))
@ -88,7 +90,7 @@
(let [coll "things" (let [coll "things"
oid (ObjectId.)] oid (ObjectId.)]
(mgcol/insert coll { :_id oid :weight 10.0 }) (mgcol/insert coll { :_id oid :weight 10.0 })
(mgcol/update coll { :_id oid } { "$set" { :weight 20.5 :height 25.6 } }) (mgcol/update coll { :_id oid } {$set { :weight 20.5 :height 25.6 } })
(is (= { :_id oid :weight 20.5 :height 25.6 } (mgcol/find-map-by-id coll oid [:weight]))))) (is (= { :_id oid :weight 20.5 :height 25.6 } (mgcol/find-map-by-id coll oid [:weight])))))
@ -100,7 +102,7 @@
(let [coll "docs" (let [coll "docs"
oid (ObjectId.)] oid (ObjectId.)]
(mgcol/insert coll { :_id oid :title "Document 1" :published true }) (mgcol/insert coll { :_id oid :title "Document 1" :published true })
(mgcol/update coll { :_id oid } { "$unset" { :published 1 } }) (mgcol/update coll { :_id oid } { $unset { :published 1 } })
(is (= { :_id oid :title "Document 1" } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title "Document 1" } (mgcol/find-map-by-id coll oid)))))
@ -108,7 +110,7 @@
(let [coll "docs" (let [coll "docs"
oid (ObjectId.)] oid (ObjectId.)]
(mgcol/insert coll { :_id oid :title "Document 1" :published true :featured true }) (mgcol/insert coll { :_id oid :title "Document 1" :published true :featured true })
(mgcol/update coll { :_id oid } { "$unset" { :published 1 :featured true } }) (mgcol/update coll { :_id oid } { $unset { :published 1 :featured true } })
(is (= { :_id oid :title "Document 1" } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title "Document 1" } (mgcol/find-map-by-id coll oid)))))
@ -116,7 +118,7 @@
(let [coll "docs" (let [coll "docs"
oid (ObjectId.)] oid (ObjectId.)]
(mgcol/insert coll { :_id oid :title "Document 1" :published true }) (mgcol/insert coll { :_id oid :title "Document 1" :published true })
(is (mgres/ok? (mgcol/update coll { :_id oid } { "$unset" { :published 1 :featured true } }))) (is (mgres/ok? (mgcol/update coll { :_id oid } { $unset { :published 1 :featured true } })))
(is (= { :_id oid :title "Document 1" } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title "Document 1" } (mgcol/find-map-by-id coll oid)))))
@ -129,7 +131,7 @@
oid (ObjectId.) oid (ObjectId.)
title "$push modifier appends value to field"] title "$push modifier appends value to field"]
(mgcol/insert coll { :_id oid :title title }) (mgcol/insert coll { :_id oid :title title })
(mgcol/update coll { :_id oid } { "$push" { :tags "modifiers" } }) (mgcol/update coll { :_id oid } { $push { :tags "modifiers" } })
(is (= { :_id oid :title title :tags ["modifiers"] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :tags ["modifiers"] } (mgcol/find-map-by-id coll oid)))))
(deftest add-value-to-an-existing-array-using-$push-modifier (deftest add-value-to-an-existing-array-using-$push-modifier
@ -137,7 +139,7 @@
oid (ObjectId.) oid (ObjectId.)
title "$push modifier appends value to field"] title "$push modifier appends value to field"]
(mgcol/insert coll { :_id oid :title title :tags ["mongodb"] }) (mgcol/insert coll { :_id oid :title title :tags ["mongodb"] })
(mgcol/update coll { :_id oid } { "$push" { :tags "modifiers" } }) (mgcol/update coll { :_id oid } { $push { :tags "modifiers" } })
(is (= { :_id oid :title title :tags ["mongodb" "modifiers"] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :tags ["mongodb" "modifiers"] } (mgcol/find-map-by-id coll oid)))))
@ -148,7 +150,7 @@
oid (ObjectId.) oid (ObjectId.)
title "$push modifier appends value to field"] title "$push modifier appends value to field"]
(mgcol/insert coll { :_id oid :title title :tags ["mongodb"] }) (mgcol/insert coll { :_id oid :title title :tags ["mongodb"] })
(mgcol/update coll { :_id oid } { "$push" { :tags ["modifiers"] } }) (mgcol/update coll { :_id oid } { $push { :tags ["modifiers"] } })
(is (= { :_id oid :title title :tags ["mongodb" ["modifiers"]] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :tags ["mongodb" ["modifiers"]] } (mgcol/find-map-by-id coll oid)))))
@ -158,8 +160,8 @@
oid (ObjectId.) oid (ObjectId.)
title "$push modifier appends value to field"] title "$push modifier appends value to field"]
(mgcol/insert coll { :_id oid :title title :tags ["mongodb"] }) (mgcol/insert coll { :_id oid :title title :tags ["mongodb"] })
(mgcol/update coll { :_id oid } { "$push" { :tags "modifiers" } }) (mgcol/update coll { :_id oid } { $push { :tags "modifiers" } })
(mgcol/update coll { :_id oid } { "$push" { :tags "modifiers" } }) (mgcol/update coll { :_id oid } { $push { :tags "modifiers" } })
(is (= { :_id oid :title title :tags ["mongodb" "modifiers" "modifiers"] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :tags ["mongodb" "modifiers" "modifiers"] } (mgcol/find-map-by-id coll oid)))))
@ -172,7 +174,7 @@
oid (ObjectId.) oid (ObjectId.)
title "$pushAll modifier appends multiple values to field"] title "$pushAll modifier appends multiple values to field"]
(mgcol/insert coll { :_id oid :title title }) (mgcol/insert coll { :_id oid :title title })
(mgcol/update coll { :_id oid } { "$pushAll" { :tags ["mongodb" "docs"] } }) (mgcol/update coll { :_id oid } { $pushAll { :tags ["mongodb" "docs"] } })
(is (= { :_id oid :title title :tags ["mongodb" "docs"] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :tags ["mongodb" "docs"] } (mgcol/find-map-by-id coll oid)))))
(deftest add-value-to-an-existing-array-using-$pushAll-modifier (deftest add-value-to-an-existing-array-using-$pushAll-modifier
@ -180,7 +182,7 @@
oid (ObjectId.) oid (ObjectId.)
title "$pushAll modifier appends multiple values to field"] title "$pushAll modifier appends multiple values to field"]
(mgcol/insert coll { :_id oid :title title :tags ["mongodb"] }) (mgcol/insert coll { :_id oid :title title :tags ["mongodb"] })
(mgcol/update coll { :_id oid } { "$pushAll" { :tags ["modifiers" "docs"] } }) (mgcol/update coll { :_id oid } { $pushAll { :tags ["modifiers" "docs"] } })
(is (= { :_id oid :title title :tags ["mongodb" "modifiers" "docs"] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :tags ["mongodb" "modifiers" "docs"] } (mgcol/find-map-by-id coll oid)))))
@ -189,7 +191,7 @@
oid (ObjectId.) oid (ObjectId.)
title "$pushAll modifier appends multiple values to field"] title "$pushAll modifier appends multiple values to field"]
(mgcol/insert coll { :_id oid :title title :tags ["mongodb" "docs"] }) (mgcol/insert coll { :_id oid :title title :tags ["mongodb" "docs"] })
(mgcol/update coll { :_id oid } { "$pushAll" { :tags ["modifiers" "docs"] } }) (mgcol/update coll { :_id oid } { $pushAll { :tags ["modifiers" "docs"] } })
(is (= { :_id oid :title title :tags ["mongodb" "docs" "modifiers" "docs"] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :tags ["mongodb" "docs" "modifiers" "docs"] } (mgcol/find-map-by-id coll oid)))))
@ -202,7 +204,7 @@
oid (ObjectId.) oid (ObjectId.)
title "$addToSet modifier appends value to field unless it is already there"] title "$addToSet modifier appends value to field unless it is already there"]
(mgcol/insert coll { :_id oid :title title }) (mgcol/insert coll { :_id oid :title title })
(mgcol/update coll { :_id oid } { "$addToSet" { :tags "modifiers" } }) (mgcol/update coll { :_id oid } { $addToSet { :tags "modifiers" } })
(is (= { :_id oid :title title :tags ["modifiers"] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :tags ["modifiers"] } (mgcol/find-map-by-id coll oid)))))
(deftest add-value-to-an-existing-array-using-$addToSet-modifier (deftest add-value-to-an-existing-array-using-$addToSet-modifier
@ -210,7 +212,7 @@
oid (ObjectId.) oid (ObjectId.)
title "$addToSet modifier appends value to field unless it is already there"] title "$addToSet modifier appends value to field unless it is already there"]
(mgcol/insert coll { :_id oid :title title :tags ["mongodb"] }) (mgcol/insert coll { :_id oid :title title :tags ["mongodb"] })
(mgcol/update coll { :_id oid } { "$addToSet" { :tags "modifiers" } }) (mgcol/update coll { :_id oid } { $addToSet { :tags "modifiers" } })
(is (= { :_id oid :title title :tags ["mongodb" "modifiers"] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :tags ["mongodb" "modifiers"] } (mgcol/find-map-by-id coll oid)))))
@ -219,8 +221,8 @@
oid (ObjectId.) oid (ObjectId.)
title "$addToSet modifier appends value to field unless it is already there"] title "$addToSet modifier appends value to field unless it is already there"]
(mgcol/insert coll { :_id oid :title title :tags ["mongodb"] }) (mgcol/insert coll { :_id oid :title title :tags ["mongodb"] })
(mgcol/update coll { :_id oid } { "$addToSet" { :tags "modifiers" } }) (mgcol/update coll { :_id oid } { $addToSet { :tags "modifiers" } })
(mgcol/update coll { :_id oid } { "$addToSet" { :tags "modifiers" } }) (mgcol/update coll { :_id oid } { $addToSet { :tags "modifiers" } })
(is (= { :_id oid :title title :tags ["mongodb" "modifiers"] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :tags ["mongodb" "modifiers"] } (mgcol/find-map-by-id coll oid)))))
@ -233,7 +235,7 @@
oid (ObjectId.) oid (ObjectId.)
title "$pop modifier removes last or first value in the array"] title "$pop modifier removes last or first value in the array"]
(mgcol/insert coll { :_id oid :title title :tags ["products" "apple" "reviews"] }) (mgcol/insert coll { :_id oid :title title :tags ["products" "apple" "reviews"] })
(mgcol/update coll { :_id oid } { "$pop" { :tags 1 } }) (mgcol/update coll { :_id oid } { $pop { :tags 1 } })
(is (= { :_id oid :title title :tags ["products" "apple"] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :tags ["products" "apple"] } (mgcol/find-map-by-id coll oid)))))
(deftest unshift-first-value-in-the-array-using-$pop-modifier (deftest unshift-first-value-in-the-array-using-$pop-modifier
@ -241,7 +243,7 @@
oid (ObjectId.) oid (ObjectId.)
title "$pop modifier removes last or first value in the array"] title "$pop modifier removes last or first value in the array"]
(mgcol/insert coll { :_id oid :title title :tags ["products" "apple" "reviews"] }) (mgcol/insert coll { :_id oid :title title :tags ["products" "apple" "reviews"] })
(mgcol/update coll { :_id oid } { "$pop" { :tags -1 } }) (mgcol/update coll { :_id oid } { $pop { :tags -1 } })
(is (= { :_id oid :title title :tags ["apple" "reviews"] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :tags ["apple" "reviews"] } (mgcol/find-map-by-id coll oid)))))
(deftest pop-last-values-from-multiple-arrays-using-$pop-modifier (deftest pop-last-values-from-multiple-arrays-using-$pop-modifier
@ -249,7 +251,7 @@
oid (ObjectId.) oid (ObjectId.)
title "$pop modifier removes last or first value in the array"] title "$pop modifier removes last or first value in the array"]
(mgcol/insert coll { :_id oid :title title :tags ["products" "apple" "reviews"] :categories ["apple" "reviews" "drafts"] }) (mgcol/insert coll { :_id oid :title title :tags ["products" "apple" "reviews"] :categories ["apple" "reviews" "drafts"] })
(mgcol/update coll { :_id oid } { "$pop" { :tags 1 :categories 1 } }) (mgcol/update coll { :_id oid } { $pop { :tags 1 :categories 1 } })
(is (= { :_id oid :title title :tags ["products" "apple"] :categories ["apple" "reviews"] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :tags ["products" "apple"] :categories ["apple" "reviews"] } (mgcol/find-map-by-id coll oid)))))
@ -262,7 +264,7 @@
oid (ObjectId.) oid (ObjectId.)
title "$pull modifier removes all value entries in the array"] title "$pull modifier removes all value entries in the array"]
(mgcol/insert coll { :_id oid :title title :measurements [1.0 1.2 1.2 1.2 1.1 1.1 1.2 1.3 1.0] }) (mgcol/insert coll { :_id oid :title title :measurements [1.0 1.2 1.2 1.2 1.1 1.1 1.2 1.3 1.0] })
(mgcol/update coll { :_id oid } { "$pull" { :measurements 1.2 } }) (mgcol/update coll { :_id oid } { $pull { :measurements 1.2 } })
(is (= { :_id oid :title title :measurements [1.0 1.1 1.1 1.3 1.0] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :measurements [1.0 1.1 1.1 1.3 1.0] } (mgcol/find-map-by-id coll oid)))))
@ -275,7 +277,7 @@
oid (ObjectId.) oid (ObjectId.)
title "$pullAll modifier removes entries of multiple values in the array"] title "$pullAll modifier removes entries of multiple values in the array"]
(mgcol/insert coll { :_id oid :title title :measurements [1.0 1.2 1.2 1.2 1.1 1.1 1.2 1.3 1.0] }) (mgcol/insert coll { :_id oid :title title :measurements [1.0 1.2 1.2 1.2 1.1 1.1 1.2 1.3 1.0] })
(mgcol/update coll { :_id oid } { "$pullAll" { :measurements [1.0 1.1 1.2] } }) (mgcol/update coll { :_id oid } { $pullAll { :measurements [1.0 1.1 1.2] } })
(is (= { :_id oid :title title :measurements [1.3] } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :measurements [1.3] } (mgcol/find-map-by-id coll oid)))))
@ -289,5 +291,5 @@
title "$rename renames fields" title "$rename renames fields"
v [1.0 1.2 1.2 1.2 1.1 1.1 1.2 1.3 1.0]] v [1.0 1.2 1.2 1.2 1.1 1.1 1.2 1.3 1.0]]
(mgcol/insert coll { :_id oid :title title :measurements v }) (mgcol/insert coll { :_id oid :title title :measurements v })
(mgcol/update coll { :_id oid } { "$rename" { :measurements "results" } }) (mgcol/update coll { :_id oid } { $rename { :measurements "results" } })
(is (= { :_id oid :title title :results v } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title title :results v } (mgcol/find-map-by-id coll oid)))))