Update swagger test

This commit is contained in:
Tommi Reiman 2020-05-16 14:08:08 +03:00
parent 5cd1cfa5e2
commit ddf56624b7

View file

@ -9,7 +9,8 @@
[reitit.coercion.schema :as schema] [reitit.coercion.schema :as schema]
[schema.core :refer [Int]] [schema.core :refer [Int]]
[muuntaja.core :as m] [muuntaja.core :as m]
[spec-tools.data-spec :as ds])) [spec-tools.data-spec :as ds]
[schema.core :as s]))
(def app (def app
(ring/ring-handler (ring/ring-handler
@ -79,40 +80,147 @@
500 {:description "fail"}} 500 {:description "fail"}}
:handler (fn [{{{:keys [x y]} :query :handler (fn [{{{:keys [x y]} :query
{:keys [z]} :path} :parameters}] {:keys [z]} :path} :parameters}]
{:status 200, :body {:total (+ x y z)}})}}]]] {:status 200, :body {:total (+ x y z)}})}
:post {:summary "plus with body"
:parameters {:body (s/maybe [s/Int])
:path {:z s/Int}}
:swagger {:responses {400 {:schema {:type "string"}
:description "kosh"}}}
:responses {200 {:body {:total s/Int}}
500 {:description "fail"}}
:handler (fn [{{{:keys [z]} :path
xs :body} :parameters}]
{:status 200, :body {:total (+ (reduce + xs) z)}})}}]]]
{:data {:middleware [swagger/swagger-feature {:data {:middleware [swagger/swagger-feature
rrc/coerce-exceptions-middleware rrc/coerce-exceptions-middleware
rrc/coerce-request-middleware rrc/coerce-request-middleware
rrc/coerce-response-middleware]}}))) rrc/coerce-response-middleware]}})))
(require '[fipp.edn])
(deftest swagger-test (deftest swagger-test
(testing "endpoints work" (testing "endpoints work"
(testing "spec" (testing "spec"
(is (= {:body {:total 6}, :status 200} (is (= {:body {:total 6}, :status 200}
(app (app {:request-method :get
{:request-method :get
:uri "/api/spec/plus/3" :uri "/api/spec/plus/3"
:query-params {:x "2", :y "1"}}))) :query-params {:x "2", :y "1"}})))
(is (= {:body {:total 7}, :status 200} (is (= {:body {:total 7}, :status 200}
(app (app {:request-method :post
{:request-method :post
:uri "/api/spec/plus/3" :uri "/api/spec/plus/3"
:body-params [1 3]})))) :body-params [1 3]}))))
(testing "schema" (testing "schema"
(is (= {:body {:total 6}, :status 200} (is (= {:body {:total 6}, :status 200}
(app (app {:request-method :get
{:request-method :get
:uri "/api/schema/plus/3" :uri "/api/schema/plus/3"
:query-params {:x "2", :y "1"}}))))) :query-params {:x "2", :y "1"}})))))
(testing "swagger-spec" (testing "swagger-spec"
(let [spec (:body (app (let [spec (:body (app {:request-method :get
{:request-method :get
:uri "/api/swagger.json"})) :uri "/api/swagger.json"}))
expected {:x-id #{::math} expected {:x-id #{::math}
:swagger "2.0" :swagger "2.0"
:info {:title "my-api"} :info {:title "my-api"}
:paths {"/api/schema/plus/{z}" {:get {:parameters [{:description "" :paths {"/api/spec/plus/{z}" {:get {:parameters [{:in "query"
:name "x"
:description ""
:required true
:type "integer"
:format "int64"}
{:in "query"
:name "y"
:description ""
:required true
:type "integer"
:format "int64"}
{:in "path"
:name "z"
:description ""
:required true
:type "integer"
:format "int64"}]
:responses {200 {:description ""
:schema {:type "object"
:properties {"total" {:format "int64"
:type "integer"}}
:required ["total"]}}
400 {:schema {:type "string"}
:description "kosh"}
500 {:description "fail"}}
:summary "plus"}
:post {:parameters [{:in "body",
:name "",
:description "",
:required false,
:schema {:type "array",
:items {:type "integer",
:format "int64"}
:x-nullable true}}
{:in "path"
:name "z"
:description ""
:type "integer"
:required true
:format "int64"}]
:responses {200 {:schema {:properties {"total" {:format "int64"
:type "integer"}}
:required ["total"]
:type "object"}
:description ""}
400 {:schema {:type "string"}
:description "kosh"}
500 {:description "fail"}}
:summary "plus with body"}}
"/api/malli/plus/{z}" {:get {:parameters [{:in "query"
:name :x
:description ""
:required true
:type "integer"
:format "int64"}
{:in "query"
:name :y
:description ""
:required true
:type "integer"
:format "int64"}
{:in "path"
:name :z
:description ""
:required true
:type "integer"
:format "int64"}]
:responses {200 {:schema {:type "object"
:properties {:total {:format "int64"
:type "integer"}}
:required [:total]}
:description ""}
400 {:schema {:type "string"}
:description "kosh"}
500 {:description "fail"}}
:summary "plus"}
:post {:parameters [{:in "body",
:name "body",
:description "",
:required false,
:schema {:type "array",
:items {:type "integer",
:format "int64"}
:x-nullable true}}
{:in "path"
:name :z
:description ""
:type "integer"
:required true
:format "int64"}]
:responses {200 {:description ""
:schema {:properties {:total {:format "int64"
:type "integer"}}
:required [:total]
:type "object"}}
400 {:schema {:type "string"}
:description "kosh"}
500 {:description "fail"}}
:summary "plus with body"}}
"/api/schema/plus/{z}" {:get {:parameters [{:description ""
:format "int32" :format "int32"
:in "query" :in "query"
:name "x" :name "x"
@ -139,101 +247,25 @@
400 {:schema {:type "string"} 400 {:schema {:type "string"}
:description "kosh"} :description "kosh"}
500 {:description "fail"}} 500 {:description "fail"}}
:summary "plus"}}
"/api/malli/plus/{z}" {:get {:parameters [{:description ""
:format "int64"
:in "query"
:name :x
:required true
:type "integer"}
{:description ""
:format "int64"
:in "query"
:name :y
:required true
:type "integer"}
{:in "path"
:name :z
:description ""
:type "integer"
:required true
:format "int64"}]
:responses {200 {:description ""
:schema {:properties {:total {:format "int64"
:type "integer"}}
:required [:total]
:type "object"}}
400 {:schema {:type "string"}
:description "kosh"}
500 {:description "fail"}}
:summary "plus"} :summary "plus"}
:post {:parameters [{:in "body", :post {:parameters [{:in "body",
:name "", :name "body",
:description "", :description "",
:required false, :required false,
:schema {:type "array", :schema {:type "array",
:items {:type "integer", :items {:type "integer",
:format "int64"} :format "int32"}
:x-nullable true}}
{:in "path"
:name :z
:description ""
:type "integer"
:required true
:format "int64"}]
:responses {200 {:description ""
:schema {:properties {:total {:format "int64"
:type "integer"}}
:required [:total]
:type "object"}}
400 {:schema {:type "string"}
:description "kosh"}
500 {:description "fail"}}
:summary "plus with body"}}
"/api/spec/plus/{z}" {:get {:parameters [{:description ""
:format "int64"
:in "query"
:name "x"
:required true
:type "integer"}
{:description ""
:format "int64"
:in "query"
:name "y"
:required true
:type "integer"}
{:in "path"
:name "z"
:description ""
:type "integer"
:required true
:format "int64"}]
:responses {200 {:description ""
:schema {:properties {"total" {:format "int64"
:type "integer"}}
:required ["total"]
:type "object"}}
400 {:schema {:type "string"}
:description "kosh"}
500 {:description "fail"}}
:summary "plus"}
:post {:parameters [{:in "body",
:name "",
:description "",
:required false,
:schema {:type "array",
:items {:type "integer",
:format "int64"}
:x-nullable true}} :x-nullable true}}
{:in "path" {:in "path"
:name "z" :name "z"
:description "" :description ""
:type "integer" :type "integer"
:required true :required true
:format "int64"}] :format "int32"}]
:responses {200 {:description "" :responses {200 {:description ""
:schema {:properties {"total" {:format "int64" :schema {:properties {"total" {:format "int32"
:type "integer"}} :type "integer"}}
:additionalProperties false
:required ["total"] :required ["total"]
:type "object"}} :type "object"}}
400 {:schema {:type "string"} 400 {:schema {:type "string"}