git.iko.soy/typeable/swagger2
on master help sign in
Add a few ToParamSchema tests
commitf8ca4ec1665804f7b771dddfa7d878e0a1749315 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateWed 9 Dec 2015, 15:29 (11 years ago) parent1434b71
1 file changed+78
test/Data/Swagger/ParamSchemaSpec.hs+78
test/Data/Swagger/ParamSchemaSpec.hsnew file+78
@@ -0,0 +1,78 @@
1+{-# LANGUAGE DeriveGeneric #-}
2+{-# LANGUAGE DeriveAnyClass #-}
3+{-# LANGUAGE ScopedTypeVariables #-}
4+{-# LANGUAGE TemplateHaskell #-}
5+{-# LANGUAGE QuasiQuotes #-}
6+module Data.Swagger.ParamSchemaSpec where
7+
8+import Data.Aeson
9+import Data.Aeson.QQ
10+import Data.Char
11+import Data.Proxy
12+import Data.Set (Set)
13+import GHC.Generics
14+
15+import Data.Swagger
16+
17+import SpecCommon
18+import Test.Hspec
19+
20+checkToParamSchema :: ToParamSchema a => Proxy a -> Value -> Spec
21+checkToParamSchema proxy js = (toParamSchema proxy :: ParamSchema Param Items) <=> js
22+
23+spec :: Spec
24+spec = do
25+ describe "Generic ToParamSchema" $ do
26+ context "Color (bounded enum)" $ checkToParamSchema (Proxy :: Proxy Color) colorSchemaJSON
27+ context "Unary records" $ do
28+ context "Email (unary record)" $ checkToParamSchema (Proxy :: Proxy Email) emailSchemaJSON
29+ context "UserId (non-record newtype)" $ checkToParamSchema (Proxy :: Proxy UserId) userIdSchemaJSON
30+
31+main :: IO ()
32+main = hspec spec
33+
34+-- ========================================================================
35+-- Color (enum)
36+-- ========================================================================
37+data Color
38+ = Red
39+ | Green
40+ | Blue
41+ deriving (Generic, ToParamSchema)
42+
43+colorSchemaJSON :: Value
44+colorSchemaJSON = [aesonQQ|
45+{
46+ "type": "string",
47+ "enum": ["Red", "Green", "Blue"]
48+}
49+|]
50+
51+-- ========================================================================
52+-- Email (newtype with unwrapUnaryRecords set to True)
53+-- ========================================================================
54+
55+newtype Email = Email { getEmail :: String }
56+ deriving (Generic, ToParamSchema)
57+
58+emailSchemaJSON :: Value
59+emailSchemaJSON = [aesonQQ|
60+{
61+ "type": "string"
62+}
63+|]
64+
65+-- ========================================================================
66+-- UserId (non-record newtype)
67+-- ========================================================================
68+
69+newtype UserId = UserId Integer
70+ deriving (Generic, ToParamSchema)
71+
72+userIdSchemaJSON :: Value
73+userIdSchemaJSON = [aesonQQ|
74+{
75+ "type": "integer"
76+}
77+|]
78+