reifyhealth / specmonstah

Specmonstah will eat the face off your test fixtures
MIT License
359 stars 18 forks source link

Collection relations don't reflect order of refs #60

Closed stevebuik closed 4 years ago

stevebuik commented 5 years ago

It's useful to declare relations from parent to child and query for children in a specific order. This does not generate the child ids in the order declared in the parent query.

I'm happy to close this if I've missed this in the docs or source somewhere.

If there's a workaround I'd love to hear it :) My current workaround is to post-process the entities before the visitor step, restoring the ids to the correct order by using the query.

Test case in the comment below...

stevebuik commented 5 years ago
(ns specmonstah-fiddle2
  (:require [reifyhealth.specmonstah.spec-gen :as sg]
            [reifyhealth.specmonstah.core :as sm]
            [clojure.spec.alpha :as s]))

(s/def ::id pos-int?)

(def schema {:todo-list {:prefix      :tl
                         :spec        (s/keys :req-un [::id])
                         :relations   {:todos [:todo :id]}
                         :constraints {:todos #{:coll}}}
             :todo      {:prefix :t
                         :spec   (s/keys :req-un [::id])}})

; this query expresses the desire to do the cleaning before the shopping
(def query {:todo-list [[:list/home {:refs {:todos [:todo/cleaning :todo/shopping]}}]]
            :todo      [[:todo/cleaning]
                        [:todo/shopping]]})

(let [db (sg/ent-db-spec-gen {:schema schema} query)
      list-ids (-> (sm/ent-attrs db :list/home) :spec-gen :todos)
      desired-ids (->> [:todo/cleaning :todo/shopping]
                       (map #(get-in (sm/ent-attrs db %) [:spec-gen :id])))]
  {:ids     list-ids
   :desired desired-ids
   ; the ids generated in the home list are in reverse order
   :equals  (= list-ids desired-ids)})

=> {:ids (268278428 37439), :desired (37439 268278428), :equals false}