Move and refactor the Directives tests

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5267
GitOrigin-RevId: ec37b8bcea8f50b97fca4008dec7575929ed1841
This commit is contained in:
Tom Harding 2022-08-01 13:04:48 +01:00 committed by hasura-bot
parent b85b018108
commit 08c47b7568
6 changed files with 691 additions and 335 deletions

View File

@ -1244,7 +1244,6 @@ test-suite tests-hspec
Test.DataConnector.AggregateQuerySpec
Test.DataConnector.QuerySpec
Test.DataConnector.SelectPermissionsSpec
Test.DirectivesSpec
Test.DisableRootFields.Common
Test.DisableRootFields.DefaultRootFieldsSpec
Test.DisableRootFields.SelectPermission.DisableAllRootFieldsRelationshipSpec
@ -1267,6 +1266,10 @@ test-suite tests-hspec
Test.NestedRelationshipsSpec
Test.ObjectRelationshipsLimitSpec
Test.Postgres.TimestampSpec
Test.Queries.DirectivesSpec
Test.Queries.Directives.IncludeSpec
Test.Queries.Directives.IncludeAndSkipSpec
Test.Queries.Directives.SkipSpec
Test.Queries.FilterSearchSpec
Test.Queries.SortSpec
Test.Queries.Paginate.LimitSpec

View File

@ -1,334 +0,0 @@
{-# LANGUAGE QuasiQuotes #-}
-- | Test directives.
module Test.DirectivesSpec (spec) where
import Data.Aeson (Value (..), object, (.=))
import Harness.Backend.BigQuery qualified as Bigquery
import Harness.Backend.Citus qualified as Citus
import Harness.Backend.Mysql as Mysql
import Harness.Backend.Postgres qualified as Postgres
import Harness.Backend.Sqlserver qualified as Sqlserver
import Harness.GraphqlEngine qualified as GraphqlEngine
import Harness.Quoter.Graphql (graphql)
import Harness.Quoter.Yaml
import Harness.Test.Context qualified as Context
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.TestEnvironment (TestEnvironment)
import Harness.Yaml
import Test.Hspec
import Prelude
--------------------------------------------------------------------------------
-- Preamble
spec :: SpecWith TestEnvironment
spec =
Context.run
[ Context.Context
{ name = Context.Backend Context.MySQL,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Mysql.setup schema,
teardown = Mysql.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.Postgres,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Postgres.setup schema,
teardown = Postgres.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.Citus,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Citus.setup schema,
teardown = Citus.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.SQLServer,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Sqlserver.setup schema,
teardown = Sqlserver.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.BigQuery,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Bigquery.setup schema,
teardown = Bigquery.teardown schema,
customOptions =
Just $
Context.Options
{ stringifyNumbers = True
}
}
]
tests
--------------------------------------------------------------------------------
-- Schema
schema :: [Schema.Table]
schema = [author]
author :: Schema.Table
author =
(table "author")
{ tableColumns =
[ Schema.column "id" Schema.TInt,
Schema.column "name" Schema.TStr
],
tablePrimaryKey = ["id"],
tableData =
[ [Schema.VInt 1, Schema.VStr "Author 1"],
[Schema.VInt 2, Schema.VStr "Author 2"]
]
}
--------------------------------------------------------------------------------
-- Tests
data QueryParams = QueryParams
{ includeId :: Bool,
skipId :: Bool
}
query :: QueryParams -> Value
query QueryParams {includeId, skipId} =
[graphql|
query author_with_both {
hasura_author {
id @include(if: #{includeId}) @skip(if: #{skipId})
name
}
}
|]
tests :: Context.Options -> SpecWith TestEnvironment
tests opts = do
it "Skip id field conditionally" \testEnvironment ->
shouldReturnOneOfYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
(query QueryParams {includeId = False, skipId = False})
)
(combinationsObjectUsingValue authorResponse authorNames)
it "Skip id field conditionally, includeId=true" \testEnvironment ->
shouldReturnOneOfYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
(query QueryParams {includeId = True, skipId = False})
)
(combinationsObjectUsingValue authorResponse authorNameIds)
it "Skip id field conditionally, skipId=true" \testEnvironment ->
shouldReturnOneOfYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
(query QueryParams {includeId = False, skipId = True})
)
(combinationsObjectUsingValue authorResponse authorNames)
it "Skip id field conditionally, skipId=true, includeId=true" \testEnvironment ->
shouldReturnOneOfYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
(query QueryParams {includeId = True, skipId = True})
)
(combinationsObjectUsingValue authorResponse authorNames)
it "Author with skip id" \testEnvironment ->
shouldReturnOneOfYaml
opts
( GraphqlEngine.postGraphqlYaml
testEnvironment
[yaml|
query: |
query author_with_skip($skipId: Boolean!, $skipName: Boolean!) {
hasura_author {
id @skip(if: $skipId)
name @skip(if: $skipName)
}
}
variables:
skipId: true
skipName: false
|]
)
(combinationsObjectUsingValue authorResponse authorNames)
it "Author with skip name" \testEnvironment ->
shouldReturnOneOfYaml
opts
( GraphqlEngine.postGraphqlYaml
testEnvironment
[yaml|
query: |
query author_with_skip($skipId: Boolean!, $skipName: Boolean!) {
hasura_author {
id @skip(if: $skipId)
name @skip(if: $skipName)
}
}
variables:
skipId: false
skipName: true
|]
)
(combinationsObjectUsingValue authorResponse authorIds)
it "Rejects unknown directives" \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphqlYaml
testEnvironment
[yaml|
query: |
query {
hasura_author {
id @exclude(if: true)
name
}
}
|]
)
[yaml|
errors:
- extensions:
path: $.selectionSet.hasura_author.selectionSet
code: validation-failed
message: |-
directive 'exclude' is not defined in the schema
|]
it "Rejects duplicate directives" \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphqlYaml
testEnvironment
[yaml|
query: |
query {
hasura_author {
id @include(if: true) @include(if: true)
name
}
}
|]
)
[yaml|
errors:
- extensions:
path: $.selectionSet.hasura_author.selectionSet
code: validation-failed
message: |-
the following directives are used more than once: ['include']
|]
it "Rejects directives on wrong element" \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphqlYaml
testEnvironment
[yaml|
query: |
query @include(if: true) {
hasura_author {
id
name
}
}
|]
)
[yaml|
errors:
- extensions:
path: $
code: validation-failed
message: |-
directive 'include' is not allowed on a query
|]
it "works with includeId as True includeName as False" \testEnvironment ->
shouldReturnOneOfYaml
opts
( GraphqlEngine.postGraphqlWithPair
testEnvironment
[graphql|
query author_with_include($includeId: Boolean!, $includeName: Boolean!) {
hasura_author {
id @include(if: $includeId)
name @include(if: $includeName)
}
}
|]
["variables" .= object ["includeId" .= True, "includeName" .= False]]
)
(combinationsObjectUsingValue authorResponse authorIds)
it "works with includeId as False includeName as True" \testEnvironment ->
shouldReturnOneOfYaml
opts
( GraphqlEngine.postGraphqlWithPair
testEnvironment
[graphql|
query author_with_include($includeId: Boolean!, $includeName: Boolean!) {
hasura_author {
id @include(if: $includeId)
name @include(if: $includeName)
}
}
|]
["variables" .= object ["includeId" .= False, "includeName" .= True]]
)
(combinationsObjectUsingValue authorResponse authorNames)
authorNames :: [Value]
authorNames =
[ object
[ "name"
.= String "Author 1"
],
object
[ "name"
.= String "Author 2"
]
]
authorIds :: [Value]
authorIds =
[ object
[ "id"
.= (1 :: Int)
],
object
[ "id"
.= (2 :: Int)
]
]
authorNameIds :: [Value]
authorNameIds =
[ object
[ "name"
.= String "Author 1",
"id" .= (1 :: Int)
],
object
[ "name"
.= String "Author 2",
"id" .= (2 :: Int)
]
]
authorResponse :: Value -> Value
authorResponse authors =
[yaml|
data:
hasura_author: *authors
|]

View File

@ -0,0 +1,202 @@
{-# LANGUAGE QuasiQuotes #-}
-- |
-- Tests for the interactions between @skip and @include query directives.
--
-- https://spec.graphql.org/June2018/#sec-Type-System.Directives
-- https://hasura.io/docs/latest/queries/postgres/variables-aliases-fragments-directives/
-- https://hasura.io/docs/latest/queries/ms-sql-server/variables-aliases-fragments-directives/
-- https://hasura.io/docs/latest/queries/bigquery/variables-aliases-fragments-directives/
module Test.Queries.Directives.IncludeAndSkipSpec (spec) where
import Data.Aeson (Value, object, (.=))
import Harness.Backend.BigQuery qualified as BigQuery
import Harness.Backend.Citus qualified as Citus
import Harness.Backend.Mysql qualified as Mysql
import Harness.Backend.Postgres qualified as Postgres
import Harness.Backend.Sqlserver qualified as Sqlserver
import Harness.GraphqlEngine (postGraphql, postGraphqlWithPair)
import Harness.Quoter.Graphql (graphql)
import Harness.Quoter.Yaml (shouldReturnYaml, yaml)
import Harness.Test.Context (Options (..))
import Harness.Test.Context qualified as Context
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.TestEnvironment (TestEnvironment)
import Test.Hspec (SpecWith, describe, it)
import Prelude
spec :: SpecWith TestEnvironment
spec =
Context.run
[ Context.Context
{ name = Context.Backend Context.MySQL,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Mysql.setup schema,
teardown = Mysql.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.Postgres,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Postgres.setup schema,
teardown = Postgres.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.Citus,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Citus.setup schema,
teardown = Citus.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.SQLServer,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Sqlserver.setup schema,
teardown = Sqlserver.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.BigQuery,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = BigQuery.setup schema,
teardown = BigQuery.teardown schema,
customOptions =
Just $
Context.Options
{ stringifyNumbers = True
}
}
]
tests
--------------------------------------------------------------------------------
-- Schema
schema :: [Schema.Table]
schema =
[ (table "author")
{ tableColumns =
[ Schema.column "id" Schema.TInt,
Schema.column "name" Schema.TStr
],
tablePrimaryKey = ["id"],
tableData =
[ [Schema.VInt 1, Schema.VStr "Author 1"],
[Schema.VInt 2, Schema.VStr "Author 2"]
]
}
]
--------------------------------------------------------------------------------
-- Tests
tests :: Context.Options -> SpecWith TestEnvironment
tests opts = do
let shouldBe :: IO Value -> Value -> IO ()
shouldBe = shouldReturnYaml opts
describe "Mixes @include and @skip directives" do
it "Returns the field when @include(if: true) and @skip(if: false)" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author:
- id: 1
name: Author 1
- id: 2
name: Author 2
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author(order_by: [{ id: asc }]) {
id @include(if: true) @skip(if: false)
name
}
}
|]
actual `shouldBe` expected
it "Doesn't return the field when @include(if: false) and @skip(if: false)" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author:
- name: Author 1
- name: Author 2
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author(order_by: [{ id: asc }]) {
id @include(if: false) @skip(if: false)
name
}
}
|]
actual `shouldBe` expected
it "Doesn't return the field when @include(if: false) and @skip(if: true)" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author:
- name: Author 1
- name: Author 2
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author(order_by: [{ id: asc }]) {
id @include(if: false) @skip(if: true)
name
}
}
|]
actual `shouldBe` expected
it "Doesn't return the field when @include(if: true) and @skip(if: true)" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author:
- name: Author 1
- name: Author 2
|]
actual :: IO Value
actual =
postGraphqlWithPair
testEnvironment
[graphql|
query test($skip: Boolean!, $include: Boolean!) {
hasura_author(order_by: [{ id: asc }]) {
id @include(if: $include) @skip(if: $skip)
name
}
}
|]
["variables" .= object ["skip" .= True, "include" .= True]]
actual `shouldBe` expected

View File

@ -0,0 +1,152 @@
{-# LANGUAGE QuasiQuotes #-}
-- |
-- Tests for the GraphQL @include query directive.
--
-- https://spec.graphql.org/June2018/#sec-Type-System.Directives
-- https://hasura.io/docs/latest/queries/postgres/variables-aliases-fragments-directives/
-- https://hasura.io/docs/latest/queries/ms-sql-server/variables-aliases-fragments-directives/
-- https://hasura.io/docs/latest/queries/bigquery/variables-aliases-fragments-directives/
module Test.Queries.Directives.IncludeSpec (spec) where
import Data.Aeson (Value, object, (.=))
import Harness.Backend.BigQuery qualified as BigQuery
import Harness.Backend.Citus qualified as Citus
import Harness.Backend.Mysql qualified as Mysql
import Harness.Backend.Postgres qualified as Postgres
import Harness.Backend.Sqlserver qualified as Sqlserver
import Harness.GraphqlEngine (postGraphql, postGraphqlWithPair)
import Harness.Quoter.Graphql (graphql)
import Harness.Quoter.Yaml (shouldReturnYaml, yaml)
import Harness.Test.Context (Options (..))
import Harness.Test.Context qualified as Context
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.TestEnvironment (TestEnvironment)
import Test.Hspec (SpecWith, describe, it)
import Prelude
spec :: SpecWith TestEnvironment
spec =
Context.run
[ Context.Context
{ name = Context.Backend Context.MySQL,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Mysql.setup schema,
teardown = Mysql.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.Postgres,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Postgres.setup schema,
teardown = Postgres.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.Citus,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Citus.setup schema,
teardown = Citus.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.SQLServer,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Sqlserver.setup schema,
teardown = Sqlserver.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.BigQuery,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = BigQuery.setup schema,
teardown = BigQuery.teardown schema,
customOptions =
Just $
Context.Options
{ stringifyNumbers = True
}
}
]
tests
--------------------------------------------------------------------------------
-- Schema
schema :: [Schema.Table]
schema =
[ (table "author")
{ tableColumns =
[ Schema.column "id" Schema.TInt,
Schema.column "name" Schema.TStr
],
tablePrimaryKey = ["id"],
tableData =
[ [Schema.VInt 1, Schema.VStr "Author 1"],
[Schema.VInt 2, Schema.VStr "Author 2"]
]
}
]
--------------------------------------------------------------------------------
-- Tests
tests :: Context.Options -> SpecWith TestEnvironment
tests opts = do
let shouldBe :: IO Value -> Value -> IO ()
shouldBe = shouldReturnYaml opts
describe "Include fields conditionally" do
it "Includes field with @include(if: true)" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author:
- id: 1
name: Author 1
- id: 2
name: Author 2
|]
actual :: IO Value
actual =
postGraphqlWithPair
testEnvironment
[graphql|
query test($include: Boolean!) {
hasura_author(order_by: [{ id: asc }]) {
id @include(if: $include)
name
}
}
|]
["variables" .= object ["include" .= True]]
actual `shouldBe` expected
it "Doesn't include field with @include(if: false)" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author:
- name: Author 1
- name: Author 2
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author(order_by: [{ id: asc }]) {
id @include(if: false)
name
}
}
|]
actual `shouldBe` expected

View File

@ -0,0 +1,153 @@
{-# LANGUAGE QuasiQuotes #-}
-- |
-- Tests for the GraphQL @skip query directive.
--
-- https://spec.graphql.org/June2018/#sec-Type-System.Directives
-- https://hasura.io/docs/latest/queries/postgres/variables-aliases-fragments-directives/
-- https://hasura.io/docs/latest/queries/ms-sql-server/variables-aliases-fragments-directives/
-- https://hasura.io/docs/latest/queries/bigquery/variables-aliases-fragments-directives/
module Test.Queries.Directives.SkipSpec (spec) where
import Data.Aeson (Value, object, (.=))
import Harness.Backend.BigQuery qualified as BigQuery
import Harness.Backend.Citus qualified as Citus
import Harness.Backend.Mysql qualified as Mysql
import Harness.Backend.Postgres qualified as Postgres
import Harness.Backend.Sqlserver qualified as Sqlserver
import Harness.GraphqlEngine (postGraphql, postGraphqlWithPair)
import Harness.Quoter.Graphql (graphql)
import Harness.Quoter.Yaml (shouldReturnYaml, yaml)
import Harness.Test.Context (Options (..))
import Harness.Test.Context qualified as Context
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.TestEnvironment (TestEnvironment)
import Test.Hspec (SpecWith, describe, it)
import Prelude
spec :: SpecWith TestEnvironment
spec =
Context.run
[ Context.Context
{ name = Context.Backend Context.MySQL,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Mysql.setup schema,
teardown = Mysql.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.Postgres,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Postgres.setup schema,
teardown = Postgres.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.Citus,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Citus.setup schema,
teardown = Citus.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.SQLServer,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Sqlserver.setup schema,
teardown = Sqlserver.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.BigQuery,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = BigQuery.setup schema,
teardown = BigQuery.teardown schema,
customOptions =
Just $
Context.Options
{ stringifyNumbers = True
}
}
]
tests
--------------------------------------------------------------------------------
-- Schema
schema :: [Schema.Table]
schema =
[ (table "author")
{ tableColumns =
[ Schema.column "id" Schema.TInt,
Schema.column "name" Schema.TStr
],
tablePrimaryKey = ["id"],
tableData =
[ [Schema.VInt 1, Schema.VStr "Author 1"],
[Schema.VInt 2, Schema.VStr "Author 2"]
]
}
]
--------------------------------------------------------------------------------
-- Tests
tests :: Context.Options -> SpecWith TestEnvironment
tests opts = do
let shouldBe :: IO Value -> Value -> IO ()
shouldBe = shouldReturnYaml opts
describe "Skip fields conditionally" do
it "Skips field with @skip(if: true)" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author:
- name: Author 1
- name: Author 2
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author(order_by: [{ id: asc }]) {
id @skip(if: true)
name
}
}
|]
actual `shouldBe` expected
it "Doesn't skip field with @skip(if: false)" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author:
- id: 1
name: Author 1
- id: 2
name: Author 2
|]
actual :: IO Value
actual =
postGraphqlWithPair
testEnvironment
[graphql|
query test($skip: Boolean!) {
hasura_author(order_by: [{ id: asc }]) {
id @skip(if: $skip)
name
}
}
|]
[ "variables" .= object ["skip" .= False]
]
actual `shouldBe` expected

View File

@ -0,0 +1,180 @@
{-# LANGUAGE QuasiQuotes #-}
-- |
-- Tests for GraphQL query directives.
--
-- https://spec.graphql.org/June2018/#sec-Type-System.Directives
-- https://hasura.io/docs/latest/queries/postgres/variables-aliases-fragments-directives/
-- https://hasura.io/docs/latest/queries/ms-sql-server/variables-aliases-fragments-directives/
-- https://hasura.io/docs/latest/queries/bigquery/variables-aliases-fragments-directives/
module Test.Queries.DirectivesSpec (spec) where
import Data.Aeson (Value)
import Harness.Backend.BigQuery qualified as BigQuery
import Harness.Backend.Citus qualified as Citus
import Harness.Backend.Mysql qualified as Mysql
import Harness.Backend.Postgres qualified as Postgres
import Harness.Backend.Sqlserver qualified as Sqlserver
import Harness.GraphqlEngine (postGraphql)
import Harness.Quoter.Graphql (graphql)
import Harness.Quoter.Yaml (shouldReturnYaml, yaml)
import Harness.Test.Context (Options (..))
import Harness.Test.Context qualified as Context
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.TestEnvironment (TestEnvironment)
import Test.Hspec (SpecWith, describe, it)
import Prelude
spec :: SpecWith TestEnvironment
spec =
Context.run
[ Context.Context
{ name = Context.Backend Context.MySQL,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Mysql.setup schema,
teardown = Mysql.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.Postgres,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Postgres.setup schema,
teardown = Postgres.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.Citus,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Citus.setup schema,
teardown = Citus.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.SQLServer,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Sqlserver.setup schema,
teardown = Sqlserver.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.BigQuery,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = BigQuery.setup schema,
teardown = BigQuery.teardown schema,
customOptions =
Just $
Context.Options
{ stringifyNumbers = True
}
}
]
tests
--------------------------------------------------------------------------------
-- Schema
schema :: [Schema.Table]
schema =
[ (table "author")
{ tableColumns =
[ Schema.column "id" Schema.TInt,
Schema.column "name" Schema.TStr
],
tablePrimaryKey = ["id"],
tableData =
[ [Schema.VInt 1, Schema.VStr "Author 1"],
[Schema.VInt 2, Schema.VStr "Author 2"]
]
}
]
--------------------------------------------------------------------------------
-- Tests
tests :: Context.Options -> SpecWith TestEnvironment
tests opts = do
let shouldBe :: IO Value -> Value -> IO ()
shouldBe = shouldReturnYaml opts
describe "Directives" do
it "Rejects unknown directives" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
errors:
- extensions:
path: $.selectionSet.hasura_author.selectionSet
code: validation-failed
message: |-
directive 'exclude' is not defined in the schema
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author {
id @exclude(if: true)
name
}
}
|]
actual `shouldBe` expected
it "Rejects duplicate directives" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
errors:
- extensions:
path: $.selectionSet.hasura_author.selectionSet
code: validation-failed
message: |-
the following directives are used more than once: ['include']
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author {
id @include(if: true) @include(if: true)
name
}
}
|]
actual `shouldBe` expected
it "Rejects directives on wrong element" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
errors:
- extensions:
path: $
code: validation-failed
message: |-
directive 'include' is not allowed on a query
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query @include(if: true) {
hasura_author {
id
name
}
}
|]
actual `shouldBe` expected