on migrations2 help sign in
schematic/test/SchemaSpec.hs
59 lines 1.8 kB history raw
1module SchemaSpec (spec, main) where
3import Control.Monad
4import Data.ByteString.Lazy
5import Data.Aeson
6import Data.Proxy
7import Data.Schematic
8import Data.Singletons
9import Data.Vinyl
10import Test.Hspec
11import Test.Hspec.SmallCheck
12import Test.SmallCheck
13import Test.SmallCheck.Series.Instances
16type SchemaExample
17 = SchemaObject
18 '[ '("foo", SchemaArray '[AEq 1] (SchemaNumber '[NGt 10]))
19 , '("bar", SchemaOptional (SchemaText '[TRegex "\\w+", TEnum '["foo", "bar"]]))]
21exampleTest :: JsonRepr (SchemaOptional (SchemaText '[TEq 3]))
22exampleTest = ReprOptional (Just (ReprText "lil"))
24exampleNumber :: JsonRepr (SchemaNumber '[NGt 10])
25exampleNumber = ReprNumber 12
27exampleArray :: JsonRepr (SchemaArray '[AEq 1] (SchemaNumber '[NGt 10]))
28exampleArray = ReprArray [exampleNumber]
30jsonExample :: JsonRepr SchemaExample
31jsonExample = ReprObject $
32 FieldRepr (ReprArray [ReprNumber 12])
33 :& FieldRepr (ReprOptional (Just (ReprText "bar")))
34 :& RNil
36schemaJson :: ByteString
37schemaJson = "{\"foo\": [13], \"bar\": null}"
39schemaJson2 :: ByteString
40schemaJson2 = "{\"foo\": [3], \"bar\": null}"
42spec :: Spec
43spec = do
44 -- it "show/read JsonRepr properly" $
45 -- read (show example) == example
46 it "decode/encode JsonRepr properly" $
47 decode (encode jsonExample) == Just jsonExample
48 it "validates correct representation" $
49 ((decodeAndValidateJson schemaJson) :: ParseResult (JsonRepr SchemaExample))
50 `shouldSatisfy` isValid
51 it "returns decoding error on structurally incorrect input" $
52 ((decodeAndValidateJson "{}") :: ParseResult (JsonRepr SchemaExample))
53 `shouldSatisfy` isDecodingError
54 it "validates incorrect representation" $
55 ((decodeAndValidateJson schemaJson2) :: ParseResult (JsonRepr SchemaExample))
56 `shouldSatisfy` isValidationError
58main :: IO ()
59main = hspec spec