Update primary key hspec tests to match the docs, add failure cases

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5753
GitOrigin-RevId: ac3070b6a5dbfecdabead209a5f4ee7daa948066
This commit is contained in:
Tom Harding 2022-09-05 11:35:56 +01:00 committed by hasura-bot
parent 52a77a0bfe
commit 2ac1680817

View File

@ -3,9 +3,8 @@
-- |
-- Accessing objects based on their primary keys.
--
-- https://hasura.io/docs/latest/queries/postgres/simple-object-queries/
-- https://hasura.io/docs/latest/queries/ms-sql-server/simple-object-queries/
-- https://hasura.io/docs/latest/queries/bigquery/simple-object-queries/
-- https://hasura.io/docs/latest/queries/postgres/simple-object-queries/#fetch-an-object-using-its-primary-key
-- TODO: Add MSSQL link when it has been documented.
module Test.Queries.Simple.PrimaryKeySpec (spec) where
import Data.Aeson (Value)
@ -15,7 +14,7 @@ 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 (yaml)
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
@ -29,18 +28,18 @@ spec =
Fixture.run
( NE.fromList
[ (Fixture.fixture $ Fixture.Backend Fixture.Postgres)
{ Fixture.setupTeardown = \(testEnv, _) ->
[ Postgres.setupTablesAction schema testEnv
{ Fixture.setupTeardown = \(testEnvironment, _) ->
[ Postgres.setupTablesAction schema testEnvironment
]
},
(Fixture.fixture $ Fixture.Backend Fixture.Citus)
{ Fixture.setupTeardown = \(testEnv, _) ->
[ Citus.setupTablesAction schema testEnv
{ Fixture.setupTeardown = \(testEnvironment, _) ->
[ Citus.setupTablesAction schema testEnvironment
]
},
(Fixture.fixture $ Fixture.Backend Fixture.SQLServer)
{ Fixture.setupTeardown = \(testEnv, _) ->
[ Sqlserver.setupTablesAction schema testEnv
{ Fixture.setupTeardown = \(testEnvironment, _) ->
[ Sqlserver.setupTablesAction schema testEnvironment
]
}
]
@ -52,7 +51,7 @@ spec =
schema :: [Schema.Table]
schema =
[ (table "author")
[ (table "authors")
{ tableColumns =
[ Schema.column "id" Schema.TInt,
Schema.column "name" Schema.TStr
@ -60,10 +59,16 @@ schema =
tablePrimaryKey = ["id"],
tableData =
[ [ Schema.VInt 1,
Schema.VStr "Author 1"
Schema.VStr "Justin"
],
[ Schema.VInt 2,
Schema.VStr "Author 2"
Schema.VStr "Beltran"
],
[ Schema.VInt 3,
Schema.VStr "Sidney"
],
[ Schema.VInt 4,
Schema.VStr "Anjela"
]
]
}
@ -79,14 +84,8 @@ tests opts = do
describe "Primary key queries" do
it "Lookup with primary key" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author_by_pk:
id: 1
name: Author 1
|]
let schemaName :: Schema.SchemaName
schemaName = Schema.getSchemaName testEnvironment
actual :: IO Value
actual =
@ -94,22 +93,27 @@ tests opts = do
testEnvironment
[graphql|
query {
hasura_author_by_pk(id: 1) {
#{schemaName}_authors_by_pk(id: 1) {
id
name
}
}
|]
expected :: Value
expected =
[interpolateYaml|
data:
#{schemaName}_authors_by_pk:
id: 1
name: Justin
|]
actual `shouldBe` expected
it "Lookup with (missing) primary key" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author_by_pk: null
|]
let schemaName :: Schema.SchemaName
schemaName = Schema.getSchemaName testEnvironment
actual :: IO Value
actual =
@ -117,11 +121,134 @@ tests opts = do
testEnvironment
[graphql|
query {
hasura_author_by_pk(id: 4) {
#{schemaName}_authors_by_pk(id: 5) {
id
name
}
}
|]
expected :: Value
expected =
[interpolateYaml|
data:
#{schemaName}_authors_by_pk: null
|]
actual `shouldBe` expected
it "Fails on missing tables" \testEnvironment -> do
let schemaName :: Schema.SchemaName
schemaName = Schema.getSchemaName testEnvironment
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
#{schemaName}_unknown_by_pk(id: 5) {
id
name
}
}
|]
expected :: Value
expected =
[interpolateYaml|
errors:
- extensions:
code: validation-failed
path: $.selectionSet.#{schemaName}_unknown_by_pk
message: |-
field '#{schemaName}_unknown_by_pk' not found in type: 'query_root'
|]
actual `shouldBe` expected
it "Fails on missing fields" \testEnvironment -> do
let schemaName :: Schema.SchemaName
schemaName = Schema.getSchemaName testEnvironment
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
#{schemaName}_authors_by_pk(id: 5) {
unknown
}
}
|]
expected :: Value
expected =
[interpolateYaml|
errors:
- extensions:
code: validation-failed
path: $.selectionSet.#{schemaName}_authors_by_pk.selectionSet.unknown
message: |-
field 'unknown' not found in type: '#{schemaName}_authors'
|]
actual `shouldBe` expected
it "Fails on missing primary key value" \testEnvironment -> do
let schemaName :: Schema.SchemaName
schemaName = Schema.getSchemaName testEnvironment
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
#{schemaName}_authors_by_pk {
id
}
}
|]
expected :: Value
expected =
[interpolateYaml|
errors:
- extensions:
code: validation-failed
path: $.selectionSet.#{schemaName}_authors_by_pk.args.id
message: |-
missing required field 'id'
|]
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_by_pk {
}
}
|]
expected :: Value
expected =
[interpolateYaml|
errors:
- extensions:
code: validation-failed
path: $.query
message: |-
not a valid graphql query
|]
actual `shouldBe` expected