if I have something like this:
(defn uuid-gen []
(g/return (-> g/uuid g/generate str)))
{:table "my-table"
:key (uuid-gen)
:rows (-> (g/hash-map
:id (uuid-gen)
:external-reference-id g/string-alphanumeric)
(g/vector 1000) g/not-empty g/generate)}
how can I ensure that uuids generated for :key
and :id
are always unique?I think I need to use something like gen/let
?
@ag in cases where I wanted my uuids to be unique, and … thus uuid’y, I’ve just cheated by doing something like this: (gen/no-shrink (gen/fmap (fn [_] (java.util.UUID/randomUUID)) gen/int))
the gen/int bit is especially more cheaty. There may be a nicer way to do it
@lucasbradstreet: trying that, thanks
excuse my complete noob question (I’m lazy). what is shrinking?
test.check will shrink down your test cases so that it can find the smallest failing case e.g. integers might shrink from 232323976 to 0, a vector might shrink from a many element vector to a small one
since all we’re doing is generating a UUID, and not using the int generated by gen/int, there’s no point in shrinking this case
Oh, I can see what your problem is in that example
Hmm, maybe not
what you’re doing wrong is actually generating a uuid sample via g/generate
and then using gen/return
try (gen/fmap str gen/uuid)
That’ll give you a uuid str generator
Your problem was that you were essentially going (gen/return "5813d2ec-c486-4428-833d-e8373910ae14”)
which will, of course, always return the same value
Thank you for enabling my laziness and explaining in great detail 🙂
happy to help spread the love and the word of the church of property test
ag: lucasbradstreet: this might not be in the docstring but gen/uuid generates unique uuids
it's sort of in the docstring. It could be more clear.
@gfredericks: yeah I think I’m doing something totally wacky here… no one to blame but myself.
still learning
Yeah. I realised this which is why I investigated why the original solution didn't work. Thanks for saying that
I should also add things to the docstring for generate saying it's just a dev tool
ag: ⇑
I bet people use it for real generators more often than I was expecting
The improved suggestion will be more reproducible than the one that I suggested. The original one will give different results when you use the same seed
It's easy enough to do starting out. Try some stuff out, and then you need a generator again so you go and stick it into return or elements without realising
yeah :/
gen/sample does sound more devvy than gen/generate. I could see how people could get confused there
A docstring note would be good anyway
yeah, probably on both of them