Remember object ids of inserted documents
This commit is contained in:
parent
3f64310b03
commit
dfb7bd3fc8
2 changed files with 13 additions and 1 deletions
|
|
@ -43,6 +43,7 @@
|
|||
|
||||
(def factories (atom {}))
|
||||
(def defaults (atom {}))
|
||||
(def last-oids (atom {}))
|
||||
|
||||
(defn defaults-for
|
||||
[f-group & { :as attributes }]
|
||||
|
|
@ -55,7 +56,7 @@
|
|||
(assoc-in a [(name f-group) (name f-name)] attributes))))
|
||||
|
||||
|
||||
(declare build seed)
|
||||
(declare build seed remember-oid)
|
||||
(defn- expand-associate-for-building
|
||||
[f]
|
||||
(let [mt (meta f)
|
||||
|
|
@ -102,6 +103,7 @@
|
|||
merged (merge { :_id (ObjectId.) } d attributes overrides)
|
||||
expanded (expand-all-with merged expand-for-seeding)]
|
||||
(assert (mr/ok? (mc/insert f-group expanded)))
|
||||
(remember-oid f-group f-name (:_id expanded))
|
||||
expanded)))
|
||||
|
||||
(defn embedded-doc
|
||||
|
|
@ -114,3 +116,12 @@
|
|||
(with-meta (fn []
|
||||
[f-group f-name]) { :associate-gen true :parent-gen true }))
|
||||
|
||||
(defn- remember-oid
|
||||
[f-group f-name oid]
|
||||
(swap! last-oids (fn [a]
|
||||
(assoc-in a [(name f-group) (name f-name)] oid))))
|
||||
|
||||
(defn last-oid-of
|
||||
"Returns last object id of a document inserted using given factory"
|
||||
[f-group f-name]
|
||||
(get-in @last-oids [(name f-group) (name f-name)]))
|
||||
|
|
|
|||
|
|
@ -90,6 +90,7 @@
|
|||
doc (seed "domains" "clojure" :created-at t)]
|
||||
(is (= 1 (mc/count "domains")))
|
||||
(is (:_id doc))
|
||||
(is (= (:_id doc) (last-oid-of "domains" "clojure")))
|
||||
(is (= t (:created-at doc)))
|
||||
(is (= "clojure.org" (:name doc)))
|
||||
(is (false? (:ipv6-enabled doc)))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue