Move PrimaryKeySpec into the new test structure

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5182
GitOrigin-RevId: 26dafbf95ff6d2cf2f7d3e9d9eec2441f2ef02ce
This commit is contained in:
Tom Harding 2022-07-27 12:01:55 +01:00 committed by hasura-bot
parent 61bc9c498b
commit 63cc498619
3 changed files with 130 additions and 112 deletions

View File

@ -1260,11 +1260,11 @@ test-suite tests-hspec
Test.Queries.FilterSearchSpec
Test.Queries.SortSpec
Test.PostgresTypesSpec
Test.PrimaryKeySpec
Test.Queries.Paginate.LimitSpec
Test.Queries.Paginate.OffsetSpec
Test.Queries.Simple.ObjectQueriesSpec
Test.Queries.Simple.OperationNameSpec
Test.Queries.Simple.PrimaryKeySpec
Test.RemoteRelationship.FromRemoteSchemaSpec
Test.RemoteRelationship.MetadataAPI.ClearMetadataSpec
Test.RemoteRelationship.MetadataAPI.Common

View File

@ -1,111 +0,0 @@
{-# LANGUAGE QuasiQuotes #-}
-- | Primary key related queries
module Test.PrimaryKeySpec (spec) where
import Harness.Backend.Citus qualified as Citus
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 (shouldReturnYaml, 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 Test.Hspec (SpecWith, describe, it)
import Prelude
--------------------------------------------------------------------------------
-- Preamble
spec :: SpecWith TestEnvironment
spec =
Context.run
[ 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
}
]
tests
schema :: [Schema.Table]
schema = [author]
author :: Schema.Table
author =
(table "author")
{ tableColumns =
[ Schema.column "id" Schema.TInt,
Schema.column "name" Schema.TStr,
Schema.column "createdAt" Schema.TUTCTime
],
tablePrimaryKey = ["id"],
tableData =
[ [ Schema.VInt 1,
Schema.VStr "Author 1",
Schema.parseUTCTimeOrError "2017-09-21 09:39:44"
],
[ Schema.VInt 2,
Schema.VStr "Author 2",
Schema.parseUTCTimeOrError "2017-09-21 09:50:44"
]
]
}
tests :: Context.Options -> SpecWith TestEnvironment
tests opts = describe "PrimaryKeySpec" $ do
it "works with primary key" $ \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
[graphql|
query {
hasura_author_by_pk(id: 1) {
id
name
}
}|]
)
[yaml|
data:
hasura_author_by_pk:
id: 1
name: Author 1
|]
it "works with non existent primary key" $ \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
[graphql|
query {
hasura_author_by_pk(id: 4) {
id
name
}
}|]
)
[yaml|
data:
hasura_author_by_pk: null
|]

View File

@ -0,0 +1,129 @@
{-# LANGUAGE QuasiQuotes #-}
-- |
-- 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/
module Test.Queries.Simple.PrimaryKeySpec (spec) where
import Data.Aeson (Value)
import Harness.Backend.Citus qualified as Citus
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 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.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
}
]
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 "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
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author_by_pk(id: 1) {
id
name
}
}
|]
actual `shouldBe` expected
it "Lookup with (missing) primary key" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author_by_pk: null
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author_by_pk(id: 4) {
id
name
}
}
|]
actual `shouldBe` expected