Introduce monger.testing/defcleaner that supposed to be used with clojure.test/use-fixtures

This commit is contained in:
Michael S. Klishin 2011-12-16 07:18:16 +04:00
parent e6f81e52a7
commit f49e272f9f

28
src/monger/testing.clj Normal file
View file

@ -0,0 +1,28 @@
(ns monger.testing
(:require [monger collection]))
;;
;; API
;;
(defmacro defcleaner
"Defines a fixture function that removes all documents from a collection. If collection is not specified,
a conventionally named var will be used. Supposed to be used with clojure.test/use-fixtures but may
be useful on its own.
Examples:
(defcleaner events) ;; collection name will be taken from the events-collection var
(defcleaner people \"accounts\") ;; collection name is given
"
[entities & coll-name]
(let [coll-arg (if coll-name
(str (first coll-name))
(symbol (str entities "-collection")))
fn-name (symbol (str "purge-" entities))]
`(defn ~fn-name
[f#]
(monger.collection/remove ~coll-arg)
(f#)
(monger.collection/remove ~coll-arg))))