Tidy up simple object query tests, match fixtures to docs

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5752
GitOrigin-RevId: 2e308504a188745ad5714d60d0fe449a65d6f950
This commit is contained in:
Tom Harding 2022-09-05 12:08:13 +01:00 committed by hasura-bot
parent 2ac1680817
commit 136349a70a
3 changed files with 238 additions and 91 deletions

View File

@ -1291,6 +1291,7 @@ test-suite tests-hspec
Test.Queries.NestedObjectSpec Test.Queries.NestedObjectSpec
Test.Queries.Paginate.LimitSpec Test.Queries.Paginate.LimitSpec
Test.Queries.Paginate.OffsetSpec Test.Queries.Paginate.OffsetSpec
Test.Queries.Simple.NoQueriesAvailableSpec
Test.Queries.Simple.ObjectQueriesSpec Test.Queries.Simple.ObjectQueriesSpec
Test.Queries.Simple.OperationNameSpec Test.Queries.Simple.OperationNameSpec
Test.Queries.Simple.PrimaryKeySpec Test.Queries.Simple.PrimaryKeySpec

View File

@ -0,0 +1,135 @@
{-# LANGUAGE QuasiQuotes #-}
-- |
-- Can we see @no_queries_available@ or not?
module Test.Queries.Simple.NoQueriesAvailableSpec (spec) where
import Data.Aeson (Value)
import Data.List.NonEmpty qualified as NE
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 (interpolateYaml)
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.TestEnvironment (TestEnvironment)
import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude
import Test.Hspec (SpecWith, describe, it)
spec :: SpecWith TestEnvironment
spec = do
let execute :: [Schema.Table] -> (Fixture.Options -> SpecWith TestEnvironment) -> SpecWith TestEnvironment
execute tables =
Fixture.run
( NE.fromList
[ (Fixture.fixture $ Fixture.Backend Fixture.MySQL)
{ Fixture.setupTeardown = \(testEnvironment, _) ->
[ Mysql.setupTablesAction tables testEnvironment
]
},
(Fixture.fixture $ Fixture.Backend Fixture.Postgres)
{ Fixture.setupTeardown = \(testEnvironment, _) ->
[ Postgres.setupTablesAction tables testEnvironment
]
},
(Fixture.fixture $ Fixture.Backend Fixture.Citus)
{ Fixture.setupTeardown = \(testEnvironment, _) ->
[ Citus.setupTablesAction tables testEnvironment
]
},
(Fixture.fixture $ Fixture.Backend Fixture.SQLServer)
{ Fixture.setupTeardown = \(testEnvironment, _) ->
[ Sqlserver.setupTablesAction tables testEnvironment
]
},
(Fixture.fixture $ Fixture.Backend Fixture.BigQuery)
{ Fixture.setupTeardown = \(testEnvironment, _) ->
[ BigQuery.setupTablesAction tables testEnvironment
],
Fixture.customOptions =
Just $
Fixture.Options
{ stringifyNumbers = True
}
}
]
)
describe "`no_queries_available`" do
execute schema queriesAvailable
execute mempty noQueriesAvailable
--------------------------------------------------------------------------------
-- Schema
schema :: [Schema.Table]
schema =
[ (table "authors")
{ tableColumns = [Schema.column "id" Schema.TInt]
}
]
--------------------------------------------------------------------------------
-- Tests
queriesAvailable :: Fixture.Options -> SpecWith TestEnvironment
queriesAvailable opts = do
let shouldBe :: IO Value -> Value -> IO ()
shouldBe = shouldReturnYaml opts
it "Should disappear when queries are available" \testEnvironment -> do
let actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
no_queries_available
}
|]
expected :: Value
expected =
[interpolateYaml|
errors:
- extensions:
code: validation-failed
path: $.selectionSet.no_queries_available
message: |-
field 'no_queries_available' not found in type: 'query_root'
|]
actual `shouldBe` expected
noQueriesAvailable :: Fixture.Options -> SpecWith TestEnvironment
noQueriesAvailable opts = do
let shouldBe :: IO Value -> Value -> IO ()
shouldBe = shouldReturnYaml opts
it "Should be present when queries are not" \testEnvironment -> do
let actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
no_queries_available
}
|]
expected :: Value
expected =
[interpolateYaml|
data:
no_queries_available: There are no queries available to the current role. Either
there are no sources or remote schemas configured, or the current role doesn't
have the required permissions.
|]
actual `shouldBe` expected

View File

@ -3,9 +3,9 @@
-- | -- |
-- Simple queries on single objects. -- Simple queries on single objects.
-- --
-- https://hasura.io/docs/latest/queries/postgres/simple-object-queries/ -- https://hasura.io/docs/latest/queries/postgres/simple-object-queries/#fetch-list-of-objects
-- https://hasura.io/docs/latest/queries/ms-sql-server/simple-object-queries/ -- https://hasura.io/docs/latest/queries/ms-sql-server/simple-object-queries/#fetch-list-of-objects
-- https://hasura.io/docs/latest/queries/bigquery/simple-object-queries/ -- https://hasura.io/docs/latest/queries/bigquery/simple-object-queries/#fetch-list-of-objects
module Test.Queries.Simple.ObjectQueriesSpec (spec) where module Test.Queries.Simple.ObjectQueriesSpec (spec) where
import Data.Aeson (Value) import Data.Aeson (Value)
@ -17,7 +17,7 @@ import Harness.Backend.Postgres qualified as Postgres
import Harness.Backend.Sqlserver qualified as Sqlserver import Harness.Backend.Sqlserver qualified as Sqlserver
import Harness.GraphqlEngine (postGraphql) import Harness.GraphqlEngine (postGraphql)
import Harness.Quoter.Graphql (graphql) import Harness.Quoter.Graphql (graphql)
import Harness.Quoter.Yaml (interpolateYaml, yaml) import Harness.Quoter.Yaml (interpolateYaml)
import Harness.Test.Fixture qualified as Fixture import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table) import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema import Harness.Test.Schema qualified as Schema
@ -31,28 +31,28 @@ spec = do
Fixture.run Fixture.run
( NE.fromList ( NE.fromList
[ (Fixture.fixture $ Fixture.Backend Fixture.MySQL) [ (Fixture.fixture $ Fixture.Backend Fixture.MySQL)
{ Fixture.setupTeardown = \(testEnv, _) -> { Fixture.setupTeardown = \(testEnvironment, _) ->
[ Mysql.setupTablesAction schema testEnv [ Mysql.setupTablesAction schema testEnvironment
] ]
}, },
(Fixture.fixture $ Fixture.Backend Fixture.Postgres) (Fixture.fixture $ Fixture.Backend Fixture.Postgres)
{ Fixture.setupTeardown = \(testEnv, _) -> { Fixture.setupTeardown = \(testEnvironment, _) ->
[ Postgres.setupTablesAction schema testEnv [ Postgres.setupTablesAction schema testEnvironment
] ]
}, },
(Fixture.fixture $ Fixture.Backend Fixture.Citus) (Fixture.fixture $ Fixture.Backend Fixture.Citus)
{ Fixture.setupTeardown = \(testEnv, _) -> { Fixture.setupTeardown = \(testEnvironment, _) ->
[ Citus.setupTablesAction schema testEnv [ Citus.setupTablesAction schema testEnvironment
] ]
}, },
(Fixture.fixture $ Fixture.Backend Fixture.SQLServer) (Fixture.fixture $ Fixture.Backend Fixture.SQLServer)
{ Fixture.setupTeardown = \(testEnv, _) -> { Fixture.setupTeardown = \(testEnvironment, _) ->
[ Sqlserver.setupTablesAction schema testEnv [ Sqlserver.setupTablesAction schema testEnvironment
] ]
}, },
(Fixture.fixture $ Fixture.Backend Fixture.BigQuery) (Fixture.fixture $ Fixture.Backend Fixture.BigQuery)
{ Fixture.setupTeardown = \(testEnv, _) -> { Fixture.setupTeardown = \(testEnvironment, _) ->
[ BigQuery.setupTablesAction schema testEnv [ BigQuery.setupTablesAction schema testEnvironment
], ],
Fixture.customOptions = Fixture.customOptions =
Just $ Just $
@ -69,7 +69,7 @@ spec = do
schema :: [Schema.Table] schema :: [Schema.Table]
schema = schema =
[ (table "author") [ (table "authors")
{ tableColumns = { tableColumns =
[ Schema.column "id" Schema.TInt, [ Schema.column "id" Schema.TInt,
Schema.column "name" Schema.TStr Schema.column "name" Schema.TStr
@ -77,10 +77,16 @@ schema =
tablePrimaryKey = ["id"], tablePrimaryKey = ["id"],
tableData = tableData =
[ [ Schema.VInt 1, [ [ Schema.VInt 1,
Schema.VStr "Author 1" Schema.VStr "Justin"
], ],
[ Schema.VInt 2, [ Schema.VInt 2,
Schema.VStr "Author 2" Schema.VStr "Beltran"
],
[ Schema.VInt 3,
Schema.VStr "Sidney"
],
[ Schema.VInt 4,
Schema.VStr "Anjela"
] ]
] ]
} }
@ -94,115 +100,120 @@ tests opts = do
let shouldBe :: IO Value -> Value -> IO () let shouldBe :: IO Value -> Value -> IO ()
shouldBe = shouldReturnYaml opts shouldBe = shouldReturnYaml opts
describe "Simple object queries" do describe "Fetch a list of objects" do
it "Fetch a list of authors" \testEnvironment -> do it "Fetch a list of authors" \testEnvironment -> do
let schemaName = Schema.getSchemaName testEnvironment let schemaName :: Schema.SchemaName
schemaName = Schema.getSchemaName testEnvironment
let expected :: Value actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
#{schemaName}_authors(order_by: { id: asc }) {
id
name
}
}
|]
expected :: Value
expected = expected =
[interpolateYaml| [interpolateYaml|
data: data:
#{schemaName}_author: #{schemaName}_authors:
- id: 1 - id: 1
name: "Author 1" name: Justin
- id: 2 - id: 2
name: "Author 2" name: Beltran
- id: 3
name: Sidney
- id: 4
name: Anjela
|] |]
-- We have to set an ordering for BigQuery, as return order isn't actual `shouldBe` expected
-- guaranteed.
it "Fails on unknown tables" \testEnvironment -> do
let schemaName :: Schema.SchemaName
schemaName = Schema.getSchemaName testEnvironment
actual :: IO Value actual :: IO Value
actual = actual =
postGraphql postGraphql
testEnvironment testEnvironment
[graphql| [graphql|
query { query {
#{schemaName}_author(order_by: [{ id: asc }]) { #{schemaName}_unknown {
id id
name
} }
} }
|] |]
actual `shouldBe` expected expected :: Value
it "Removes `no_queries_available` when queries are available" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
errors:
- extensions:
code: validation-failed
path: $.selectionSet.no_queries_available
message: |-
field 'no_queries_available' not found in type: 'query_root'
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
no_queries_available
}
|]
actual `shouldBe` expected
it "Fails on missing tables" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
errors:
- extensions:
code: validation-failed
path: $.selectionSet.random
message: |-
field 'random' not found in type: 'query_root'
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
random {
id
name
}
}
|]
actual `shouldBe` expected
it "Fails on missing fields" \testEnvironment -> do
let schemaName = Schema.getSchemaName testEnvironment
let expected :: Value
expected = expected =
[interpolateYaml| [interpolateYaml|
errors: errors:
- extensions: - extensions:
code: validation-failed code: validation-failed
path: $.selectionSet.#{schemaName}_author.selectionSet.notPresentCol path: $.selectionSet.#{schemaName}_unknown
message: |- message: 'field ''#{schemaName}_unknown'' not found in type: ''query_root'''
field 'notPresentCol' not found in type: '#{schemaName}_author'
|] |]
actual `shouldBe` expected
it "Fails on unknown fields" \testEnvironment -> do
let schemaName :: Schema.SchemaName
schemaName = Schema.getSchemaName testEnvironment
actual :: IO Value actual :: IO Value
actual = actual =
postGraphql postGraphql
testEnvironment testEnvironment
[graphql| [graphql|
query { query {
#{schemaName}_author { #{schemaName}_authors {
id unknown
name
notPresentCol
} }
} }
|] |]
expected :: Value
expected =
[interpolateYaml|
errors:
- extensions:
code: validation-failed
path: $.selectionSet.#{schemaName}_authors.selectionSet.unknown
message: 'field ''unknown'' not found in type: ''#{schemaName}_authors'''
|]
actual `shouldBe` expected
it "Fails on empty query" \testEnvironment -> do
let schemaName :: Schema.SchemaName
schemaName = Schema.getSchemaName testEnvironment
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
#{schemaName}_authors {
}
}
|]
expected :: Value
expected =
[interpolateYaml|
errors:
- extensions:
code: validation-failed
path: $.query
message: not a valid graphql query
|]
actual `shouldBe` expected actual `shouldBe` expected