graphql-engine/server/tests-hspec/Test/LimitOffsetSpec.hs
Tom Harding 307071840f Add the basic limit query
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5183
GitOrigin-RevId: 76e1f59c3e5212f69b4b13e431627cfff141a8b5
2022-07-26 13:32:16 +00:00

108 lines
2.5 KiB
Haskell

{-# 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
|]