2022-08-01 15:04:48 +03:00
|
|
|
{-# 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, (.=))
|
2022-08-11 18:03:04 +03:00
|
|
|
import Data.List.NonEmpty qualified as NE
|
2022-08-01 15:04:48 +03:00
|
|
|
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)
|
2022-08-08 17:28:17 +03:00
|
|
|
import Harness.Quoter.Yaml (interpolateYaml)
|
2022-08-02 21:01:34 +03:00
|
|
|
import Harness.Test.Fixture qualified as Fixture
|
2022-08-01 15:04:48 +03:00
|
|
|
import Harness.Test.Schema (Table (..), table)
|
|
|
|
import Harness.Test.Schema qualified as Schema
|
|
|
|
import Harness.TestEnvironment (TestEnvironment)
|
2022-08-05 12:34:25 +03:00
|
|
|
import Harness.Yaml (shouldReturnYaml)
|
2022-08-03 17:18:43 +03:00
|
|
|
import Hasura.Prelude
|
2022-08-01 15:04:48 +03:00
|
|
|
import Test.Hspec (SpecWith, describe, it)
|
|
|
|
|
|
|
|
spec :: SpecWith TestEnvironment
|
2022-08-02 21:01:34 +03:00
|
|
|
spec = do
|
|
|
|
Fixture.run
|
2022-08-11 18:03:04 +03:00
|
|
|
( NE.fromList
|
|
|
|
[ (Fixture.fixture $ Fixture.Backend Fixture.MySQL)
|
|
|
|
{ Fixture.setupTeardown = \(testEnv, _) ->
|
|
|
|
[ Mysql.setupTablesAction schema testEnv
|
|
|
|
]
|
|
|
|
},
|
|
|
|
(Fixture.fixture $ Fixture.Backend Fixture.Postgres)
|
|
|
|
{ Fixture.setupTeardown = \(testEnv, _) ->
|
|
|
|
[ Postgres.setupTablesAction schema testEnv
|
|
|
|
]
|
|
|
|
},
|
|
|
|
(Fixture.fixture $ Fixture.Backend Fixture.Citus)
|
|
|
|
{ Fixture.setupTeardown = \(testEnv, _) ->
|
|
|
|
[ Citus.setupTablesAction schema testEnv
|
|
|
|
]
|
|
|
|
},
|
|
|
|
(Fixture.fixture $ Fixture.Backend Fixture.SQLServer)
|
|
|
|
{ Fixture.setupTeardown = \(testEnv, _) ->
|
|
|
|
[ Sqlserver.setupTablesAction schema testEnv
|
|
|
|
]
|
|
|
|
},
|
|
|
|
(Fixture.fixture $ Fixture.Backend Fixture.BigQuery)
|
|
|
|
{ Fixture.setupTeardown = \(testEnv, _) ->
|
|
|
|
[ BigQuery.setupTablesAction schema testEnv
|
|
|
|
],
|
|
|
|
Fixture.customOptions =
|
|
|
|
Just $
|
|
|
|
Fixture.Options
|
|
|
|
{ stringifyNumbers = True
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
2022-08-01 15:04:48 +03:00
|
|
|
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
|
|
|
|
|
2022-08-02 21:01:34 +03:00
|
|
|
tests :: Fixture.Options -> SpecWith TestEnvironment
|
2022-08-01 15:04:48 +03:00
|
|
|
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
|
2022-08-09 12:33:39 +03:00
|
|
|
let schemaName = Schema.getSchemaName testEnvironment
|
2022-08-08 17:28:17 +03:00
|
|
|
|
2022-08-01 15:04:48 +03:00
|
|
|
let expected :: Value
|
|
|
|
expected =
|
2022-08-08 17:28:17 +03:00
|
|
|
[interpolateYaml|
|
2022-08-01 15:04:48 +03:00
|
|
|
data:
|
2022-08-08 17:28:17 +03:00
|
|
|
#{schemaName}_author:
|
2022-08-01 15:04:48 +03:00
|
|
|
- id: 1
|
|
|
|
name: Author 1
|
|
|
|
- id: 2
|
|
|
|
name: Author 2
|
|
|
|
|]
|
|
|
|
|
|
|
|
actual :: IO Value
|
|
|
|
actual =
|
|
|
|
postGraphql
|
|
|
|
testEnvironment
|
|
|
|
[graphql|
|
|
|
|
query {
|
2022-08-08 17:28:17 +03:00
|
|
|
#{schemaName}_author(order_by: [{ id: asc }]) {
|
2022-08-01 15:04:48 +03:00
|
|
|
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
|
2022-08-09 12:33:39 +03:00
|
|
|
let schemaName = Schema.getSchemaName testEnvironment
|
2022-08-08 17:28:17 +03:00
|
|
|
|
2022-08-01 15:04:48 +03:00
|
|
|
let expected :: Value
|
|
|
|
expected =
|
2022-08-08 17:28:17 +03:00
|
|
|
[interpolateYaml|
|
2022-08-01 15:04:48 +03:00
|
|
|
data:
|
2022-08-08 17:28:17 +03:00
|
|
|
#{schemaName}_author:
|
2022-08-01 15:04:48 +03:00
|
|
|
- name: Author 1
|
|
|
|
- name: Author 2
|
|
|
|
|]
|
|
|
|
|
|
|
|
actual :: IO Value
|
|
|
|
actual =
|
|
|
|
postGraphql
|
|
|
|
testEnvironment
|
|
|
|
[graphql|
|
|
|
|
query {
|
2022-08-08 17:28:17 +03:00
|
|
|
#{schemaName}_author(order_by: [{ id: asc }]) {
|
2022-08-01 15:04:48 +03:00
|
|
|
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
|
2022-08-09 12:33:39 +03:00
|
|
|
let schemaName = Schema.getSchemaName testEnvironment
|
2022-08-08 17:28:17 +03:00
|
|
|
|
2022-08-01 15:04:48 +03:00
|
|
|
let expected :: Value
|
|
|
|
expected =
|
2022-08-08 17:28:17 +03:00
|
|
|
[interpolateYaml|
|
2022-08-01 15:04:48 +03:00
|
|
|
data:
|
2022-08-08 17:28:17 +03:00
|
|
|
#{schemaName}_author:
|
2022-08-01 15:04:48 +03:00
|
|
|
- name: Author 1
|
|
|
|
- name: Author 2
|
|
|
|
|]
|
|
|
|
|
|
|
|
actual :: IO Value
|
|
|
|
actual =
|
|
|
|
postGraphql
|
|
|
|
testEnvironment
|
|
|
|
[graphql|
|
|
|
|
query {
|
2022-08-08 17:28:17 +03:00
|
|
|
#{schemaName}_author(order_by: [{ id: asc }]) {
|
2022-08-01 15:04:48 +03:00
|
|
|
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
|
2022-08-09 12:33:39 +03:00
|
|
|
let schemaName = Schema.getSchemaName testEnvironment
|
2022-08-08 17:28:17 +03:00
|
|
|
|
2022-08-01 15:04:48 +03:00
|
|
|
let expected :: Value
|
|
|
|
expected =
|
2022-08-08 17:28:17 +03:00
|
|
|
[interpolateYaml|
|
2022-08-01 15:04:48 +03:00
|
|
|
data:
|
2022-08-08 17:28:17 +03:00
|
|
|
#{schemaName}_author:
|
2022-08-01 15:04:48 +03:00
|
|
|
- name: Author 1
|
|
|
|
- name: Author 2
|
|
|
|
|]
|
|
|
|
|
|
|
|
actual :: IO Value
|
|
|
|
actual =
|
|
|
|
postGraphqlWithPair
|
|
|
|
testEnvironment
|
|
|
|
[graphql|
|
|
|
|
query test($skip: Boolean!, $include: Boolean!) {
|
2022-08-08 17:28:17 +03:00
|
|
|
#{schemaName}_author(order_by: [{ id: asc }]) {
|
2022-08-01 15:04:48 +03:00
|
|
|
id @include(if: $include) @skip(if: $skip)
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
["variables" .= object ["skip" .= True, "include" .= True]]
|
|
|
|
|
|
|
|
actual `shouldBe` expected
|