Add Update tests for Cockroach

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6271
GitOrigin-RevId: ecb38e084f433e29e3ae4e2a6f2039907afcf6d3
This commit is contained in:
Tom Harding 2022-10-11 17:17:21 +02:00 committed by hasura-bot
parent 4237c56419
commit 2f94bcf6c7
5 changed files with 352 additions and 29 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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")