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,88 +1,88 @@
(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]))
(use-fixtures :each purge-collections)
(deftest test-basic-single-stage-$project-aggregation
(let [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 #{{:quantity 1 :state "CA"}
{:quantity 2 :state "NY"}
{:quantity 1 :state "NY"}
{:quantity 2 :state "IL"}
{:quantity 2 :state "CA"}
{:quantity 3 :state "IL"}}]
(mc/insert-batch db coll batch)
(is (= 6 (mc/count db coll)))
(let [result (set (map #(select-keys % [:state :quantity])
(mc/aggregate db coll [{$project {:state 1 :quantity 1}}])))]
(is (= expected result)))))
(helper/connect!) (deftest test-basic-projection-with-multiplication
(let [batch [{ :state "CA" :quantity 1 :price 199.00 }
(use-fixtures :each purge-docs) { :state "NY" :quantity 2 :price 199.00 }
{ :state "NY" :quantity 1 :price 299.00 }
(deftest test-basic-single-stage-$project-aggregation { :state "IL" :quantity 2 :price 11.50 }
(let [collection "docs" { :state "CA" :quantity 2 :price 2.95 }
batch [{ :state "CA" :quantity 1 :price 199.00 } { :state "IL" :quantity 3 :price 5.50 }]
{ :state "NY" :quantity 2 :price 199.00 } expected #{{:_id "NY" :subtotal 398.0}
{ :state "NY" :quantity 1 :price 299.00 } {:_id "NY" :subtotal 299.0}
{ :state "IL" :quantity 2 :price 11.50 } {:_id "IL" :subtotal 23.0}
{ :state "CA" :quantity 2 :price 2.95 } {:_id "CA" :subtotal 5.9}
{ :state "IL" :quantity 3 :price 5.50 }] {:_id "IL" :subtotal 16.5}
expected #{{:quantity 1 :state "CA"} {:_id "CA" :subtotal 199.0}}]
{:quantity 2 :state "NY"} (mc/insert-batch db coll batch)
{:quantity 1 :state "NY"} (let [result (set (mc/aggregate db coll [{$project {:subtotal {$multiply ["$quantity", "$price"]}
{:quantity 2 :state "IL"} :_id "$state"}}]))]
{:quantity 2 :state "CA"} (is (= expected result)))))
{:quantity 3 :state "IL"}}]
(mc/insert-batch collection batch)
(is (= 6 (mc/count collection)))
(let [result (set (map #(select-keys % [:state :quantity])
(mc/aggregate "docs" [{$project {:state 1 :quantity 1}}])))]
(is (= expected result)))))
(deftest test-basic-projection-with-multiplication (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 "NY" :subtotal 398.0} (mc/insert-batch db coll batch)
{:_id "NY" :subtotal 299.0} (let [result (set (mc/aggregate db coll [{$project {:subtotal {$multiply ["$quantity", "$price"]}
{:_id "IL" :subtotal 23.0} :_id 1
{:_id "CA" :subtotal 5.9} :state 1}}
{:_id "IL" :subtotal 16.5} {$group {:_id "$state"
{:_id "CA" :subtotal 199.0}}] :total {$sum "$subtotal"}}}]))]
(mc/insert-batch collection batch) (is (= expected result)))))
(let [result (set (mc/aggregate "docs" [{$project {:subtotal {$multiply ["$quantity", "$price"]}
:_id "$state"}}]))]
(is (= expected result)))))
(deftest test-basic-total-aggregation (deftest test-$first-aggregation-operator
(let [collection "docs" (let [batch [{ :state "CA" }
batch [{ :state "CA" :quantity 1 :price 199.00 } { :state "IL"}]
{ :state "NY" :quantity 2 :price 199.00 } expected "CA"]
{ :state "NY" :quantity 1 :price 299.00 } (mc/insert-batch db coll batch)
{ :state "IL" :quantity 2 :price 11.50 } (let [result (:state (first (mc/aggregate db coll [{$group { :_id 1 :state {$first "$state"} }}])))]
{ :state "CA" :quantity 2 :price 2.95 } (is (= expected result)))))
{ :state "IL" :quantity 3 :price 5.50 }]
expected #{{:_id "CA" :total 204.9} {:_id "IL" :total 39.5} {:_id "NY" :total 697.0}}]
(mc/insert-batch collection batch)
(let [result (set (mc/aggregate "docs" [{$project {:subtotal {$multiply ["$quantity", "$price"]}
:_id 1
:state 1}}
{$group {:_id "$state"
:total {$sum "$subtotal"}}}]))]
(is (= expected result)))))
(deftest test-$last-aggregation-operator
(deftest test-$first-aggregation-operator (let [batch [{ :state "CA" }
(let [collection "docs" { :state "IL"}]
batch [{ :state "CA" } expected "IL"]
{ :state "IL"}] (mc/insert-batch db coll batch)
expected "CA"] (let [result (:state (first (mc/aggregate db coll [{$group { :_id 1 :state {$last "$state"} }}])))]
(mc/insert-batch collection batch) (is (= expected result))))))
(let [result (:state (first (mc/aggregate collection [{$group { :_id 1 :state {$first "$state"} }}])))]
(is (= expected result)))))
(deftest test-$last-aggregation-operator
(let [collection "docs"
batch [{ :state "CA" }
{ :state "IL"}]
expected "IL"]
(mc/insert-batch collection batch)
(let [result (:state (first (mc/aggregate collection [{$group { :_id 1 :state {$last "$state"} }}])))]
(is (= expected result)))))