test-check

ag 2016-06-09T17:40:49.000025Z

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?

ag 2016-06-09T17:45:44.000027Z

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

lucasbradstreet 2016-06-09T17:49:33.000028Z

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

lucasbradstreet 2016-06-09T17:50:14.000029Z

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

ag 2016-06-09T17:51:38.000030Z

@lucasbradstreet: trying that, thanks

ag 2016-06-09T17:56:57.000032Z

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

lucasbradstreet 2016-06-09T18:02:15.000033Z

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

lucasbradstreet 2016-06-09T18:02:34.000034Z

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

lucasbradstreet 2016-06-09T18:04:13.000035Z

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

lucasbradstreet 2016-06-09T18:04:29.000036Z

Hmm, maybe not

lucasbradstreet 2016-06-09T18:05:29.000037Z

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

lucasbradstreet 2016-06-09T18:05:32.000038Z

and then using gen/return

lucasbradstreet 2016-06-09T18:05:42.000039Z

try (gen/fmap str gen/uuid)

lucasbradstreet 2016-06-09T18:06:00.000040Z

That’ll give you a uuid str generator

lucasbradstreet 2016-06-09T18:06:51.000041Z

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

lucasbradstreet 2016-06-09T18:06:59.000042Z

which will, of course, always return the same value

ag 2016-06-09T18:09:14.000043Z

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

lucasbradstreet 2016-06-09T18:10:24.000044Z

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

2016-06-09T18:28:01.000045Z

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

2016-06-09T18:28:30.000046Z

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

ag 2016-06-09T18:29:32.000047Z

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

ag 2016-06-09T18:29:37.000048Z

still learning

lucasbradstreet 2016-06-09T18:39:50.000049Z

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

2016-06-09T18:41:08.000050Z

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

2016-06-09T18:41:11.000051Z

ag: ⇑

2016-06-09T18:41:36.000052Z

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

lucasbradstreet 2016-06-09T18:42:21.000053Z

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

lucasbradstreet 2016-06-09T18:43:11.000054Z

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

2016-06-09T18:43:28.000055Z

yeah :/

lucasbradstreet 2016-06-09T18:44:01.000056Z

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

lucasbradstreet 2016-06-09T18:44:49.000057Z

A docstring note would be good anyway

2016-06-09T18:45:09.000058Z

yeah, probably on both of them