Fork me on GitHub
#test-check
<
2016-06-09
>
ag17:06:49

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?

ag17:06:44

I think I need to use something like gen/let?

lucasbradstreet17:06:33

@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))

lucasbradstreet17:06:14

the gen/int bit is especially more cheaty. There may be a nicer way to do it

ag17:06:38

@lucasbradstreet: trying that, thanks

ag17:06:57

excuse my complete noob question (I’m lazy). what is shrinking?

lucasbradstreet18:06:15

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

lucasbradstreet18:06:34

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

lucasbradstreet18:06:13

Oh, I can see what your problem is in that example

lucasbradstreet18:06:29

what you’re doing wrong is actually generating a uuid sample via g/generate

lucasbradstreet18:06:32

and then using gen/return

lucasbradstreet18:06:42

try (gen/fmap str gen/uuid)

lucasbradstreet18:06:00

That’ll give you a uuid str generator

lucasbradstreet18:06:51

Your problem was that you were essentially going (gen/return "5813d2ec-c486-4428-833d-e8373910ae14”)

lucasbradstreet18:06:59

which will, of course, always return the same value

ag18:06:14

Thank you for enabling my laziness and explaining in great detail 🙂

lucasbradstreet18:06:24

happy to help spread the love and the word of the church of property test

gfredericks18:06:01

ag: lucasbradstreet: this might not be in the docstring but gen/uuid generates unique uuids

gfredericks18:06:30

it's sort of in the docstring. It could be more clear.

ag18:06:32

@gfredericks: yeah I think I’m doing something totally wacky here… no one to blame but myself.

ag18:06:37

still learning

lucasbradstreet18:06:50

Yeah. I realised this which is why I investigated why the original solution didn't work. Thanks for saying that

gfredericks18:06:08

I should also add things to the docstring for generate saying it's just a dev tool

gfredericks18:06:36

I bet people use it for real generators more often than I was expecting

lucasbradstreet18:06:21

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

lucasbradstreet18:06:11

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

lucasbradstreet18:06:01

gen/sample does sound more devvy than gen/generate. I could see how people could get confused there

lucasbradstreet18:06:49

A docstring note would be good anyway

gfredericks18:06:09

yeah, probably on both of them