2022-07-27 12:07:14 +03:00
|
|
|
{-# 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)
|
2022-08-05 12:34:25 +03:00
|
|
|
import Harness.Quoter.Yaml (yaml)
|
|
|
|
import Harness.Test.Context (Options (..))
|
2022-08-02 21:01:34 +03:00
|
|
|
import Harness.Test.Fixture qualified as Fixture
|
2022-07-27 12:07:14 +03:00
|
|
|
import Harness.Test.Schema (Table (..), table)
|
|
|
|
import Harness.Test.Schema qualified as Schema
|
|
|
|
import Harness.TestEnvironment (TestEnvironment)
|
2022-08-05 12:34:25 +03:00
|
|
|
import Harness.Yaml (shouldReturnYaml)
|
2022-08-03 17:18:43 +03:00
|
|
|
import Hasura.Prelude
|
2022-07-27 12:07:14 +03:00
|
|
|
import Test.Hspec (SpecWith, describe, it)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Preamble
|
|
|
|
|
|
|
|
spec :: SpecWith TestEnvironment
|
|
|
|
spec = do
|
2022-08-02 21:01:34 +03:00
|
|
|
Fixture.run
|
|
|
|
[ (Fixture.fixture $ Fixture.Backend Fixture.MySQL)
|
|
|
|
{ Fixture.setupTeardown = \(testEnv, _) ->
|
|
|
|
[ Mysql.setupTablesAction schema testEnv
|
|
|
|
]
|
2022-07-27 12:07:14 +03:00
|
|
|
},
|
2022-08-02 21:01:34 +03:00
|
|
|
(Fixture.fixture $ Fixture.Backend Fixture.Postgres)
|
|
|
|
{ Fixture.setupTeardown = \(testEnv, _) ->
|
|
|
|
[ Postgres.setupTablesAction schema testEnv
|
|
|
|
]
|
2022-07-27 12:07:14 +03:00
|
|
|
},
|
2022-08-02 21:01:34 +03:00
|
|
|
(Fixture.fixture $ Fixture.Backend Fixture.Citus)
|
|
|
|
{ Fixture.setupTeardown = \(testEnv, _) ->
|
|
|
|
[ Citus.setupTablesAction schema testEnv
|
|
|
|
]
|
2022-07-27 12:07:14 +03:00
|
|
|
},
|
2022-08-02 21:01:34 +03:00
|
|
|
(Fixture.fixture $ Fixture.Backend Fixture.SQLServer)
|
|
|
|
{ Fixture.setupTeardown = \(testEnv, _) ->
|
|
|
|
[ Sqlserver.setupTablesAction schema testEnv
|
|
|
|
]
|
2022-07-27 12:07:14 +03:00
|
|
|
},
|
2022-08-02 21:01:34 +03:00
|
|
|
(Fixture.fixture $ Fixture.Backend Fixture.BigQuery)
|
|
|
|
{ Fixture.setupTeardown = \(testEnv, _) ->
|
|
|
|
[ BigQuery.setupTablesAction schema testEnv
|
|
|
|
],
|
|
|
|
Fixture.customOptions =
|
2022-07-27 12:07:14 +03:00
|
|
|
Just $
|
2022-08-02 21:01:34 +03:00
|
|
|
Fixture.Options
|
2022-07-27 12:07:14 +03:00
|
|
|
{ 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
|
|
|
|
|
2022-08-02 21:01:34 +03:00
|
|
|
tests :: Fixture.Options -> SpecWith TestEnvironment
|
2022-07-27 12:07:14 +03:00
|
|
|
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
|