Aggregation framework tests now pass

This commit is contained in:
Michael Klishin 2014-05-10 16:28:02 -04:00
parent 9ad38a9e7a
commit b0cd0bf671

View file

@ -1,18 +1,22 @@
(ns monger.test.aggregation-framework-test (ns monger.test.aggregation-framework-test
(:require monger.core [monger.collection :as mc] (:require [monger.core :as mg]
[monger.test.helper :as helper] [monger.collection :as mc]
[clojure.test :refer :all] [clojure.test :refer :all]
[monger.operators :refer :all] [monger.operators :refer :all]))
[monger.test.fixtures :refer :all]))
(let [conn (mg/connect)
db (mg/get-db conn "monger-test")
coll "docs"]
(defn purge-collections
[f]
(mc/purge-many db [coll])
(f)
(mc/purge-many db [coll]))
(helper/connect!) (use-fixtures :each purge-collections)
(use-fixtures :each purge-docs)
(deftest test-basic-single-stage-$project-aggregation (deftest test-basic-single-stage-$project-aggregation
(let [collection "docs" (let [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 }
@ -24,16 +28,15 @@
{:quantity 2 :state "IL"} {:quantity 2 :state "IL"}
{:quantity 2 :state "CA"} {:quantity 2 :state "CA"}
{:quantity 3 :state "IL"}}] {:quantity 3 :state "IL"}}]
(mc/insert-batch collection batch) (mc/insert-batch db coll batch)
(is (= 6 (mc/count collection))) (is (= 6 (mc/count db coll)))
(let [result (set (map #(select-keys % [:state :quantity]) (let [result (set (map #(select-keys % [:state :quantity])
(mc/aggregate "docs" [{$project {:state 1 :quantity 1}}])))] (mc/aggregate db coll [{$project {:state 1 :quantity 1}}])))]
(is (= expected result))))) (is (= expected result)))))
(deftest test-basic-projection-with-multiplication (deftest test-basic-projection-with-multiplication
(let [collection "docs" (let [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 }
@ -45,23 +48,22 @@
{:_id "CA" :subtotal 5.9} {:_id "CA" :subtotal 5.9}
{:_id "IL" :subtotal 16.5} {:_id "IL" :subtotal 16.5}
{:_id "CA" :subtotal 199.0}}] {:_id "CA" :subtotal 199.0}}]
(mc/insert-batch collection batch) (mc/insert-batch db coll batch)
(let [result (set (mc/aggregate "docs" [{$project {:subtotal {$multiply ["$quantity", "$price"]} (let [result (set (mc/aggregate db coll [{$project {:subtotal {$multiply ["$quantity", "$price"]}
:_id "$state"}}]))] :_id "$state"}}]))]
(is (= expected result))))) (is (= expected result)))))
(deftest test-basic-total-aggregation (deftest test-basic-total-aggregation
(let [collection "docs" (let [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" :total 204.9} {:_id "IL" :total 39.5} {:_id "NY" :total 697.0}}] expected #{{:_id "CA" :total 204.9} {:_id "IL" :total 39.5} {:_id "NY" :total 697.0}}]
(mc/insert-batch collection batch) (mc/insert-batch db coll batch)
(let [result (set (mc/aggregate "docs" [{$project {:subtotal {$multiply ["$quantity", "$price"]} (let [result (set (mc/aggregate db coll [{$project {:subtotal {$multiply ["$quantity", "$price"]}
:_id 1 :_id 1
:state 1}} :state 1}}
{$group {:_id "$state" {$group {:_id "$state"
@ -70,19 +72,17 @@
(deftest test-$first-aggregation-operator (deftest test-$first-aggregation-operator
(let [collection "docs" (let [batch [{ :state "CA" }
batch [{ :state "CA" }
{ :state "IL"}] { :state "IL"}]
expected "CA"] expected "CA"]
(mc/insert-batch collection batch) (mc/insert-batch db coll batch)
(let [result (:state (first (mc/aggregate collection [{$group { :_id 1 :state {$first "$state"} }}])))] (let [result (:state (first (mc/aggregate db coll [{$group { :_id 1 :state {$first "$state"} }}])))]
(is (= expected result))))) (is (= expected result)))))
(deftest test-$last-aggregation-operator (deftest test-$last-aggregation-operator
(let [collection "docs" (let [batch [{ :state "CA" }
batch [{ :state "CA" }
{ :state "IL"}] { :state "IL"}]
expected "IL"] expected "IL"]
(mc/insert-batch collection batch) (mc/insert-batch db coll batch)
(let [result (:state (first (mc/aggregate collection [{$group { :_id 1 :state {$last "$state"} }}])))] (let [result (:state (first (mc/aggregate db coll [{$group { :_id 1 :state {$last "$state"} }}])))]
(is (= expected result))))) (is (= expected result))))))