This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-04-26
Channels
- # aleph (2)
- # beginners (119)
- # boot (18)
- # cider (19)
- # cljs-dev (46)
- # cljsjs (1)
- # cljsrn (30)
- # clojure (101)
- # clojure-dusseldorf (12)
- # clojure-finland (1)
- # clojure-greece (7)
- # clojure-india (2)
- # clojure-italy (6)
- # clojure-poland (4)
- # clojure-russia (120)
- # clojure-sg (3)
- # clojure-spec (147)
- # clojure-uk (75)
- # clojurescript (86)
- # cursive (4)
- # datomic (50)
- # docker (1)
- # emacs (4)
- # juxt (51)
- # leiningen (16)
- # liberator (1)
- # luminus (1)
- # lumo (116)
- # mount (2)
- # off-topic (2)
- # onyx (38)
- # pedestal (4)
- # protorepl (2)
- # re-frame (44)
- # reagent (8)
- # ring-swagger (16)
- # schema (5)
- # specter (16)
- # test-check (226)
morning!
random core function of the day:
-------------------------
clojure.core/with-redefs
([bindings & body])
Macro
binding => var-symbol temp-value-expr
Temporarily redefines Vars while executing the body. The
temp-value-exprs will be evaluated and each resulting value will
replace in parallel the root value of its Var. After the body is
executed, the root values of all the Vars will be set back to their
old values. These temporary changes will be visible in all threads.
Useful for mocking out functions during testing.
http://blog.cognitect.com/blog/2016/9/15/works-on-my-machine-understanding-var-bindings-and-roots a valuable read
If I see with-redefs
outside test code I automatically go into alert mode. See an interesting snippets my team (cc: @omartell) recently pointed out:
(defn TEN [] 10)
(dorun (pmap #(with-redefs [TEN (fn [] %)] (TEN)) (range 100)))
(TEN)
morning!
random useful clojure thing i keep forgetting - keyword fns take optional default values
@mccraigmccraig one of my faves
@reborg Don't think I've ever used it outside of tests and even then it feels wrong
@reborg that is hilarious
@agile_geek perhaps. I use it a lot in tests tho, but not usually test first. It's when I come in in untested code, cover it a bit, refactor. Then usually the side effects get injected instead of staying deep in the call stack
@reborg yeah test after the fact (which I'm doing a lot before making changes in existing codebase) is where I use it a lot. I have used it once or twice in my own code where separating the side effecty bit of a fn from a simple transform seems artificial but often I end up deleting the test in the end as it's all mock and no substance!
I got a random number generator
i got different values each run @glenjamin
though they were all in the 90s, so perhaps not suitable for a cryptographic source of randomness
I just got 4
I got 96 every time...don't you just love deterministic non-deterministic code
imagine if you based an architectural decision on my result! WOMM
Oh got an 85 that time! FTW!
i've gotten a couple of values down in the 50s... mostly 90s and 80s though... i've not seen any results below 50 yet
We seem to have discovered an inverse Benford's Law number generator https://en.wikipedia.org/wiki/Benford%27s_law
stopping now. @reborg is a bad person who should feel bad
Got a 1! I win!
On a more interesting note I got caught out by Benford's law when trying to balance even loads across HBase clusters! I would have bene stuck for ages if I hadn't read about it before. Annoyingly I'd actually predicted this effect before starting the work but had completely forgotten about it!
oh, ok, it's easy
in other fun news, i discovered yesterday that ms-word was responsible for tripling our SMS bill
Today is another day in the saga of my wanting to "do" things with Datomic and finding Datalog completely non-intuitive...
@mccraigmccraig - How the blazes did that happen?
it had put an em-dash —
into a sentence instead of a -
, and since the em-dash isn't in the GSM7 character set our texts were being sent in UCS2, meaning 3 message segments for 150 chars of message instead of 1. changing the —
to a -
and the messages gets GSM7 encoded and takes only a single message segment
@mccraigmccraig SMSing entire MS-Word docs?
@mccraigmccraig - That is a "special" edge case...
@maleghast you get your head around datalog after a bit - i haven't used datomic for more than a brief play, but i did a lot of stuff in cascalog a while back and it does become intuitive
@mccraigmccraig - I hope so...
I am reaching my limit of frustration not being paid-off by "good things" at the moment... 😉
In regards to avoiding with-redef
in testing. Am I understanding it correctly in that you write you functions to take in depending functions but then you still need to use with-redef
to test when you actually put everything together at a higher level? Any example projects/code/resources you recommend looking at?
@tbaldridge suggests not using with-redef even in tests. Because if those tests do any kinds of threads (promises, futures, etc.) they won't work.
@shan simplistically if all your fn's are pure you can test the whole stack without mocking anything
with-redefs is global, so should be fine as long as you don’t do multiple tests at once
You have a fat "middle" which is pure, and an outer ring of impure that does the db.
you still gotta test the db-related code
If you're doing a full "integration" test to ensure your system mutates the db correctly, you don't want to mock things anyway, right?
not so - say i'm e.g. testing notifications get sent to the right users (who don't have any applicable mutes set) - i want to interact with the db, where all the user state is, but i want a mock notification service so i can examine the results
Are there any open source projects you lot recommend that has good test examples? or talks or blog posts?
but i side with @tbaldridge in that the way to get your mocks in is by argument passing and dynamic vars on occasion, subject to the rules of dynamic var club
even then, what’s the difference between that and a global variable if you only run one thing at a time?
well, global variables in a component are ok until you come to use multiple instances of a component etc - using closures to capture your dependencies isn't going to bite you in the future
what dyu mean ?