Permalink
Browse files

Implement parent-id seeding in the factory DSL

  • Loading branch information...
1 parent 94abe56 commit 0154e334704380b5d32c6c87aaac3499eb422271 @michaelklishin committed Mar 7, 2012
Showing with 36 additions and 4 deletions.
  1. +22 −4 src/monger/testing.clj
  2. +14 −0 test/monger/test/factory_dsl.clj
View
@@ -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 }]
@@ -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)))))

0 comments on commit 0154e33

Please sign in to comment.