Skip to content

Commit

Permalink
Automagic batched hydration SHOULD hydrate nil when PKs are nil
Browse files Browse the repository at this point in the history
  • Loading branch information
camsaul committed Jan 17, 2023
1 parent 2780879 commit d42e86f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/toucan2/tools/hydrate.clj
Expand Up @@ -332,7 +332,7 @@
(log/debugf :hydrate "Attempting to hydrate %s rows out of %s" (count (filter ::fk rows)) (count rows))
(for [row rows]
(if-not (::fk row)
row
(assoc row dest-key nil)
(let [fk-vals (::fk row)
;; convert fk to from [id] to id if it only has one key. This is what `select-pk->fn` returns.
fk-vals (if (= (count fk-vals) 1)
Expand All @@ -341,8 +341,8 @@
fetched-instance (get pk->fetched-instance fk-vals)]
(log/tracef :hydrate "Hydrate %s %s with %s" dest-key [row] (or fetched-instance
"nil (no matching fetched row)"))
(cond-> (dissoc row ::fk)
fetched-instance (assoc dest-key fetched-instance))))))
(-> (dissoc row ::fk)
(assoc dest-key fetched-instance))))))

(m/defmethod hydrate-with-strategy ::automagic-batched
[model _strategy dest-key rows]
Expand Down
23 changes: 15 additions & 8 deletions test/toucan2/tools/hydrate_test.clj
Expand Up @@ -71,7 +71,8 @@

(defn- remove-venues-timestamps [rows]
(for [result rows]
(update result ::venue #(dissoc % :updated-at :created-at))))
(cond-> result
(::venue result) (update ::venue #(dissoc % :updated-at :created-at)))))

(deftest ^:parallel automagic-hydration-test
(is (= [{:venue-id 1
Expand All @@ -80,13 +81,15 @@
::venue {:category :bar, :name "Ho's Tavern", :id 2}}]
(remove-venues-timestamps
(hydrate/hydrate [{:venue-id 1} {:venue-id 2}] ::venue))))
(testing "nil FKs"
(testing "nil or non-existent FKs"
(is (= [{:venue-id nil
::venue nil}
{:venue-id nil
::venue nil}
{:venue-id 1000
::venue nil}]
(remove-venues-timestamps
(hydrate/hydrate [{:venue-id nil} {:venue-id nil}] ::venue)))))
(hydrate/hydrate [{:venue-id nil} {:venue-id nil} {:venue-id 1000}] ::venue)))))
(testing "Alternative fk-keys-for-automagic-hydration impl"
(is (= [{:venue_id nil
::venue nil}
Expand Down Expand Up @@ -384,8 +387,12 @@
{:id 2
:name "Sam"
:created-at (OffsetDateTime/parse "2019-01-11T23:56Z")})}
{:person-id 3, :person-name "Bird"}
{:persion-id nil, :person-name "Pam"}]
{:person-id 3
:person-name "Bird"
::people nil}
{:persion-id nil
:person-name "Pam"
::people nil}]
(hydrate/hydrate [(instance/instance nil {:person-id 1, :person-name "Cam"})
{:person-id 2, :person-name "Sam"}
{:person-id 3, :person-name "Bird"}
Expand All @@ -411,7 +418,7 @@
{:person-id nil, :person-name nil}]]
(testing (format "source map = %s" (pr-str m))
(execute/with-call-count [call-count]
(is (= m
(is (= (assoc m ::people nil)
(hydrate/hydrate (instance/instance nil m)
::people)))
(is (= 0
Expand Down Expand Up @@ -457,8 +464,8 @@
(is (= false
(:good-bird? bad-bird)))
(is (contains? bad-birds (::birb bad-bird))))
(testing "don't hydrate nil keys"
(is (= {:good-bird? nil}
(testing "DO hydrate nil keys"
(is (= {:good-bird? nil, ::birb nil}
no-bird))))))

(deftest ^:parallel unnest-first-result-test
Expand Down

0 comments on commit d42e86f

Please sign in to comment.