Greatly speed up monger.multi.* tests, esp. with MongoDB 2.6
This commit is contained in:
parent
6d3b2be339
commit
373c4db7a4
6 changed files with 173 additions and 152 deletions
|
|
@ -6,7 +6,7 @@
|
|||
[java.util Date])
|
||||
(:require [monger.core :as mg]
|
||||
[monger core util]
|
||||
[monger.multi.collection :as mgcol]
|
||||
[monger.multi.collection :as mc]
|
||||
[monger.result :as mgres]
|
||||
[monger.test.helper :as helper]
|
||||
[clojure.test :refer :all]
|
||||
|
|
@ -15,12 +15,16 @@
|
|||
|
||||
(helper/connect!)
|
||||
|
||||
(defn drop-altdb
|
||||
(def db (mg/get-db "altdb"))
|
||||
|
||||
(defn purge-altdb
|
||||
[f]
|
||||
(mg/drop-db "altdb")
|
||||
(mc/remove db "scores")
|
||||
(mc/remove db "things")
|
||||
(mc/remove db "docs")
|
||||
(f))
|
||||
|
||||
(use-fixtures :each drop-altdb)
|
||||
(use-fixtures :each purge-altdb)
|
||||
|
||||
;;
|
||||
;; $inc
|
||||
|
|
@ -30,35 +34,35 @@
|
|||
(let [db (mg/get-db "altdb")
|
||||
coll "scores"
|
||||
oid (ObjectId.)]
|
||||
(mgcol/insert db coll { :_id oid :username "l33r0y" :score 100 })
|
||||
(mgcol/update db coll { :_id oid } { $inc { :score 20 } })
|
||||
(is (= 120 (:score (mgcol/find-map-by-id db coll oid))))))
|
||||
(mc/insert db coll { :_id oid :username "l33r0y" :score 100 })
|
||||
(mc/update db coll { :_id oid } { $inc { :score 20 } })
|
||||
(is (= 120 (:score (mc/find-map-by-id db coll oid))))))
|
||||
|
||||
(deftest set-a-single-non-existing-field-using-$inc-modifier
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "scores"
|
||||
oid (ObjectId.)]
|
||||
(mgcol/insert db coll { :_id oid :username "l33r0y" })
|
||||
(mgcol/update db coll { :_id oid } { $inc { :score 30 } })
|
||||
(is (= 30 (:score (mgcol/find-map-by-id db coll oid))))))
|
||||
(mc/insert db coll { :_id oid :username "l33r0y" })
|
||||
(mc/update db coll { :_id oid } { $inc { :score 30 } })
|
||||
(is (= 30 (:score (mc/find-map-by-id db coll oid))))))
|
||||
|
||||
|
||||
(deftest increment-multiple-existing-fields-using-$inc-modifier
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "scores"
|
||||
oid (ObjectId.)]
|
||||
(mgcol/insert db coll { :_id oid :username "l33r0y" :score 100 :bonus 0 })
|
||||
(mgcol/update db coll { :_id oid } {$inc { :score 20 :bonus 10 } })
|
||||
(is (= { :_id oid :score 120 :bonus 10 :username "l33r0y" } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :username "l33r0y" :score 100 :bonus 0 })
|
||||
(mc/update db coll { :_id oid } {$inc { :score 20 :bonus 10 } })
|
||||
(is (= { :_id oid :score 120 :bonus 10 :username "l33r0y" } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
|
||||
(deftest increment-and-set-multiple-existing-fields-using-$inc-modifier
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "scores"
|
||||
oid (ObjectId.)]
|
||||
(mgcol/insert db coll { :_id oid :username "l33r0y" :score 100 })
|
||||
(mgcol/update db coll { :_id oid } { $inc { :score 20 :bonus 10 } })
|
||||
(is (= { :_id oid :score 120 :bonus 10 :username "l33r0y" } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :username "l33r0y" :score 100 })
|
||||
(mc/update db coll { :_id oid } { $inc { :score 20 :bonus 10 } })
|
||||
(is (= { :_id oid :score 120 :bonus 10 :username "l33r0y" } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
|
||||
|
||||
|
|
@ -70,34 +74,34 @@
|
|||
(let [db (mg/get-db "altdb")
|
||||
coll "things"
|
||||
oid (ObjectId.)]
|
||||
(mgcol/insert db coll { :_id oid :weight 10.0 })
|
||||
(mgcol/update db coll { :_id oid } { $set { :weight 20.5 } })
|
||||
(is (= 20.5 (:weight (mgcol/find-map-by-id db coll oid [:weight]))))))
|
||||
(mc/insert db coll { :_id oid :weight 10.0 })
|
||||
(mc/update db coll { :_id oid } { $set { :weight 20.5 } })
|
||||
(is (= 20.5 (:weight (mc/find-map-by-id db coll oid [:weight]))))))
|
||||
|
||||
(deftest set-a-single-non-existing-field-using-$set-modifier
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "things"
|
||||
oid (ObjectId.)]
|
||||
(mgcol/insert db coll { :_id oid :weight 10.0 })
|
||||
(mgcol/update db coll { :_id oid } { $set { :height 17.2 } })
|
||||
(is (= 17.2 (:height (mgcol/find-map-by-id db coll oid [:height]))))))
|
||||
(mc/insert db coll { :_id oid :weight 10.0 })
|
||||
(mc/update db coll { :_id oid } { $set { :height 17.2 } })
|
||||
(is (= 17.2 (:height (mc/find-map-by-id db coll oid [:height]))))))
|
||||
|
||||
(deftest update-multiple-existing-fields-using-$set-modifier
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "things"
|
||||
oid (ObjectId.)]
|
||||
(mgcol/insert db coll { :_id oid :weight 10.0 :height 15.2 })
|
||||
(mgcol/update db coll { :_id oid } { $set { :weight 20.5 :height 25.6 } })
|
||||
(is (= { :_id oid :weight 20.5 :height 25.6 } (mgcol/find-map-by-id db coll oid [:weight :height])))))
|
||||
(mc/insert db coll { :_id oid :weight 10.0 :height 15.2 })
|
||||
(mc/update db coll { :_id oid } { $set { :weight 20.5 :height 25.6 } })
|
||||
(is (= { :_id oid :weight 20.5 :height 25.6 } (mc/find-map-by-id db coll oid [:weight :height])))))
|
||||
|
||||
|
||||
(deftest update-and-set-multiple-fields-using-$set-modifier
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "things"
|
||||
oid (ObjectId.)]
|
||||
(mgcol/insert db coll { :_id oid :weight 10.0 })
|
||||
(mgcol/update db coll { :_id oid } {$set { :weight 20.5 :height 25.6 } })
|
||||
(is (= { :_id oid :weight 20.5 :height 25.6 } (mgcol/find-map-by-id db coll oid [:weight :height])))))
|
||||
(mc/insert db coll { :_id oid :weight 10.0 })
|
||||
(mc/update db coll { :_id oid } {$set { :weight 20.5 :height 25.6 } })
|
||||
(is (= { :_id oid :weight 20.5 :height 25.6 } (mc/find-map-by-id db coll oid [:weight :height])))))
|
||||
|
||||
|
||||
;;
|
||||
|
|
@ -108,27 +112,27 @@
|
|||
(let [db (mg/get-db "altdb")
|
||||
coll "docs"
|
||||
oid (ObjectId.)]
|
||||
(mgcol/insert db coll { :_id oid :title "Document 1" :published true })
|
||||
(mgcol/update db coll { :_id oid } { $unset { :published 1 } })
|
||||
(is (= { :_id oid :title "Document 1" } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title "Document 1" :published true })
|
||||
(mc/update db coll { :_id oid } { $unset { :published 1 } })
|
||||
(is (= { :_id oid :title "Document 1" } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
|
||||
(deftest unset-multiple-existing-fields-using-$unset-modifier
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "docs"
|
||||
oid (ObjectId.)]
|
||||
(mgcol/insert db coll { :_id oid :title "Document 1" :published true :featured true })
|
||||
(mgcol/update db coll { :_id oid } { $unset { :published 1 :featured true } })
|
||||
(is (= { :_id oid :title "Document 1" } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title "Document 1" :published true :featured true })
|
||||
(mc/update db coll { :_id oid } { $unset { :published 1 :featured true } })
|
||||
(is (= { :_id oid :title "Document 1" } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
|
||||
(deftest unsetting-an-unexisting-field-using-$unset-modifier-is-not-considered-an-issue
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "docs"
|
||||
oid (ObjectId.)]
|
||||
(mgcol/insert db coll { :_id oid :title "Document 1" :published true })
|
||||
(is (mgres/ok? (mgcol/update db coll { :_id oid } { $unset { :published 1 :featured true } })))
|
||||
(is (= { :_id oid :title "Document 1" } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title "Document 1" :published true })
|
||||
(is (mgres/ok? (mc/update db coll { :_id oid } { $unset { :published 1 :featured true } })))
|
||||
(is (= { :_id oid :title "Document 1" } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
;;
|
||||
;; $setOnInsert
|
||||
|
|
@ -139,8 +143,8 @@
|
|||
coll "docs"
|
||||
now 456
|
||||
oid (ObjectId.)]
|
||||
(mgcol/find-and-modify db coll {:_id oid} {$set {:lastseen now} $setOnInsert {:firstseen now}} :upsert true)
|
||||
(is (= { :_id oid :lastseen now :firstseen now} (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/find-and-modify db coll {:_id oid} {$set {:lastseen now} $setOnInsert {:firstseen now}} :upsert true)
|
||||
(is (= { :_id oid :lastseen now :firstseen now} (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
(deftest setOnInsert-in-upsert-for-existing-document
|
||||
(let [db (mg/get-db "altdb")
|
||||
|
|
@ -148,9 +152,9 @@
|
|||
before 123
|
||||
now 456
|
||||
oid (ObjectId.)]
|
||||
(mgcol/insert db coll { :_id oid :firstseen before :lastseen before})
|
||||
(mgcol/find-and-modify db coll {:_id oid} {$set {:lastseen now} $setOnInsert {:firstseen now}} :upsert true)
|
||||
(is (= { :_id oid :lastseen now :firstseen before} (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :firstseen before :lastseen before})
|
||||
(mc/find-and-modify db coll {:_id oid} {$set {:lastseen now} $setOnInsert {:firstseen now}} :upsert true)
|
||||
(is (= { :_id oid :lastseen now :firstseen before} (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
;;
|
||||
;; $push
|
||||
|
|
@ -161,18 +165,18 @@
|
|||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$push modifier appends value to field"]
|
||||
(mgcol/insert db coll { :_id oid :title title })
|
||||
(mgcol/update db coll { :_id oid } { $push { :tags "modifiers" } })
|
||||
(is (= { :_id oid :title title :tags ["modifiers"] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title })
|
||||
(mc/update db coll { :_id oid } { $push { :tags "modifiers" } })
|
||||
(is (= { :_id oid :title title :tags ["modifiers"] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
(deftest add-value-to-an-existing-array-using-$push-modifier
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$push modifier appends value to field"]
|
||||
(mgcol/insert db coll { :_id oid :title title :tags ["mongodb"] })
|
||||
(mgcol/update db coll { :_id oid } { $push { :tags "modifiers" } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" "modifiers"] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title :tags ["mongodb"] })
|
||||
(mc/update db coll { :_id oid } { $push { :tags "modifiers" } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" "modifiers"] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
|
||||
;; this is a common mistake, I leave it here to demonstrate it. You almost never
|
||||
|
|
@ -182,9 +186,9 @@
|
|||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$push modifier appends value to field"]
|
||||
(mgcol/insert db coll { :_id oid :title title :tags ["mongodb"] })
|
||||
(mgcol/update db coll { :_id oid } { $push { :tags ["modifiers" "operators"] } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" ["modifiers" "operators"]] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title :tags ["mongodb"] })
|
||||
(mc/update db coll { :_id oid } { $push { :tags ["modifiers" "operators"] } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" ["modifiers" "operators"]] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
|
||||
|
||||
|
|
@ -193,10 +197,10 @@
|
|||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$push modifier appends value to field"]
|
||||
(mgcol/insert db coll { :_id oid :title title :tags ["mongodb"] })
|
||||
(mgcol/update db coll { :_id oid } { $push { :tags "modifiers" } })
|
||||
(mgcol/update db coll { :_id oid } { $push { :tags "modifiers" } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" "modifiers" "modifiers"] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title :tags ["mongodb"] })
|
||||
(mc/update db coll { :_id oid } { $push { :tags "modifiers" } })
|
||||
(mc/update db coll { :_id oid } { $push { :tags "modifiers" } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" "modifiers" "modifiers"] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
;;
|
||||
;; $pushAll
|
||||
|
|
@ -207,18 +211,18 @@
|
|||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$pushAll modifier appends multiple values to field"]
|
||||
(mgcol/insert db coll { :_id oid :title title })
|
||||
(mgcol/update db coll { :_id oid } { $pushAll { :tags ["mongodb" "docs"] } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" "docs"] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title })
|
||||
(mc/update db coll { :_id oid } { $pushAll { :tags ["mongodb" "docs"] } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" "docs"] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
(deftest add-value-to-an-existing-array-using-$pushAll-modifier
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$pushAll modifier appends multiple values to field"]
|
||||
(mgcol/insert db coll { :_id oid :title title :tags ["mongodb"] })
|
||||
(mgcol/update db coll { :_id oid } { $pushAll { :tags ["modifiers" "docs"] } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" "modifiers" "docs"] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title :tags ["mongodb"] })
|
||||
(mc/update db coll { :_id oid } { $pushAll { :tags ["modifiers" "docs"] } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" "modifiers" "docs"] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
|
||||
(deftest double-add-value-to-an-existing-array-using-$pushAll-modifier
|
||||
|
|
@ -226,9 +230,9 @@
|
|||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$pushAll modifier appends multiple values to field"]
|
||||
(mgcol/insert db coll { :_id oid :title title :tags ["mongodb" "docs"] })
|
||||
(mgcol/update db coll { :_id oid } { $pushAll { :tags ["modifiers" "docs"] } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" "docs" "modifiers" "docs"] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title :tags ["mongodb" "docs"] })
|
||||
(mc/update db coll { :_id oid } { $pushAll { :tags ["modifiers" "docs"] } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" "docs" "modifiers" "docs"] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
|
||||
;;
|
||||
|
|
@ -240,18 +244,18 @@
|
|||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$addToSet modifier appends value to field unless it is already there"]
|
||||
(mgcol/insert db coll { :_id oid :title title })
|
||||
(mgcol/update db coll { :_id oid } { $addToSet { :tags "modifiers" } })
|
||||
(is (= { :_id oid :title title :tags ["modifiers"] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title })
|
||||
(mc/update db coll { :_id oid } { $addToSet { :tags "modifiers" } })
|
||||
(is (= { :_id oid :title title :tags ["modifiers"] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
(deftest add-value-to-an-existing-array-using-$addToSet-modifier
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$addToSet modifier appends value to field unless it is already there"]
|
||||
(mgcol/insert db coll { :_id oid :title title :tags ["mongodb"] })
|
||||
(mgcol/update db coll { :_id oid } { $addToSet { :tags "modifiers" } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" "modifiers"] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title :tags ["mongodb"] })
|
||||
(mc/update db coll { :_id oid } { $addToSet { :tags "modifiers" } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" "modifiers"] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
|
||||
(deftest double-add-value-to-an-existing-array-using-$addToSet-modifier
|
||||
|
|
@ -259,10 +263,10 @@
|
|||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$addToSet modifier appends value to field unless it is already there"]
|
||||
(mgcol/insert db coll { :_id oid :title title :tags ["mongodb"] })
|
||||
(mgcol/update db coll { :_id oid } { $addToSet { :tags "modifiers" } })
|
||||
(mgcol/update db coll { :_id oid } { $addToSet { :tags "modifiers" } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" "modifiers"] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title :tags ["mongodb"] })
|
||||
(mc/update db coll { :_id oid } { $addToSet { :tags "modifiers" } })
|
||||
(mc/update db coll { :_id oid } { $addToSet { :tags "modifiers" } })
|
||||
(is (= { :_id oid :title title :tags ["mongodb" "modifiers"] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
|
||||
;;
|
||||
|
|
@ -274,27 +278,27 @@
|
|||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$pop modifier removes last or first value in the array"]
|
||||
(mgcol/insert db coll { :_id oid :title title :tags ["products" "apple" "reviews"] })
|
||||
(mgcol/update db coll { :_id oid } { $pop { :tags 1 } })
|
||||
(is (= { :_id oid :title title :tags ["products" "apple"] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title :tags ["products" "apple" "reviews"] })
|
||||
(mc/update db coll { :_id oid } { $pop { :tags 1 } })
|
||||
(is (= { :_id oid :title title :tags ["products" "apple"] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
(deftest unshift-first-value-in-the-array-using-$pop-modifier
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$pop modifier removes last or first value in the array"]
|
||||
(mgcol/insert db coll { :_id oid :title title :tags ["products" "apple" "reviews"] })
|
||||
(mgcol/update db coll { :_id oid } { $pop { :tags -1 } })
|
||||
(is (= { :_id oid :title title :tags ["apple" "reviews"] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title :tags ["products" "apple" "reviews"] })
|
||||
(mc/update db coll { :_id oid } { $pop { :tags -1 } })
|
||||
(is (= { :_id oid :title title :tags ["apple" "reviews"] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
(deftest pop-last-values-from-multiple-arrays-using-$pop-modifier
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$pop modifier removes last or first value in the array"]
|
||||
(mgcol/insert db coll { :_id oid :title title :tags ["products" "apple" "reviews"] :categories ["apple" "reviews" "drafts"] })
|
||||
(mgcol/update db coll { :_id oid } { $pop { :tags 1 :categories 1 } })
|
||||
(is (= { :_id oid :title title :tags ["products" "apple"] :categories ["apple" "reviews"] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title :tags ["products" "apple" "reviews"] :categories ["apple" "reviews" "drafts"] })
|
||||
(mc/update db coll { :_id oid } { $pop { :tags 1 :categories 1 } })
|
||||
(is (= { :_id oid :title title :tags ["products" "apple"] :categories ["apple" "reviews"] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
|
||||
;;
|
||||
|
|
@ -306,18 +310,18 @@
|
|||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$pull modifier removes all value entries in the array"]
|
||||
(mgcol/insert db coll { :_id oid :title title :measurements [1.0 1.2 1.2 1.2 1.1 1.1 1.2 1.3 1.0] })
|
||||
(mgcol/update db coll { :_id oid } { $pull { :measurements 1.2 } })
|
||||
(is (= { :_id oid :title title :measurements [1.0 1.1 1.1 1.3 1.0] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title :measurements [1.0 1.2 1.2 1.2 1.1 1.1 1.2 1.3 1.0] })
|
||||
(mc/update db coll { :_id oid } { $pull { :measurements 1.2 } })
|
||||
(is (= { :_id oid :title title :measurements [1.0 1.1 1.1 1.3 1.0] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
(deftest remove-all-value-entries-from-array-using-$pull-modifier-based-on-a-condition
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$pull modifier removes all value entries in the array"]
|
||||
(mgcol/insert db coll { :_id oid :title title :measurements [1.0 1.2 1.2 1.2 1.1 1.1 1.2 1.3 1.0] })
|
||||
(mgcol/update db coll { :_id oid } { $pull { :measurements { $gte 1.2 } } })
|
||||
(is (= { :_id oid :title title :measurements [1.0 1.1 1.1 1.0] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title :measurements [1.0 1.2 1.2 1.2 1.1 1.1 1.2 1.3 1.0] })
|
||||
(mc/update db coll { :_id oid } { $pull { :measurements { $gte 1.2 } } })
|
||||
(is (= { :_id oid :title title :measurements [1.0 1.1 1.1 1.0] } (mc/find-map-by-id db coll oid)))))
|
||||
;;
|
||||
;; $pullAll
|
||||
;;
|
||||
|
|
@ -327,9 +331,9 @@
|
|||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
title "$pullAll modifier removes entries of multiple values in the array"]
|
||||
(mgcol/insert db coll { :_id oid :title title :measurements [1.0 1.2 1.2 1.2 1.1 1.1 1.2 1.3 1.0] })
|
||||
(mgcol/update db coll { :_id oid } { $pullAll { :measurements [1.0 1.1 1.2] } })
|
||||
(is (= { :_id oid :title title :measurements [1.3] } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title :measurements [1.0 1.2 1.2 1.2 1.1 1.1 1.2 1.3 1.0] })
|
||||
(mc/update db coll { :_id oid } { $pullAll { :measurements [1.0 1.1 1.2] } })
|
||||
(is (= { :_id oid :title title :measurements [1.3] } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
|
||||
;;
|
||||
|
|
@ -342,9 +346,9 @@
|
|||
oid (ObjectId.)
|
||||
title "$rename renames fields"
|
||||
v [1.0 1.2 1.2 1.2 1.1 1.1 1.2 1.3 1.0]]
|
||||
(mgcol/insert db coll { :_id oid :title title :measurements v })
|
||||
(mgcol/update db coll { :_id oid } { $rename { :measurements "results" } })
|
||||
(is (= { :_id oid :title title :results v } (mgcol/find-map-by-id db coll oid)))))
|
||||
(mc/insert db coll { :_id oid :title title :measurements v })
|
||||
(mc/update db coll { :_id oid } { $rename { :measurements "results" } })
|
||||
(is (= { :_id oid :title title :results v } (mc/find-map-by-id db coll oid)))))
|
||||
|
||||
|
||||
;;
|
||||
|
|
@ -358,8 +362,8 @@
|
|||
doc {:_id oid :name "Sophie Bangs" :level 42}
|
||||
conditions {:name "Sophie Bangs"}
|
||||
update {$inc {:level 1}}]
|
||||
(mgcol/insert db coll doc)
|
||||
(let [res (mgcol/find-and-modify db coll conditions update :return-new true)]
|
||||
(mc/insert db coll doc)
|
||||
(let [res (mc/find-and-modify db coll conditions update :return-new true)]
|
||||
(is (= (select-keys res [:name :level]) {:name "Sophie Bangs" :level 43})))))
|
||||
|
||||
|
||||
|
|
@ -369,10 +373,10 @@
|
|||
oid (ObjectId.)
|
||||
doc {:_id oid :name "Sophie Bangs" :level 42}
|
||||
conditions {:name "Sophie Bangs"}]
|
||||
(mgcol/insert db coll doc)
|
||||
(let [res (mgcol/find-and-modify db coll conditions {} :remove true)]
|
||||
(mc/insert db coll doc)
|
||||
(let [res (mc/find-and-modify db coll conditions {} :remove true)]
|
||||
(is (= (select-keys res [:name :level]) {:name "Sophie Bangs" :level 42}))
|
||||
(is (empty? (mgcol/find-maps db coll conditions))))))
|
||||
(is (empty? (mc/find-maps db coll conditions))))))
|
||||
|
||||
|
||||
(deftest find-and-modify-upsert-a-document
|
||||
|
|
@ -381,17 +385,17 @@
|
|||
coll "docs"
|
||||
oid (ObjectId.)
|
||||
doc {:_id oid :name "Sophie Bangs" :level 42}]
|
||||
(let [res (mgcol/find-and-modify db coll doc doc :upsert true)]
|
||||
(let [res (mc/find-and-modify db coll doc doc :upsert true)]
|
||||
(is (empty? res))
|
||||
(is (select-keys (mgcol/find-map-by-id db coll oid) [:name :level]) (dissoc doc :_id)))))
|
||||
(is (select-keys (mc/find-map-by-id db coll oid) [:name :level]) (dissoc doc :_id)))))
|
||||
(testing "case 2"
|
||||
(let [db (mg/get-db "altdb")
|
||||
coll "docs"
|
||||
query {:name "Sophie Bangs"}
|
||||
doc (merge query {:level 42})]
|
||||
(let [res (mgcol/find-and-modify db coll query doc :upsert true :return-new true)]
|
||||
(let [res (mc/find-and-modify db coll query doc :upsert true :return-new true)]
|
||||
(is (:_id res))
|
||||
(is (select-keys (mgcol/find-map-by-id db coll (:_id res)) [:name :level]) doc)))))
|
||||
(is (select-keys (mc/find-map-by-id db coll (:_id res)) [:name :level]) doc)))))
|
||||
|
||||
|
||||
(deftest find-and-modify-after-sort
|
||||
|
|
@ -402,6 +406,6 @@
|
|||
doc {:name "Sophie Bangs"}
|
||||
doc1 (assoc doc :_id oid :level 42)
|
||||
doc2 (assoc doc :_id oid2 :level 0)]
|
||||
(mgcol/insert-batch db coll [doc1 doc2])
|
||||
(let [res (mgcol/find-and-modify db coll doc {$inc {:level 1}} :sort {:level -1})]
|
||||
(mc/insert-batch db coll [doc1 doc2])
|
||||
(let [res (mc/find-and-modify db coll doc {$inc {:level 1}} :sort {:level -1})]
|
||||
(is (= (select-keys res [:name :level]) {:name "Sophie Bangs" :level 42})))))
|
||||
|
|
|
|||
|
|
@ -14,12 +14,15 @@
|
|||
|
||||
(helper/connect!)
|
||||
|
||||
(defn drop-altdb
|
||||
(def db (mg/get-db "altdb"))
|
||||
|
||||
(defn purge-altdb
|
||||
[f]
|
||||
(mg/drop-db "altdb")
|
||||
(mc/remove db "things")
|
||||
(mc/remove db "libraries")
|
||||
(f))
|
||||
|
||||
(use-fixtures :each drop-altdb)
|
||||
(use-fixtures :each purge-altdb)
|
||||
|
||||
(deftest get-collection-size
|
||||
(let [db (mg/get-db "altdb")
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
java.util.Date)
|
||||
(:require [monger.core :as mg]
|
||||
[monger.util :as mu]
|
||||
[monger.multi.collection :as mgcol]
|
||||
[monger.multi.collection :as mc]
|
||||
[monger.test.helper :as helper]
|
||||
[monger.conversion :as mgcnv]
|
||||
[clojure.test :refer :all]
|
||||
|
|
@ -14,12 +14,15 @@
|
|||
|
||||
(helper/connect!)
|
||||
|
||||
(defn drop-altdb
|
||||
(def db (mg/get-db "altdb"))
|
||||
|
||||
(defn purge-altdb
|
||||
[f]
|
||||
(mg/drop-db "altdb")
|
||||
(mc/remove db "libraries")
|
||||
(mc/remove db "docs")
|
||||
(f))
|
||||
|
||||
(use-fixtures :each drop-altdb)
|
||||
(use-fixtures :each purge-altdb)
|
||||
|
||||
;;
|
||||
;; find
|
||||
|
|
@ -28,119 +31,119 @@
|
|||
(deftest find-full-document-when-collection-is-empty
|
||||
(let [db (mg/get-db "altdb")
|
||||
collection "docs"
|
||||
cursor (mgcol/find db collection)]
|
||||
cursor (mc/find db collection)]
|
||||
(is (empty? (iterator-seq cursor)))))
|
||||
|
||||
(deftest find-document-seq-when-collection-is-empty
|
||||
(let [db (mg/get-db "altdb")
|
||||
collection "docs"]
|
||||
(is (empty? (mgcol/find-seq db collection)))))
|
||||
(is (empty? (mc/find-seq db collection)))))
|
||||
|
||||
(deftest find-multiple-documents-when-collection-is-empty
|
||||
(let [db (mg/get-db "altdb")
|
||||
collection "libraries"]
|
||||
(is (empty? (mgcol/find db collection { :language "Scala" })))))
|
||||
(is (empty? (mc/find db collection { :language "Scala" })))))
|
||||
|
||||
(deftest find-multiple-maps-when-collection-is-empty
|
||||
(let [db (mg/get-db "altdb")
|
||||
collection "libraries"]
|
||||
(is (empty? (mgcol/find-maps db collection { :language "Scala" })))))
|
||||
(is (empty? (mc/find-maps db collection { :language "Scala" })))))
|
||||
|
||||
(deftest find-multiple-documents-by-regex
|
||||
(let [db (mg/get-db "altdb")
|
||||
collection "libraries"]
|
||||
(mgcol/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
(mc/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
{ :language "Java", :name "nhibernate" }
|
||||
{ :language "JavaScript", :name "sprout-core" }])
|
||||
(is (= 2 (monger.core/count (mgcol/find db collection { :language #"Java*" }))))))
|
||||
(is (= 2 (monger.core/count (mc/find db collection { :language #"Java*" }))))))
|
||||
|
||||
(deftest find-multiple-documents
|
||||
(let [db (mg/get-db "altdb")
|
||||
collection "libraries"]
|
||||
(mgcol/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
(mc/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
{ :language "Clojure", :name "langohr" }
|
||||
{ :language "Clojure", :name "incanter" }
|
||||
{ :language "Scala", :name "akka" }])
|
||||
(is (= 1 (monger.core/count (mgcol/find db collection { :language "Scala" }))))
|
||||
(is (= 3 (.count (mgcol/find db collection { :language "Clojure" }))))
|
||||
(is (empty? (mgcol/find db collection { :language "Java" })))))
|
||||
(is (= 1 (monger.core/count (mc/find db collection { :language "Scala" }))))
|
||||
(is (= 3 (.count (mc/find db collection { :language "Clojure" }))))
|
||||
(is (empty? (mc/find db collection { :language "Java" })))))
|
||||
|
||||
|
||||
(deftest find-document-specify-fields
|
||||
(let [db (mg/get-db "altdb")
|
||||
collection "libraries"
|
||||
_ (mgcol/insert db collection { :language "Clojure", :name "monger" })
|
||||
result (mgcol/find db collection { :language "Clojure"} [:language])]
|
||||
_ (mc/insert db collection { :language "Clojure", :name "monger" })
|
||||
result (mc/find db collection { :language "Clojure"} [:language])]
|
||||
(is (= (seq [:_id :language]) (keys (mgcnv/from-db-object (.next result) true))))))
|
||||
|
||||
(deftest find-and-iterate-over-multiple-documents-the-hard-way
|
||||
(let [db (mg/get-db "altdb")
|
||||
collection "libraries"]
|
||||
(mgcol/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
(mc/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
{ :language "Clojure", :name "langohr" }
|
||||
{ :language "Clojure", :name "incanter" }
|
||||
{ :language "Scala", :name "akka" }])
|
||||
(doseq [doc (take 3 (map (fn [dbo]
|
||||
(mgcnv/from-db-object dbo true))
|
||||
(mgcol/find-seq db collection { :language "Clojure" })))]
|
||||
(mc/find-seq db collection { :language "Clojure" })))]
|
||||
(is (= "Clojure" (:language doc))))))
|
||||
|
||||
(deftest find-and-iterate-over-multiple-documents
|
||||
(let [db (mg/get-db "altdb")
|
||||
collection "libraries"]
|
||||
(mgcol/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
(mc/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
{ :language "Clojure", :name "langohr" }
|
||||
{ :language "Clojure", :name "incanter" }
|
||||
{ :language "Scala", :name "akka" }])
|
||||
(doseq [doc (take 3 (mgcol/find-maps db collection { :language "Clojure" }))]
|
||||
(doseq [doc (take 3 (mc/find-maps db collection { :language "Clojure" }))]
|
||||
(is (= "Clojure" (:language doc))))))
|
||||
|
||||
|
||||
(deftest find-multiple-maps
|
||||
(let [db (mg/get-db "altdb")
|
||||
collection "libraries"]
|
||||
(mgcol/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
(mc/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
{ :language "Clojure", :name "langohr" }
|
||||
{ :language "Clojure", :name "incanter" }
|
||||
{ :language "Scala", :name "akka" }])
|
||||
(is (= 1 (count (mgcol/find-maps db collection { :language "Scala" }))))
|
||||
(is (= 3 (count (mgcol/find-maps db collection { :language "Clojure" }))))
|
||||
(is (empty? (mgcol/find-maps db collection { :language "Java" })))
|
||||
(is (empty? (mgcol/find-maps db collection { :language "Java" } [:language :name])))))
|
||||
(is (= 1 (count (mc/find-maps db collection { :language "Scala" }))))
|
||||
(is (= 3 (count (mc/find-maps db collection { :language "Clojure" }))))
|
||||
(is (empty? (mc/find-maps db collection { :language "Java" })))
|
||||
(is (empty? (mc/find-maps db collection { :language "Java" } [:language :name])))))
|
||||
|
||||
|
||||
|
||||
(deftest find-multiple-partial-documents
|
||||
(let [db (mg/get-db "altdb")
|
||||
collection "libraries"]
|
||||
(mgcol/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
(mc/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
{ :language "Clojure", :name "langohr" }
|
||||
{ :language "Clojure", :name "incanter" }
|
||||
{ :language "Scala", :name "akka" }])
|
||||
(let [scala-libs (mgcol/find db collection { :language "Scala" } [:name])
|
||||
clojure-libs (mgcol/find db collection { :language "Clojure"} [:language])]
|
||||
(let [scala-libs (mc/find db collection { :language "Scala" } [:name])
|
||||
clojure-libs (mc/find db collection { :language "Clojure"} [:language])]
|
||||
(is (= 1 (.count scala-libs)))
|
||||
(is (= 3 (.count clojure-libs)))
|
||||
(doseq [i clojure-libs]
|
||||
(let [doc (mgcnv/from-db-object i true)]
|
||||
(is (= (:language doc) "Clojure"))))
|
||||
(is (empty? (mgcol/find db collection { :language "Erlang" } [:name]))))))
|
||||
(is (empty? (mc/find db collection { :language "Erlang" } [:name]))))))
|
||||
|
||||
(deftest finds-one-as-map
|
||||
(let [db (mg/get-db "altdb")
|
||||
collection "libraries"]
|
||||
(mgcol/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
(mc/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
{ :language "Clojure", :name "langohr" }])
|
||||
(let [res (mgcol/find-one-as-map db collection { :name "langohr" })]
|
||||
(let [res (mc/find-one-as-map db collection { :name "langohr" })]
|
||||
(is (map? res))
|
||||
(is (= "langohr" (:name res)))
|
||||
(is (= "Clojure" (:language res))))
|
||||
(is (= 2 (count (mgcol/find-one-as-map db collection { :name "langohr" } [:name]))))
|
||||
(is (= "langohr" (get (mgcol/find-one-as-map db collection { :name "langohr" } [:name] false) "name")))))
|
||||
(is (= 2 (count (mc/find-one-as-map db collection { :name "langohr" } [:name]))))
|
||||
(is (= "langohr" (get (mc/find-one-as-map db collection { :name "langohr" } [:name] false) "name")))))
|
||||
|
||||
(deftest find-and-modify
|
||||
(let [db (mg/get-db "altdb")
|
||||
collection "libraries"]
|
||||
(mgcol/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
(mc/insert-batch db collection [{ :language "Clojure", :name "monger" }
|
||||
{ :language "Clojure", :name "langohr" }])))
|
||||
|
||||
|
|
|
|||
|
|
@ -11,12 +11,15 @@
|
|||
|
||||
(helper/connect!)
|
||||
|
||||
(defn drop-altdb
|
||||
(def db (mg/get-db "altdb"))
|
||||
|
||||
(defn purge-altdb
|
||||
[f]
|
||||
(mg/drop-db "altdb")
|
||||
(mc/remove db "libraries")
|
||||
(mc/remove db "recent_events")
|
||||
(f))
|
||||
|
||||
(use-fixtures :each drop-altdb)
|
||||
(use-fixtures :each purge-altdb)
|
||||
|
||||
(deftest ^{:indexing true} test-creating-and-dropping-indexes
|
||||
(let [db (mg/get-db "altdb")
|
||||
|
|
|
|||
|
|
@ -13,12 +13,15 @@
|
|||
|
||||
(helper/connect!)
|
||||
|
||||
(defn drop-altdb
|
||||
(def db (mg/get-db "altdb"))
|
||||
|
||||
(defn purge-altdb
|
||||
[f]
|
||||
(mg/drop-db "altdb")
|
||||
(mc/remove db "people")
|
||||
(mc/remove db "widgets")
|
||||
(f))
|
||||
|
||||
(use-fixtures :each drop-altdb)
|
||||
(use-fixtures :each purge-altdb)
|
||||
|
||||
;;
|
||||
;; insert
|
||||
|
|
|
|||
|
|
@ -16,12 +16,17 @@
|
|||
|
||||
(helper/connect!)
|
||||
|
||||
(defn drop-altdb
|
||||
(def db (mg/get-db "altdb"))
|
||||
|
||||
(defn purge-altdb
|
||||
[f]
|
||||
(mg/drop-db "altdb")
|
||||
(mc/remove db "libraries")
|
||||
(mc/remove db "people")
|
||||
(mc/remove db "matches")
|
||||
(f))
|
||||
|
||||
(use-fixtures :each drop-altdb)
|
||||
(use-fixtures :each purge-altdb)
|
||||
|
||||
|
||||
;;
|
||||
;; update, save
|
||||
|
|
|
|||
Loading…
Reference in a new issue