Inserting tests now pass
This commit is contained in:
parent
c29587f66d
commit
b3c501a73a
1 changed files with 153 additions and 144 deletions
|
|
@ -5,15 +5,28 @@
|
|||
(:require [monger.core :as mg]
|
||||
[monger.util :as mu]
|
||||
[monger.collection :as mc]
|
||||
[monger.test.helper :as helper]
|
||||
[clojure.test :refer :all]
|
||||
[monger.operators :refer :all]
|
||||
[monger.conversion :refer :all]
|
||||
[monger.test.fixtures :refer :all]))
|
||||
[monger.conversion :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
|
||||
(let [collection "people"
|
||||
doc {:name "Joe" :age 30}]
|
||||
(is (monger.result/ok? (mc/insert "people" doc)))
|
||||
(is (= 1 (mc/count collection)))))
|
||||
(is (monger.result/ok? (mc/insert db collection doc)))
|
||||
(is (= 1 (mc/count db collection)))))
|
||||
|
||||
(deftest insert-a-basic-document-with-explicitly-passed-database-without-id-and-with-default-write-concern
|
||||
(let [collection "people"
|
||||
doc {:name "Joe" :age 30}]
|
||||
(dotimes [n 5]
|
||||
(is (monger.result/ok? (mc/insert monger.core/*mongodb-database* "people" doc WriteConcern/SAFE))))
|
||||
(is (= 5 (mc/count collection)))))
|
||||
(is (monger.result/ok? (mc/insert db collection doc WriteConcern/SAFE))))
|
||||
(is (= 5 (mc/count db collection)))))
|
||||
|
||||
(deftest insert-a-basic-document-without-id-and-with-explicit-write-concern
|
||||
(let [collection "people"
|
||||
doc {:name "Joe" :age 30}]
|
||||
(is (monger.result/ok? (mc/insert "people" doc WriteConcern/SAFE)))
|
||||
(is (= 1 (mc/count collection)))))
|
||||
(is (monger.result/ok? (mc/insert db collection doc WriteConcern/SAFE)))
|
||||
(is (= 1 (mc/count db collection)))))
|
||||
|
||||
(deftest insert-a-basic-db-object-without-id-and-with-default-write-concern
|
||||
(let [collection "people"
|
||||
doc (to-db-object {:name "Joe" :age 30})]
|
||||
(is (nil? (.get ^DBObject doc "_id")))
|
||||
(mc/insert "people" doc)
|
||||
(mc/insert db collection doc)
|
||||
(is (not (nil? (monger.util/get-id doc))))))
|
||||
|
||||
(deftest insert-a-map-with-id-and-with-default-write-concern
|
||||
(let [collection "people"
|
||||
id (ObjectId.)
|
||||
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)))))
|
||||
|
||||
(deftest insert-a-document-with-clojure-ratio-in-it
|
||||
(let [collection "widgets"
|
||||
id (ObjectId.)
|
||||
doc {:ratio 11/2 "_id" id}
|
||||
result (mc/insert "widgets" doc)]
|
||||
(is (= 5.5 (:ratio (mc/find-map-by-id collection id))))))
|
||||
result (mc/insert db collection doc)]
|
||||
(is (= 5.5 (:ratio (mc/find-map-by-id db collection id))))))
|
||||
|
||||
(deftest insert-a-document-with-clojure-keyword-in-it
|
||||
(let [collection "widgets"
|
||||
id (ObjectId.)
|
||||
doc {:keyword :kwd "_id" id}
|
||||
result (mc/insert "widgets" doc)]
|
||||
(is (= (name :kwd) (:keyword (mc/find-map-by-id collection id))))))
|
||||
result (mc/insert db collection doc)]
|
||||
(is (= (name :kwd) (:keyword (mc/find-map-by-id db collection id))))))
|
||||
|
||||
(deftest insert-a-document-with-clojure-keyword-in-a-set-in-it
|
||||
(let [collection "widgets"
|
||||
id (ObjectId.)
|
||||
doc {:keyword1 {:keyword2 #{:kw1 :kw2}} "_id" id}
|
||||
result (mc/insert "widgets" doc)]
|
||||
result (mc/insert db collection doc)]
|
||||
(is (= (sort ["kw1" "kw2"])
|
||||
(sort (get-in (mc/find-map-by-id collection id) [:keyword1 :keyword2]))))))
|
||||
|
||||
|
||||
(defrecord Metrics
|
||||
[rps eps])
|
||||
(sort (get-in (mc/find-map-by-id db collection id) [:keyword1 :keyword2]))))))
|
||||
|
||||
(deftest insert-a-document-with-clojure-record-in-it
|
||||
(let [collection "widgets"
|
||||
id (ObjectId.)
|
||||
doc {:record (Metrics. 10 20) "_id" id}
|
||||
result (mc/insert "widgets" doc)]
|
||||
(is (= {:rps 10 :eps 20} (:record (mc/find-map-by-id collection id))))))
|
||||
result (mc/insert db collection doc)]
|
||||
(is (= {:rps 10 :eps 20} (:record (mc/find-map-by-id db collection id))))))
|
||||
|
||||
(deftest test-insert-a-document-with-dbref
|
||||
(mc/remove "widgets")
|
||||
(mc/remove "owners")
|
||||
(mc/remove db "widgets")
|
||||
(mc/remove db "owners")
|
||||
(let [coll1 "widgets"
|
||||
coll2 "owners"
|
||||
oid (ObjectId.)
|
||||
joe (mc/insert "owners" {:name "Joe" :_id oid})
|
||||
dbref (DBRef. (mg/current-db) coll2 oid)]
|
||||
(mc/insert coll1 {:type "pentagon" :owner dbref})
|
||||
(let [fetched (mc/find-one-as-map coll1 {:type "pentagon"})
|
||||
joe (mc/insert db coll2 {:name "Joe" :_id oid})
|
||||
dbref (DBRef. db coll2 oid)]
|
||||
(mc/insert db coll1 {:type "pentagon" :owner dbref})
|
||||
(let [fetched (mc/find-one-as-map db coll1 {:type "pentagon"})
|
||||
fo (:owner fetched)]
|
||||
(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
|
||||
(let [collection "people"
|
||||
doc {:name "Joe" :age 30}
|
||||
result (mc/insert-and-return :people doc)]
|
||||
result (mc/insert-and-return db collection doc)]
|
||||
(is (= (:name doc)
|
||||
(:name result)))
|
||||
(is (= (:age doc)
|
||||
(:age 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
|
||||
(let [collection "people"
|
||||
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)
|
||||
(:name result)))
|
||||
(is (= (:age doc)
|
||||
|
|
@ -126,15 +135,15 @@
|
|||
(is (= (:ratio doc)
|
||||
(:ratio result)))
|
||||
(is (:_id result))
|
||||
(is (= 1 (mc/count collection)))))
|
||||
(is (= 1 (mc/count db collection)))))
|
||||
|
||||
(deftest insert-and-return-with-a-provided-id
|
||||
(let [collection "people"
|
||||
oid (ObjectId.)
|
||||
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 (= 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
|
||||
(let [collection "people"
|
||||
docs [{:name "Joe" :age 30} {:name "Paul" :age 27}]]
|
||||
(is (monger.result/ok? (mc/insert-batch "people" docs)))
|
||||
(is (= 2 (mc/count collection)))))
|
||||
(is (monger.result/ok? (mc/insert-batch db collection docs)))
|
||||
(is (= 2 (mc/count db collection)))))
|
||||
|
||||
(deftest insert-a-batch-of-basic-documents-without-ids-and-with-explicit-write-concern
|
||||
(let [collection "people"
|
||||
docs [{:name "Joe" :age 30} {:name "Paul" :age 27}]]
|
||||
(is (monger.result/ok? (mc/insert-batch "people" docs WriteConcern/NORMAL)))
|
||||
(is (= 2 (mc/count collection)))))
|
||||
(is (monger.result/ok? (mc/insert-batch db collection docs WriteConcern/NORMAL)))
|
||||
(is (= 2 (mc/count db collection)))))
|
||||
|
||||
(deftest insert-a-batch-of-basic-documents-with-explicit-database-without-ids-and-with-explicit-write-concern
|
||||
(let [collection "people"
|
||||
docs [{:name "Joe" :age 30} {:name "Paul" :age 27}]]
|
||||
(dotimes [n 44]
|
||||
(is (monger.result/ok? (mc/insert-batch monger.core/*mongodb-database* "people" docs WriteConcern/NORMAL))))
|
||||
(is (= 88 (mc/count collection)))))
|
||||
(is (monger.result/ok? (mc/insert-batch db collection docs WriteConcern/NORMAL))))
|
||||
(is (= 88 (mc/count db collection)))))
|
||||
|
||||
(deftest insert-a-batch-of-basic-documents-from-a-lazy-sequence
|
||||
(let [collection "people"
|
||||
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})
|
||||
numbers))))
|
||||
(is (= (count numbers) (mc/count collection)))))
|
||||
(is (= (count numbers) (mc/count db collection))))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue