test-check

ag 2016-07-26T21:03:24.000032Z

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

metametadata 2016-07-26T21:18:12.000033Z

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

ag 2016-07-26T21:20:56.000034Z

@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

ag 2016-07-26T21:21:18.000035Z

thanks

1😀
ag 2016-07-26T21:38:12.000036Z

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

ag 2016-07-26T21:39:43.000037Z

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)

ag 2016-07-26T21:40:08.000038Z

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

ag 2016-07-26T21:46:26.000039Z

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

ag 2016-07-26T22:03:29.000040Z

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

ag 2016-07-26T22:04:53.000041Z

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

ag 2016-07-26T22:07:44.000042Z

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

ag 2016-07-26T22:29:27.000043Z

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 😞

2016-07-26T22:49:19.000045Z

um

2016-07-26T22:49:30.000046Z

I would do

2016-07-26T22:51:19.000047Z

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

2016-07-26T22:51:22.000048Z

@ag