From bb169d3ae361cffac8ddfb08d9f9233b553bc3b2 Mon Sep 17 00:00:00 2001 From: Oleksandr Petrov Date: Tue, 8 Nov 2011 00:12:10 +0100 Subject: [PATCH] Adding operators DSL in order to use Clojure functions instead of hashes. Casbah has a similar concept called Bareword Operators. Korma uses operators a bit differently, but idea is quite same, too. One of the obvious advantages is that you can make sure you didn't screw make a typo in operator name during compile-time. Plus, we can guarantee that there is a subset of used / supported operators, and add them gradually, keeping track of them in one place, which will simplify tests, too. --- src/monger/operators.clj | 28 ++++++++++++++++++++++++++ test/monger/test/atomic_modifiers.clj | 29 +++++++++++++++------------ 2 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 src/monger/operators.clj diff --git a/src/monger/operators.clj b/src/monger/operators.clj new file mode 100644 index 0000000..d6e0017 --- /dev/null +++ b/src/monger/operators.clj @@ -0,0 +1,28 @@ +(ns monger.operators) + +(defmacro defoperator + [operator] + (let [operator-name (symbol (str operator))] + `(defn ~operator-name + [arg#] + { (str '~operator-name) arg# } + ))) +(defoperator $gt) +(defoperator $inc) +(defoperator $set) +(defoperator $unset) + +;; $lt +;; $lte +;; $all +;; $in +;; $set +;; $unset +;; $inc +;; $push +;; $pushAll +;; $addToSet +;; $pop +;; $pull +;; $pullAll +;; $rename \ No newline at end of file diff --git a/test/monger/test/atomic_modifiers.clj b/test/monger/test/atomic_modifiers.clj index f4a3e7a..02128fd 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)) @@ -28,8 +30,9 @@ (deftest increment-a-single-existing-field-using-$inc-modifier (let [coll "scores" oid (ObjectId.)] + (println ($inc { :score 20 } )) (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 +40,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 +48,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 +56,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 +69,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 +91,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 +103,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 +111,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 +119,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)))))