Fork me on GitHub
#test-check
<
2016-07-26
>
ag21:07:24

can someone help me with creating a generator, that generates a map that contains keys like this

{
 :amount-debit  (gen/pos-int)
 :amount-credit (gen/pos-int)
}
The problem is it has to be either debit or credit, and never both. And always one of them should have a value, they can't be null both at the same time

metametadata21:07:12

@ag like this?

(def gen-card (gen/let
                [card-type (gen/elements [:amount-credit :amount-debit])
                 value gen/s-pos-int]
                (hash-map card-type value)))

(println "Sample:" (gen/sample gen-card 5))
;; Sample: ({:amount-debit 1} {:amount-debit 1} {:amount-credit 1} {:amount-debit 3} {:amount-credit 2})

ag21:07:56

@metametadata: hmmm interesting, my current code is slightly different, but I see what you’re suggesting. I’ll try to see if I can use this style without having to refactor a lot

ag21:07:12

Although it should generate a map with both debit and credit, yet only one of them should have a value

ag21:07:43

I started with gen/let where I’m using is-debit gen/boolean and then I think I want to try to use something like :amount-debit (g/such-that (fn [_] (= is-debit true)) g/pos-int)

ag21:07:08

but this fails with Couldn’t satisfy such-that predicate after 10 tries

ag21:07:26

ah nevermind using regular if fork with pos-int and return 0 seems worked

ag22:07:29

eh, actually it didn’t, I need a need to have a let block scoped insid gen/hash-map block

ag22:07:53

or I have to assoc two gen/hash-maps somehow

ag22:07:44

is there a way to “merge” multiple hash-map generators?

ag22:07:27

so this:

(g/let [debit? g/boolean]
  (g/hash-map
    :created-at            (timestamp-gen)
    :amount-debit          (g/fmap #(when debit? %) g/pos-int)
    :amount-credit         (g/fmap #(when-not debit? %) g/pos-int)))
does not work 😞

gfredericks22:07:19

(g/fmap (fn [[amount key created-at]] (assoc {:amount-debit nil :amount-credit nil :created-at created-at} key amount)) (g/tuple g/pos-int (g/elements [:amount-debit :amount-credit]) (timestamp-gen)))