2020-12-25 00:18:42 +03:00
|
|
|
# Example
|
|
|
|
|
|
|
|
Our api under test:
|
|
|
|
|
2021-01-05 18:38:15 +03:00
|
|
|
``` haskell
|
2020-12-25 00:18:42 +03:00
|
|
|
-- Obligatory fancy-types pragma tax
|
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
{-# LANGUAGE DerivingStrategies #-}
|
|
|
|
{-# LANGUAGE StandaloneDeriving #-}
|
|
|
|
{-# LANGUAGE DerivingVia #-}
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
|
|
|
|
import Roboservant
|
|
|
|
import Test.Hspec
|
|
|
|
import Servant
|
|
|
|
import GHC.Generics
|
|
|
|
import Data.Typeable
|
|
|
|
import Data.Hashable
|
2021-01-05 18:38:15 +03:00
|
|
|
import Data.Maybe(isNothing, isJust)
|
2020-12-25 00:18:42 +03:00
|
|
|
|
|
|
|
newtype A = A Int
|
|
|
|
deriving (Generic, Eq, Show, Typeable)
|
|
|
|
deriving newtype (Hashable, FromHttpApiData, ToHttpApiData)
|
|
|
|
|
|
|
|
newtype B = B Int
|
|
|
|
deriving (Generic, Eq, Show, Typeable)
|
|
|
|
deriving newtype (Hashable, FromHttpApiData, ToHttpApiData)
|
|
|
|
|
|
|
|
type Api =
|
|
|
|
"item" :> Get '[JSON] A
|
|
|
|
:<|> "itemAdd" :> Capture "one" B :> Capture "two" B :> Get '[JSON] B
|
|
|
|
:<|> "item" :> Capture "itemId" B :> Get '[JSON] ()
|
|
|
|
|
2021-01-05 18:38:15 +03:00
|
|
|
server :: Handler A -> Server Api
|
2020-12-25 00:18:42 +03:00
|
|
|
server introduce = introduce :<|> combine :<|> eliminate
|
|
|
|
where
|
|
|
|
combine (B i) (B j) = pure $ B (i + j)
|
|
|
|
eliminate (B i)
|
|
|
|
| i > 10 = error "give up, eleven is way too big and probably not even real"
|
|
|
|
| otherwise = pure ()
|
|
|
|
```
|
|
|
|
|
2021-01-05 18:38:15 +03:00
|
|
|
We have a "good" server, that never generates anything other than a 0. This means repeated application of
|
2021-01-05 18:41:35 +03:00
|
|
|
the combination/addition rule can never bring us to the dangerous state of numbers larger than 10.
|
2021-01-05 18:38:15 +03:00
|
|
|
|
|
|
|
``` haskell
|
|
|
|
goodServer, badServer :: Server Api
|
|
|
|
goodServer = server (pure $ A 0)
|
|
|
|
badServer = server (pure $ A 1)
|
|
|
|
```
|
|
|
|
|
2021-01-03 19:38:22 +03:00
|
|
|
In the test file, we first define the tests: the faulty server should fail and the good server should pass.
|
2020-12-26 19:02:34 +03:00
|
|
|
|
|
|
|
```haskell
|
2021-01-05 18:38:15 +03:00
|
|
|
main :: IO ()
|
|
|
|
main = hspec spec
|
|
|
|
|
|
|
|
spec :: Spec
|
2020-12-25 00:18:42 +03:00
|
|
|
spec = describe "example" $ do
|
|
|
|
it "good server should not fail" $ do
|
2021-01-03 19:52:12 +03:00
|
|
|
fuzz @Api goodServer config
|
2020-12-25 00:18:42 +03:00
|
|
|
>>= (`shouldSatisfy` isNothing)
|
|
|
|
it "bad server should fail" $ do
|
2021-01-03 19:52:12 +03:00
|
|
|
fuzz @Api badServer config
|
2021-01-05 18:38:15 +03:00
|
|
|
>>= (`shouldSatisfy` isJust)
|
|
|
|
```
|
|
|
|
|
2021-01-03 19:52:12 +03:00
|
|
|
|
2021-01-05 18:38:15 +03:00
|
|
|
We expect to be able to cover the whole api from our starting point, so let's set the coverage to 0.99.
|
|
|
|
There are other tweakable things in the config, like maximum runtime, reps,
|
|
|
|
per-request healthchecks, seeds, and verbose logging. Have a look at
|
|
|
|
Roboservant.Types.Config for details.
|
|
|
|
|
|
|
|
``` haskell
|
|
|
|
config :: Config
|
2021-01-03 19:52:12 +03:00
|
|
|
config = defaultConfig
|
2021-01-05 18:38:15 +03:00
|
|
|
{ coverageThreshold = 0.99
|
2021-01-03 19:52:12 +03:00
|
|
|
}
|
2020-12-25 00:18:42 +03:00
|
|
|
```
|
|
|
|
|
2021-01-05 18:38:15 +03:00
|
|
|
Unless we want to ship roboservant and all its dependencies to production, we also need
|
2020-12-25 00:18:42 +03:00
|
|
|
some orphan instances: because As are the only value we can get without
|
|
|
|
an input, we need to be able to break them down.
|
|
|
|
|
2021-01-05 18:38:15 +03:00
|
|
|
``` haskell
|
2020-12-25 00:18:42 +03:00
|
|
|
deriving via (Compound A) instance Breakdown A
|
2021-01-05 18:38:15 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
if we wanted to assemble As from parts as well, we'd derive using Compound, but in this case we don't care.
|
|
|
|
|
|
|
|
``` haskell
|
2020-12-25 00:18:42 +03:00
|
|
|
deriving via (Atom A) instance BuildFrom A
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
Similarly, to generate the first B from the Ints we got from inside the A, we need to be able to
|
|
|
|
build it up from components.
|
|
|
|
|
|
|
|
```haskell
|
|
|
|
deriving via (Compound B) instance BuildFrom B
|
|
|
|
deriving via (Atom B) instance Breakdown B
|
|
|
|
```
|