grub-fork/spec/clj/grub/db_spec.clj

63 lines
2.5 KiB
Clojure
Raw Normal View History

2013-08-17 14:29:30 +00:00
(ns grub.db-spec
(:require [speclj.core :refer :all]
[grub.db :as db]
[monger.collection :as mc]
[clojure.core.async :refer [>!! <!! timeout]]))
(def test-db "grub-test")
(defn short-delay []
2013-08-18 11:13:55 +00:00
(<!! (timeout 20)))
(defn get-test-grub []
{:_id (str "grub" (rand-int 10000))
:grub (str "testtext" (rand-int 1000))
:completed ([true false] (rand-int 1))})
2013-08-17 14:29:30 +00:00
(describe
"grub.db"
2013-08-18 11:13:55 +00:00
(before-all (db/connect-and-handle-events test-db))
(before (db/clear-grubs))
(describe "Add"
(it "should add a grub when an add event comes"
2013-08-17 14:29:30 +00:00
(let [test-grub "testgrub"
test-id 12345]
(>!! @db/incoming-events {:event :add
:_id test-id
:grub test-grub
:completed false})
2013-08-17 14:29:30 +00:00
(short-delay)
(should=
{:_id test-id :grub test-grub :completed false}
(mc/find-one-as-map db/grub-collection {:_id test-id})))))
(describe "Complete"
2013-08-17 14:29:30 +00:00
(it "should complete a grub when a complete event comes"
(let [test-grub {:_id 123456 :completed false}]
(mc/insert db/grub-collection test-grub)
(>!! @db/incoming-events {:event :complete :_id (:_id test-grub)})
(short-delay)
(should=
{:_id (:_id test-grub) :completed true}
(mc/find-one-as-map db/grub-collection {:_id (:_id test-grub)})))))
(describe "Uncomplete"
2013-08-17 14:29:30 +00:00
(it "should uncomplete a grub when an uncomplete event comes"
(let [test-grub {:_id 123456 :completed true}]
(mc/insert db/grub-collection test-grub)
(>!! @db/incoming-events {:event :uncomplete :_id (:_id test-grub)})
(short-delay)
(should=
{:_id (:_id test-grub) :completed false}
(mc/find-one-as-map db/grub-collection {:_id (:_id test-grub)})))))
(describe "Delete"
2013-08-17 14:29:30 +00:00
(it "should delete a grub when a delete event comes"
(let [test-grub {:_id 123456 :completed true}]
(mc/insert db/grub-collection test-grub)
(>!! @db/incoming-events {:event :delete :_id (:_id test-grub)})
(short-delay)
(should=
nil
(mc/find-one-as-map db/grub-collection {:_id (:_id test-grub)}))))))