Skip to content

Commit

Permalink
[with-temp] Delay pprint to set testing context in do-with-temp
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-yakushev committed Jan 28, 2025
1 parent b412026 commit 312f715
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/toucan2/tools/with_temp.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
:dispatch-value-spec (s/nonconforming ::types/dispatch-value.model)}
u/dispatch-on-first-arg)

(defn delayed-string
"Return a CharSequence that only executes `(f)` when toString is called. Caches the result."
[f]
(let [d (delay (f))]
(reify CharSequence
(toString [_] @d))))

(m/defmethod do-with-temp* :default
[model explicit-attributes f]
(assert (some? model) (format "%s model cannot be nil." `with-temp))
Expand All @@ -61,9 +68,10 @@
(let [temp-object (first (insert/insert-returning-instances! model merged-attributes))]
(log/debugf "[with-temp] => %s" temp-object)
(try
(t/testing (format "\nwith temporary %s with attributes\n%s\n"
(pr-str model)
(with-out-str (pprint/pprint merged-attributes)))
(t/testing (delayed-string
#(format "\nwith temporary %s with attributes\n%s\n"
(pr-str model)
(with-out-str (pprint/pprint merged-attributes))))
(f temp-object))
(finally
(delete/delete! model :toucan/pk ((model/select-pks-fn model) temp-object))))))))
Expand Down

0 comments on commit 312f715

Please sign in to comment.