Fork me on GitHub
#beginners
<
2022-09-26
>
Abhi Saxena14:09:47

Hi Team, In Clojure test, How do we assert for an Error thrown during insertion of a duplicate record, e.g. org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "unique_constraint_name"

dpsutton14:09:42

two straightforward ways: • check out (doc clojure.test/is). It has a way to assert exceptions are thrown: > (is (thrown-with-msg? c re body)) checks that an instance of c is > thrown AND that the message on the exception matches (with > re-find) the regular expression re. • Another way is to just (let [e (try (insert! record) (catch Exception e … ) and just have the exception as a value in your test. You can do whatever you want with it

🙌 1
👍 1
Abhi Saxena17:09:13

@U11BV7MTK @U03BYUDJLJF - Thanks both, it worked 🙂

👍 1
true.neutral15:09:17

Hey! Suppose I wanna write a macro to call a Java object method, by specifying the object and the method.

(defmacro java-call [method object]
  `(. ~object ~method))
Now I'd like to write a helper function that would pin the object, something like this:
(defn java-call-on [method]
  (java-call method defined-somewhere-else))
But I can't really get how do I do that, since the parameter passed to the function will be evaluated, and I can't seem to find a way to retrieve a symbol for a method of a given instance. Is this possible at all?

hiredman17:09:00

What you want is java reflection

Luis Santos17:09:54

Just learned something while reading the reitit code base:

(-> result method :handler (or default-handler))
Never thought about using (or) in a threaded macro to handle fallback values. Feels very idiomatic. I think I will overuse this going forward. 😄

😆 3
delaguardo18:09:22

(-> result method (doto (prn)) :handler (or default-handler))
into this stack of threading macro tips 🙂 add debug logging without changing threading macro result

👍 2
👀 2
💯 1
pppaul18:09:49

you can also do this with let if you build your vector well... things still are a bit crazy to read. something i do a lot is pipe into (cond-> then you get lots of easy to read conditionals at almost no penalty to read your code

Ed21:09:06

Be careful when expecting to get a Boolean value out of this sort of thing.

pppaul02:09:27

wrap (boolean) when wanting true/false values