diff --git a/test/monger/test/atomic_modifiers.clj b/test/monger/test/atomic_modifiers.clj new file mode 100644 index 0000000..8e57d7f --- /dev/null +++ b/test/monger/test/atomic_modifiers.clj @@ -0,0 +1,50 @@ +(set! *warn-on-reflection* true) + +(ns monger.test.atomic-modifiers + (:import [com.mongodb WriteResult WriteConcern DBCursor DBObject CommandResult$CommandFailure] + [org.bson.types ObjectId]) + (:require [monger core util] + [monger.collection :as mgcol] + [monger.result :as mgres]) + (:use [clojure.test] + [monger.test.fixtures])) + + +(use-fixtures :each purge-docs-collection purge-things-collection) + +(monger.core/set-default-write-concern! WriteConcern/SAFE) + + + +;; +;; $set +;; + +(deftest update-a-single-existing-field-using-$set-operator + (let [coll "things" + oid (ObjectId.)] + (mgcol/insert coll { :_id oid :weight 10.0 }) + (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-operator + (let [coll "things" + oid (ObjectId.)] + (mgcol/insert coll { :_id oid :weight 10.0 }) + (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-operator + (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 } }) + (is (= { :_id oid :weight 20.5 :height 25.6 } (mgcol/find-map-by-id coll oid [:weight]))))) + + +(deftest update-and-set-multiple-fields-using-$set-operator + (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 } }) + (is (= { :_id oid :weight 20.5 :height 25.6 } (mgcol/find-map-by-id coll oid [:weight]))))) diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index 8d28fa2..94a1a67 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -10,37 +10,13 @@ [monger.result :as mgres] [monger.conversion :as mgcnv] [monger.js :as js]) - (:use [clojure.test])) + (:use [clojure.test] + [monger.test.fixtures])) (monger.core/connect!) (monger.core/set-db! (monger.core/get-db "monger-test")) -;; -;; fixture functions -;; - -(defn purge-collection - [collection-name, f] - (mgcol/remove collection-name) - (f) - (mgcol/remove collection-name)) - -(defn purge-people-collection - [f] - (purge-collection "people" f)) - -(defn purge-docs-collection - [f] - (purge-collection "docs" f)) - -(defn purge-things-collection - [f] - (purge-collection "things" f)) - -(defn purge-libraries-collection - [f] - (purge-collection "libraries" f)) (use-fixtures :each purge-people-collection purge-docs-collection purge-things-collection purge-libraries-collection) diff --git a/test/monger/test/fixtures.clj b/test/monger/test/fixtures.clj new file mode 100644 index 0000000..de6e721 --- /dev/null +++ b/test/monger/test/fixtures.clj @@ -0,0 +1,28 @@ +(ns monger.test.fixtures + (:require [monger.collection :as mgcol])) + +;; +;; fixture functions +;; + +(defn purge-collection + [collection-name, f] + (mgcol/remove collection-name) + (f) + (mgcol/remove collection-name)) + +(defn purge-people-collection + [f] + (purge-collection "people" f)) + +(defn purge-docs-collection + [f] + (purge-collection "docs" f)) + +(defn purge-things-collection + [f] + (purge-collection "things" f)) + +(defn purge-libraries-collection + [f] + (purge-collection "libraries" f))