Clojure.spec: 基于生成器中其他字段的字段存在

Clojure.spec: field existence based on other field in generator

假设我们有 API 将不同类型文件的属性保存到 DB* 中:文本、图像、音频和视频文件。它应该能够根据其类型获取以下字段:
所有文件的基本属性:

{"file-type": "text",
"location": "/Documents",
"creation-time": "2020-03-02",
"name": "sometext.txt"}

仅针对某些类型的附加道具:

text: only base props
video file: base props + {"duration(s)":123, "resolution":"4k"}
audio file: base props + {"duration(s)":123}
image: base props + {"resolution":"2048x1536"}

如我们所见,某些字段应该为 nil,具体取决于 "file-type"。 假设我们需要验证这种输入,它可以是所描述的任何类型。 所以规格是:

(s/def ::file-type (s/with-gen (s/and string? not-empty) #(s/gen #{"text" "image" "video" "audio"})))
(s/def ::location (s/with-gen (s/and string? not-empty) #(s/gen #{"/Documents"})))
(s/def ::creation-time (s/with-gen (s/and string? not-empty) #(s/gen #{"2020-03-02"}))) ;for simplicity
(s/def ::name (s/with-gen (s/and string? not-empty) #(s/gen #{"sometext.txt" "image.jpg" "video.mp4" "audio.mp3"}))) ;for simplicity
(s/def ::duration (s/and int? not-empty))                   ;for simplicity
(s/def ::resolution (s/with-gen (s/and string? not-empty) #(s/gen #{"4k" "2048x1536"}))) ;for simplicity
(s/def ::files-input (s/keys :req-un [::file-type ::location ::creation-time ::extension] :opt-un [::duration ::resolution]))

我们还假设有一个程序化验证器检查是否为每种文件类型传递了正确的字段集(例如,"video" 有 "duration" 字段,但 "text" 没有).
问题是:如何 生成 完整的现成 输入以及单元测试的那些依赖项 (包括依赖字段)?

*(让我们暂且不考虑它是否适合 API 的问题,因为这个例子并非来自现实生活,仅用于演示目的)

基于multi-spec doc,我们必须为每种情况添加具有自己的字段集的单独方法:

(s/def ::file-type (s/with-gen (s/and string? not-empty) #(s/gen #{"text" "image" "video" "audio"})))
(s/def ::location (s/with-gen (s/and string? not-empty) #(s/gen #{"/Documents"})))
(s/def ::creation-time (s/with-gen (s/and string? not-empty) #(s/gen #{"2020-03-02"}))) ;for simplicity
(s/def ::name (s/with-gen (s/and string? not-empty) #(s/gen #{"sometext.txt" "image.jpg" "video.mp4" "audio.mp3"}))) ;for simplicity
(s/def ::duration pos-int?)                                 ;for simplicity
(s/def ::resolution (s/with-gen (s/and string? not-empty) #(s/gen #{"4k" "2048x1536"}))) ;for simplicity
(s/def ::base-props (s/keys :req-un [::file-type ::location ::creation-time ::name]))
(s/def ::file-type-key (s/with-gen keyword? #(s/gen #{:text :image :video :audio})))

(defmulti file :file-type-key)
(defmethod file :text [_]
    (s/merge (s/keys :req-un [::file-type-key]) ::base-props))
(defmethod file :image [_]
    (s/merge (s/keys :req-un [::file-type-key ::resolution]) ::base-props))
(defmethod file :video [_]
    (s/merge (s/keys :req-un [::file-type-key ::duration ::resolution]) ::base-props))
(defmethod file :audio [_]
    (s/merge (s/keys :req-un [::file-type-key ::duration]) ::base-props))
(defmethod file :default [_]
    (s/merge (s/keys :req-un [::file-type-key]) ::base-props))

(s/def ::file-input (s/and (s/multi-spec file :file-type-key)
                           (fn [{:keys [file-type file-type-key]}]
                               (= file-type-key (keyword file-type)))))

将为单元测试提供生成的输入(可由stest/check检查):

(gen/sample (s/gen ::file-input) 2)
=>
({:file-type-key :text, :file-type "text", :location "/Documents", :creation-time "2020-03-02", :name "sometext.txt"}
 {:file-type-key :image,
  :resolution "4k",
  :file-type "image",
  :location "/Documents",
  :creation-time "2020-03-02",
  :name "sometext.txt"})

我们必须再添加一个字段 ::file-type-key 作为选择器(必须是关键字),但这不会影响测试并且可以 dissoc 轻松编辑。