diff --git a/test/monger/test/updating_test.clj b/test/monger/test/updating_test.clj index f039dd5..00ec2e8 100644 --- a/test/monger/test/updating_test.clj +++ b/test/monger/test/updating_test.clj @@ -5,13 +5,13 @@ org.bson.types.ObjectId java.util.Date) (:require [monger core util] - [monger.collection :as mgcol] - [monger.result :as mgres] - [monger.conversion :as mgcnv] + [monger.collection :as mc] + [monger.result :as mr] [monger.test.helper :as helper]) (:use clojure.test monger.operators - monger.test.fixtures)) + monger.test.fixtures + [monger.conversion :only [to-db-object]])) (helper/connect!) @@ -28,10 +28,10 @@ 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 }] - (mgcol/insert collection doc) - (is (= (doc (mgcol/find-by-id collection doc-id)))) - (mgcol/update collection { :_id doc-id } { :language "Erlang" }) - (is (= (modified-doc (mgcol/find-by-id collection doc-id)))))) + (mc/insert collection doc) + (is (= (doc (mc/find-by-id collection doc-id)))) + (mc/update collection { :_id doc-id } { :language "Erlang" }) + (is (= (modified-doc (mc/find-by-id collection doc-id)))))) (deftest update-document-by-id-without-upsert-using-update-by-id (let [collection "libraries" @@ -39,10 +39,10 @@ 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 }] - (mgcol/insert collection doc) - (is (= (doc (mgcol/find-by-id collection doc-id)))) - (mgcol/update-by-id collection doc-id { :language "Erlang" }) - (is (= (modified-doc (mgcol/find-by-id collection 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 update-nested-document-fields-without-upsert-using-update-by-id (let [collection "libraries" @@ -50,39 +50,39 @@ 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 }] - (mgcol/insert collection doc) - (is (= (doc (mgcol/find-by-id collection doc-id)))) - (mgcol/update-by-id collection doc-id { $set { "language.primary" "Erlang" }}) - (is (= (modified-doc (mgcol/find-by-id collection 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 update-multiple-documents (let [collection "libraries"] - (mgcol/insert collection { :language "Clojure", :name "monger" }) - (mgcol/insert collection { :language "Clojure", :name "langohr" }) - (mgcol/insert collection { :language "Clojure", :name "incanter" }) - (mgcol/insert collection { :language "Scala", :name "akka" }) - (is (= 3 (mgcol/count collection { :language "Clojure" }))) - (is (= 1 (mgcol/count collection { :language "Scala" }))) - (is (= 0 (mgcol/count collection { :language "Python" }))) - (mgcol/update collection { :language "Clojure" } { $set { :language "Python" } } :multi true) - (is (= 0 (mgcol/count collection { :language "Clojure" }))) - (is (= 1 (mgcol/count collection { :language "Scala" }))) - (is (= 3 (mgcol/count collection { :language "Python" }))))) + (mc/insert collection { :language "Clojure", :name "monger" }) + (mc/insert collection { :language "Clojure", :name "langohr" }) + (mc/insert collection { :language "Clojure", :name "incanter" }) + (mc/insert collection { :language "Scala", :name "akka" }) + (is (= 3 (mc/count collection { :language "Clojure" }))) + (is (= 1 (mc/count collection { :language "Scala" }))) + (is (= 0 (mc/count collection { :language "Python" }))) + (mc/update collection { :language "Clojure" } { $set { :language "Python" } } :multi true) + (is (= 0 (mc/count collection { :language "Clojure" }))) + (is (= 1 (mc/count collection { :language "Scala" }))) + (is (= 3 (mc/count collection { :language "Python" }))))) (deftest save-a-new-document (let [collection "people" document { :name "Joe", :age 30 }] - (is (monger.result/ok? (mgcol/save "people" document))) - (is (= 1 (mgcol/count collection))))) + (is (monger.result/ok? (mc/save "people" document))) + (is (= 1 (mc/count collection))))) (deftest save-a-new-basic-db-object (let [collection "people" - doc (mgcnv/to-db-object { :name "Joe", :age 30 })] + doc (to-db-object { :name "Joe", :age 30 })] (is (nil? (monger.util/get-id doc))) - (mgcol/save monger.core/*mongodb-database* "people" doc WriteConcern/SAFE) + (mc/save monger.core/*mongodb-database* "people" doc WriteConcern/SAFE) (is (not (nil? (monger.util/get-id doc)))))) @@ -91,21 +91,32 @@ (let [collection "people" doc-id "people-1" document { :_id doc-id, :name "Joe", :age 30 }] - (is (monger.result/ok? (mgcol/insert "people" document))) - (is (= 1 (mgcol/count collection))) - (mgcol/save collection { :_id doc-id, :name "Alan", :age 40 }) - (is (= 1 (mgcol/count collection { :name "Alan", :age 40 }))))) + (is (monger.result/ok? (mc/insert "people" document))) + (is (= 1 (mc/count collection))) + (mc/save collection { :_id doc-id, :name "Alan", :age 40 }) + (is (= 1 (mc/count collection { :name "Alan", :age 40 }))))) (deftest set-an-attribute-on-existing-document-using-update (let [collection "people" doc-id (monger.util/object-id) document { :_id doc-id, :name "Joe", :age 30 }] - (is (monger.result/ok? (mgcol/insert "people" document))) - (is (= 1 (mgcol/count collection))) - (is (= 0 (mgcol/count collection { :has_kids true }))) - (mgcol/update collection { :_id doc-id } { $set { :has_kids true } }) - (is (= 1 (mgcol/count collection { :has_kids true }))))) + (is (monger.result/ok? (mc/insert "people" document))) + (is (= 1 (mc/count collection))) + (is (= 0 (mc/count collection { :has_kids true }))) + (mc/update collection { :_id doc-id } { $set { :has_kids true } }) + (is (= 1 (mc/count collection { :has_kids true }))))) + + +(deftest increment-multiple-fields-using-exists-operator-and-update + (let [collection "matches" + doc-id (monger.util/object-id) + document { :_id doc-id :abc 0 :def 10 }] + (mc/remove collection) + (is (monger.result/ok? (mc/insert collection document))) + (is (= 1 (mc/count collection {:abc {$exists true} :def {$exists true}}))) + (mc/update collection {:abc {$exists true} :def {$exists true}} {$inc {:abc 1 :def 0}}) + (is (= 1 (mc/count collection { :abc 1 }))))) @@ -115,9 +126,9 @@ 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 }] - (is (not (monger.result/updated-existing? (mgcol/update collection { :language "Clojure" } doc :upsert true)))) - (is (= 1 (mgcol/count collection))) - (is (monger.result/updated-existing? (mgcol/update collection { :language "Clojure" } modified-doc :multi false :upsert true))) - (is (= 1 (mgcol/count collection))) - (is (= (modified-doc (mgcol/find-by-id collection doc-id)))) - (mgcol/remove collection))) + (is (not (monger.result/updated-existing? (mc/update collection { :language "Clojure" } doc :upsert true)))) + (is (= 1 (mc/count collection))) + (is (monger.result/updated-existing? (mc/update collection { :language "Clojure" } modified-doc :multi false :upsert true))) + (is (= 1 (mc/count collection))) + (is (= (modified-doc (mc/find-by-id collection doc-id)))) + (mc/remove collection)))