Add a new test, more spring cleanup before 1.0.0

This commit is contained in:
Michael S. Klishin 2012-06-23 19:11:54 +04:00
parent 326af3ef5a
commit f9fa17f572

View file

@ -5,13 +5,13 @@
org.bson.types.ObjectId org.bson.types.ObjectId
java.util.Date) java.util.Date)
(:require [monger core util] (:require [monger core util]
[monger.collection :as mgcol] [monger.collection :as mc]
[monger.result :as mgres] [monger.result :as mr]
[monger.conversion :as mgcnv]
[monger.test.helper :as helper]) [monger.test.helper :as helper])
(:use clojure.test (:use clojure.test
monger.operators monger.operators
monger.test.fixtures)) monger.test.fixtures
[monger.conversion :only [to-db-object]]))
(helper/connect!) (helper/connect!)
@ -28,10 +28,10 @@
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 }]
(mgcol/insert collection doc) (mc/insert collection doc)
(is (= (doc (mgcol/find-by-id collection doc-id)))) (is (= (doc (mc/find-by-id collection doc-id))))
(mgcol/update collection { :_id doc-id } { :language "Erlang" }) (mc/update collection { :_id doc-id } { :language "Erlang" })
(is (= (modified-doc (mgcol/find-by-id collection doc-id)))))) (is (= (modified-doc (mc/find-by-id collection doc-id))))))
(deftest update-document-by-id-without-upsert-using-update-by-id (deftest update-document-by-id-without-upsert-using-update-by-id
(let [collection "libraries" (let [collection "libraries"
@ -39,10 +39,10 @@
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 }]
(mgcol/insert collection doc) (mc/insert collection doc)
(is (= (doc (mgcol/find-by-id collection doc-id)))) (is (= (doc (mc/find-by-id collection doc-id))))
(mgcol/update-by-id collection doc-id { :language "Erlang" }) (mc/update-by-id collection doc-id { :language "Erlang" })
(is (= (modified-doc (mgcol/find-by-id collection doc-id)))))) (is (= (modified-doc (mc/find-by-id collection doc-id))))))
(deftest update-nested-document-fields-without-upsert-using-update-by-id (deftest update-nested-document-fields-without-upsert-using-update-by-id
(let [collection "libraries" (let [collection "libraries"
@ -50,39 +50,39 @@
date (Date.) date (Date.)
doc { :created-at date :data-store "MongoDB" :language { :primary "Clojure" } :_id doc-id } 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 }] modified-doc { :created-at date :data-store "MongoDB" :language { :primary "Erlang" } :_id doc-id }]
(mgcol/insert collection doc) (mc/insert collection doc)
(is (= (doc (mgcol/find-by-id collection doc-id)))) (is (= (doc (mc/find-by-id collection doc-id))))
(mgcol/update-by-id collection doc-id { $set { "language.primary" "Erlang" }}) (mc/update-by-id collection doc-id { $set { "language.primary" "Erlang" }})
(is (= (modified-doc (mgcol/find-by-id collection doc-id)))))) (is (= (modified-doc (mc/find-by-id collection doc-id))))))
(deftest update-multiple-documents (deftest update-multiple-documents
(let [collection "libraries"] (let [collection "libraries"]
(mgcol/insert collection { :language "Clojure", :name "monger" }) (mc/insert collection { :language "Clojure", :name "monger" })
(mgcol/insert collection { :language "Clojure", :name "langohr" }) (mc/insert collection { :language "Clojure", :name "langohr" })
(mgcol/insert collection { :language "Clojure", :name "incanter" }) (mc/insert collection { :language "Clojure", :name "incanter" })
(mgcol/insert collection { :language "Scala", :name "akka" }) (mc/insert collection { :language "Scala", :name "akka" })
(is (= 3 (mgcol/count collection { :language "Clojure" }))) (is (= 3 (mc/count collection { :language "Clojure" })))
(is (= 1 (mgcol/count collection { :language "Scala" }))) (is (= 1 (mc/count collection { :language "Scala" })))
(is (= 0 (mgcol/count collection { :language "Python" }))) (is (= 0 (mc/count collection { :language "Python" })))
(mgcol/update collection { :language "Clojure" } { $set { :language "Python" } } :multi true) (mc/update collection { :language "Clojure" } { $set { :language "Python" } } :multi true)
(is (= 0 (mgcol/count collection { :language "Clojure" }))) (is (= 0 (mc/count collection { :language "Clojure" })))
(is (= 1 (mgcol/count collection { :language "Scala" }))) (is (= 1 (mc/count collection { :language "Scala" })))
(is (= 3 (mgcol/count collection { :language "Python" }))))) (is (= 3 (mc/count collection { :language "Python" })))))
(deftest save-a-new-document (deftest save-a-new-document
(let [collection "people" (let [collection "people"
document { :name "Joe", :age 30 }] document { :name "Joe", :age 30 }]
(is (monger.result/ok? (mgcol/save "people" document))) (is (monger.result/ok? (mc/save "people" document)))
(is (= 1 (mgcol/count collection))))) (is (= 1 (mc/count collection)))))
(deftest save-a-new-basic-db-object (deftest save-a-new-basic-db-object
(let [collection "people" (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))) (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)))))) (is (not (nil? (monger.util/get-id doc))))))
@ -91,21 +91,32 @@
(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 (monger.result/ok? (mgcol/insert "people" document))) (is (monger.result/ok? (mc/insert "people" document)))
(is (= 1 (mgcol/count collection))) (is (= 1 (mc/count collection)))
(mgcol/save collection { :_id doc-id, :name "Alan", :age 40 }) (mc/save collection { :_id doc-id, :name "Alan", :age 40 })
(is (= 1 (mgcol/count collection { :name "Alan", :age 40 }))))) (is (= 1 (mc/count collection { :name "Alan", :age 40 })))))
(deftest set-an-attribute-on-existing-document-using-update (deftest set-an-attribute-on-existing-document-using-update
(let [collection "people" (let [collection "people"
doc-id (monger.util/object-id) doc-id (monger.util/object-id)
document { :_id doc-id, :name "Joe", :age 30 }] document { :_id doc-id, :name "Joe", :age 30 }]
(is (monger.result/ok? (mgcol/insert "people" document))) (is (monger.result/ok? (mc/insert "people" document)))
(is (= 1 (mgcol/count collection))) (is (= 1 (mc/count collection)))
(is (= 0 (mgcol/count collection { :has_kids true }))) (is (= 0 (mc/count collection { :has_kids true })))
(mgcol/update collection { :_id doc-id } { $set { :has_kids true } }) (mc/update collection { :_id doc-id } { $set { :has_kids true } })
(is (= 1 (mgcol/count collection { :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.) 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 (monger.result/updated-existing? (mgcol/update collection { :language "Clojure" } doc :upsert true)))) (is (not (monger.result/updated-existing? (mc/update collection { :language "Clojure" } doc :upsert true))))
(is (= 1 (mgcol/count collection))) (is (= 1 (mc/count collection)))
(is (monger.result/updated-existing? (mgcol/update collection { :language "Clojure" } modified-doc :multi false :upsert true))) (is (monger.result/updated-existing? (mc/update collection { :language "Clojure" } modified-doc :multi false :upsert true)))
(is (= 1 (mgcol/count collection))) (is (= 1 (mc/count collection)))
(is (= (modified-doc (mgcol/find-by-id collection doc-id)))) (is (= (modified-doc (mc/find-by-id collection doc-id))))
(mgcol/remove collection))) (mc/remove collection)))