Initial set of tests for atomic modifiers
Mostly to serve as examples but also to see where we may be beneficial for Monger to offer special support for some of them.
This commit is contained in:
parent
592451b2a1
commit
9bd9b93fa1
3 changed files with 80 additions and 26 deletions
50
test/monger/test/atomic_modifiers.clj
Normal file
50
test/monger/test/atomic_modifiers.clj
Normal file
|
|
@ -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])))))
|
||||||
|
|
@ -10,37 +10,13 @@
|
||||||
[monger.result :as mgres]
|
[monger.result :as mgres]
|
||||||
[monger.conversion :as mgcnv]
|
[monger.conversion :as mgcnv]
|
||||||
[monger.js :as js])
|
[monger.js :as js])
|
||||||
(:use [clojure.test]))
|
(:use [clojure.test]
|
||||||
|
[monger.test.fixtures]))
|
||||||
|
|
||||||
(monger.core/connect!)
|
(monger.core/connect!)
|
||||||
(monger.core/set-db! (monger.core/get-db "monger-test"))
|
(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)
|
(use-fixtures :each purge-people-collection purge-docs-collection purge-things-collection purge-libraries-collection)
|
||||||
|
|
||||||
|
|
|
||||||
28
test/monger/test/fixtures.clj
Normal file
28
test/monger/test/fixtures.clj
Normal file
|
|
@ -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))
|
||||||
Loading…
Reference in a new issue