diff --git a/src/monger/operators.clj b/src/monger/operators.clj new file mode 100644 index 0000000..ad94a82 --- /dev/null +++ b/src/monger/operators.clj @@ -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) diff --git a/test/monger/test/atomic_modifiers.clj b/test/monger/test/atomic_modifiers.clj index f4a3e7a..95e5ebb 100644 --- a/test/monger/test/atomic_modifiers.clj +++ b/test/monger/test/atomic_modifiers.clj @@ -3,14 +3,16 @@ (ns monger.test.atomic-modifiers (:import [com.mongodb WriteResult WriteConcern DBCursor DBObject CommandResult$CommandFailure] [org.bson.types ObjectId] - [java.util Date]) + [java.util Date] + + ) (:require [monger core util] [monger.collection :as mgcol] [monger.result :as mgres]) (:use [clojure.test] + [monger.operators] [monger.test.fixtures])) - (defn purge-scores-collection [f] (purge-collection "scores" f)) @@ -29,7 +31,7 @@ (let [coll "scores" oid (ObjectId.)] (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)))))) @@ -37,7 +39,7 @@ (let [coll "scores" oid (ObjectId.)] (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)))))) @@ -45,7 +47,7 @@ (let [coll "scores" oid (ObjectId.)] (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))))) @@ -53,7 +55,7 @@ (let [coll "scores" oid (ObjectId.)] (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))))) @@ -66,21 +68,21 @@ (let [coll "things" oid (ObjectId.)] (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])))))) (deftest set-a-single-non-existing-field-using-$set-modifier (let [coll "things" oid (ObjectId.)] (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])))))) (deftest update-multiple-existing-fields-using-$set-modifier (let [coll "things" oid (ObjectId.)] (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]))))) @@ -88,7 +90,7 @@ (let [coll "things" oid (ObjectId.)] (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]))))) @@ -100,7 +102,7 @@ (let [coll "docs" oid (ObjectId.)] (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))))) @@ -108,7 +110,7 @@ (let [coll "docs" oid (ObjectId.)] (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))))) @@ -116,7 +118,7 @@ (let [coll "docs" oid (ObjectId.)] (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))))) @@ -129,7 +131,7 @@ oid (ObjectId.) title "$push modifier appends value to field"] (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))))) (deftest add-value-to-an-existing-array-using-$push-modifier @@ -137,7 +139,7 @@ oid (ObjectId.) title "$push modifier appends value to field"] (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))))) @@ -148,7 +150,7 @@ oid (ObjectId.) title "$push modifier appends value to field"] (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))))) @@ -158,8 +160,8 @@ oid (ObjectId.) title "$push modifier appends value to field"] (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))))) @@ -172,7 +174,7 @@ oid (ObjectId.) title "$pushAll modifier appends multiple values to field"] (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))))) (deftest add-value-to-an-existing-array-using-$pushAll-modifier @@ -180,7 +182,7 @@ oid (ObjectId.) title "$pushAll modifier appends multiple values to field"] (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))))) @@ -189,7 +191,7 @@ oid (ObjectId.) title "$pushAll modifier appends multiple values to field"] (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))))) @@ -202,7 +204,7 @@ oid (ObjectId.) title "$addToSet modifier appends value to field unless it is already there"] (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))))) (deftest add-value-to-an-existing-array-using-$addToSet-modifier @@ -210,7 +212,7 @@ oid (ObjectId.) title "$addToSet modifier appends value to field unless it is already there"] (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))))) @@ -219,8 +221,8 @@ oid (ObjectId.) title "$addToSet modifier appends value to field unless it is already there"] (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))))) @@ -233,7 +235,7 @@ oid (ObjectId.) title "$pop modifier removes last or first value in the array"] (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))))) (deftest unshift-first-value-in-the-array-using-$pop-modifier @@ -241,7 +243,7 @@ oid (ObjectId.) title "$pop modifier removes last or first value in the array"] (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))))) (deftest pop-last-values-from-multiple-arrays-using-$pop-modifier @@ -249,7 +251,7 @@ oid (ObjectId.) 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/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))))) @@ -262,7 +264,7 @@ oid (ObjectId.) 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/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))))) @@ -275,7 +277,7 @@ oid (ObjectId.) 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/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))))) @@ -289,5 +291,5 @@ title "$rename renames fields" 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/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)))))