git.iko.soy/typeable/swagger2
on master help sign in
Add test suite for Data.Swagger.Schema
commit09899f6eabaf4192194b3d0d74c53bdb5d50512b authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateMon 23 Nov 2015, 16:26 (11 years ago) parent83d4aec
4 files changed+87−23
swagger2.cabal+2 test/Data/Swagger/SchemaSpec.hs+45 test/Data/SwaggerSpec.hs+1−23 test/SpecCommon.hs+39
swagger2.cabal+2
@@ -54,7 +54,9 @@ test-suite spec
5454 , unordered-containers
5555 , vector
5656 other-modules:
57+ SpecCommon
5758 Data.SwaggerSpec
59+ Data.Swagger.SchemaSpec
5860 default-language: Haskell2010
5961
6062source-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
1515import Data.Text (Text)
1616
1717import Data.Swagger
18−
18+import SpecCommon
1919import Test.Hspec
2020
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−
4321spec :: Spec
4422spec = do
4523 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+