Fork me on GitHub
#minimallist
<
2020-11-22
>
respatialized17:11:03

hello! I really like the design of minimallist. Its integration of test.check has made it very easy to upgrade models from schemas to generators seamlessly. I'm facing an issue when I'm trying to generate map data with some required keys and some optional keys. Here's what reproduces the issue for me:

(require '[minimallist.helper :as h])
(require '[clojure.test.check.generators :as gen])

(-> (h/map)
      (h/with-optional-entries [:str (-> (h/fn string?)
                                         (h/with-test-check-gen gen/string-ascii))])
      (h/with-entries [:int (-> (h/fn int?)
                                (h/with-test-check-gen gen/nat))])
      mg/gen
      (gen/sample 20))
A few samples show plenty of results that have the :str key without the :int key. What I expect (at least sometimes) to see is the reverse: maps that have the :int key but not the :str key. But I don't:
(-> (h/map)
      (h/with-optional-entries [:str (-> (h/fn string?)
                                         (h/with-test-check-gen gen/string-ascii))])
      (h/with-entries [:int (-> (h/fn int?)
                                (h/with-test-check-gen gen/nat))])
      mg/gen
      (gen/sample 2000)
      (#(filter (fn [m] (and (contains? m :int)
                             (not (contains? m :str)))) %)))
This always returns an empty list, regardless of sample size. Am I specifying the model incorrectly?

respatialized20:11:51

https://github.com/green-coder/minimallist/blob/1469349924c766c3aeb033e8f7016f8429e4af69/src/minimallist/generator.cljc#L424 It looks like this may be part of the reason for why I'm seeing what I'm seeing. There don't appear to be any guarantees that all the required-entries appear in selected-entries after the collection is shuffled, so those entries may not show up in the collection built up by the call to mapcat at the end of this expression. If you like, I can add a test and work on a PR to try and add that guarantee - it looks like a relatively simple concat operation after the shuffle. Of course, this is my first time reading through the code, so it may not be as simple as I think. If there are other things I've missed or need to take into account here, please let me know.

Vincent Cantin10:11:22

You are right, there might be a problem in the logic of my implementation. I would gladly accept a PR if you want to contribute and fix this bug.