From 2f94bcf6c74203e1590894a5537eaeaf2716216f Mon Sep 17 00:00:00 2001 From: Tom Harding Date: Tue, 11 Oct 2022 17:17:21 +0200 Subject: [PATCH] Add `Update` tests for Cockroach PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6271 GitOrigin-RevId: ecb38e084f433e29e3ae4e2a6f2039907afcf6d3 --- server/lib/api-tests/api-tests.cabal | 2 + .../test/Test/Mutations/Update/FieldSpec.hs | 175 ++++++++++++++++++ .../Test/Mutations/Update/PrimaryKeySpec.hs | 175 ++++++++++++++++++ .../update/basic/author_by_pk.yaml | 23 --- server/tests-py/test_graphql_mutations.py | 6 - 5 files changed, 352 insertions(+), 29 deletions(-) create mode 100644 server/lib/api-tests/test/Test/Mutations/Update/FieldSpec.hs create mode 100644 server/lib/api-tests/test/Test/Mutations/Update/PrimaryKeySpec.hs delete mode 100644 server/tests-py/queries/graphql_mutation/update/basic/author_by_pk.yaml diff --git a/server/lib/api-tests/api-tests.cabal b/server/lib/api-tests/api-tests.cabal index 46fa5060b35..1a81b710758 100644 --- a/server/lib/api-tests/api-tests.cabal +++ b/server/lib/api-tests/api-tests.cabal @@ -94,6 +94,8 @@ executable api-tests Test.Mutations.Insert.MultipleSpec Test.Mutations.Insert.SingleSpec Test.Mutations.MultiplePerRequest.UpdateManySpec + Test.Mutations.Update.FieldSpec + Test.Mutations.Update.PrimaryKeySpec Test.Postgres.BackendOnlyPermissionsSpec Test.Postgres.DataValidation.PermissionSpec Test.Postgres.DefaultValuesSpec diff --git a/server/lib/api-tests/test/Test/Mutations/Update/FieldSpec.hs b/server/lib/api-tests/test/Test/Mutations/Update/FieldSpec.hs new file mode 100644 index 00000000000..d335e19ea84 --- /dev/null +++ b/server/lib/api-tests/test/Test/Mutations/Update/FieldSpec.hs @@ -0,0 +1,175 @@ +{-# LANGUAGE QuasiQuotes #-} + +-- | +-- Updating an object identified by its fields. +-- +-- https://hasura.io/docs/latest/mutations/postgres/update/#update-objects-based-on-their-fields +-- https://hasura.io/docs/latest/mutations/ms-sql-server/update/#update-objects-based-on-their-fields +module Test.Mutations.Update.FieldSpec where + +import Data.Aeson (Value) +import Data.List.NonEmpty qualified as NE +import Harness.Backend.Citus qualified as Citus +import Harness.Backend.Cockroach qualified as Cockroach +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 (interpolateYaml) +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, it) + +spec :: SpecWith TestEnvironment +spec = do + Fixture.run + ( NE.fromList + [ (Fixture.fixture $ Fixture.Backend Fixture.Postgres) + { Fixture.setupTeardown = \(testEnvironment, _) -> + [ Postgres.setupTablesAction schema testEnvironment + ] + }, + (Fixture.fixture $ Fixture.Backend Fixture.Citus) + { Fixture.setupTeardown = \(testEnvironment, _) -> + [ Citus.setupTablesAction schema testEnvironment + ] + }, + (Fixture.fixture $ Fixture.Backend Fixture.Cockroach) + { Fixture.setupTeardown = \(testEnv, _) -> + [ Cockroach.setupTablesAction schema testEnv + ], + Fixture.customOptions = + Just $ + Fixture.defaultOptions + { Fixture.stringifyNumbers = True + } + }, + (Fixture.fixture $ Fixture.Backend Fixture.SQLServer) + { Fixture.setupTeardown = \(testEnvironment, _) -> + [ Sqlserver.setupTablesAction schema testEnvironment + ] + } + ] + ) + 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 "Justin" + ], + [ Schema.VInt 2, + Schema.VStr "Beltran" + ], + [ Schema.VInt 3, + Schema.VStr "Sidney" + ], + [ Schema.VInt 4, + Schema.VStr "Anjela" + ] + ] + }, + (table "article") + { tableColumns = + [ Schema.column "id" Schema.TInt, + Schema.column "title" Schema.TStr, + Schema.column "content" Schema.TStr, + Schema.column "author_id" Schema.TInt, + Schema.column "rating" Schema.TInt, + Schema.column "is_published" Schema.TBool + ], + tablePrimaryKey = ["id"], + tableReferences = + [ Schema.Reference "author_id" "author" "id" + ], + tableData = + [ [ Schema.VInt 1, + Schema.VStr "Article 1", + Schema.VStr "Content 1", + Schema.VInt 1, + Schema.VInt 2, + Schema.VBool True + ], + [ Schema.VInt 2, + Schema.VStr "Article 2", + Schema.VStr "Content 2", + Schema.VInt 2, + Schema.VInt 5, + Schema.VBool True + ], + [ Schema.VInt 3, + Schema.VStr "Article 3", + Schema.VStr "Content 3", + Schema.VInt 3, + Schema.VInt 1, + Schema.VBool True + ] + ] + } + ] + +-------------------------------------------------------------------------------- +-- Tests + +tests :: Fixture.Options -> SpecWith TestEnvironment +tests opts = do + let shouldBe :: IO Value -> Value -> IO () + shouldBe = shouldReturnYaml opts + + it "Update an article by its fields" \testEnvironment -> do + let schemaName :: Schema.SchemaName + schemaName = Schema.getSchemaName testEnvironment + + actual :: IO Value + actual = + postGraphql + testEnvironment + [graphql| + mutation update_article { + update_#{schemaName}_article( + where: {rating: {_lte: 2}}, + _set: { + rating: 1, + is_published: false + } + ) { + affected_rows + returning { + id + title + rating + } + } + } + |] + + expected :: Value + expected = + [interpolateYaml| + data: + update_#{schemaName}_article: + affected_rows: 2 + returning: + - id: 1 + title: "Article 1" + rating: 1 + - id: 3 + title: "Article 3" + rating: 1 + |] + + actual `shouldBe` expected diff --git a/server/lib/api-tests/test/Test/Mutations/Update/PrimaryKeySpec.hs b/server/lib/api-tests/test/Test/Mutations/Update/PrimaryKeySpec.hs new file mode 100644 index 00000000000..7ba397792e2 --- /dev/null +++ b/server/lib/api-tests/test/Test/Mutations/Update/PrimaryKeySpec.hs @@ -0,0 +1,175 @@ +{-# LANGUAGE QuasiQuotes #-} + +-- | +-- Updating an object identified by primary key. +-- +-- https://hasura.io/docs/latest/mutations/postgres/update/#update-an-object-by-its-primary-key +-- https://hasura.io/docs/latest/mutations/ms-sql-server/update/#update-an-object-by-its-primary-key +module Test.Mutations.Update.PrimaryKeySpec where + +import Data.Aeson (Value) +import Data.List.NonEmpty qualified as NE +import Harness.Backend.Citus qualified as Citus +import Harness.Backend.Cockroach qualified as Cockroach +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 (interpolateYaml) +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, it) + +spec :: SpecWith TestEnvironment +spec = do + Fixture.run + ( NE.fromList + [ (Fixture.fixture $ Fixture.Backend Fixture.Postgres) + { Fixture.setupTeardown = \(testEnvironment, _) -> + [ Postgres.setupTablesAction schema testEnvironment + ] + }, + (Fixture.fixture $ Fixture.Backend Fixture.Citus) + { Fixture.setupTeardown = \(testEnvironment, _) -> + [ Citus.setupTablesAction schema testEnvironment + ] + }, + (Fixture.fixture $ Fixture.Backend Fixture.Cockroach) + { Fixture.setupTeardown = \(testEnv, _) -> + [ Cockroach.setupTablesAction schema testEnv + ], + Fixture.customOptions = + Just $ + Fixture.defaultOptions + { Fixture.stringifyNumbers = True + } + }, + (Fixture.fixture $ Fixture.Backend Fixture.SQLServer) + { Fixture.setupTeardown = \(testEnvironment, _) -> + [ Sqlserver.setupTablesAction schema testEnvironment + ] + } + ] + ) + 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 "Justin" + ], + [ Schema.VInt 2, + Schema.VStr "Beltran" + ], + [ Schema.VInt 3, + Schema.VStr "Sidney" + ], + [ Schema.VInt 4, + Schema.VStr "Anjela" + ] + ] + }, + (table "article") + { tableColumns = + [ Schema.column "id" Schema.TInt, + Schema.column "title" Schema.TStr, + Schema.column "content" Schema.TStr, + Schema.column "author_id" Schema.TInt, + Schema.column "is_published" Schema.TBool + ], + tablePrimaryKey = ["id"], + tableReferences = + [ Schema.Reference "author_id" "author" "id" + ], + tableData = + [ [ Schema.VInt 1, + Schema.VStr "Article 1", + Schema.VStr "Content 1", + Schema.VInt 1, + Schema.VBool False + ] + ] + } + ] + +-------------------------------------------------------------------------------- +-- Tests + +tests :: Fixture.Options -> SpecWith TestEnvironment +tests opts = do + let shouldBe :: IO Value -> Value -> IO () + shouldBe = shouldReturnYaml opts + + it "Update an article by its primary key" \testEnvironment -> do + let schemaName :: Schema.SchemaName + schemaName = Schema.getSchemaName testEnvironment + + actual :: IO Value + actual = + postGraphql + testEnvironment + [graphql| + mutation update_an_article { + update_#{schemaName}_article_by_pk ( + pk_columns: { id: 1 } + _set: { is_published: true } + ) { + id + is_published + } + } + |] + + expected :: Value + expected = + [interpolateYaml| + data: + update_#{schemaName}_article_by_pk: + id: 1 + is_published: true + |] + + actual `shouldBe` expected + + it "Does nothing for nonexistent primary keys" \testEnvironment -> do + let schemaName :: Schema.SchemaName + schemaName = Schema.getSchemaName testEnvironment + + actual :: IO Value + actual = + postGraphql + testEnvironment + [graphql| + mutation update_an_article { + update_#{schemaName}_article_by_pk ( + pk_columns: { id: 2 } + _set: { is_published: true } + ) { + id + is_published + } + } + |] + + expected :: Value + expected = + [interpolateYaml| + data: + update_#{schemaName}_article_by_pk: null + |] + + actual `shouldBe` expected diff --git a/server/tests-py/queries/graphql_mutation/update/basic/author_by_pk.yaml b/server/tests-py/queries/graphql_mutation/update/basic/author_by_pk.yaml deleted file mode 100644 index 936cb59001c..00000000000 --- a/server/tests-py/queries/graphql_mutation/update/basic/author_by_pk.yaml +++ /dev/null @@ -1,23 +0,0 @@ -description: Update a row of author by primary key -url: /v1/graphql -status: 200 -query: - query: | - mutation { - update_author_by_pk( - pk_columns: {id: 1} - _set: {emails: "{author1@hasura.io}"} - ){ - id - name - emails - } - } - -response: - data: - update_author_by_pk: - id: 1 - name: Author 1 - emails: - - author1@hasura.io diff --git a/server/tests-py/test_graphql_mutations.py b/server/tests-py/test_graphql_mutations.py index 93e014212b4..9bf24dacdeb 100644 --- a/server/tests-py/test_graphql_mutations.py +++ b/server/tests-py/test_graphql_mutations.py @@ -426,12 +426,6 @@ class TestGraphqlUpdateBasic: def test_column_in_multiple_operators(self, hge_ctx): check_query_f(hge_ctx, self.dir() + "/article_column_multiple_operators.yaml") - def test_author_by_pk(self, hge_ctx): - check_query_f(hge_ctx, self.dir() + "/author_by_pk.yaml") - - def test_author_by_pk_null(self, hge_ctx): - check_query_f(hge_ctx, self.dir() + "/author_by_pk_null.yaml") - def test_numerics_inc(self, hge_ctx): check_query_f(hge_ctx, self.dir() + "/numerics_inc.yaml")