Create and organise OffsetSpec, remove LimitOffsetSpec

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5186
GitOrigin-RevId: 08687b3e77a6e8663cf0ec59e638aa917a334574
This commit is contained in:
Tom Harding 2022-07-27 10:45:45 +01:00 committed by hasura-bot
parent 04887f00de
commit bd1fde3d8b
3 changed files with 155 additions and 108 deletions

View File

@ -1254,7 +1254,6 @@ test-suite tests-hspec
Test.InsertDefaultsSpec
Test.InsertEnumColumnSpec
Test.InsertOnConflictSpec
Test.LimitOffsetSpec
Test.LongIdentifiersSpec
Test.NestedRelationshipsSpec
Test.ObjectRelationshipsLimitSpec
@ -1263,6 +1262,7 @@ test-suite tests-hspec
Test.PostgresTypesSpec
Test.PrimaryKeySpec
Test.Queries.Paginate.LimitSpec
Test.Queries.Paginate.OffsetSpec
Test.Queries.Simple.ObjectQueriesSpec
Test.Queries.Simple.OperationNameSpec
Test.RemoteRelationship.FromRemoteSchemaSpec

View File

@ -1,107 +0,0 @@
{-# LANGUAGE QuasiQuotes #-}
-- | Tests for limit/offset.
module Test.LimitOffsetSpec (spec) where
import Harness.Backend.Mysql as Mysql
import Harness.GraphqlEngine qualified as GraphqlEngine
import Harness.Quoter.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 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
}
]
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"],
[Schema.VInt 3, Schema.VStr "Author 3"],
[Schema.VInt 4, Schema.VStr "Author 4"]
]
}
--------------------------------------------------------------------------------
-- Tests
tests :: Context.Options -> SpecWith TestEnvironment
tests opts = do
-- Technically without an ORDER, the results are UB-ish. Keep an eye
-- on ordering with tests like this.
it "Basic offset query" $ \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
[graphql|
query {
hasura_author(offset: 1) {
name
id
}
}
|]
)
[yaml|
data:
hasura_author:
- name: Author 2
id: 2
- name: Author 3
id: 3
- name: Author 4
id: 4
|]
-- We use ordering here, which yields a stable result.
it "order descending, offset 2, limit 1" $ \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
[graphql|
query {
hasura_author(limit: 1, offset: 2, order_by: {id: desc}) {
id
name
}
}
|]
)
[yaml|
data:
hasura_author:
- id: 2
name: Author 2
|]

View File

@ -0,0 +1,154 @@
{-# LANGUAGE QuasiQuotes #-}
-- |
-- Offset queries
--
-- https://hasura.io/docs/latest/queries/postgres/pagination/
-- https://hasura.io/docs/latest/queries/ms-sql-server/pagination/
-- https://hasura.io/docs/latest/queries/bigquery/pagination/
module Test.Queries.Paginate.OffsetSpec (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 = do
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"],
[Schema.VInt 3, Schema.VStr "Author 3"],
[Schema.VInt 4, Schema.VStr "Author 4"]
]
}
]
--------------------------------------------------------------------------------
-- Tests
tests :: Context.Options -> SpecWith TestEnvironment
tests opts = do
let shouldBe :: IO Value -> Value -> IO ()
shouldBe = shouldReturnYaml opts
describe "Paginate query results" do
it "Offsets results by one element" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author:
- name: Author 2
id: 2
- name: Author 3
id: 3
- name: Author 4
id: 4
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author(order_by: [{ id: asc }], offset: 1) {
name
id
}
}
|]
actual `shouldBe` expected
it "Correctly handles ordering, offsets, and limits" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author:
- id: 2
name: Author 2
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author(limit: 1, offset: 2, order_by: [{ id: desc }]) {
id
name
}
}
|]
actual `shouldBe` expected