Map/Reduce tests now pass
This commit is contained in:
parent
5fb665e5eb
commit
58d426d6aa
1 changed files with 49 additions and 53 deletions
|
|
@ -2,69 +2,65 @@
|
||||||
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject MapReduceOutput MapReduceCommand MapReduceCommand$OutputType]
|
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject MapReduceOutput MapReduceCommand MapReduceCommand$OutputType]
|
||||||
org.bson.types.ObjectId
|
org.bson.types.ObjectId
|
||||||
java.util.Date)
|
java.util.Date)
|
||||||
(:require [monger core util]
|
(:require [monger.collection :as mc]
|
||||||
[monger.collection :as mc]
|
[monger.core :as mg]
|
||||||
[monger.result :as mgres]
|
[monger.result :as mgres]
|
||||||
[clojurewerkz.support.js :as js]
|
[clojurewerkz.support.js :as js]
|
||||||
[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!)
|
(let [conn (mg/connect)
|
||||||
|
db (mg/get-db conn "monger-test")]
|
||||||
|
(use-fixtures :each (fn [f]
|
||||||
|
(mc/remove db "widgets")
|
||||||
|
(f)
|
||||||
|
(mc/remove db "widgets")))
|
||||||
|
|
||||||
(use-fixtures :each purge-people purge-docs purge-things purge-libraries)
|
(let [collection "widgets"
|
||||||
|
mapper (js/load-resource "resources/mongo/js/mapfun1.js")
|
||||||
|
reducer "function(key, values) {
|
||||||
;;
|
|
||||||
;; Map/Reduce
|
|
||||||
;;
|
|
||||||
|
|
||||||
(let [collection "widgets"
|
|
||||||
mapper (js/load-resource "resources/mongo/js/mapfun1.js")
|
|
||||||
reducer "function(key, values) {
|
|
||||||
var result = 0;
|
var result = 0;
|
||||||
values.forEach(function(v) { result += v });
|
values.forEach(function(v) { result += v });
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}"
|
}"
|
||||||
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 }
|
||||||
{ :state "NY" :quantity 1 :price 299.00 }
|
{ :state "NY" :quantity 1 :price 299.00 }
|
||||||
{ :state "IL" :quantity 2 :price 11.50 }
|
{ :state "IL" :quantity 2 :price 11.50 }
|
||||||
{ :state "CA" :quantity 2 :price 2.95 }
|
{ :state "CA" :quantity 2 :price 2.95 }
|
||||||
{ :state "IL" :quantity 3 :price 5.50 }]
|
{ :state "IL" :quantity 3 :price 5.50 }]
|
||||||
expected [{:_id "CA", :value 204.9} {:_id "IL", :value 39.5} {:_id "NY", :value 697.0}]]
|
expected [{:_id "CA", :value 204.9} {:_id "IL", :value 39.5} {:_id "NY", :value 697.0}]]
|
||||||
(deftest test-basic-inline-map-reduce-example
|
(deftest test-basic-inline-map-reduce-example
|
||||||
(mc/remove monger.core/*mongodb-database* collection {})
|
(mc/remove db collection)
|
||||||
(is (mgres/ok? (mc/insert-batch collection batch)))
|
(is (mgres/ok? (mc/insert-batch db collection batch)))
|
||||||
(let [output (mc/map-reduce collection mapper reducer nil MapReduceCommand$OutputType/INLINE {})
|
(let [output (mc/map-reduce db collection mapper reducer nil MapReduceCommand$OutputType/INLINE {})
|
||||||
results (from-db-object ^DBObject (.results ^MapReduceOutput output) true)]
|
results (from-db-object ^DBObject (.results ^MapReduceOutput output) true)]
|
||||||
(mgres/ok? output)
|
(mgres/ok? output)
|
||||||
(is (= expected results))))
|
(is (= expected results))))
|
||||||
|
|
||||||
(deftest test-basic-map-reduce-example-that-replaces-named-collection
|
(deftest test-basic-map-reduce-example-that-replaces-named-collection
|
||||||
(mc/remove monger.core/*mongodb-database* collection {})
|
(mc/remove db collection)
|
||||||
(is (mgres/ok? (mc/insert-batch collection batch)))
|
(is (mgres/ok? (mc/insert-batch db collection batch)))
|
||||||
(let [output (mc/map-reduce collection mapper reducer "mr_outputs" {})
|
(let [output (mc/map-reduce db collection mapper reducer "mr_outputs" {})
|
||||||
results (from-db-object ^DBObject (.results ^MapReduceOutput output) true)]
|
results (from-db-object ^DBObject (.results ^MapReduceOutput output) true)]
|
||||||
(mgres/ok? output)
|
(mgres/ok? output)
|
||||||
(is (= 3 (monger.core/count results)))
|
(is (= 3 (mg/count results)))
|
||||||
(is (= expected
|
(is (= expected
|
||||||
(map #(from-db-object % true) (seq results))))
|
(map #(from-db-object % true) (seq results))))
|
||||||
(is (= expected
|
(is (= expected
|
||||||
(map #(from-db-object % true) (mc/find "mr_outputs"))))
|
(map #(from-db-object % true) (mc/find db "mr_outputs"))))
|
||||||
(.drop ^MapReduceOutput output)))
|
(.drop ^MapReduceOutput output)))
|
||||||
|
|
||||||
(deftest test-basic-map-reduce-example-that-merged-results-into-named-collection
|
(deftest test-basic-map-reduce-example-that-merged-results-into-named-collection
|
||||||
(mc/remove monger.core/*mongodb-database* collection {})
|
(mc/remove db collection)
|
||||||
(is (mgres/ok? (mc/insert-batch collection batch)))
|
(is (mgres/ok? (mc/insert-batch db collection batch)))
|
||||||
(mc/map-reduce collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {})
|
(mc/map-reduce db collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {})
|
||||||
(is (mgres/ok? (mc/insert collection { :state "OR" :price 17.95 :quantity 4 })))
|
(is (mgres/ok? (mc/insert db collection { :state "OR" :price 17.95 :quantity 4 })))
|
||||||
(let [^MapReduceOutput output (mc/map-reduce collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {})]
|
(let [^MapReduceOutput output (mc/map-reduce db collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {})]
|
||||||
(mgres/ok? output)
|
(mgres/ok? output)
|
||||||
(is (= 4 (monger.core/count output)))
|
(is (= 4 (mg/count output)))
|
||||||
(is (= ["CA" "IL" "NY" "OR"]
|
(is (= ["CA" "IL" "NY" "OR"]
|
||||||
(map :_id (mc/find-maps "merged_mr_outputs"))))
|
(map :_id (mc/find-maps db "merged_mr_outputs"))))
|
||||||
(.drop ^MapReduceOutput output))))
|
(.drop ^MapReduceOutput output)))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue