-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_test.clj
220 lines (197 loc) · 11.6 KB
/
core_test.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
(ns malli-select.core-test
(:require
[clojure.pprint :refer [pprint]]
[clojure.test :as t :refer [deftest is testing]]
[malli-select.core :as sut :refer [select selector]]
[malli.core :as m]
[malli.util :as mu]))
(defonce ^:private ^:dynamic
*schema* nil)
(defonce ^:private ^:dynamic
*selector* nil)
(defn pps [o]
(with-out-str (pprint o)))
(defmacro expect-selection-to-validate [sel & data+maybe-reason]
`(if ~sel
(let [data# ~(first data+maybe-reason)
sel-schema# (if *selector* (*selector* ~sel) (select *schema* ~sel (meta ~sel)))
result# (or (m/validate sel-schema# data#) (m/explain sel-schema# data#))]
(is (true? result#)
(cond-> (str "Expected data:\n" (pps data#) "to be valid given schema:\n" (pps (m/form sel-schema#)))
~(second data+maybe-reason) (str "because:\n" ~(second data+maybe-reason))
:always (str "\nvalidate errors:\n" (pps (:errors result#))))))
*schema*))
(defmacro expect-selection-to-invalidate [sel & data+maybe-reason]
`(if ~sel
(let [data# ~(first data+maybe-reason)
sel-schema# (if *selector* (*selector* ~sel) (select *schema* ~sel (meta ~sel)))]
(is (false? (m/validate sel-schema# data#))
(cond-> (str "Expected data:\n" (pps data#) "to be *invalid* given schema:\n" (pps (m/form sel-schema#)))
~(second data+maybe-reason) (str "because:\n" ~(second data+maybe-reason)))))
*schema*))
(deftest select-test
(testing "common selections"
(let [S1 [:map
[:name string?]
[:handle string?]
[:address [:maybe [:map
[:street string?]
[:zip int?]
[:country [:map-of
string? [:map
[:iso string?] [:name string?]]]]]]]
[:roles [:set [:map
[:name string?]]]]]]
(testing "all optional"
(binding [*schema* S1]
(expect-selection-to-validate [] {})
(expect-selection-to-validate [] {:address {:country {}}})
(expect-selection-to-invalidate [] {:address {:country {:dk {}}}}
"types should still match")))
(testing "root attributes"
(binding [*schema* S1]
(expect-selection-to-invalidate [:name] {})
(expect-selection-to-validate [:name] {:name "Gert"})
(expect-selection-to-invalidate [:handle :name] {:name "Gert"})
(expect-selection-to-validate [:name :handle] {:name "Gert" :handle "eval"})
(expect-selection-to-invalidate [:address] {}
":address required")
(expect-selection-to-validate [:address] {:address nil}
":address key provided - nothing needed from address"))
;; wrapped
(binding [*schema* [:maybe [:vector S1]]]
(expect-selection-to-validate nil nil "normal malli behavior")
(expect-selection-to-validate nil [{}] "normal malli behavior")
(expect-selection-to-invalidate [:name] [{}])
(expect-selection-to-validate [:name] [{:name "Gert"}])
(expect-selection-to-invalidate [:handle :name] [{:name "Gert"}])
(expect-selection-to-validate [:name :handle] [{:name "Gert" :handle "eval"}])))
(testing "nested attributes"
(binding [*schema* S1]
(expect-selection-to-validate [{:address [:street]}] {} ":address not provided")
(expect-selection-to-invalidate [{:address [:street]}] {:address {}})
(expect-selection-to-invalidate [:address {:address [:street]}] {})
(expect-selection-to-validate [:address {:address [:street]}] {:address {:street "Main"}}
"the root :address should not interfere with the nested selection")
(expect-selection-to-validate [{:address [:street]} :address] {:address {:street "Main"}}
"order of items should not matter here")
;; star selection
(expect-selection-to-validate [{:address ['*]}] {} ":address not provided")
(expect-selection-to-invalidate [{:address ['*]}] {:address {:street "Main"}})
(expect-selection-to-validate [{:address ['*]}] {:address {:street "Main"
:zip 1234
:country {}}}
"all keys of address-map should be required - contents of country optional!")
(expect-selection-to-validate [{:address ['* {:country []}]}]
{:address {:street "Main"
:zip 1234
:country {}}}
"override star-selection: country-content is optional")
;; selecting through a set
(expect-selection-to-validate [{:roles [:name]}] {:roles #{}} "no role provided")
(expect-selection-to-invalidate [{:roles [:name]}] {:roles #{{}}})
(expect-selection-to-validate [{:roles [:name]}] {:roles #{{:name "Admin"}}})
(expect-selection-to-validate [{:address [:street]} {:address [:zip]}] {:address {:zip 1234}}
"last selection of :address wins")
(expect-selection-to-invalidate [{:address [:street] :roles [:name]}
{:address [:zip]}]
{:address {:zip 1234} :roles [{}]}
"selection maps are merged")
(expect-selection-to-validate [{:address [:street] :roles [:name]}
{:address [:zip]}]
{:address {:zip 1234} :roles #{{:name "Admin"}}}
"[:address :street] is overridden by [:address :zip]")
(expect-selection-to-validate [{:address [{:country [:name]}]}]
{:address {:country {"DK" {:name "Denmark"}}}}
"multi-level nesting")))))
(testing "schema with refs"
(let [S1 [:schema {:registry {"Other" [:map
[:other boolean?]]}}
[:map
[:this boolean?]
[:that "Other"]]]]
(testing "marking all optional"
(binding [*schema* S1]
(expect-selection-to-validate [] {})
(expect-selection-to-validate [] {:that {}} "it should walk refs")))
(testing "star selections"
(binding [*schema* (select S1 [])] ;; ensure all optional
(expect-selection-to-invalidate ['*] {})
(expect-selection-to-validate ['*] {:this true :that {}})
(expect-selection-to-invalidate [{:that ['*]}] {:that {}}
"it should walk refs and require all keys from Other")
(expect-selection-to-validate [{:that ['*]}] {:that {:other true}}
"it has all keys of Other-schema")))))
(testing "options"
(testing "prune-optionals"
(testing "common"
(binding [*schema* [:map
[:name string?]
[:age int?]
[:addresses [:maybe [:vector [:map
[:street string?]
[:zip int?]]]]]]]
(expect-selection-to-validate ^:only [:name]
{:name "Gert" :age "N/A" :addresses 1}
":age can now be anything as it's no longer part of the schema")
(expect-selection-to-invalidate ^:only [{:addresses [:street]}]
{:addresses [{}]}
"optional :addresses doesn't trigger a deep prune")
(expect-selection-to-validate ^:only [:addresses {:addresses [:street]}]
{:addresses [{:street "Main"}]})))
(testing "optionals with aggregates"
(binding [*schema* [:maybe [:map
[:address [:map [:street string?]]]
[:friends [:maybe [:vector [:map [:name string?]]]]]
[:countries [:map-of string? [:map [:name string?]]]]]]]
(expect-selection-to-validate ^:only []
{:countries 1}
":countries is no longer part of schema")
(expect-selection-to-invalidate ^:only [:countries]
{:countries 1}
":countries should require a map-of string map")
(expect-selection-to-invalidate ^:only [:countries]
{:countries {"foo" 1}}
":countries should require a map-of string map")
(expect-selection-to-validate ^:only [:countries]
{:countries {"foo" {}}}
":countries is no longer part of the schema")
(expect-selection-to-validate ^:only [:friends]
{:friends [{}]}
":friends requires just a vector of maps")
(expect-selection-to-invalidate ^:only [:friends]
{:friends [1]}
":friends requires just a vector of maps")))
(testing "schema with refs"
(binding [*schema* [:schema {:registry {"Other" [:map
[:other boolean?]]}}
[:map
[:this boolean?]
[:that "Other"]]]]
(expect-selection-to-validate ^:only []
{:that {:other "?"}}
":other can be a string as it should no longer be part of the schema"))))
(testing "verify-selection"
(is (thrown-with-msg? AssertionError #"unknown paths: \(\[:a\]\)"
(select int? [:a])))
(testing "disabling it"
(is (some? (select int? [:a] {:verify-selection :skip})))
(is (some? (select int? [:a] {:verify-selection nil})))))))
(deftest selector-test
(binding [*selector* (selector [:map
[:name string?]
[:age int?]
[:addresses [:maybe [:vector [:map
[:street string?]
[:zip int?]]]]]])]
(expect-selection-to-validate [:name]
{:name "Foo"}
"All but :name optional")
(expect-selection-to-invalidate [:name]
{:name "Foo" :age "NaN"}
"All but :name optional")))
(comment
(select [:map [:address [:map [:street string?]]]] [{:address [:street]}] {:prune-optionals true})
(mu/update-properties :map assoc :closed true)
(m/validate [:map {:closed true}] {})
)