Simple Midje tests for server db code
This commit is contained in:
parent
991807700b
commit
08f726b963
5 changed files with 40 additions and 19 deletions
|
@ -13,6 +13,7 @@
|
||||||
[prismatic/dommy "0.1.1"]
|
[prismatic/dommy "0.1.1"]
|
||||||
[core.async "0.1.0-SNAPSHOT"]
|
[core.async "0.1.0-SNAPSHOT"]
|
||||||
[com.novemberain/monger "1.5.0"]]
|
[com.novemberain/monger "1.5.0"]]
|
||||||
|
:profiles {:dev {:dependencies [[midje "1.5.1"]]}}
|
||||||
:plugins [[lein-cljsbuild "0.3.2"]
|
:plugins [[lein-cljsbuild "0.3.2"]
|
||||||
[lein-ring "0.8.6"]]
|
[lein-ring "0.8.6"]]
|
||||||
:cljsbuild {
|
:cljsbuild {
|
||||||
|
|
|
@ -36,4 +36,5 @@
|
||||||
(handler/site routes))))
|
(handler/site routes))))
|
||||||
|
|
||||||
(defn -main [& args]
|
(defn -main [& args]
|
||||||
|
(db/connect-and-handle-events)
|
||||||
(httpkit/run-server app {:port 3000}))
|
(httpkit/run-server app {:port 3000}))
|
||||||
|
|
|
@ -7,10 +7,7 @@
|
||||||
|
|
||||||
(def grub-collection "grubs")
|
(def grub-collection "grubs")
|
||||||
|
|
||||||
(def incoming-events (chan))
|
(def incoming-events (atom nil))
|
||||||
|
|
||||||
(defn get-incoming-events []
|
|
||||||
incoming-events)
|
|
||||||
|
|
||||||
(defmulti handle-event :event :default :unknown-event)
|
(defmulti handle-event :event :default :unknown-event)
|
||||||
|
|
||||||
|
@ -18,7 +15,7 @@
|
||||||
(let [grub (-> event
|
(let [grub (-> event
|
||||||
(select-keys [:_id :grub])
|
(select-keys [:_id :grub])
|
||||||
(assoc :completed false))]
|
(assoc :completed false))]
|
||||||
(mc/insert grub-collection grub)))
|
(when (and (:_id grub) (:grub grub)) (mc/insert grub-collection grub))))
|
||||||
|
|
||||||
(defmethod handle-event :complete [event]
|
(defmethod handle-event :complete [event]
|
||||||
(mc/update grub-collection
|
(mc/update grub-collection
|
||||||
|
@ -36,16 +33,15 @@
|
||||||
(defmethod handle-event :unknown-event [event]
|
(defmethod handle-event :unknown-event [event]
|
||||||
(println "Cannot handle unknown event:" event))
|
(println "Cannot handle unknown event:" event))
|
||||||
|
|
||||||
(defn handle-incoming-events []
|
(defn handle-incoming-events! []
|
||||||
(go-loop (let [event (<! (get-incoming-events))]
|
(reset! incoming-events (chan))
|
||||||
(println "DB handling" event)
|
(go-loop (let [event (<! @incoming-events)]
|
||||||
(handle-event event))))
|
(handle-event event))))
|
||||||
|
|
||||||
(defn get-current-grubs-as-events []
|
(defn get-current-grubs-as-events []
|
||||||
(let [grubs (mc/find-maps grub-collection)
|
(let [grubs (mc/find-maps grub-collection)
|
||||||
sorted-grubs (sort-by :_id (vec grubs))
|
sorted-grubs (sort-by :_id (vec grubs))
|
||||||
out (chan)]
|
out (chan)]
|
||||||
(println "sorted-grubs:" sorted-grubs)
|
|
||||||
(go (doseq [grub sorted-grubs]
|
(go (doseq [grub sorted-grubs]
|
||||||
(let [grub-event (-> grub
|
(let [grub-event (-> grub
|
||||||
(select-keys [:_id :grub :completed])
|
(select-keys [:_id :grub :completed])
|
||||||
|
@ -53,10 +49,11 @@
|
||||||
(>! out grub-event))))
|
(>! out grub-event))))
|
||||||
out))
|
out))
|
||||||
|
|
||||||
(defn connect-to-db []
|
(def default-db "grub")
|
||||||
(println "Connect to db")
|
|
||||||
(m/connect!)
|
|
||||||
(m/set-db! (m/get-db "monger-test")))
|
|
||||||
|
|
||||||
(connect-to-db)
|
(defn connect-and-handle-events
|
||||||
(handle-incoming-events)
|
([] (connect-and-handle-events default-db))
|
||||||
|
([db-name]
|
||||||
|
(handle-incoming-events!)
|
||||||
|
(m/connect!)
|
||||||
|
(m/set-db! (m/get-db db-name))))
|
||||||
|
|
|
@ -1,7 +1,3 @@
|
||||||
(ns grub.core-test
|
(ns grub.core-test
|
||||||
(:use clojure.test
|
(:use clojure.test
|
||||||
grub.core))
|
grub.core))
|
||||||
|
|
||||||
(deftest a-test
|
|
||||||
(testing "FIXME, I fail."
|
|
||||||
(is (= 0 1))))
|
|
||||||
|
|
26
test/grub/db_test.clj
Normal file
26
test/grub/db_test.clj
Normal file
|
@ -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?)))
|
Loading…
Reference in a new issue