diff --git a/project.clj b/project.clj index b9c1da8..0cbab66 100644 --- a/project.clj +++ b/project.clj @@ -13,6 +13,7 @@ [prismatic/dommy "0.1.1"] [core.async "0.1.0-SNAPSHOT"] [com.novemberain/monger "1.5.0"]] + :profiles {:dev {:dependencies [[midje "1.5.1"]]}} :plugins [[lein-cljsbuild "0.3.2"] [lein-ring "0.8.6"]] :cljsbuild { diff --git a/src-clj/grub/core.clj b/src-clj/grub/core.clj index 0a30406..80e17ad 100644 --- a/src-clj/grub/core.clj +++ b/src-clj/grub/core.clj @@ -36,4 +36,5 @@ (handler/site routes)))) (defn -main [& args] + (db/connect-and-handle-events) (httpkit/run-server app {:port 3000})) diff --git a/src-clj/grub/db.clj b/src-clj/grub/db.clj index 50233f7..56a226c 100644 --- a/src-clj/grub/db.clj +++ b/src-clj/grub/db.clj @@ -7,10 +7,7 @@ (def grub-collection "grubs") -(def incoming-events (chan)) - -(defn get-incoming-events [] - incoming-events) +(def incoming-events (atom nil)) (defmulti handle-event :event :default :unknown-event) @@ -18,7 +15,7 @@ (let [grub (-> event (select-keys [:_id :grub]) (assoc :completed false))] - (mc/insert grub-collection grub))) + (when (and (:_id grub) (:grub grub)) (mc/insert grub-collection grub)))) (defmethod handle-event :complete [event] (mc/update grub-collection @@ -36,16 +33,15 @@ (defmethod handle-event :unknown-event [event] (println "Cannot handle unknown event:" event)) -(defn handle-incoming-events [] - (go-loop (let [event ( grub (select-keys [:_id :grub :completed]) @@ -53,10 +49,11 @@ (>! out grub-event)))) out)) -(defn connect-to-db [] - (println "Connect to db") - (m/connect!) - (m/set-db! (m/get-db "monger-test"))) +(def default-db "grub") -(connect-to-db) -(handle-incoming-events) +(defn connect-and-handle-events + ([] (connect-and-handle-events default-db)) + ([db-name] + (handle-incoming-events!) + (m/connect!) + (m/set-db! (m/get-db db-name)))) diff --git a/test/grub/core_test.clj b/test/grub/core_test.clj index 1f7207c..b9d5a55 100644 --- a/test/grub/core_test.clj +++ b/test/grub/core_test.clj @@ -1,7 +1,3 @@ (ns grub.core-test (:use clojure.test grub.core)) - -(deftest a-test - (testing "FIXME, I fail." - (is (= 0 1)))) diff --git a/test/grub/db_test.clj b/test/grub/db_test.clj new file mode 100644 index 0000000..aaabb79 --- /dev/null +++ b/test/grub/db_test.clj @@ -0,0 +1,26 @@ +(ns grub.db-test + (:require [midje.sweet :refer [fact with-state-changes contains anything]] + [monger.collection :as mc] + [grub.db :as db] + [clojure.core.async :refer [>!!]])) + +(def test-db "grub-test") + +(with-state-changes [(before :facts (do (mc/drop db/grub-collection) + (db/connect-and-handle-events test-db)))] + (fact + "Add grub" + (fact "sending an event creates a grub" + (let [test-grub "testgrub" + test-id 12345] + (>!! @db/incoming-events {:event :create :_id test-id :grub test-grub}) + (mc/find-one-as-map db/grub-collection {:_id test-id}) => + {:_id test-id :grub test-grub :completed false})) + (fact "missing an id means grub is not created" + (>!! @db/incoming-events {:event :create :grub "testgrub"}) + (mc/find-maps db/grub-collection) => + empty?) + (fact "missing grub text means grub is not created" + (>!! @db/incoming-events {:event :create :_id 12345}) + (mc/find-maps db/grub-collection) => + empty?)))