Skip to content

Commit

Permalink
test: Add tests to show multi usage
Browse files Browse the repository at this point in the history
Wasn't able to see examples of this in the docs or tests, so figured it
might be useful to make this capability more discoverable
  • Loading branch information
tomjkidd committed Sep 27, 2024
1 parent bc80f3f commit 74969bc
Showing 1 changed file with 69 additions and 2 deletions.
71 changes: 69 additions & 2 deletions test/malli/util_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,25 @@
(is (true? (m/validate
(mu/update schema :b identity)
{:a 1
:c "a string"}))))))
:c "a string"}))))
(testing "Multi schema can update its individual dispatch schemas"
(let [multi-schema (m/schema [:multi {:dispatch :type}
[:sized [:map
[:type keyword?]
[:size int?]]]
[:human [:map
[:type keyword?]
[:name string?]
[:address [:map [:country keyword?]]]]]])]
(is (mu/equals (-> multi-schema
(mu/update :sized mu/select-keys [:size])
(mu/update :human mu/select-keys [:name :address]))
[:multi {:dispatch :type}
[:sized [:map
[:size int?]]]
[:human [:map
[:name string?]
[:address [:map [:country keyword?]]]]]]))))))

(deftest assoc-in-test
(is (mu/equals (mu/assoc-in (m/schema [:vector int?]) [0] string?) [:vector string?]))
Expand All @@ -523,7 +541,56 @@
(is (mu/equals (mu/assoc-in (m/schema [:map]) [:a :b :c :d] int?)
[:map [:a [:map [:b [:map [:c [:map [:d int?]]]]]]]]))
(is (mu/equals (mu/assoc-in (m/schema [:ref {:registry {::a int?, ::b string?}} ::a]) [0] ::b) [:ref {:registry {::a int?, ::b string?}} ::b]))
(is (mu/equals (mu/assoc-in (m/schema [:schema int?]) [0] string?) [:schema string?])))
(is (mu/equals (mu/assoc-in (m/schema [:schema int?]) [0] string?) [:schema string?]))

(testing "Multi schema can use assoc-in to introduce new dispatch schemas"
(is (mu/equals (-> (m/schema [:multi {:dispatch :type}
[:sized [:map
[:type keyword?]
[:size int?]]]
[:human [:map
[:type keyword?]
[:name string?]
[:address [:map [:country keyword?]]]]]])
(mu/assoc-in [:robot] [:map
[:type keyword?]
[:serial-number string?]
[:charging-station [:map [:id string?]]]]))
[:multi {:dispatch :type}
[:sized [:map
[:type keyword?]
[:size int?]]]
[:human [:map
[:type keyword?]
[:name string?]
[:address [:map [:country keyword?]]]]]
[:robot [:map
[:type keyword?]
[:serial-number string?]
[:charging-station [:map [:id string?]]]]]])))
(testing "Multi schema can use assoc-in to extend existing schemas"
(let [multi-schema (m/schema [:multi {:dispatch :type}
[:sized [:map
[:type keyword?]
[:size int?]]]
[:human [:map
[:type keyword?]
[:name string?]
[:address [:map [:country keyword?]]]]]])]
(is (mu/equals (-> multi-schema
(mu/assoc-in [:sized :unit] string?)
(mu/assoc-in [:human :address :state] string?))
[:multi {:dispatch :type}
[:sized [:map
[:type keyword?]
[:size int?]
[:unit string?]]]
[:human [:map
[:type keyword?]
[:name string?]
[:address [:map
[:country keyword?]
[:state string?]]]]]])))))

(deftest update-in-test
(is (mu/equals (mu/update-in (m/schema [:vector int?]) [0] (constantly string?)) [:vector string?]))
Expand Down

0 comments on commit 74969bc

Please sign in to comment.