spec - s/gen validation error

114 views
Skip to first unread message

Sven Richter

unread,
May 30, 2016, 1:38:19 AM5/30/16
to Clojure
Hi,

So I looked at generating specs for tests and I hit an error I just dont understand / neither can fix it. This is my code:

(s/def ::column-types #{:int :varchar :boolean :text :time :date
                        :char :binary :smallint :bigint :decimal
                        :float :double :real :timestamp})

(s/def ::name (s/and string? #(not= "" %)))
(s/def ::boolean (s/or :t true? :f false?))

(s/def ::type ::column-types)
(s/def ::null ::boolean)
(s/def ::max-length (s/and integer? #(< 0 %)))
(s/def ::required ::boolean)
(s/def ::pk ::boolean)
(s/def ::autoinc ::boolean)
(s/def ::unique ::boolean)
(s/def ::default (s/or :string string? :boolean ::boolean))
(s/def ::refs ::none-empty-string)
(s/def ::fk-name ::none-empty-string)

(s/def ::column (s/keys :req-un [::name ::type]
                        :opt-un [::null ::max-length ::required ::pk ::autoinc ::unique ::default
                                 ::refs ::fk-name]))
(s/def ::columns (s/cat :column (s/+ ::column)))

;function
(s/fdef remove-autoinc-columns :args (s/cat :cols (s/spec columns))
        :ret ::schem/columns)
(defn remove-autoinc-columns [cols]
  (vec (remove #(= true (:autoinc %)) cols)))
 
 
   
; generative test
(defspec remove-autoinc-column 100
         (prop/for-all [columns (s/gen ::schem/columns)]
                       (= (h/remove-autoinc-columns  columns))))
                      
; will fail with something along the lines of:
ERROR in (remove-autoinc-column) (core.clj:4617)
Uncaught exception, not in assertion.
expected: nil
  actual: clojure.lang.ExceptionInfo: Call to #'de.sveri.clospcrud.helper/remove-autoinc-columns did not conform to spec:
val: () fails spec: :de.sveri.clospcrud.spec.clospcrud/columns at: [:ret :column] predicate: :de.sveri.clospcrud.spec.clospcrud/column,  Insufficient input
:clojure.spec/args  (({:name "QpJ50qrS1m24V", :type :varchar, :unique true, :required false, :autoinc true, :null true, :pk true, :max-length 14, :fk-name "Tp9tG8hwUXK0"}))

I cannot see where the error is coming from, I generate data according to a spec which I pass to a function that takes this spec exactly.

Any ideas?

Thanks,
Sven

Sven Richter

unread,
May 30, 2016, 5:23:33 PM5/30/16
to Clojure
So I tried some more and figured out that its the ::autoinc key that causes this:
(s/def ::column (s/keys :req-un [::name ::type]
                        :opt-un [::null ::max-length
                                 ::required ::pk
                                 ::autoinc                            ;;;;;;;; this one
                                 ::unique ::default
                                 ::refs ::fk-name]))

When I comment that key, no error occurs, if I uncomment it, spec validation errors repeatedly.

The only connection I see is that I remove exactly this k/v pair from the map in my specced function:

(s/fdef remove-autoinc-columns :args (s/cat :cols (s/spec ::schem/columns))
        :ret ::schem/columns)
(defn remove-autoinc-columns [cols]
  (remove #(= true (:autoinc %)) cols))

But still, as it is an optional key I dont see why it should cause errors?

Thanks,
Sven
Reply all
Reply to author
Forward
0 new messages