From 04887f00de671e55c0a4d2a7f534e4664021089f Mon Sep 17 00:00:00 2001 From: Tom Harding Date: Wed, 27 Jul 2022 10:07:14 +0100 Subject: [PATCH] Remove `Test.OrderingSpec`, create `Test.Queries.SortSpec` PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5188 GitOrigin-RevId: 8d5ca048a838a29991ca4b4425ce0439c60f94b1 --- server/graphql-engine.cabal | 2 +- server/tests-hspec/Test/OrderingSpec.hs | 148 -------------- server/tests-hspec/Test/Queries/SortSpec.hs | 210 ++++++++++++++++++++ 3 files changed, 211 insertions(+), 149 deletions(-) delete mode 100644 server/tests-hspec/Test/OrderingSpec.hs create mode 100644 server/tests-hspec/Test/Queries/SortSpec.hs diff --git a/server/graphql-engine.cabal b/server/graphql-engine.cabal index af9cbf853a3..c6619299d6e 100644 --- a/server/graphql-engine.cabal +++ b/server/graphql-engine.cabal @@ -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 diff --git a/server/tests-hspec/Test/OrderingSpec.hs b/server/tests-hspec/Test/OrderingSpec.hs deleted file mode 100644 index f8b289ace19..00000000000 --- a/server/tests-hspec/Test/OrderingSpec.hs +++ /dev/null @@ -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 -|] diff --git a/server/tests-hspec/Test/Queries/SortSpec.hs b/server/tests-hspec/Test/Queries/SortSpec.hs new file mode 100644 index 00000000000..921a69dab48 --- /dev/null +++ b/server/tests-hspec/Test/Queries/SortSpec.hs @@ -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