Fork me on GitHub
#clojure-spec
<
2018-07-03
>
fominok18:07:40

Hi there! I suppose this is a right place for questions and I have one: is it possible to have references with spec generators? Like this: I have an author spec and a book spec, book has an author_id, can I generate one author and n books with proper id?

noisesmith18:07:12

I think gen/fmap could do that

taylor20:07:17

@fominok you could also use test.check's let macro like this:

(s/def ::author-id uuid?)
(s/def ::name string?)
(s/def ::author (s/keys :req-un [::author-id ::name]))
(s/def ::book (s/keys :req-un [::author-id ::name]))
(s/def ::books (s/coll-of ::book))

(gen/sample
 (gen/let [author (s/gen ::author)
           books  (s/gen ::books)]
   {:author author
    :books  (map #(assoc % :author-id (:author-id author))
                 books)}))
which is generating a map of author + books from their individual specs, and just associng the author's ID over each book