Implement parent-id seeding in the factory DSL

This commit is contained in:
Michael S. Klishin 2012-03-07 10:21:57 +04:00
parent 94abe56605
commit 0154e33470
2 changed files with 36 additions and 4 deletions

View file

@ -63,12 +63,28 @@
(:_id (build f-group f-name))))
(defn- expand-for-building
"Expands functions, treating those with association metadata (see `parent-id` for example) specially"
[f]
(let [mt (meta f)]
(if (:associate-gen mt)
(expand-associate-for-building f)
(f))))
(defn- expand-associate-for-seeding
[f]
(let [mt (meta f)
[f-group f-name] (f)]
(:_id (seed f-group f-name))))
(defn- expand-for-seeding
"Expands functions, treating those with association metadata (see `parent-id` for example) specially,
making sure parent documents are persisted first"
[f]
(let [mt (meta f)]
(if (:associate-gen mt)
(expand-associate-for-seeding f)
(f))))
(defn build
"Generates a new document and returns it"
[f-group f-name & { :as overrides }]
@ -81,10 +97,12 @@
"Generates and inserts a new document, then returns it"
[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)))
doc)))
(let [d (@defaults (name f-group))
attributes (get-in @factories [(name f-group) (name f-name)])
merged (merge { :_id (ObjectId.) } d attributes overrides)
expanded (expand-all-with merged expand-for-seeding)]
(assert (mr/ok? (mc/insert f-group expanded)))
expanded)))
(defn embedded-doc
[f-group f-name & { :as overrides }]

View file

@ -105,3 +105,17 @@
(is (= ["erlang" "python" "ruby"] (get-in loaded [:related :terms])))
(is (= "elixir-lang.org" (:name loaded)))
(is (not (:ipv6-enabled loaded)))))
(deftest test-seeding-child-documents-with-a-parent-ref-case-1
(is (mc/empty? "domains"))
(is (mc/empty? "pages"))
(let [page (seed "pages" "http://clojure.org/rationale")
domain (mc/find-map-by-id "domains" (:domain-id page))]
(is (= 1 (mc/count "domains")))
(is (= 1 (mc/count "pages")))
(is domain)
(is (:domain-id page))
(is (= "clojure.org" (:name domain)))
(is (= "/rationale" (:name page)))))