graphql-engine/server/tests-hspec/Test/Quoter/YamlSpec.hs
Daniel Harvey f4df7f0162 tests: fix interpolateYaml quasi quoter when parsing '*'
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5491
GitOrigin-RevId: 3fbea70c9c9ebf4584cb140854e98db680a4137f
2022-08-15 17:17:44 +00:00

157 lines
4.4 KiB
Haskell

{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE QuasiQuotes #-}
-- who tests the test framework?
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 Hasura.Prelude
import Test.Hspec
--------------------------------------------------------------------------------
-- 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
it "Interpolation does not fail when parsing a '*'" $ const do
let input :: Aeson.Value
input =
[interpolateYaml|
"*"
|]
expected =
[yaml|
"*"
|]
input `shouldBe` expected