git.iko.soy/typeable/swagger2
on master help sign in
Tests' changes

- make checkDefs stricter (tests) - add multiple-fields tests

commit2a46257cb1e5fc548cdfbb6212a0b0b3cf82d2ac authorOleg Grenrus <oleg.grenrus@iki.fi> dateFri 4 Mar 2016, 14:17 (11 years ago) parente924625
3 files changed+54−7
src/Data/Swagger/Internal/Schema.hs+5−5 swagger2.cabal+1 test/Data/Swagger/SchemaSpec.hs+48−2
src/Data/Swagger/Internal/Schema.hs+5−5
@@ -567,8 +567,8 @@ gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts prox
567567
568568instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
569569 gdeclareNamedSchema opts _ schema = do
570− NamedSchema _ gschema <- gdeclareNamedSchema opts (Proxy :: Proxy g) schema
571− gdeclareNamedSchema opts (Proxy :: Proxy f) gschema
570+ NamedSchema _ gschema <- gdeclareNamedSchema opts (Proxy :: Proxy f) schema
571+ gdeclareNamedSchema opts (Proxy :: Proxy g) gschema
572572
573573instance (Datatype d, GToSchema f) => GToSchema (D1 d f) where
574574 gdeclareNamedSchema opts _ s = rename name <$> gdeclareNamedSchema opts (Proxy :: Proxy f) s
@@ -617,7 +617,7 @@ gdeclareSchemaRef opts proxy = do
617617
618618appendItem :: Referenced Schema -> Maybe (SwaggerItems Schema) -> Maybe (SwaggerItems Schema)
619619appendItem x Nothing = Just (SwaggerItemsArray [x])
620−appendItem x (Just (SwaggerItemsArray xs)) = Just (SwaggerItemsArray (x:xs))
620+appendItem x (Just (SwaggerItemsArray xs)) = Just (SwaggerItemsArray (xs ++ [x]))
621621appendItem _ _ = error "GToSchema.appendItem: cannot append to SwaggerItemsObject"
622622
623623withFieldSchema :: forall proxy s f. (Selector s, GToSchema f) =>
@@ -633,7 +633,7 @@ withFieldSchema opts _ isRequiredField schema = do
633633 & type_ .~ SwaggerObject
634634 & properties . at fname ?~ ref
635635 & if isRequiredField
636− then required %~ (fname :)
636+ then required %~ (++ [fname])
637637 else id
638638 where
639639 fname = T.pack (fieldLabelModifier opts (selName (Proxy3 :: Proxy3 s f p)))
@@ -673,7 +673,7 @@ class GSumToSchema f where
673673 gsumToSchema :: SchemaOptions -> proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
674674
675675instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
676− gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) <=< gsumToSchema opts (Proxy :: Proxy g)
676+ gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) >=> gsumToSchema opts (Proxy :: Proxy g)
677677
678678gsumConToSchemaWith :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
679679 Referenced Schema -> SchemaOptions -> proxy (C1 c f) -> Schema -> Schema
swagger2.cabal+1
@@ -68,6 +68,7 @@ test-suite spec
6868 , containers
6969 , hashable
7070 , hspec
71+ , insert-ordered-containers
7172 , HUnit
7273 , mtl
7374 , QuickCheck
test/Data/Swagger/SchemaSpec.hs+48−2
@@ -7,17 +7,18 @@ module Data.Swagger.SchemaSpec where
77import Prelude ()
88import Prelude.Compat
99
10+import Control.Lens ((^.))
1011import Data.Aeson
1112import Data.Aeson.QQ
1213import Data.Char
1314import qualified Data.HashMap.Strict as HashMap
15+import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
1416import Data.Proxy
1517import Data.Set (Set)
1618import qualified Data.Set as Set
1719import qualified Data.Text as Text
1820import GHC.Generics
1921
20−import qualified Data.Swagger.OrdHashMap as OrdHashMap
2122import Data.Swagger
2223import Data.Swagger.Declare
2324
@@ -35,10 +36,17 @@ checkSchemaName sname proxy =
3536checkDefs :: ToSchema a => Proxy a -> [String] -> Spec
3637checkDefs proxy names =
3738 it ("uses these definitions " ++ show names) $
38− Set.fromList (OrdHashMap.keys defs) `shouldBe` Set.fromList (map Text.pack names)
39+ InsOrdHashMap.keys defs `shouldBe` map Text.pack names
3940 where
4041 defs = execDeclare (declareNamedSchema proxy) mempty
4142
43+checkProperties :: ToSchema a => Proxy a -> [String] -> Spec
44+checkProperties proxy names =
45+ it ("has these fields in order " ++ show names) $
46+ InsOrdHashMap.keys fields `shouldBe` map Text.pack names
47+ where
48+ fields = toSchema proxy ^. properties
49+
4250checkInlinedSchema :: ToSchema a => Proxy a -> Value -> Spec
4351checkInlinedSchema proxy js = toInlinedSchema proxy <=> js
4452
@@ -59,6 +67,9 @@ spec = do
5967 context "Person" $ checkToSchema (Proxy :: Proxy Person) personSchemaJSON
6068 context "ISPair" $ checkToSchema (Proxy :: Proxy ISPair) ispairSchemaJSON
6169 context "Point (fieldLabelModifier)" $ checkToSchema (Proxy :: Proxy Point) pointSchemaJSON
70+ context "Point5 (many field record)" $ do
71+ checkToSchema (Proxy :: Proxy Point5) point5SchemaJSON
72+ checkProperties (Proxy :: Proxy Point5) point5Properties
6273 context "Color (bounded enum)" $ checkToSchema (Proxy :: Proxy Color) colorSchemaJSON
6374 context "Shade (paramSchemaToNamedSchema)" $ checkToSchema (Proxy :: Proxy Shade) shadeSchemaJSON
6475 context "Paint (record with bounded enum field)" $ checkToSchema (Proxy :: Proxy Paint) paintSchemaJSON
@@ -144,6 +155,7 @@ ispairSchemaJSON = [aesonQQ|
144155-- ========================================================================
145156-- Point (record data type with custom fieldLabelModifier)
146157-- ========================================================================
158+
147159data Point = Point
148160 { pointX :: Double
149161 , pointY :: Double
@@ -166,6 +178,40 @@ pointSchemaJSON = [aesonQQ|
166178}
167179|]
168180
181+-- ========================================================================
182+-- Point (record data type with multiple fields)
183+-- ========================================================================
184+
185+data Point5 = Point5
186+ { point5X :: Double
187+ , point5Y :: Double
188+ , point5Z :: Double
189+ , point5U :: Double
190+ , point5V :: Double -- 5 dimensional!
191+ } deriving (Generic)
192+
193+instance ToSchema Point5 where
194+ declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
195+ { fieldLabelModifier = map toLower . drop (length "point5") }
196+
197+point5SchemaJSON :: Value
198+point5SchemaJSON = [aesonQQ|
199+{
200+ "type": "object",
201+ "properties":
202+ {
203+ "x": { "type": "number", "format": "double" },
204+ "y": { "type": "number", "format": "double" },
205+ "z": { "type": "number", "format": "double" },
206+ "u": { "type": "number", "format": "double" },
207+ "v": { "type": "number", "format": "double" }
208+ },
209+ "required": ["x", "y", "z", "u", "v"]
210+}
211+|]
212+
213+point5Properties :: [String]
214+point5Properties = ["x", "y", "z", "u", "v"]
169215
170216-- ========================================================================
171217-- Color (enum)