graphql-engine/server/tests-hspec/Test/Queries/SortSpec.hs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

203 lines
5.5 KiB
Haskell
Raw Normal View History

{-# LANGUAGE QuasiQuotes #-}
-- |
-- Tests for sorting query results according to values of different types.
--
-- https://hasura.io/docs/latest/queries/postgres/sorting/
-- https://hasura.io/docs/latest/queries/ms-sql-server/sorting/
-- https://hasura.io/docs/latest/queries/bigquery/sorting/
module Test.Queries.SortSpec (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 (yaml)
import Harness.Test.Context (Options (..))
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)
--------------------------------------------------------------------------------
-- Preamble
spec :: SpecWith TestEnvironment
spec = do
Fixture.run
[ (Fixture.fixture $ Fixture.Backend Fixture.MySQL)
{ Fixture.setupTeardown = \(testEnv, _) ->
[ Mysql.setupTablesAction schema testEnv
]
},
(Fixture.fixture $ Fixture.Backend Fixture.Postgres)
{ Fixture.setupTeardown = \(testEnv, _) ->
[ Postgres.setupTablesAction schema testEnv
]
},
(Fixture.fixture $ Fixture.Backend Fixture.Citus)
{ Fixture.setupTeardown = \(testEnv, _) ->
[ Citus.setupTablesAction schema testEnv
]
},
(Fixture.fixture $ Fixture.Backend Fixture.SQLServer)
{ Fixture.setupTeardown = \(testEnv, _) ->
[ Sqlserver.setupTablesAction schema testEnv
]
},
(Fixture.fixture $ Fixture.Backend Fixture.BigQuery)
{ Fixture.setupTeardown = \(testEnv, _) ->
[ BigQuery.setupTablesAction schema testEnv
],
Fixture.customOptions =
Just $
Fixture.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 "Bob"],
[Schema.VInt 2, Schema.VStr "Alice"]
]
}
]
--------------------------------------------------------------------------------
-- Tests
tests :: Fixture.Options -> SpecWith TestEnvironment
tests opts = do
let shouldBe :: IO Value -> Value -> IO ()
shouldBe = shouldReturnYaml opts
describe "Sorting results by IDs" do
it "Ascending" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author:
- name: Bob
id: 1
- name: Alice
id: 2
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author (order_by: [{ id: asc }]) {
name
id
}
}
|]
actual `shouldBe` expected
it "Descending" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author:
- name: Alice
id: 2
- name: Bob
id: 1
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author (order_by: [{ id: desc }]) {
name
id
}
}
|]
actual `shouldBe` expected
describe "Sorting results by strings" do
it "Ascending" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author:
- name: Alice
id: 2
- name: Bob
id: 1
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author (order_by: [{ name: asc }]) {
name
id
}
}
|]
actual `shouldBe` expected
it "Descending" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author:
- name: Bob
id: 1
- name: Alice
id: 2
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author (order_by: [{ name: desc }]) {
name
id
}
}
|]
actual `shouldBe` expected