git.iko.soy/typeable/openapi3
on master help sign in
Add test suite for Data.Swagger.Schema
09899f6Nickolay Kudasov
swagger2.cabal+2
@@ -54,7 +54,9 @@ test-suite spec
54 , unordered-containers
54 , unordered-containers
55 , vector
55 , vector
56 other-modules:
56 other-modules:
57+ SpecCommon
57 Data.SwaggerSpec
58 Data.SwaggerSpec
59+ Data.Swagger.SchemaSpec
58 default-language: Haskell2010
60 default-language: Haskell2010
59
61
60source-repository head
62source-repository head
test/Data/Swagger/SchemaSpec.hsnew file+45
@@ -0,0 +1,45 @@
1+{-# LANGUAGE DeriveGeneric #-}
2+{-# LANGUAGE DeriveAnyClass #-}
3+{-# LANGUAGE TemplateHaskell #-}
4+{-# LANGUAGE QuasiQuotes #-}
5+module Data.Swagger.SchemaSpec where
6+
7+import Data.Aeson
8+import Data.Aeson.QQ
9+import Data.Proxy
10+import GHC.Generics
11+
12+import Data.Swagger
13+import SpecCommon
14+import Test.Hspec
15+
16+checkToSchema :: ToSchema a => Proxy a -> Value -> Spec
17+checkToSchema proxy js = toSchema proxy <~> js
18+
19+spec :: Spec
20+spec = do
21+ describe "Generic ToSchema" $
22+ context "Person" $ checkToSchema (Proxy :: Proxy Person) personSchemaJSON
23+
24+main :: IO ()
25+main = hspec spec
26+
27+data Person = Person
28+ { name :: String
29+ , phone :: Integer
30+ , email :: Maybe String
31+ } deriving (Generic, ToSchema)
32+
33+personSchemaJSON :: Value
34+personSchemaJSON = [aesonQQ|
35+{
36+ "type": "object",
37+ "properties":
38+ {
39+ "name": { "type": "string" },
40+ "phone": { "type": "integer" },
41+ "email": { "type": "string" }
42+ },
43+ "required": ["name", "phone"]
44+}
45+|]
test/Data/SwaggerSpec.hs+1−23
@@ -15,31 +15,9 @@ import qualified Data.Vector as Vector
15import Data.Text (Text)
15import Data.Text (Text)
16
16
17import Data.Swagger
17import Data.Swagger
18−
18+import SpecCommon
19import Test.Hspec
19import Test.Hspec
20
20
21−isSubJSON :: Value -> Value -> Bool
22−isSubJSON Null _ = True
23−isSubJSON (Object x) (Object y) = HashMap.keys x == HashMap.keys i && F.and i
24− where
25− i = HashMap.intersectionWith isSubJSON x y
26−isSubJSON (Array xs) (Array ys) = Vector.length xs == Vector.length ys && F.and (Vector.zipWith isSubJSON xs ys)
27−isSubJSON x y = x == y
28−
29−(<~>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec
30−x <~> json = do
31− it "encodes correctly (probably with extra properties)" $ do
32− toJSON x `shouldSatisfy` (json `isSubJSON`)
33− it "decodes correctly" $ do
34− fromJSON json `shouldBe` Success x
35−
36−(<=>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec
37−x <=> json = do
38− it "encodes correctly" $ do
39− toJSON x `shouldBe` json
40− it "decodes correctly" $ do
41− fromJSON json `shouldBe` Success x
42−
43spec :: Spec
21spec :: Spec
44spec = do
22spec = do
45 describe "License Object" $ licenseExample <=> licenseExampleJSON
23 describe "License Object" $ licenseExample <=> licenseExampleJSON
test/SpecCommon.hsnew file+39
@@ -0,0 +1,39 @@
1+module SpecCommon where
2+
3+import Data.Aeson
4+import Data.Aeson.QQ
5+import qualified Data.Foldable as F
6+import Data.HashMap.Strict (HashMap)
7+import qualified Data.HashMap.Strict as HashMap
8+import Data.Maybe
9+import Data.Monoid
10+import qualified Data.Vector as Vector
11+import Data.Text (Text)
12+
13+import Data.Swagger
14+
15+import Test.Hspec
16+
17+isSubJSON :: Value -> Value -> Bool
18+isSubJSON Null _ = True
19+isSubJSON (Object x) (Object y) = HashMap.keys x == HashMap.keys i && F.and i
20+ where
21+ i = HashMap.intersectionWith isSubJSON x y
22+isSubJSON (Array xs) (Array ys) = Vector.length xs == Vector.length ys && F.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+
32+(<=>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec
33+x <=> json = do
34+ it "encodes correctly" $ do
35+ toJSON x `shouldBe` json
36+ it "decodes correctly" $ do
37+ fromJSON json `shouldBe` Success x
38+
39+