Remove Test.OrderingSpec, create Test.Queries.SortSpec

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5188
GitOrigin-RevId: 8d5ca048a838a29991ca4b4425ce0439c60f94b1
This commit is contained in:
Tom Harding 2022-07-27 10:07:14 +01:00 committed by hasura-bot
parent 9a55b50d0e
commit 04887f00de
3 changed files with 211 additions and 149 deletions

View File

@ -1259,7 +1259,7 @@ test-suite tests-hspec
Test.NestedRelationshipsSpec
Test.ObjectRelationshipsLimitSpec
Test.Queries.FilterSearchSpec
Test.OrderingSpec
Test.Queries.SortSpec
Test.PostgresTypesSpec
Test.PrimaryKeySpec
Test.Queries.Paginate.LimitSpec

View File

@ -1,148 +0,0 @@
{-# LANGUAGE QuasiQuotes #-}
-- | Test ordering by fields.
module Test.OrderingSpec (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"]
]
}
--------------------------------------------------------------------------------
-- Tests
tests :: Context.Options -> SpecWith TestEnvironment
tests opts = do
it "Order by id ascending" $ \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
[graphql|
query {
hasura_author (order_by: {id: asc}) {
name
id
}
}
|]
)
[yaml|
data:
hasura_author:
- name: Author 1
id: 1
- name: Author 2
id: 2
|]
it "Order by id descending" $ \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
[graphql|
query {
hasura_author (order_by: {id: desc}) {
name
id
}
}
|]
)
[yaml|
data:
hasura_author:
- name: Author 2
id: 2
- name: Author 1
id: 1
|]
it "Order by name ascending" $ \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
[graphql|
query {
hasura_author (order_by: {name: asc}) {
name
id
}
}
|]
)
[yaml|
data:
hasura_author:
- name: Author 1
id: 1
- name: Author 2
id: 2
|]
it "Order by name descending" $ \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
[graphql|
query {
hasura_author (order_by: {name: desc}) {
name
id
}
}
|]
)
[yaml|
data:
hasura_author:
- name: Author 2
id: 2
- name: Author 1
id: 1
|]

View File

@ -0,0 +1,210 @@
{-# 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 (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
--------------------------------------------------------------------------------
-- Preamble
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 "Bob"],
[Schema.VInt 2, Schema.VStr "Alice"]
]
}
]
--------------------------------------------------------------------------------
-- Tests
tests :: Context.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