Updating test now passes
This commit is contained in:
parent
9752950d82
commit
c64f5a66d9
1 changed files with 140 additions and 134 deletions
|
|
@ -2,162 +2,168 @@
|
||||||
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject]
|
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject]
|
||||||
org.bson.types.ObjectId
|
org.bson.types.ObjectId
|
||||||
java.util.Date)
|
java.util.Date)
|
||||||
(:require [monger core util]
|
(:require [monger.core :as mg]
|
||||||
[monger.collection :as mc]
|
[monger.collection :as mc]
|
||||||
|
[monger.util :as mu]
|
||||||
[monger.result :as mr]
|
[monger.result :as mr]
|
||||||
[monger.test.helper :as helper]
|
|
||||||
[clojure.test :refer :all]
|
[clojure.test :refer :all]
|
||||||
[monger.operators :refer :all]
|
[monger.operators :refer :all]
|
||||||
[monger.test.fixtures :refer :all]
|
|
||||||
[monger.conversion :refer [to-db-object]]))
|
[monger.conversion :refer [to-db-object]]))
|
||||||
|
|
||||||
(helper/connect!)
|
(let [conn (mg/connect)
|
||||||
|
db (mg/get-db conn "monger-test")]
|
||||||
|
(defn purge-collections
|
||||||
|
[f]
|
||||||
|
(mc/remove db "people")
|
||||||
|
(mc/remove db "docs")
|
||||||
|
(mc/remove db "things")
|
||||||
|
(mc/remove db "libraries")
|
||||||
|
(f)
|
||||||
|
(mc/remove db "people")
|
||||||
|
(mc/remove db "docs")
|
||||||
|
(mc/remove db "things")
|
||||||
|
(mc/remove db "libraries"))
|
||||||
|
|
||||||
(use-fixtures :each purge-people purge-docs purge-things purge-libraries)
|
(use-fixtures :each purge-collections)
|
||||||
|
|
||||||
|
(deftest ^{:updating true} update-document-by-id-without-upsert
|
||||||
|
(let [collection "libraries"
|
||||||
|
doc-id (mu/random-uuid)
|
||||||
|
date (Date.)
|
||||||
|
doc { :created-at date, :data-store "MongoDB", :language "Clojure", :_id doc-id }
|
||||||
|
modified-doc { :created-at date, :data-store "MongoDB", :language "Erlang", :_id doc-id }]
|
||||||
|
(mc/insert db collection doc)
|
||||||
|
(is (= (doc (mc/find-by-id db collection doc-id))))
|
||||||
|
(mc/update db collection { :_id doc-id } { :language "Erlang" })
|
||||||
|
(is (= (modified-doc (mc/find-by-id db collection doc-id))))))
|
||||||
|
|
||||||
|
(deftest ^{:updating true} update-document-by-id-without-upsert-using-update-by-id
|
||||||
|
(let [collection "libraries"
|
||||||
|
doc-id (mu/random-uuid)
|
||||||
|
date (Date.)
|
||||||
|
doc { :created-at date, :data-store "MongoDB", :language "Clojure", :_id doc-id }
|
||||||
|
modified-doc { :created-at date, :data-store "MongoDB", :language "Erlang", :_id doc-id }]
|
||||||
|
(mc/insert db collection doc)
|
||||||
|
(is (= (doc (mc/find-by-id db collection doc-id))))
|
||||||
|
(mc/update-by-id db collection doc-id { :language "Erlang" })
|
||||||
|
(is (= (modified-doc (mc/find-by-id db collection doc-id))))))
|
||||||
|
|
||||||
|
(deftest ^{:updating true} update-nested-document-fields-without-upsert-using-update-by-id
|
||||||
|
(let [collection "libraries"
|
||||||
|
doc-id (ObjectId.)
|
||||||
|
date (Date.)
|
||||||
|
doc { :created-at date :data-store "MongoDB" :language { :primary "Clojure" } :_id doc-id }
|
||||||
|
modified-doc { :created-at date :data-store "MongoDB" :language { :primary "Erlang" } :_id doc-id }]
|
||||||
|
(mc/insert db collection doc)
|
||||||
|
(is (= (doc (mc/find-by-id db collection doc-id))))
|
||||||
|
(mc/update-by-id db collection doc-id { $set { "language.primary" "Erlang" }})
|
||||||
|
(is (= (modified-doc (mc/find-by-id db collection doc-id))))))
|
||||||
|
|
||||||
|
|
||||||
;;
|
(deftest ^{:updating true} update-multiple-documents
|
||||||
;; update, save
|
(let [collection "libraries"]
|
||||||
;;
|
(mc/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||||
|
{ :language "Clojure", :name "langohr" }
|
||||||
(deftest ^{:updating true} update-document-by-id-without-upsert
|
{ :language "Clojure", :name "incanter" }
|
||||||
(let [collection "libraries"
|
{ :language "Scala", :name "akka" }])
|
||||||
doc-id (monger.util/random-uuid)
|
(is (= 3 (mc/count db collection { :language "Clojure" })))
|
||||||
date (Date.)
|
(is (= 1 (mc/count db collection { :language "Scala" })))
|
||||||
doc { :created-at date, :data-store "MongoDB", :language "Clojure", :_id doc-id }
|
(is (= 0 (mc/count db collection { :language "Python" })))
|
||||||
modified-doc { :created-at date, :data-store "MongoDB", :language "Erlang", :_id doc-id }]
|
(mc/update db collection { :language "Clojure" } { $set { :language "Python" } } {:multi true})
|
||||||
(mc/insert collection doc)
|
(is (= 0 (mc/count db collection { :language "Clojure" })))
|
||||||
(is (= (doc (mc/find-by-id collection doc-id))))
|
(is (= 1 (mc/count db collection { :language "Scala" })))
|
||||||
(mc/update collection { :_id doc-id } { :language "Erlang" })
|
(is (= 3 (mc/count db collection { :language "Python" })))))
|
||||||
(is (= (modified-doc (mc/find-by-id collection doc-id))))))
|
|
||||||
|
|
||||||
(deftest ^{:updating true} update-document-by-id-without-upsert-using-update-by-id
|
|
||||||
(let [collection "libraries"
|
|
||||||
doc-id (monger.util/random-uuid)
|
|
||||||
date (Date.)
|
|
||||||
doc { :created-at date, :data-store "MongoDB", :language "Clojure", :_id doc-id }
|
|
||||||
modified-doc { :created-at date, :data-store "MongoDB", :language "Erlang", :_id doc-id }]
|
|
||||||
(mc/insert collection doc)
|
|
||||||
(is (= (doc (mc/find-by-id collection doc-id))))
|
|
||||||
(mc/update-by-id collection doc-id { :language "Erlang" })
|
|
||||||
(is (= (modified-doc (mc/find-by-id collection doc-id))))))
|
|
||||||
|
|
||||||
(deftest ^{:updating true} update-nested-document-fields-without-upsert-using-update-by-id
|
|
||||||
(let [collection "libraries"
|
|
||||||
doc-id (ObjectId.)
|
|
||||||
date (Date.)
|
|
||||||
doc { :created-at date :data-store "MongoDB" :language { :primary "Clojure" } :_id doc-id }
|
|
||||||
modified-doc { :created-at date :data-store "MongoDB" :language { :primary "Erlang" } :_id doc-id }]
|
|
||||||
(mc/insert collection doc)
|
|
||||||
(is (= (doc (mc/find-by-id collection doc-id))))
|
|
||||||
(mc/update-by-id collection doc-id { $set { "language.primary" "Erlang" }})
|
|
||||||
(is (= (modified-doc (mc/find-by-id collection doc-id))))))
|
|
||||||
|
|
||||||
|
|
||||||
(deftest ^{:updating true} update-multiple-documents
|
(deftest ^{:updating true} save-a-new-document
|
||||||
(let [collection "libraries"]
|
(let [collection "people"
|
||||||
(mc/insert collection { :language "Clojure", :name "monger" })
|
document {:name "Joe" :age 30}]
|
||||||
(mc/insert collection { :language "Clojure", :name "langohr" })
|
(is (mr/ok? (mc/save db "people" document)))
|
||||||
(mc/insert collection { :language "Clojure", :name "incanter" })
|
(is (= 1 (mc/count db collection)))))
|
||||||
(mc/insert collection { :language "Scala", :name "akka" })
|
|
||||||
(is (= 3 (mc/count collection { :language "Clojure" })))
|
(deftest ^{:updating true} save-and-return-a-new-document
|
||||||
(is (= 1 (mc/count collection { :language "Scala" })))
|
(let [collection "people"
|
||||||
(is (= 0 (mc/count collection { :language "Python" })))
|
document {:name "Joe" :age 30}
|
||||||
(mc/update collection { :language "Clojure" } { $set { :language "Python" } } :multi true)
|
returned (mc/save-and-return db "people" document)]
|
||||||
(is (= 0 (mc/count collection { :language "Clojure" })))
|
(is (:_id returned))
|
||||||
(is (= 1 (mc/count collection { :language "Scala" })))
|
(is (= document (dissoc returned :_id)))
|
||||||
(is (= 3 (mc/count collection { :language "Python" })))))
|
(is (= 1 (mc/count db collection)))))
|
||||||
|
|
||||||
|
|
||||||
(deftest ^{:updating true} save-a-new-document
|
(deftest ^{:updating true} save-a-new-basic-db-object
|
||||||
(let [collection "people"
|
(let [collection "people"
|
||||||
document {:name "Joe" :age 30}]
|
doc (to-db-object {:name "Joe" :age 30})]
|
||||||
(is (mr/ok? (mc/save "people" document)))
|
(is (nil? (mu/get-id doc)))
|
||||||
(is (= 1 (mc/count collection)))))
|
(mc/save db "people" doc WriteConcern/SAFE)
|
||||||
|
(is (not (nil? (mu/get-id doc))))))
|
||||||
(deftest ^{:updating true} save-and-return-a-new-document
|
|
||||||
(let [collection "people"
|
|
||||||
document {:name "Joe" :age 30}
|
|
||||||
returned (mc/save-and-return "people" document)]
|
|
||||||
(is (:_id returned))
|
|
||||||
(is (= document (dissoc returned :_id)))
|
|
||||||
(is (= 1 (mc/count collection)))))
|
|
||||||
|
|
||||||
|
|
||||||
(deftest ^{:updating true} save-a-new-basic-db-object
|
|
||||||
(let [collection "people"
|
|
||||||
doc (to-db-object {:name "Joe" :age 30})]
|
|
||||||
(is (nil? (monger.util/get-id doc)))
|
|
||||||
(mc/save monger.core/*mongodb-database* "people" doc WriteConcern/SAFE)
|
|
||||||
(is (not (nil? (monger.util/get-id doc))))))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(deftest ^{:updating true} update-an-existing-document-using-save
|
(deftest ^{:updating true} update-an-existing-document-using-save
|
||||||
(let [collection "people"
|
(let [collection "people"
|
||||||
doc-id "people-1"
|
doc-id "people-1"
|
||||||
document { :_id doc-id, :name "Joe", :age 30 }]
|
document { :_id doc-id, :name "Joe", :age 30 }]
|
||||||
(is (mr/ok? (mc/insert "people" document)))
|
(is (mr/ok? (mc/insert db collection document)))
|
||||||
(is (= 1 (mc/count collection)))
|
(is (= 1 (mc/count db collection)))
|
||||||
(mc/save collection { :_id doc-id, :name "Alan", :age 40 })
|
(mc/save db collection { :_id doc-id, :name "Alan", :age 40 })
|
||||||
(is (= 1 (mc/count collection { :name "Alan", :age 40 })))))
|
(is (= 1 (mc/count db collection { :name "Alan", :age 40 })))))
|
||||||
|
|
||||||
(deftest ^{:updating true} update-an-existing-document-using-save-and-return
|
(deftest ^{:updating true} update-an-existing-document-using-save-and-return
|
||||||
(let [collection "people"
|
(let [collection "people"
|
||||||
document (mc/insert-and-return "people" {:name "Joe" :age 30})
|
document (mc/insert-and-return db collection {:name "Joe" :age 30})
|
||||||
doc-id (:_id document)
|
doc-id (:_id document)
|
||||||
updated (mc/save-and-return collection {:_id doc-id :name "Alan" :age 40})]
|
updated (mc/save-and-return db collection {:_id doc-id :name "Alan" :age 40})]
|
||||||
(is (= {:_id doc-id :name "Alan" :age 40} updated))
|
(is (= {:_id doc-id :name "Alan" :age 40} updated))
|
||||||
(is (= 1 (mc/count collection)))
|
(is (= 1 (mc/count db collection)))
|
||||||
(is (= 1 (mc/count collection {:name "Alan" :age 40})))))
|
(is (= 1 (mc/count db collection {:name "Alan" :age 40})))))
|
||||||
|
|
||||||
|
|
||||||
(deftest ^{:updating true} set-an-attribute-on-existing-document-using-update
|
(deftest ^{:updating true} set-an-attribute-on-existing-document-using-update
|
||||||
(let [collection "people"
|
(let [collection "people"
|
||||||
doc-id (monger.util/object-id)
|
doc-id (mu/object-id)
|
||||||
document { :_id doc-id, :name "Joe", :age 30 }]
|
document { :_id doc-id, :name "Joe", :age 30 }]
|
||||||
(is (mr/ok? (mc/insert "people" document)))
|
(is (mr/ok? (mc/insert db collection document)))
|
||||||
(is (= 1 (mc/count collection)))
|
(is (= 1 (mc/count db collection)))
|
||||||
(is (= 0 (mc/count collection { :has_kids true })))
|
(is (= 0 (mc/count db collection { :has_kids true })))
|
||||||
(mc/update collection { :_id doc-id } { $set { :has_kids true } })
|
(mc/update db collection { :_id doc-id } { $set { :has_kids true } })
|
||||||
(is (= 1 (mc/count collection { :has_kids true })))))
|
(is (= 1 (mc/count db collection { :has_kids true })))))
|
||||||
|
|
||||||
|
|
||||||
(deftest ^{:updating true} increment-multiple-fields-using-exists-operator-and-update
|
(deftest ^{:updating true} increment-multiple-fields-using-exists-operator-and-update
|
||||||
(let [collection "matches"
|
(let [collection "matches"
|
||||||
doc-id (monger.util/object-id)
|
doc-id (mu/object-id)
|
||||||
document { :_id doc-id :abc 0 :def 10 }]
|
document { :_id doc-id :abc 0 :def 10 }]
|
||||||
(mc/remove collection)
|
(mc/remove db collection)
|
||||||
(is (mr/ok? (mc/insert collection document)))
|
(is (mr/ok? (mc/insert db collection document)))
|
||||||
(is (= 1 (mc/count collection {:abc {$exists true} :def {$exists true}})))
|
(is (= 1 (mc/count db collection {:abc {$exists true} :def {$exists true}})))
|
||||||
(mc/update collection {:abc {$exists true} :def {$exists true}} {$inc {:abc 1 :def 0}})
|
(mc/update db collection {:abc {$exists true} :def {$exists true}} {$inc {:abc 1 :def 0}})
|
||||||
(is (= 1 (mc/count collection { :abc 1 })))))
|
(is (= 1 (mc/count db collection { :abc 1 })))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(deftest ^{:updating true} upsert-a-document-using-update
|
(deftest ^{:updating true} upsert-a-document-using-update
|
||||||
(let [collection "libraries"
|
(let [collection "libraries"
|
||||||
doc-id (monger.util/random-uuid)
|
doc-id (mu/random-uuid)
|
||||||
date (Date.)
|
date (Date.)
|
||||||
doc { :created-at date, :data-store "MongoDB", :language "Clojure", :_id doc-id }
|
doc { :created-at date, :data-store "MongoDB", :language "Clojure", :_id doc-id }
|
||||||
modified-doc { :created-at date, :data-store "MongoDB", :language "Erlang", :_id doc-id }]
|
modified-doc { :created-at date, :data-store "MongoDB", :language "Erlang", :_id doc-id }]
|
||||||
(is (not (mr/updated-existing? (mc/update collection { :language "Clojure" } doc :upsert true))))
|
(is (not (mr/updated-existing? (mc/update db collection { :language "Clojure" } doc {:upsert true}))))
|
||||||
(is (= 1 (mc/count collection)))
|
(is (= 1 (mc/count db collection)))
|
||||||
(is (mr/updated-existing? (mc/update collection { :language "Clojure" } modified-doc :multi false :upsert true)))
|
(is (mr/updated-existing? (mc/update db collection { :language "Clojure" } modified-doc {:multi false :upsert true})))
|
||||||
(is (= 1 (mc/count collection)))
|
(is (= 1 (mc/count db collection)))
|
||||||
(is (= (modified-doc (mc/find-by-id collection doc-id))))
|
(is (= (modified-doc (mc/find-by-id db collection doc-id))))
|
||||||
(mc/remove collection)))
|
(mc/remove db collection)))
|
||||||
|
|
||||||
(deftest ^{:updating true} upsert-a-document-using-upsert
|
(deftest ^{:updating true} upsert-a-document-using-upsert
|
||||||
(let [collection "libraries"
|
(let [collection "libraries"
|
||||||
doc-id (monger.util/random-uuid)
|
doc-id (mu/random-uuid)
|
||||||
date (Date.)
|
date (Date.)
|
||||||
doc {:created-at date :data-store "MongoDB" :language "Clojure" :_id doc-id}
|
doc {:created-at date :data-store "MongoDB" :language "Clojure" :_id doc-id}
|
||||||
modified-doc {:created-at date :data-store "MongoDB" :language "Erlang" :_id doc-id}]
|
modified-doc {:created-at date :data-store "MongoDB" :language "Erlang" :_id doc-id}]
|
||||||
(mc/remove collection)
|
(mc/remove db collection)
|
||||||
(is (not (mr/updated-existing? (mc/upsert collection {:language "Clojure"} doc))))
|
(is (not (mr/updated-existing? (mc/upsert db collection {:language "Clojure"} doc))))
|
||||||
(is (= 1 (mc/count collection)))
|
(is (= 1 (mc/count db collection)))
|
||||||
(is (mr/updated-existing? (mc/upsert collection {:language "Clojure"} modified-doc :multi false)))
|
(is (mr/updated-existing? (mc/upsert db collection {:language "Clojure"} modified-doc {:multi false})))
|
||||||
(is (= 1 (mc/count collection)))
|
(is (= 1 (mc/count db collection)))
|
||||||
(is (= (modified-doc (mc/find-by-id collection doc-id))))
|
(is (= (modified-doc (mc/find-by-id db collection doc-id))))
|
||||||
(mc/remove collection)))
|
(mc/remove db collection))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue