What's the proper way to generate random emails for testing?
If you're using spec you could use a generator
<https://medium.com/appsflyer/clojure-automated-property-based-tests-for-complicated-inputs-786de23de8f0>
shows one such approach
<https://gist.github.com/conan/2edca210999b96ad26d38c1ee96dfe40>
another
Yes, I am trying to learn spec to use it : ) Would be a stretch that I am using it now : )
I see, so these are valid emails but they only generate a relatively small subset of possible email formats
@ashnur Check out https://github.com/miner/strgen , ignoring that it still uses clojure.spec
in it's examples, I think everything else should work.
I was just about to try test.chuck
or (shameless plug): Regal https://lambdaisland.github.io/regal-playground/
I tried out regal, and I like it, but I am not entirely sure I understand what is happening.I tried out regal, and I like it, but I am not entirely sure I understand what is happening.
that link is broken 😞
I am having an extremely frustrating experience while trying out test chuck. Even though the namespace is included, the example code doesn't work, methods like string-from-regexp are nil. I am not even sure in this situation where should I go to ask for help, probably #beginners ?
here's probably fine - can you share a snippet of what you're doing?
https://lambdaisland.github.io/land-of-regal/ looks to be the right link
% clj -Sdeps '{:deps {com.gfredericks/test.chuck {:mvn/version "0.2.10"}}}'
Clojure 1.10.1
user=> (require '[com.gfredericks.test.chuck.generators :as gen'])
nil
user=> (require '[clojure.test.check.generators :as gen])
nil
user=> (gen/sample (gen'/string-from-regex #"([☃-♥]{3}|B(A|OO)M)*"))
("" "" "BAMBOOM" "♛♜☖☐♢☴♏♡♍" "☹♍♈☈☠♇BOOM" "BAMBAM♥♞☬♗☒☄" "" "BOOM" "♤☺☵☗☃☻BAM☨♥♗♕♥☈BOOMBOOM♊♅♝" "☪☓☉☠☓☔BOOM")
so this only works with java I am guessing
https://gist.github.com/ashnur/3f5b7fd20d80a3831fe5a1a4c45bb55a
Probably because java regex is not the same as js regex. But then I am probably better off with one of the other options that were mentioned.
it should work with both
did you (require '[clojure.test.check.generators :as gen]) ?
test.chuck builds on top of test.check, so you need to include both of the generator namespaces
hmm
I did, but this gives me an idea
it seems from those errors that you have issues with both the gen and gen' namespaces
I need help with multi-spec
:
(def a #{1 2 3})
(def b #{4 5 6})
(s/def ::type #{:foo :bar})
;;; This is pseudocode
(if (= ::type :foo)
; I want ::new-sym to be defined as a
else (= ::type :bar)
; I want ::new-sym to be defined as b
; ::type and ::new-sym appear in the same map
I am pretty sure this is done with multi-spec
but I am stumped as to how to make it work...test.chuck's regex generator is jvm-only
ah, sorry
@hadilsabbagh18 s/multi-spec is for the case where you can look at the data and return different specs based on the data
what is your actual data?
in general, spec is designed primarily for the case where attributes always have the same spec so you are probably going against that grain a bit.
if the attribute is in a map, you could create (s/and (s/keys :req-un [::new-sym]) arbitrary-condition))
to tighten a more general spec
and then use an s/multi-spec to choose which of those applies
Thank you @alexmiller! Here is the actual snippet:
(s/def :budget-item/main-category #{"Household" "Personal" "Savings" "Investments" "Other"})
(defn budget-categories
[main-category]
(into #{}
(mapv first
(d/q '[:find ?cat :in $ ?main :where
[?e :expense/budget-category ?main]
[?e :expense/categories ?cat]] (db) main-category))))
(defmulti ordered-categories :budget-item/main-category)
(defmethod ordered-categories "Household" [_]
(budget-categories "Household"))
(defmethod ordered-categories "Personal" [_]
(budget-categories "Personal"))
(defmethod ordered-categories "Savings" [_]
(budget-categories "Savings"))
(defmethod ordered-categories "Investments" [_]
(budget-categories "Investments"))
(defmethod ordered-categories "Other" [_]
(budget-categories "Other"))
(s/def :budget-item/ordered-categories (s/multi-spec ordered-categories ,,,))
I want to make :budget-item/ordered-categories
dependent on what :budget-item/main-category
is set to. They will be in a map together...yeah, I don't think you should do this in a spec
or just make it a valid-category?
predicate
just spec ordered-categories as a string?
or keyword?
or whatever, then s/and the whole map predicate with a custom predicate that validates that extracts ordered-categories and main-category, and validates that one is valid according to the other
Ok, I'll do that. Thanks a lot for your help @alexmiller! I am really grateful.
dependent stuff is hard to do well - it's easiest to just specify that constraint at the level that contains all of the data