Move UpdateManySpec into Test.Mutations.MultiplePerRequest

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5206
GitOrigin-RevId: a3f1d37de38c40c2e08506ed30eb0a56ce3bb45e
This commit is contained in:
Tom Harding 2022-07-28 09:53:26 +01:00 committed by hasura-bot
parent 94ddf10df6
commit f7392a26e3
3 changed files with 250 additions and 240 deletions

View File

@ -1257,6 +1257,7 @@ test-suite tests-hspec
Test.InsertEnumColumnSpec
Test.InsertOnConflictSpec
Test.LongIdentifiersSpec
Test.Mutations.MultiplePerRequest.UpdateManySpec
Test.NestedRelationshipsSpec
Test.ObjectRelationshipsLimitSpec
Test.Queries.FilterSearchSpec
@ -1283,7 +1284,6 @@ test-suite tests-hspec
Test.SQLServer.InsertVarcharColumnSpec
Test.SerializationSpec
Test.ServiceLivenessSpec
Test.UpdateManySpec
Test.ViewsSpec
test-suite tests-dc-api

View File

@ -0,0 +1,249 @@
{-# LANGUAGE QuasiQuotes #-}
-- | Test around performing multiple update requests at the same time.
--
-- https://hasura.io/docs/latest/mutations/postgres/multiple-mutations/
module Test.Mutations.MultiplePerRequest.UpdateManySpec (spec) where
import Data.Aeson (Value)
import Harness.Backend.Citus qualified as Citus
import Harness.Backend.Postgres qualified as Postgres
import Harness.GraphqlEngine (postGraphql)
import Harness.Quoter.Graphql (graphql)
import Harness.Quoter.Yaml (shouldReturnYaml, 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 (SpecWith, describe, it)
import Prelude
spec :: SpecWith TestEnvironment
spec = do
-- TODO: this test causes an internal server error for MySQL, even if we add
-- "SERIAL" as the 'Schema.defaultSerialType' for MySQL.
Context.run
[ 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
}
]
tests
--------------------------------------------------------------------------------
-- Schema
schema :: [Schema.Table]
schema =
[ (table "author")
{ tableColumns =
[ Schema.column "id" Schema.defaultSerialType,
Schema.column "name" Schema.TStr
],
tablePrimaryKey = ["id"],
tableData =
[ [Schema.VInt 1, Schema.VStr "Author 1"],
[Schema.VInt 2, Schema.VStr "Author 2"],
[Schema.VInt 3, Schema.VStr "Author 3"]
]
}
]
--------------------------------------------------------------------------------
-- Tests
tests :: Context.Options -> SpecWith TestEnvironment
tests opts = do
let shouldBe :: IO Value -> Value -> IO ()
shouldBe = shouldReturnYaml opts
describe "Update many objects" do
it "When many equals zero" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
update_hasura_author_many:
- affected_rows: 0
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
mutation {
update_hasura_author_many(
updates: [
{ where: { id: { _eq: 10 } }
_set: { name: "test" }
}
]
) {
affected_rows
}
}
|]
actual `shouldBe` expected
it "When many equals one" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
update_hasura_author_many:
- affected_rows: 0
- affected_rows: 1
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
mutation {
update_hasura_author_many(
updates: [
{ where: { id: { _eq: 10 } }
_set: { name: "test" }
}
{ where: { id: { _gt: 2 } }
_set: { name: "test" }
}
]
){
affected_rows
}
}
|]
actual `shouldBe` expected
it "When many is more than one" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
update_hasura_author_many:
- affected_rows: 1
returning:
- name: "test"
- affected_rows: 1
returning:
- name: "changed name"
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
mutation {
update_hasura_author_many(
updates: [
{ where: { id: { _gt: 2 } }
_set: { name: "test" }
}
{ where: { name: { _eq: "test" } }
_set: { name: "changed name" }
}
]
){
affected_rows
returning {
name
}
}
}
|]
actual `shouldBe` expected
it "Update record multiple times with overlapping conditions" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
update_hasura_author_many:
- affected_rows: 1
returning:
- name: "test"
- affected_rows: 1
returning:
- name: "changed name"
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
mutation {
update_hasura_author_many(
updates: [
{ where: { id: { _gt: 2 } }
_set: { name: "test" }
}
{ where: { id: { _eq: 3 } }
_set: { name: "changed name" }
}
]
) {
affected_rows
returning {
name
}
}
}
|]
actual `shouldBe` expected
it "Reverts on error" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
errors:
- extensions:
code: validation-failed
path: $.selectionSet.update_hasura_author_many.selectionSet.returning.selectionSet.made_up_field
message: 'field ''made_up_field'' not found in type: ''hasura_author'''
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
mutation {
update_hasura_author_many(
updates: [
{ where: { id: { _eq: 1 } }
_set: { name: "test" }
}
{ where: { id: { _eq: 1 } }
_set: { name: "tested" }
}
]
) {
affected_rows
returning {
made_up_field
}
}
}
|]
actual `shouldBe` expected

View File

@ -1,239 +0,0 @@
{-# LANGUAGE QuasiQuotes #-}
-- | Test multiple updates (update_table_many).
module Test.UpdateManySpec (spec) where
import Harness.Backend.Postgres qualified as Postgres
import Harness.GraphqlEngine qualified as GraphqlEngine
import Harness.Quoter.Graphql (graphql)
import Harness.Quoter.Yaml (shouldReturnYaml, 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 (SpecWith, describe, it)
import Prelude
--------------------------------------------------------------------------------
-- ** Preamble
spec :: SpecWith TestEnvironment
spec =
describe "UpdateManySpec" $ Context.run [postgresContext] tests
where
postgresContext =
Context.Context
{ name = Context.Backend Context.Postgres,
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
setup = Postgres.setup schema,
teardown = Postgres.teardown schema,
customOptions = Nothing
}
--------------------------------------------------------------------------------
-- ** Schema
schema :: [Schema.Table]
schema =
[ artist,
album
]
artist :: Schema.Table
artist =
(table "artist")
{ tableColumns =
[ Schema.column "id" Schema.defaultSerialType,
Schema.column "name" Schema.TStr
],
tablePrimaryKey = ["id"],
tableData =
[ [Schema.VInt 1, Schema.VStr "first"],
[Schema.VInt 2, Schema.VStr "second"],
[Schema.VInt 3, Schema.VStr "third"]
]
}
album :: Schema.Table
album =
(table "album")
{ tableColumns =
[ Schema.column "id" Schema.defaultSerialType,
Schema.column "albumname" Schema.TStr,
Schema.column "artist_id" Schema.TInt
],
tablePrimaryKey = ["albumname"],
tableReferences = [Schema.Reference "artist_id" "artist" "id"],
tableData =
[ [Schema.VInt 1, Schema.VStr "first album", Schema.VInt 1],
[Schema.VInt 2, Schema.VStr "second album", Schema.VInt 2],
[Schema.VInt 3, Schema.VStr "third album", Schema.VInt 3]
]
}
--------------------------------------------------------------------------------
-- * Tests
tests :: Context.Options -> SpecWith TestEnvironment
tests opts = do
it "Update no records" $ \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
[graphql|
mutation {
update_hasura_artist_many(
updates: [
{ where: { id: { _eq: 10 } }
_set: { name: "test" }
}
]
){
affected_rows
}
}
|]
)
[yaml|
data:
update_hasura_artist_many:
- affected_rows: 0
|]
it "Update single record" $ \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
[graphql|
mutation {
update_hasura_artist_many(
updates: [
{ where: { id: { _eq: 10 } }
_set: { name: "test" }
}
{ where: { id: { _gt: 2 } }
_set: { name: "test" }
}
]
){
affected_rows
}
}
|]
)
[yaml|
data:
update_hasura_artist_many:
- affected_rows: 0
- affected_rows: 1
|]
it "Update record multiple times" $ \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
[graphql|
mutation {
update_hasura_artist_many(
updates: [
{ where: { id: { _gt: 2 } }
_set: { name: "test" }
}
{ where: { name: { _eq: "test" } }
_set: { name: "changed name" }
}
]
){
affected_rows
returning {
name
}
}
}
|]
)
[yaml|
data:
update_hasura_artist_many:
- affected_rows: 1
returning:
- name: "test"
- affected_rows: 1
returning:
- name: "changed name"
|]
it "Update record multiple times with overlapping conditions" $ \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
[graphql|
mutation {
update_hasura_artist_many(
updates: [
{ where: { id: { _gt: 2 } }
_set: { name: "test" }
}
{ where: { id: { _eq: 3 } }
_set: { name: "changed name" }
}
]
){
affected_rows
returning {
name
}
}
}
|]
)
[yaml|
data:
update_hasura_artist_many:
- affected_rows: 1
returning:
- name: "test"
- affected_rows: 1
returning:
- name: "changed name"
|]
it "Revert on error" $ \testEnvironment ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnvironment
[graphql|
mutation {
update_hasura_album_many(
updates: [
{ where: { id: { _eq: 1 } }
_set: { albumname: "test" }
}
{ where: { id: { _eq: 1 } }
_set: { artist_id: 4 }
}
]
){
affected_rows
returning {
name
}
}
}
|]
)
[yaml|
errors:
- extensions:
code: validation-failed
path: $.selectionSet.update_hasura_album_many.selectionSet.returning.selectionSet.name
message: 'field ''name'' not found in type: ''hasura_album'''
|]