git.iko.soy/typeable/swagger2
on master help sign in
Add test for Operation object
commit54752635a27e25f7377661d5d1de211b8b2d5f68 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateSun 8 Nov 2015, 00:03 (11 years ago) parentcd18970
2 files changed+161
swagger2.cabal+2 test/Data/SwaggerSpec.hs+159
swagger2.cabal+2
@@ -43,6 +43,8 @@ test-suite spec
4343 , text
4444 , aeson
4545 , aeson-qq
46+ , unordered-containers
47+ , vector
4648 other-modules:
4749 Data.SwaggerSpec
4850 default-language: Haskell2010
test/Data/SwaggerSpec.hs+159
@@ -1,14 +1,34 @@
1+{-# LANGUAGE OverloadedLists #-}
12{-# LANGUAGE OverloadedStrings #-}
23{-# LANGUAGE QuasiQuotes #-}
34module Data.SwaggerSpec where
45
56import Data.Aeson
67import Data.Aeson.QQ
8+import qualified Data.HashMap.Strict as HashMap
9+import Data.Maybe
10+import qualified Data.Vector as Vector
11+
712import Data.Swagger
813
914import Test.Hspec
1015import Test.Hspec.HUnit
1116
17+isSubJSON :: Value -> Value -> Bool
18+isSubJSON Null _ = True
19+isSubJSON (Object x) (Object y) = HashMap.keys x == HashMap.keys i && and i
20+ where
21+ i = HashMap.intersectionWith isSubJSON x y
22+isSubJSON (Array xs) (Array ys) = Vector.length xs == Vector.length ys && and (Vector.zipWith isSubJSON xs ys)
23+isSubJSON x y = x == y
24+
25+(<~>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec
26+x <~> json = do
27+ it "encodes correctly (probably with extra properties)" $ do
28+ toJSON x `shouldSatisfy` (json `isSubJSON`)
29+ it "decodes correctly" $ do
30+ fromJSON json `shouldBe` Success x
31+
1232(<=>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec
1333x <=> json = do
1434 it "encodes correctly" $ do
@@ -18,6 +38,7 @@ x <=> json = do
1838
1939spec :: Spec
2040spec = do
41+ describe "Operation Object" $ operationExample <~> operationExampleJSON
2142 describe "License Object" $ licenseExample <=> licenseExampleJSON
2243 describe "Contact Object" $ contactExample <=> contactExampleJSON
2344 describe "Info Object" $ infoExample <=> infoExampleJSON
@@ -96,3 +117,141 @@ licenseExampleJSON = [aesonQQ|
96117 "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
97118}
98119|]
120+
121+
122+-- =======================================================================
123+-- Operation object
124+-- =======================================================================
125+
126+operationExample :: SwaggerOperation
127+operationExample = SwaggerOperation
128+ { swaggerOperationTags = ["pet"]
129+ , swaggerOperationSummary = Just "Updates a pet in the store with form data"
130+ , swaggerOperationDescription = Just ""
131+ , swaggerOperationOperationId = Just "updatePetWithForm"
132+ , swaggerOperationConsumes = Just (SwaggerMimeList ["application/x-www-form-urlencoded"])
133+ , swaggerOperationProduces = Just (SwaggerMimeList ["application/json", "application/xml"])
134+ , swaggerOperationParameters = params
135+ , swaggerOperationResponses = responses
136+ , swaggerOperationSecurity = security
137+ , swaggerOperationExternalDocs = Nothing
138+ , swaggerOperationSchemes = Nothing
139+ , swaggerOperationDeprecated = False
140+ }
141+ where
142+ security = [SwaggerSecurityRequirement [("petstore_auth", ["write:pets", "read:pets"])]]
143+
144+ responses = SwaggerResponses
145+ { swaggerResponsesDefault = Nothing
146+ , swaggerResponsesResponses =
147+ [ (200, SwaggerResponse
148+ { swaggerResponseDescription = "Pet updated."
149+ , swaggerResponseSchema = Nothing
150+ , swaggerResponseHeaders = mempty
151+ , swaggerResponseExamples = Nothing } )
152+ , (405, SwaggerResponse
153+ { swaggerResponseDescription = "Invalid input"
154+ , swaggerResponseSchema = Nothing
155+ , swaggerResponseHeaders = mempty
156+ , swaggerResponseExamples = Nothing } ) ] }
157+
158+ params =
159+ [ SwaggerParameter
160+ { swaggerParameterName = "petId"
161+ , swaggerParameterDescription = Just "ID of pet that needs to be updated"
162+ , swaggerParameterRequired = True
163+ , swaggerParameterSchema = SwaggerParameterOther (stringSchema SwaggerParameterPath) }
164+ , SwaggerParameter
165+ { swaggerParameterName = "name"
166+ , swaggerParameterDescription = Just "Updated name of the pet"
167+ , swaggerParameterRequired = False
168+ , swaggerParameterSchema = SwaggerParameterOther (stringSchema SwaggerParameterFormData) }
169+ , SwaggerParameter
170+ { swaggerParameterName = "status"
171+ , swaggerParameterDescription = Just "Updated status of the pet"
172+ , swaggerParameterRequired = False
173+ , swaggerParameterSchema = SwaggerParameterOther (stringSchema SwaggerParameterFormData) }
174+ ]
175+
176+ stringSchema i = SwaggerParameterOtherSchema
177+ { swaggerParameterOtherSchemaIn = i
178+ , swaggerParameterOtherSchemaType = SwaggerParamString
179+ , swaggerParameterOtherSchemaFormat = Nothing
180+ , swaggerParameterOtherSchemaAllowEmptyValue = False
181+ , swaggerParameterOtherSchemaItems = Nothing
182+ , swaggerParameterOtherSchemaCollectionFormat = Nothing
183+ , swaggerParameterOtherSchemaCommon = emptySchemaCommon
184+ }
185+
186+ emptySchemaCommon = SwaggerSchemaCommon
187+ { swaggerSchemaDefault = Nothing
188+ , swaggerSchemaMaximum = Nothing
189+ , swaggerSchemaExclusiveMaximum = Nothing
190+ , swaggerSchemaMinimum = Nothing
191+ , swaggerSchemaExclusiveMinimum = Nothing
192+ , swaggerSchemaMaxLength = Nothing
193+ , swaggerSchemaMinLength = Nothing
194+ , swaggerSchemaPattern = Nothing
195+ , swaggerSchemaMaxItems = Nothing
196+ , swaggerSchemaMinItems = Nothing
197+ , swaggerSchemaUniqueItems = Nothing
198+ , swaggerSchemaEnum = Nothing
199+ , swaggerSchemaMultipleOf = Nothing }
200+
201+operationExampleJSON :: Value
202+operationExampleJSON = [aesonQQ|
203+{
204+ "tags": [
205+ "pet"
206+ ],
207+ "summary": "Updates a pet in the store with form data",
208+ "description": "",
209+ "operationId": "updatePetWithForm",
210+ "consumes": [
211+ "application/x-www-form-urlencoded"
212+ ],
213+ "produces": [
214+ "application/json",
215+ "application/xml"
216+ ],
217+ "parameters": [
218+ {
219+ "name": "petId",
220+ "in": "path",
221+ "description": "ID of pet that needs to be updated",
222+ "required": true,
223+ "type": "string"
224+ },
225+ {
226+ "name": "name",
227+ "in": "formData",
228+ "description": "Updated name of the pet",
229+ "required": false,
230+ "type": "string"
231+ },
232+ {
233+ "name": "status",
234+ "in": "formData",
235+ "description": "Updated status of the pet",
236+ "required": false,
237+ "type": "string"
238+ }
239+ ],
240+ "responses": {
241+ "200": {
242+ "description": "Pet updated."
243+ },
244+ "405": {
245+ "description": "Invalid input"
246+ }
247+ },
248+ "security": [
249+ {
250+ "petstore_auth": [
251+ "write:pets",
252+ "read:pets"
253+ ]
254+ }
255+ ]
256+}
257+|]