Extract map/reduce and indexing tests
This commit is contained in:
parent
52e824cdd8
commit
8f18d30e43
3 changed files with 129 additions and 105 deletions
|
|
@ -2,18 +2,15 @@
|
||||||
|
|
||||||
(ns monger.test.collection
|
(ns monger.test.collection
|
||||||
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject CommandResult$CommandFailure MapReduceOutput MapReduceCommand MapReduceCommand$OutputType]
|
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject CommandResult$CommandFailure MapReduceOutput MapReduceCommand MapReduceCommand$OutputType]
|
||||||
[org.bson.types ObjectId]
|
org.bson.types.ObjectId
|
||||||
[java.util Date])
|
java.util.Date)
|
||||||
(:require [monger core util]
|
(:require [monger core util]
|
||||||
[clojure stacktrace]
|
|
||||||
[monger.collection :as mgcol]
|
[monger.collection :as mgcol]
|
||||||
[monger.result :as mgres]
|
[monger.result :as mgres]
|
||||||
[monger.conversion :as mgcnv]
|
|
||||||
[monger.js :as js]
|
|
||||||
[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))
|
||||||
|
|
||||||
(helper/connect!)
|
(helper/connect!)
|
||||||
|
|
||||||
|
|
@ -71,29 +68,6 @@
|
||||||
(is (nil? (mgcol/find-by-id collection oid)))))
|
(is (nil? (mgcol/find-by-id collection oid)))))
|
||||||
|
|
||||||
|
|
||||||
;;
|
|
||||||
;; indexes
|
|
||||||
;;
|
|
||||||
|
|
||||||
(deftest ^{:indexing true} index-operations
|
|
||||||
(let [collection "libraries"]
|
|
||||||
(mgcol/drop-indexes collection)
|
|
||||||
(is (= "_id_"
|
|
||||||
(:name (first (mgcol/indexes-on collection)))))
|
|
||||||
(is (nil? (second (mgcol/indexes-on collection))))
|
|
||||||
(mgcol/create-index collection { "language" 1 })
|
|
||||||
(is (= "language_1"
|
|
||||||
(:name (second (mgcol/indexes-on collection)))))
|
|
||||||
(mgcol/drop-index collection "language_1")
|
|
||||||
(is (nil? (second (mgcol/indexes-on collection))))
|
|
||||||
(mgcol/ensure-index collection { "language" 1 } {:unique true})
|
|
||||||
(is (= "language_1"
|
|
||||||
(:name (second (mgcol/indexes-on collection)))))
|
|
||||||
(mgcol/ensure-index collection { "language" 1 })
|
|
||||||
(mgcol/ensure-index collection { "language" 1 } { :unique true })
|
|
||||||
(mgcol/drop-indexes collection)))
|
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; exists?, drop, create
|
;; exists?, drop, create
|
||||||
;;
|
;;
|
||||||
|
|
@ -118,65 +92,36 @@
|
||||||
(is (mgcol/exists? "gadgets"))
|
(is (mgcol/exists? "gadgets"))
|
||||||
(mgcol/drop "gadgets")))
|
(mgcol/drop "gadgets")))
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; Map/Reduce
|
;; any?, empty?
|
||||||
;;
|
;;
|
||||||
|
|
||||||
(let [collection "widgets"
|
(deftest test-any-on-empty-collection
|
||||||
mapper (js/load-resource "resources/mongo/js/mapfun1.js")
|
(let [collection "things"]
|
||||||
reducer "function(key, values) {
|
(is (not (mgcol/any? collection)))))
|
||||||
var result = 0;
|
|
||||||
values.forEach(function(v) { result += v });
|
|
||||||
|
|
||||||
return result;
|
(deftest test-any-on-non-empty-collection
|
||||||
}"
|
(let [collection "things"
|
||||||
batch [{ :state "CA" :quantity 1 :price 199.00 }
|
_ (mgcol/insert collection { :language "Clojure", :name "langohr" })]
|
||||||
{ :state "NY" :quantity 2 :price 199.00 }
|
(is (mgcol/any? "things"))
|
||||||
{ :state "NY" :quantity 1 :price 299.00 }
|
(is (mgcol/any? monger.core/*mongodb-database* "things" {:language "Clojure"}))))
|
||||||
{ :state "IL" :quantity 2 :price 11.50 }
|
|
||||||
{ :state "CA" :quantity 2 :price 2.95 }
|
|
||||||
{ :state "IL" :quantity 3 :price 5.50 }]
|
|
||||||
expected [{:_id "CA", :value 204.9} {:_id "IL", :value 39.5} {:_id "NY", :value 697.0}]]
|
|
||||||
(deftest basic-inline-map-reduce-example
|
|
||||||
(mgcol/remove monger.core/*mongodb-database* collection {})
|
|
||||||
(is (mgres/ok? (mgcol/insert-batch collection batch)))
|
|
||||||
(let [output (mgcol/map-reduce collection mapper reducer nil MapReduceCommand$OutputType/INLINE {})
|
|
||||||
results (mgcnv/from-db-object ^DBObject (.results ^MapReduceOutput output) true)]
|
|
||||||
(mgres/ok? output)
|
|
||||||
(is (= expected results))))
|
|
||||||
|
|
||||||
(deftest basic-map-reduce-example-that-replaces-named-collection
|
(deftest test-empty-on-empty-collection
|
||||||
(mgcol/remove monger.core/*mongodb-database* collection {})
|
(let [collection "things"]
|
||||||
(is (mgres/ok? (mgcol/insert-batch collection batch)))
|
(is (mgcol/empty? collection))
|
||||||
(let [output (mgcol/map-reduce collection mapper reducer "mr_outputs" {})
|
(is (mgcol/empty? monger.core/*mongodb-database* collection))))
|
||||||
results (mgcnv/from-db-object ^DBObject (.results ^MapReduceOutput output) true)]
|
|
||||||
(mgres/ok? output)
|
|
||||||
(is (= 3 (monger.core/count results)))
|
|
||||||
(is (= expected
|
|
||||||
(map #(mgcnv/from-db-object % true) (seq results))))
|
|
||||||
(is (= expected
|
|
||||||
(map #(mgcnv/from-db-object % true) (mgcol/find "mr_outputs"))))
|
|
||||||
(.drop ^MapReduceOutput output)))
|
|
||||||
|
|
||||||
(deftest basic-map-reduce-example-that-merged-results-into-named-collection
|
(deftest test-empty-on-non-empty-collection
|
||||||
(mgcol/remove monger.core/*mongodb-database* collection {})
|
(let [collection "things"
|
||||||
(is (mgres/ok? (mgcol/insert-batch collection batch)))
|
_ (mgcol/insert collection { :language "Clojure", :name "langohr" })]
|
||||||
(mgcol/map-reduce collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {})
|
(is (not (mgcol/empty? "things")))))
|
||||||
(is (mgres/ok? (mgcol/insert collection { :state "OR" :price 17.95 :quantity 4 })))
|
|
||||||
(let [output (mgcol/map-reduce collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {})]
|
|
||||||
(mgres/ok? output)
|
|
||||||
(is (= 4 (monger.core/count (.results ^MapReduceOutput output))))
|
|
||||||
(is (= ["CA" "IL" "NY" "OR"]
|
|
||||||
(map :_id (mgcol/find-maps "merged_mr_outputs"))))
|
|
||||||
(.drop ^MapReduceOutput output))))
|
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; distinct
|
;; distinct
|
||||||
;;
|
;;
|
||||||
|
|
||||||
(deftest distinct-values
|
(deftest test-distinct-values
|
||||||
(let [collection "widgets"
|
(let [collection "widgets"
|
||||||
batch [{ :state "CA" :quantity 1 :price 199.00 }
|
batch [{ :state "CA" :quantity 1 :price 199.00 }
|
||||||
{ :state "NY" :quantity 2 :price 199.00 }
|
{ :state "NY" :quantity 2 :price 199.00 }
|
||||||
|
|
@ -187,28 +132,3 @@
|
||||||
(mgcol/insert-batch collection batch)
|
(mgcol/insert-batch collection batch)
|
||||||
(is (= ["CA" "IL" "NY"] (sort (mgcol/distinct monger.core/*mongodb-database* collection :state {}))))
|
(is (= ["CA" "IL" "NY"] (sort (mgcol/distinct monger.core/*mongodb-database* collection :state {}))))
|
||||||
(is (= ["CA" "NY"] (sort (mgcol/distinct collection :state { :price { $gt 100.00 } }))))))
|
(is (= ["CA" "NY"] (sort (mgcol/distinct collection :state { :price { $gt 100.00 } }))))))
|
||||||
|
|
||||||
|
|
||||||
;;
|
|
||||||
;; any?, empty?
|
|
||||||
;;
|
|
||||||
|
|
||||||
(deftest any-on-empty-collection
|
|
||||||
(let [collection "things"]
|
|
||||||
(is (not (mgcol/any? collection)))))
|
|
||||||
|
|
||||||
(deftest any-on-non-empty-collection
|
|
||||||
(let [collection "things"
|
|
||||||
_ (mgcol/insert collection { :language "Clojure", :name "langohr" })]
|
|
||||||
(is (mgcol/any? "things"))
|
|
||||||
(is (mgcol/any? monger.core/*mongodb-database* "things" {:language "Clojure"}))))
|
|
||||||
|
|
||||||
(deftest empty-on-empty-collection
|
|
||||||
(let [collection "things"]
|
|
||||||
(is (mgcol/empty? collection))
|
|
||||||
(is (mgcol/empty? monger.core/*mongodb-database* collection))))
|
|
||||||
|
|
||||||
(deftest empty-on-non-empty-collection
|
|
||||||
(let [collection "things"
|
|
||||||
_ (mgcol/insert collection { :language "Clojure", :name "langohr" })]
|
|
||||||
(is (not (mgcol/empty? "things")))))
|
|
||||||
35
test/monger/test/indexing.clj
Normal file
35
test/monger/test/indexing.clj
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
(ns monger.test.map-reduce
|
||||||
|
(:import org.bson.types.ObjectId
|
||||||
|
java.util.Date)
|
||||||
|
(:require [monger core util]
|
||||||
|
[monger.collection :as mgcol]
|
||||||
|
[monger.result :as mgres]
|
||||||
|
[monger.test.helper :as helper])
|
||||||
|
(:use clojure.test
|
||||||
|
[monger operators conversion]
|
||||||
|
monger.test.fixtures))
|
||||||
|
|
||||||
|
(helper/connect!)
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; indexes
|
||||||
|
;;
|
||||||
|
|
||||||
|
(deftest ^{:indexing true} test-creating-and-dropping-indexes
|
||||||
|
(let [collection "libraries"]
|
||||||
|
(mgcol/drop-indexes collection)
|
||||||
|
(is (= "_id_"
|
||||||
|
(:name (first (mgcol/indexes-on collection)))))
|
||||||
|
(is (nil? (second (mgcol/indexes-on collection))))
|
||||||
|
(mgcol/create-index collection { "language" 1 })
|
||||||
|
(is (= "language_1"
|
||||||
|
(:name (second (mgcol/indexes-on collection)))))
|
||||||
|
(mgcol/drop-index collection "language_1")
|
||||||
|
(is (nil? (second (mgcol/indexes-on collection))))
|
||||||
|
(mgcol/ensure-index collection { "language" 1 } {:unique true})
|
||||||
|
(is (= "language_1"
|
||||||
|
(:name (second (mgcol/indexes-on collection)))))
|
||||||
|
(mgcol/ensure-index collection { "language" 1 })
|
||||||
|
(mgcol/ensure-index collection { "language" 1 } { :unique true })
|
||||||
|
(mgcol/drop-indexes collection)))
|
||||||
69
test/monger/test/map_reduce.clj
Normal file
69
test/monger/test/map_reduce.clj
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
(ns monger.test.map-reduce
|
||||||
|
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject CommandResult$CommandFailure MapReduceOutput MapReduceCommand MapReduceCommand$OutputType]
|
||||||
|
org.bson.types.ObjectId
|
||||||
|
java.util.Date)
|
||||||
|
(:require [monger core util]
|
||||||
|
[monger.collection :as mgcol]
|
||||||
|
[monger.result :as mgres]
|
||||||
|
[monger.js :as js]
|
||||||
|
[monger.test.helper :as helper])
|
||||||
|
(:use clojure.test
|
||||||
|
[monger operators conversion]
|
||||||
|
[monger.test.fixtures]))
|
||||||
|
|
||||||
|
(helper/connect!)
|
||||||
|
|
||||||
|
(use-fixtures :each purge-people purge-docs purge-things purge-libraries)
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; Map/Reduce
|
||||||
|
;;
|
||||||
|
|
||||||
|
(let [collection "widgets"
|
||||||
|
mapper (js/load-resource "resources/mongo/js/mapfun1.js")
|
||||||
|
reducer "function(key, values) {
|
||||||
|
var result = 0;
|
||||||
|
values.forEach(function(v) { result += v });
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}"
|
||||||
|
batch [{ :state "CA" :quantity 1 :price 199.00 }
|
||||||
|
{ :state "NY" :quantity 2 :price 199.00 }
|
||||||
|
{ :state "NY" :quantity 1 :price 299.00 }
|
||||||
|
{ :state "IL" :quantity 2 :price 11.50 }
|
||||||
|
{ :state "CA" :quantity 2 :price 2.95 }
|
||||||
|
{ :state "IL" :quantity 3 :price 5.50 }]
|
||||||
|
expected [{:_id "CA", :value 204.9} {:_id "IL", :value 39.5} {:_id "NY", :value 697.0}]]
|
||||||
|
(deftest test-basic-inline-map-reduce-example
|
||||||
|
(mgcol/remove monger.core/*mongodb-database* collection {})
|
||||||
|
(is (mgres/ok? (mgcol/insert-batch collection batch)))
|
||||||
|
(let [output (mgcol/map-reduce collection mapper reducer nil MapReduceCommand$OutputType/INLINE {})
|
||||||
|
results (from-db-object ^DBObject (.results ^MapReduceOutput output) true)]
|
||||||
|
(mgres/ok? output)
|
||||||
|
(is (= expected results))))
|
||||||
|
|
||||||
|
(deftest test-basic-map-reduce-example-that-replaces-named-collection
|
||||||
|
(mgcol/remove monger.core/*mongodb-database* collection {})
|
||||||
|
(is (mgres/ok? (mgcol/insert-batch collection batch)))
|
||||||
|
(let [output (mgcol/map-reduce collection mapper reducer "mr_outputs" {})
|
||||||
|
results (from-db-object ^DBObject (.results ^MapReduceOutput output) true)]
|
||||||
|
(mgres/ok? output)
|
||||||
|
(is (= 3 (monger.core/count results)))
|
||||||
|
(is (= expected
|
||||||
|
(map #(from-db-object % true) (seq results))))
|
||||||
|
(is (= expected
|
||||||
|
(map #(from-db-object % true) (mgcol/find "mr_outputs"))))
|
||||||
|
(.drop ^MapReduceOutput output)))
|
||||||
|
|
||||||
|
(deftest test-basic-map-reduce-example-that-merged-results-into-named-collection
|
||||||
|
(mgcol/remove monger.core/*mongodb-database* collection {})
|
||||||
|
(is (mgres/ok? (mgcol/insert-batch collection batch)))
|
||||||
|
(mgcol/map-reduce collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {})
|
||||||
|
(is (mgres/ok? (mgcol/insert collection { :state "OR" :price 17.95 :quantity 4 })))
|
||||||
|
(let [output (mgcol/map-reduce collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {})]
|
||||||
|
(mgres/ok? output)
|
||||||
|
(is (= 4 (monger.core/count (.results ^MapReduceOutput output))))
|
||||||
|
(is (= ["CA" "IL" "NY" "OR"]
|
||||||
|
(map :_id (mgcol/find-maps "merged_mr_outputs"))))
|
||||||
|
(.drop ^MapReduceOutput output))))
|
||||||
Loading…
Reference in a new issue