Initial factory/fixture DSL bits
This commit is contained in:
parent
63d40179fe
commit
19a55b5a5b
2 changed files with 42 additions and 1 deletions
|
|
@ -8,7 +8,8 @@
|
|||
;; You must not remove this notice, or any other, from this software.
|
||||
|
||||
(ns monger.testing
|
||||
(:require [monger collection]))
|
||||
(:require [monger collection])
|
||||
(:import [org.bson.types ObjectId]))
|
||||
|
||||
|
||||
;;
|
||||
|
|
@ -35,3 +36,24 @@
|
|||
(monger.collection/remove ~coll-arg)
|
||||
(f#)
|
||||
(monger.collection/remove ~coll-arg))))
|
||||
|
||||
|
||||
(def factories (atom {}))
|
||||
(def defaults (atom {}))
|
||||
|
||||
|
||||
(defn defaults-for
|
||||
[f-group & { :as attributes }]
|
||||
(swap! defaults (fn [v]
|
||||
(assoc v (name f-group) attributes))))
|
||||
|
||||
(defn factory
|
||||
[f-group f-name & { :as attributes }]
|
||||
(swap! factories (fn [a]
|
||||
(assoc-in a [(name f-group) (name f-name)] attributes))))
|
||||
|
||||
(defn build
|
||||
[f-group f-name & { :as overrides }]
|
||||
(let [d (@defaults (name f-group))
|
||||
attributes (get-in @factories [(name f-group) (name f-name)])]
|
||||
(merge { :_id (ObjectId.) } d attributes overrides)))
|
||||
|
|
|
|||
19
test/monger/test/factory_dsl.clj
Normal file
19
test/monger/test/factory_dsl.clj
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
(ns monger.test.factory-dsl
|
||||
(:use [clojure.test]
|
||||
[monger.testing]
|
||||
[clj-time.core :only [days ago weeks]]))
|
||||
|
||||
(defaults-for "domains"
|
||||
:ipv6-enabled false)
|
||||
|
||||
(factory "domains" "clojure.org"
|
||||
:name "clojure.org"
|
||||
:created-at (-> 2 days ago))
|
||||
|
||||
(deftest test-building-documents-from-a-factory-case-1
|
||||
(let [t (-> 2 weeks ago)
|
||||
doc (build "domains" "clojure.org" :created-at t)]
|
||||
(is (:_id doc))
|
||||
(is (= t (:created-at doc)))
|
||||
(is (= "clojure.org" (:name doc)))
|
||||
(is (false? (:ipv6-enabled doc)))))
|
||||
Loading…
Reference in a new issue