Implement seeding (insertion) of factory-built documents, add (experimental) oid-of function
We will see if caching object ids is a viable idea, it is not obvious at this point
This commit is contained in:
parent
35be335a71
commit
0f867b8236
3 changed files with 57 additions and 6 deletions
|
|
@ -8,7 +8,8 @@
|
||||||
;; You must not remove this notice, or any other, from this software.
|
;; You must not remove this notice, or any other, from this software.
|
||||||
|
|
||||||
(ns monger.testing
|
(ns monger.testing
|
||||||
(:require [monger collection])
|
(:require [monger.collection :as mc]
|
||||||
|
[monger.result :as mr])
|
||||||
(:import [org.bson.types ObjectId]))
|
(:import [org.bson.types ObjectId]))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -33,13 +34,14 @@
|
||||||
fn-name (symbol (str "purge-" entities))]
|
fn-name (symbol (str "purge-" entities))]
|
||||||
`(defn ~fn-name
|
`(defn ~fn-name
|
||||||
[f#]
|
[f#]
|
||||||
(monger.collection/remove ~coll-arg)
|
(mc/remove ~coll-arg)
|
||||||
(f#)
|
(f#)
|
||||||
(monger.collection/remove ~coll-arg))))
|
(mc/remove ~coll-arg))))
|
||||||
|
|
||||||
|
|
||||||
(def factories (atom {}))
|
(def factories (atom {}))
|
||||||
(def defaults (atom {}))
|
(def defaults (atom {}))
|
||||||
|
(def oids (atom {}))
|
||||||
|
|
||||||
|
|
||||||
(defn defaults-for
|
(defn defaults-for
|
||||||
|
|
@ -57,3 +59,17 @@
|
||||||
(let [d (@defaults (name f-group))
|
(let [d (@defaults (name f-group))
|
||||||
attributes (get-in @factories [(name f-group) (name f-name)])]
|
attributes (get-in @factories [(name f-group) (name f-name)])]
|
||||||
(merge { :_id (ObjectId.) } d attributes overrides)))
|
(merge { :_id (ObjectId.) } d attributes overrides)))
|
||||||
|
|
||||||
|
(defn seed
|
||||||
|
[f-group f-name & { :as overrides }]
|
||||||
|
(io!
|
||||||
|
(let [doc (apply build f-group f-name (flatten (vec overrides)))
|
||||||
|
oid (:_id doc)]
|
||||||
|
(assert (mr/ok? (mc/insert f-group doc)))
|
||||||
|
(swap! oids (fn [a]
|
||||||
|
(assoc-in a [(name f-group) (name f-name)] oid)))
|
||||||
|
doc)))
|
||||||
|
|
||||||
|
(defn oid-of
|
||||||
|
[f-group f-name]
|
||||||
|
(get-in @oids [(name f-group) (name f-name)]))
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,19 @@
|
||||||
(ns monger.test.factory-dsl
|
(ns monger.test.factory-dsl
|
||||||
(:use [clojure.test]
|
(:use [clojure.test]
|
||||||
[monger.testing]
|
[monger testing joda-time]
|
||||||
|
[monger.test.fixtures]
|
||||||
[clj-time.core :only [days ago weeks]])
|
[clj-time.core :only [days ago weeks]])
|
||||||
|
(:require [monger.collection :as mc]
|
||||||
|
[monger.test.helper :as helper])
|
||||||
(:import [org.bson.types ObjectId]))
|
(:import [org.bson.types ObjectId]))
|
||||||
|
|
||||||
|
|
||||||
|
(helper/connect!)
|
||||||
|
|
||||||
|
(use-fixtures :each purge-domains purge-pages)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(defaults-for "domains"
|
(defaults-for "domains"
|
||||||
:ipv6-enabled false)
|
:ipv6-enabled false)
|
||||||
|
|
||||||
|
|
@ -25,3 +35,26 @@
|
||||||
(is (= oid (:_id doc)))
|
(is (= oid (:_id doc)))
|
||||||
(is (= "clojure.org" (:name doc)))
|
(is (= "clojure.org" (:name doc)))
|
||||||
(is (false? (:ipv6-enabled doc)))))
|
(is (false? (:ipv6-enabled doc)))))
|
||||||
|
|
||||||
|
(deftest test-building-documents-from-a-factory-case-3
|
||||||
|
(let [oid (ObjectId.)
|
||||||
|
t (-> 3 weeks ago)
|
||||||
|
doc (build "domains" "clojure.org" :_id oid :created-at t :name "clojurewerkz.org" :ipv6-enabled true)]
|
||||||
|
(is (= oid (:_id doc)))
|
||||||
|
(is (= t (:created-at doc)))
|
||||||
|
(is (= "clojurewerkz.org" (:name doc)))
|
||||||
|
(is (:ipv6-enabled doc))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(deftest test-seeding-documents-using-a-factory-case1
|
||||||
|
(is (mc/empty? "domains"))
|
||||||
|
(let [t (-> 2 weeks ago)
|
||||||
|
doc (seed "domains" "clojure.org" :created-at t)]
|
||||||
|
(is (= 1 (mc/count "domains")))
|
||||||
|
(is (:_id doc))
|
||||||
|
(is (= (:_id doc) (oid-of "domains" "clojure.org")))
|
||||||
|
(is (= t (:created-at doc)))
|
||||||
|
(is (= "clojure.org" (:name doc)))
|
||||||
|
(is (false? (:ipv6-enabled doc)))))
|
||||||
|
|
|
||||||
|
|
@ -12,3 +12,5 @@
|
||||||
(defcleaner libraries "libraries")
|
(defcleaner libraries "libraries")
|
||||||
(defcleaner scores "scores")
|
(defcleaner scores "scores")
|
||||||
(defcleaner locations "locations")
|
(defcleaner locations "locations")
|
||||||
|
(defcleaner domains "domains")
|
||||||
|
(defcleaner pages "pages")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue