Inserting tests now pass

This commit is contained in:
Michael Klishin 2014-05-11 13:26:27 -04:00
parent c29587f66d
commit b3c501a73a

View file

@ -5,15 +5,28 @@
(:require [monger.core :as mg] (:require [monger.core :as mg]
[monger.util :as mu] [monger.util :as mu]
[monger.collection :as mc] [monger.collection :as mc]
[monger.test.helper :as helper]
[clojure.test :refer :all] [clojure.test :refer :all]
[monger.operators :refer :all] [monger.operators :refer :all]
[monger.conversion :refer :all] [monger.conversion :refer :all]))
[monger.test.fixtures :refer :all]))
(helper/connect!) (defrecord Metrics
[rps eps])
(use-fixtures :each purge-people purge-docs purge-things purge-libraries) (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 "scores")
(f)
(mc/remove db "people")
(mc/remove db "docs")
(mc/remove db "things")
(mc/remove db "scores"))
(use-fixtures :each purge-collections)
;; ;;
@ -23,79 +36,75 @@
(deftest insert-a-basic-document-without-id-and-with-default-write-concern (deftest insert-a-basic-document-without-id-and-with-default-write-concern
(let [collection "people" (let [collection "people"
doc {:name "Joe" :age 30}] doc {:name "Joe" :age 30}]
(is (monger.result/ok? (mc/insert "people" doc))) (is (monger.result/ok? (mc/insert db collection doc)))
(is (= 1 (mc/count collection))))) (is (= 1 (mc/count db collection)))))
(deftest insert-a-basic-document-with-explicitly-passed-database-without-id-and-with-default-write-concern (deftest insert-a-basic-document-with-explicitly-passed-database-without-id-and-with-default-write-concern
(let [collection "people" (let [collection "people"
doc {:name "Joe" :age 30}] doc {:name "Joe" :age 30}]
(dotimes [n 5] (dotimes [n 5]
(is (monger.result/ok? (mc/insert monger.core/*mongodb-database* "people" doc WriteConcern/SAFE)))) (is (monger.result/ok? (mc/insert db collection doc WriteConcern/SAFE))))
(is (= 5 (mc/count collection))))) (is (= 5 (mc/count db collection)))))
(deftest insert-a-basic-document-without-id-and-with-explicit-write-concern (deftest insert-a-basic-document-without-id-and-with-explicit-write-concern
(let [collection "people" (let [collection "people"
doc {:name "Joe" :age 30}] doc {:name "Joe" :age 30}]
(is (monger.result/ok? (mc/insert "people" doc WriteConcern/SAFE))) (is (monger.result/ok? (mc/insert db collection doc WriteConcern/SAFE)))
(is (= 1 (mc/count collection))))) (is (= 1 (mc/count db collection)))))
(deftest insert-a-basic-db-object-without-id-and-with-default-write-concern (deftest insert-a-basic-db-object-without-id-and-with-default-write-concern
(let [collection "people" (let [collection "people"
doc (to-db-object {:name "Joe" :age 30})] doc (to-db-object {:name "Joe" :age 30})]
(is (nil? (.get ^DBObject doc "_id"))) (is (nil? (.get ^DBObject doc "_id")))
(mc/insert "people" doc) (mc/insert db collection doc)
(is (not (nil? (monger.util/get-id doc)))))) (is (not (nil? (monger.util/get-id doc))))))
(deftest insert-a-map-with-id-and-with-default-write-concern (deftest insert-a-map-with-id-and-with-default-write-concern
(let [collection "people" (let [collection "people"
id (ObjectId.) id (ObjectId.)
doc {:name "Joe" :age 30 "_id" id} doc {:name "Joe" :age 30 "_id" id}
result (mc/insert "people" doc)] result (mc/insert db collection doc)]
(is (= id (monger.util/get-id doc))))) (is (= id (monger.util/get-id doc)))))
(deftest insert-a-document-with-clojure-ratio-in-it (deftest insert-a-document-with-clojure-ratio-in-it
(let [collection "widgets" (let [collection "widgets"
id (ObjectId.) id (ObjectId.)
doc {:ratio 11/2 "_id" id} doc {:ratio 11/2 "_id" id}
result (mc/insert "widgets" doc)] result (mc/insert db collection doc)]
(is (= 5.5 (:ratio (mc/find-map-by-id collection id)))))) (is (= 5.5 (:ratio (mc/find-map-by-id db collection id))))))
(deftest insert-a-document-with-clojure-keyword-in-it (deftest insert-a-document-with-clojure-keyword-in-it
(let [collection "widgets" (let [collection "widgets"
id (ObjectId.) id (ObjectId.)
doc {:keyword :kwd "_id" id} doc {:keyword :kwd "_id" id}
result (mc/insert "widgets" doc)] result (mc/insert db collection doc)]
(is (= (name :kwd) (:keyword (mc/find-map-by-id collection id)))))) (is (= (name :kwd) (:keyword (mc/find-map-by-id db collection id))))))
(deftest insert-a-document-with-clojure-keyword-in-a-set-in-it (deftest insert-a-document-with-clojure-keyword-in-a-set-in-it
(let [collection "widgets" (let [collection "widgets"
id (ObjectId.) id (ObjectId.)
doc {:keyword1 {:keyword2 #{:kw1 :kw2}} "_id" id} doc {:keyword1 {:keyword2 #{:kw1 :kw2}} "_id" id}
result (mc/insert "widgets" doc)] result (mc/insert db collection doc)]
(is (= (sort ["kw1" "kw2"]) (is (= (sort ["kw1" "kw2"])
(sort (get-in (mc/find-map-by-id collection id) [:keyword1 :keyword2])))))) (sort (get-in (mc/find-map-by-id db collection id) [:keyword1 :keyword2]))))))
(defrecord Metrics
[rps eps])
(deftest insert-a-document-with-clojure-record-in-it (deftest insert-a-document-with-clojure-record-in-it
(let [collection "widgets" (let [collection "widgets"
id (ObjectId.) id (ObjectId.)
doc {:record (Metrics. 10 20) "_id" id} doc {:record (Metrics. 10 20) "_id" id}
result (mc/insert "widgets" doc)] result (mc/insert db collection doc)]
(is (= {:rps 10 :eps 20} (:record (mc/find-map-by-id collection id)))))) (is (= {:rps 10 :eps 20} (:record (mc/find-map-by-id db collection id))))))
(deftest test-insert-a-document-with-dbref (deftest test-insert-a-document-with-dbref
(mc/remove "widgets") (mc/remove db "widgets")
(mc/remove "owners") (mc/remove db "owners")
(let [coll1 "widgets" (let [coll1 "widgets"
coll2 "owners" coll2 "owners"
oid (ObjectId.) oid (ObjectId.)
joe (mc/insert "owners" {:name "Joe" :_id oid}) joe (mc/insert db coll2 {:name "Joe" :_id oid})
dbref (DBRef. (mg/current-db) coll2 oid)] dbref (DBRef. db coll2 oid)]
(mc/insert coll1 {:type "pentagon" :owner dbref}) (mc/insert db coll1 {:type "pentagon" :owner dbref})
(let [fetched (mc/find-one-as-map coll1 {:type "pentagon"}) (let [fetched (mc/find-one-as-map db coll1 {:type "pentagon"})
fo (:owner fetched)] fo (:owner fetched)]
(is (= {:_id oid :name "Joe"} (from-db-object @fo true)))))) (is (= {:_id oid :name "Joe"} (from-db-object @fo true))))))
@ -107,18 +116,18 @@
(deftest insert-and-return-a-basic-document-without-id-and-with-default-write-concern (deftest insert-and-return-a-basic-document-without-id-and-with-default-write-concern
(let [collection "people" (let [collection "people"
doc {:name "Joe" :age 30} doc {:name "Joe" :age 30}
result (mc/insert-and-return :people doc)] result (mc/insert-and-return db collection doc)]
(is (= (:name doc) (is (= (:name doc)
(:name result))) (:name result)))
(is (= (:age doc) (is (= (:age doc)
(:age result))) (:age result)))
(is (:_id result)) (is (:_id result))
(is (= 1 (mc/count collection))))) (is (= 1 (mc/count db collection)))))
(deftest insert-and-return-a-basic-document-without-id-but-with-a-write-concern (deftest insert-and-return-a-basic-document-without-id-but-with-a-write-concern
(let [collection "people" (let [collection "people"
doc {:name "Joe" :age 30 :ratio 3/4} doc {:name "Joe" :age 30 :ratio 3/4}
result (mc/insert-and-return "people" doc WriteConcern/FSYNC_SAFE)] result (mc/insert-and-return db collection doc WriteConcern/FSYNC_SAFE)]
(is (= (:name doc) (is (= (:name doc)
(:name result))) (:name result)))
(is (= (:age doc) (is (= (:age doc)
@ -126,15 +135,15 @@
(is (= (:ratio doc) (is (= (:ratio doc)
(:ratio result))) (:ratio result)))
(is (:_id result)) (is (:_id result))
(is (= 1 (mc/count collection))))) (is (= 1 (mc/count db collection)))))
(deftest insert-and-return-with-a-provided-id (deftest insert-and-return-with-a-provided-id
(let [collection "people" (let [collection "people"
oid (ObjectId.) oid (ObjectId.)
doc {:name "Joe" :age 30 :_id oid} doc {:name "Joe" :age 30 :_id oid}
result (mc/insert-and-return :people doc)] result (mc/insert-and-return db collection doc)]
(is (= (:_id result) (:_id doc) oid)) (is (= (:_id result) (:_id doc) oid))
(is (= 1 (mc/count collection))))) (is (= 1 (mc/count db collection)))))
;; ;;
@ -144,26 +153,26 @@
(deftest insert-a-batch-of-basic-documents-without-ids-and-with-default-write-concern (deftest insert-a-batch-of-basic-documents-without-ids-and-with-default-write-concern
(let [collection "people" (let [collection "people"
docs [{:name "Joe" :age 30} {:name "Paul" :age 27}]] docs [{:name "Joe" :age 30} {:name "Paul" :age 27}]]
(is (monger.result/ok? (mc/insert-batch "people" docs))) (is (monger.result/ok? (mc/insert-batch db collection docs)))
(is (= 2 (mc/count collection))))) (is (= 2 (mc/count db collection)))))
(deftest insert-a-batch-of-basic-documents-without-ids-and-with-explicit-write-concern (deftest insert-a-batch-of-basic-documents-without-ids-and-with-explicit-write-concern
(let [collection "people" (let [collection "people"
docs [{:name "Joe" :age 30} {:name "Paul" :age 27}]] docs [{:name "Joe" :age 30} {:name "Paul" :age 27}]]
(is (monger.result/ok? (mc/insert-batch "people" docs WriteConcern/NORMAL))) (is (monger.result/ok? (mc/insert-batch db collection docs WriteConcern/NORMAL)))
(is (= 2 (mc/count collection))))) (is (= 2 (mc/count db collection)))))
(deftest insert-a-batch-of-basic-documents-with-explicit-database-without-ids-and-with-explicit-write-concern (deftest insert-a-batch-of-basic-documents-with-explicit-database-without-ids-and-with-explicit-write-concern
(let [collection "people" (let [collection "people"
docs [{:name "Joe" :age 30} {:name "Paul" :age 27}]] docs [{:name "Joe" :age 30} {:name "Paul" :age 27}]]
(dotimes [n 44] (dotimes [n 44]
(is (monger.result/ok? (mc/insert-batch monger.core/*mongodb-database* "people" docs WriteConcern/NORMAL)))) (is (monger.result/ok? (mc/insert-batch db collection docs WriteConcern/NORMAL))))
(is (= 88 (mc/count collection))))) (is (= 88 (mc/count db collection)))))
(deftest insert-a-batch-of-basic-documents-from-a-lazy-sequence (deftest insert-a-batch-of-basic-documents-from-a-lazy-sequence
(let [collection "people" (let [collection "people"
numbers (range 0 1000)] numbers (range 0 1000)]
(is (monger.result/ok? (mc/insert-batch "people" (map (fn [^long l] (is (monger.result/ok? (mc/insert-batch db collection (map (fn [^long l]
{:n l}) {:n l})
numbers)))) numbers))))
(is (= (count numbers) (mc/count collection))))) (is (= (count numbers) (mc/count db collection))))))