test-check

bartuka 2020-01-07T12:11:45.036900Z

I need to generate a list of maps that have distinct :number key. For example [{:number 1 ..}, {:number 2 ..}] and I need these numbers to be sequential. I tried (gen/vector-distinct-by :number (gen/hash-map :number gen/pos-int ...) and it does work but not as sequential numbers. I don't know now if I should try to create a generator to replace gen/pos-int or the vector-distinct-by. rsrs

alexmiller 2020-01-07T13:27:59.037500Z

test.check generators are generally for creating random things, not sequential things like that

alexmiller 2020-01-07T13:29:16.038400Z

there may be some way to do it (gen an int that's the size of the range, gen/fmap that with range, gen/bind that with something that makes coll of maps)

alexmiller 2020-01-07T13:29:28.038700Z

but you're going to be fighting the tide there

2020-01-07T13:59:50.039300Z

gen the collection (w/o numbers) and the minimum number separately; then gen/fmap them both to set all the numbers

2020-01-07T14:01:06.040800Z

(gen/fmap (fn [{:keys [list-of-maps min-number]}] (map #(assoc %1 :number %2) list-of-maps (iterate inc min-number))) (gen/tuple gen-list-of-maps-without-number gen/large-integer))

bartuka 2020-01-07T15:53:18.040900Z

I was thinking about that when I got into this problem. But I want to model a situation where does not make sense to have this kind of behavior for example, a list of installments for a loan. They are generally numbered here.

bartuka 2020-01-07T16:04:04.041600Z

@gfredericks thanks for the guidelines... I did some adaptations to work as I intended.

(def gen-list-of-maps-without-number (gen/not-empty (gen/list (gen/hash-map :testing gen/string-alpha-numeric
                                                                            :values gen/double))))
(gen/fmap (fn [[list-of-maps min-number]]
            (map #(assoc %1 :number %2) list-of-maps (iterate inc min-number)))
          (gen/not-empty (gen/tuple gen-list-of-maps-without-number (gen/large-integer* {:min 1 :max 72}))))

2020-01-07T16:10:27.042500Z

@iagwanderson the not-empty around tuple can be removed

👍 1
2020-01-07T16:11:11.042700Z

Otherwise looks good

bartuka 2020-01-07T23:30:39.046600Z

When I ran a test using defspec , I tried two kind of conditionals: 1) Only using a (and .. form and returning true for all branches of the test and 2) using is from clojure.test inside the (and.. form. I did this because without the is I cannot see which branch actually failed, I only get back the map with the current output and the smallest input that provides the failure.

bartuka 2020-01-07T23:30:47.046900Z

I would like to know the branch that failed

bartuka 2020-01-07T23:31:49.047800Z

the real problem is that I have 200+ assertions now being reported by clojure.test if I place the is inside it and I lose the smallest input that provided the failure

2020-01-07T23:33:08.048700Z

make each branch a separate defspec

bartuka 2020-01-07T23:35:17.050100Z

I thought about that, in this case here, I am validating the size of the returned list and them some content in the list.

bartuka 2020-01-07T23:35:38.050600Z

it seems a lot of duplication of code to test only the count for example

2020-01-07T23:36:57.051200Z

they are distinct properties however

bartuka 2020-01-07T23:43:05.052Z

seems like @gfredericks had some ideas about that in this library: https://github.com/gfredericks/test.chuck