graphql-engine/server/tests-hspec/Test/Quoter/YamlSpec.hs
Daniel Harvey 4d74bb79ab tests: interpolateYaml quasiquoter for string interpolation
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5162
GitOrigin-RevId: 5b4efb6fe2d8445eb9fad428c1fb0da6d0ac604c
2022-08-02 12:52:58 +00:00

145 lines
4.2 KiB
Haskell

{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE QuasiQuotes #-}
-- who tests the test franework?
module Test.Quoter.YamlSpec (spec) where
import Data.Aeson qualified as Aeson
import Data.Aeson.KeyMap qualified as KM
import GHC.Generics
import Harness.Quoter.Yaml (interpolateYaml, yaml)
import Harness.TestEnvironment
import Test.Hspec
import Prelude
--------------------------------------------------------------------------------
-- test datatype we will use to insert some yaml
data MakeSomeYaml = MakeSomeYaml
{ msyString :: String,
msyBool :: Bool,
msyNumber :: Int,
msyArray :: [MakeSomeYaml]
}
deriving stock (Generic)
deriving anyclass (Aeson.ToJSON)
-- ** Preamble
spec :: SpecWith TestEnvironment
spec = describe "Yaml quasiquoters" $ do
describe "yaml quoter" $ do
it "Interpolates a simple value using a Yaml anchor" $ const do
let interpolatedValue = (100 :: Int)
let input =
[yaml|
type: pg_create_select_permission
args:
limit: *interpolatedValue
|]
let expected =
[yaml|
type: pg_create_select_permission
args:
limit: 100
|]
input `shouldBe` expected
it "Interpolates a complex value using a Yaml anchor" $ const do
let complexValue =
MakeSomeYaml
{ msyString = "hello",
msyNumber = 100,
msyBool = True,
msyArray = []
}
let expected =
Aeson.Object
( KM.fromList
[ ("type", Aeson.String "thing"),
("complex", Aeson.toJSON complexValue)
]
)
let input =
[yaml|
type: thing
complex: *complexValue
|]
input `shouldBe` expected
it "Interpolates a key using a Yaml anchor" $ const do
let interpolatedValue = ("limit" :: String)
let input =
[yaml|
type: pg_create_select_permission
args:
*interpolatedValue: 100
|]
let expected =
[yaml|
type: pg_create_select_permission
args:
limit: 100
|]
input `shouldBe` expected
describe "interpolateYaml quoter" $ do
it "Copes with input that contains lots of single quotes" $ const do
let interpolateValue = ("hello" :: String)
let input =
[interpolateYaml|
errors:
- extensions:
code: validation-failed
path: $.selectionSet.hasura_author.selectionSet.notPresentCol
message: |-
field 'notPresentCol' not found in type: '#{interpolateValue}_author'
|]
let expected =
[interpolateYaml|
errors:
- extensions:
code: validation-failed
path: $.selectionSet.hasura_author.selectionSet.notPresentCol
message: |-
field 'notPresentCol' not found in type: 'hello_author'
|]
input `shouldBe` expected
it "Interpolates a Haskell value as expected" $ const do
let interpolatedValue = (100 :: Int)
let input :: Aeson.Value
input =
[interpolateYaml|
type: pg_create_select_permission
args:
limit: #{interpolatedValue}
|]
let expected :: Aeson.Value
expected =
[yaml|
type: pg_create_select_permission
args:
limit: 100
|]
input `shouldBe` expected
it "Interpolates a Haskell expression as expected" $ const do
let input :: Aeson.Value
input =
[interpolateYaml|
type: pg_create_select_permission
args:
limit: #{ (1 + 2 + 3 :: Int) }
|]
let expected :: Aeson.Value
expected =
[yaml|
type: pg_create_select_permission
args:
limit: 6
|]
input `shouldBe` expected