Copyright | (c) Plow Technologies, 2016 |
---|---|
License | MIT |
Maintainer | soenkehahn@gmail.com, mchaver@gmail.com |
Stability | Alpha |
Safe Haskell | None |
Language | Haskell2010 |
If you're using servant with either servant-client or servant-server there will be types included in your APIs that servant will convert to and from JSON. (At least for most common APIs.) This module allows you to generically obtain test-suites for JSON serialization and deserialization of those types.
Here's an example:
>>>
:set -XTypeOperators
>>>
:set -XDataKinds
>>>
:set -XDeriveGeneric
>>>
import Servant.API
>>>
import Test.Hspec (hspec)
>>>
import GHC.Generics (Generic)
>>>
import Data.Aeson (ToJSON, FromJSON)
>>>
import Test.QuickCheck (Arbitrary(..), oneof)
>>>
data Foo = Foo { a :: String, b :: Int } deriving (Eq, Show, Generic)
>>>
instance FromJSON Foo
>>>
instance ToJSON Foo
>>>
:{
instance Arbitrary Foo where arbitrary = Foo <$> arbitrary <*> arbitrary :}
>>>
data Bar = BarA | BarB { bar :: Bool } deriving (Eq, Show, Generic)
>>>
instance FromJSON Bar
>>>
instance ToJSON Bar
>>>
:{
instance Arbitrary Bar where arbitrary = oneof $ pure BarA : (BarB <$> arbitrary) : [] :}
>>>
type Api = "post" :> ReqBody '[JSON] Foo :> Get '[JSON] Bar
>>>
let api = Proxy :: Proxy Api
>>>
hspec $ apiRoundtripSpecs api
JSON encoding of Bar allows to encode values with aeson and read them back JSON encoding of Foo allows to encode values with aeson and read them back Finished in ... seconds 2 examples, 0 failures
- apiRoundtripSpecs :: HasGenericSpecs api => Proxy api -> Spec
- apiGoldenSpecs :: HasGenericSpecs api => Proxy api -> Spec
- apiGoldenSpecsWithSettings :: HasGenericSpecs api => Settings -> Proxy api -> Spec
- apiSpecs :: HasGenericSpecs api => Proxy api -> Spec
- apiSpecsWithSettings :: HasGenericSpecs api => Settings -> Proxy api -> Spec
- usedTypes :: HasGenericSpecs api => Proxy api -> [TypeRep]
- data Proxy k t :: forall k. k -> * = Proxy
- data GoldenDirectoryOption :: *
- data Settings :: * = Settings {}
- defaultSettings :: Settings
Documentation
apiRoundtripSpecs :: HasGenericSpecs api => Proxy api -> Spec Source #
Allows to obtain roundtrip tests for JSON serialization for all types used
in a servant api. It uses settings
are not used in roundtripSpecs
. There is no need to let the user pass
cusomt settings. It automatically uses defaultSettings
.
See also roundtripSpecs
.
apiGoldenSpecs :: HasGenericSpecs api => Proxy api -> Spec Source #
Allows to obtain golden tests for JSON serialization for all types used in a servant api.
See also goldenSpecs
.
apiGoldenSpecsWithSettings :: HasGenericSpecs api => Settings -> Proxy api -> Spec Source #
Same as apiGoldenSpecs
, but allows custom settings.
apiSpecs :: HasGenericSpecs api => Proxy api -> Spec Source #
Combination of apiRoundtripSpecs
and apiGoldenSpecs
.
apiSpecsWithSettings :: HasGenericSpecs api => Settings -> Proxy api -> Spec Source #
Same as apiSpecs
, but allows custom settings.
re-exports
data Proxy k t :: forall k. k -> * #
A concrete, poly-kinded proxy type
Monad (Proxy *) | |
Functor (Proxy *) | |
Applicative (Proxy *) | |
Foldable (Proxy *) | |
Generic1 (Proxy *) | |
Alternative (Proxy *) | |
MonadPlus (Proxy *) | |
Eq1 (Proxy *) | Since: 4.9.0.0 |
Ord1 (Proxy *) | Since: 4.9.0.0 |
Read1 (Proxy *) | Since: 4.9.0.0 |
Show1 (Proxy *) | Since: 4.9.0.0 |
Bounded (Proxy k s) | |
Enum (Proxy k s) | |
Eq (Proxy k s) | |
Ord (Proxy k s) | |
Read (Proxy k s) | |
Show (Proxy k s) | |
Ix (Proxy k s) | |
Generic (Proxy k t) | |
Monoid (Proxy k s) | |
FromJSON (Proxy k a) | |
ToJSON (Proxy k a) | |
type Rep1 (Proxy *) | |
type Rep (Proxy k t) | |
data GoldenDirectoryOption :: * #
A custom directory name or a preselected directory name.
Settings | |
|
The default settings for general use cases.