reifyhealth / specmonstah

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

Add dummy references for spec-gen #87

Closed mitchkyle-reify closed 1 year ago

mitchkyle-reify commented 1 year ago

Add the ability to generate non-nil values for references in spec-gen using :reifyhealth.specmonstah.core/dummy.

The use case I have for this is I have a table with a composite key primary key (id and version) and I need to be able to insert values into it that reference other row ids in same table but still generate the ids for ones that do not reference another row.

With this patch I satisfy my use case in the following way:

  (require '[clojure.spec.alpha :as s])
  (require '[reifyhealth.specmonstah
             [core :as sm]
             [spec-gen :as sg]])
  (s/def ::id uuid?)
  (s/def ::version int?)
  (s/def ::value string?)
  (s/def ::versioned-datum (s/keys :req-un [::id ::version ::value]))
  (sg/ent-db-spec-gen-attr {:schema
                            {:my-table {:spec      ::versioned-datum
                                        :relations {:id [:my-table :id]}
                                        :prefix    :mt}}}
                           {:my-table [[:first-version
                                        {:refs     {:id ::sm/dummy}
                                         :spec-gen {:version 1}}]
                                       [:second-version
                                        {:refs     {:id :first-version}
                                         :spec-gen {:version 2}}]]})

  =>
  {:first-version
   {:id #uuid "4a010cb7-bdfb-4f64-abfa-bb719eb16ca1",
    :version 1,
    :value "Jt3"},
   :second-version
   {:id #uuid "4a010cb7-bdfb-4f64-abfa-bb719eb16ca1",
    :version 2,
    :value "KA222fJFxdMuvv"}}

There might be a better way to solve this, I'm open to ideas.

mitchkyle-reify commented 1 year ago

An alternative to thinking of this as "dummy references" with ::sm/dummy we could think of them as "self references" and call the key ::sm/self. This wouldn't be a functional difference, just a semantic one.

flyingmachine commented 1 year ago

@mitchkyle-reify I wanted to check in on this, were you able to accomplish what you needed?

mitchkyle-reify commented 1 year ago

Yes, my use case is satisfied by fixing the bug with self references #88 thanks for checking in.