Fork me on GitHub
#clojure-uk
<
2020-09-09
>
dharrigan05:09:17

Good Morning!

seancorfield05:09:35

Mornin' ๐Ÿ‘‹:skin-tone-2:

dharrigan06:09:28

All good over the pond?

seancorfield06:09:36

Aside from the firestorm burning up California and the out-of-control pandemic, yeah, I guess ๐Ÿ˜

seancorfield06:09:09

Roll on November 3rd. Although we likely won't know the final result for days. Maybe weeks. Argh!

dharrigan06:09:11

Oh, I'm thinking it's going to be depressing news - no matter who wins - there's a'trouble brewing methinks.

โ˜๏ธ 3
mccraigmccraig08:09:19

ยกmawningยก

Wes Hall11:09:09

Hey folks, I started a new contract on Monday (back in Java land for a bit), but a recruiter I have been working with has just sent me a spec for a company looking to hire some Clojure permies. I hope I am not out of line mentioning it in here (I know there is a #jobs), but I do seem to remember a few people mentioning that they were currently on the lookout. Ping me with a DM if anybody wants an intro. Cheers.

Wes Hall11:09:57

I should mention, I wont be asking for or taking any referral fees for this, just thought I would ask in case it helps anybody out.

bruno.bonacci12:09:27

Hi all, the recording of the following event is now available online: William Parker - Logic programming with clara-rules @ LNDCLJ https://youtu.be/aJbdTP5hL0U

๐Ÿ‘ 6
folcon13:09:44

Thanks again for organising it @bruno.bonacci ๐Ÿ˜ƒ... The talk was interesting!

dharrigan15:09:19

I have discovered with-redefs

dominicm16:09:40

Undiscover it

dominicm16:09:43

It's useless

dharrigan16:09:11

seems to do what I want, in a test, I can "mock" out a function that calls to an external API

dharrigan16:09:19

what would you suggest otherwise?

dominicm16:09:02

Write your functions to take the result of the external API directly.

dharrigan16:09:30

But at some point, you have to invoke something

dharrigan16:09:45

there is an side effect somewhere

dominicm16:09:26

Not necessarily, you will have an orchestrating function that does this:

(-> (call-external-service) a b c)
And your test will do this:
(-> some-map a b c)

dominicm16:09:16

Instead of writing code like this:

(defn A [] (println "A") (B))
(defn B [] (println "B"))
Write this:
(defn A [] (println "A"))
(defn B [] (println "B"))

(do (A) (B))

dominicm16:09:53

Finally, if you really must do the side effect thing, use a protocol to mock the external service and pass it around. It'll cause you way less headaches, promise. e.g. if you ever turn on parallel tests in eftest.

dharrigan16:09:30

My function is very simple, I'll have to think how it can be rewritten, i.e.,

dharrigan16:09:38

(defn filter-for-valid-agreement
  [policies to app-config]
  (first (filter #(ac/query-for-associations (:agreementId %) to app-config) policies)))

dharrigan16:09:19

the ac/query-for-associations does a API lookup and returns a map or nil if an assocation is found.

dharrigan16:09:59

so I redef'ed ac/query-for-associations to return data that I'm interested in during my test.

dharrigan16:09:12

and tested the filter-for-valid-agreement function

dominicm16:09:45

What property are you trying to test?

dominicm16:09:02

I wouldn't think it too useful to test given how simple it is :)

dharrigan16:09:49

it's important to behave the way I expect, i.e., either return the first of policy (that has an associations) or a nil result

dharrigan16:09:06

a bug was discovered today that didn't take that into account, so I wrote a test specifically for that.

dominicm16:09:47

https://cognitect.com/blog/2016/9/15/works-on-my-machine-understanding-var-bindings-and-roots I remember this being more scolding, but it doesn't seem to be now. But yeah, with-redefs is hard.

dharrigan16:09:17

I'll keep that in mind and see if I can also take into account your suggestions too ๐Ÿ™‚

dharrigan16:09:19

All very helpful

dharrigan16:09:04

btw, i've updated my startrek projects, using aero and clip to be all the latest

dharrigan16:09:14

I do like me some clip - it should be promoted more!

dominicm16:09:49

Welp *scolding. Never typed this before. I assumed it was the same word.

dominicm17:09:32

So yeah, use protocols if you're testing stateful functions. Try to pass the results of stateful functions around though instead. Avoid with-redefs always.

dharrigan17:09:54

Will review and try ๐Ÿ™‚

dharrigan17:09:58

Thanks once again! ๐Ÿ™‚

dominicm17:09:25

> I do like me some clip - it should be promoted more! Promotion is not something I really know how to do :). Happy users go a long way though.

dharrigan17:09:03

I'm happy ๐Ÿ™‚