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"]
|
||||
[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 {
|
||||
|
|
|
@ -36,4 +36,5 @@
|
|||
(handler/site routes))))
|
||||
|
||||
(defn -main [& args]
|
||||
(db/connect-and-handle-events)
|
||||
(httpkit/run-server app {:port 3000}))
|
||||
|
|
|
@ -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 (<! (get-incoming-events))]
|
||||
(println "DB handling" event)
|
||||
(defn handle-incoming-events! []
|
||||
(reset! incoming-events (chan))
|
||||
(go-loop (let [event (<! @incoming-events)]
|
||||
(handle-event event))))
|
||||
|
||||
(defn get-current-grubs-as-events []
|
||||
(let [grubs (mc/find-maps grub-collection)
|
||||
sorted-grubs (sort-by :_id (vec grubs))
|
||||
out (chan)]
|
||||
(println "sorted-grubs:" sorted-grubs)
|
||||
(go (doseq [grub sorted-grubs]
|
||||
(let [grub-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))))
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
(ns grub.core-test
|
||||
(:use clojure.test
|
||||
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