mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-14 17:02:49 +03:00
Move the InsertOnConflictSpec
test to regressions
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5495 GitOrigin-RevId: be6986ca1073b76be21fe52ef217dc84f901fe81
This commit is contained in:
parent
7e6c9df291
commit
b4639fd10f
@ -1250,7 +1250,6 @@ test-suite tests-hspec
|
||||
Test.EventTrigger.EventTriggersExtensionSchemaSpec
|
||||
Test.HelloWorldSpec
|
||||
Test.InsertDefaultsSpec
|
||||
Test.InsertOnConflictSpec
|
||||
Test.LongIdentifiersSpec
|
||||
Test.Mutations.MultiplePerRequest.UpdateManySpec
|
||||
Test.ObjectRelationshipsLimitSpec
|
||||
@ -1271,6 +1270,7 @@ test-suite tests-hspec
|
||||
Test.Queries.SortSpec
|
||||
Test.Quoter.YamlSpec
|
||||
Test.Regression.DoNotTruncateSessionVariables8158Spec
|
||||
Test.Regression.InsertOnConflict8260Spec
|
||||
Test.Regression.NullsOrderParsing8780Spec
|
||||
Test.RemoteRelationship.FromRemoteSchemaSpec
|
||||
Test.RemoteRelationship.MetadataAPI.ClearMetadataSpec
|
||||
|
@ -1,146 +0,0 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE QuasiQuotes #-}
|
||||
|
||||
-- | Tests of the Postgres-specific upsert feature.
|
||||
module Test.InsertOnConflictSpec (spec) where
|
||||
|
||||
import Data.List.NonEmpty qualified as NE
|
||||
import Data.Text
|
||||
import Data.Text.Encoding (encodeUtf8)
|
||||
import Harness.Backend.Citus qualified as Citus
|
||||
import Harness.Backend.Postgres qualified as Postgres
|
||||
import Harness.GraphqlEngine qualified as GraphqlEngine
|
||||
import Harness.Quoter.Graphql
|
||||
import Harness.Quoter.Yaml
|
||||
import Harness.Test.BackendType qualified as BackendType
|
||||
import Harness.Test.Fixture
|
||||
import Harness.Test.Introspection
|
||||
import Harness.Test.Permissions (Permission (..))
|
||||
import Harness.Test.Schema
|
||||
import Harness.TestEnvironment (TestEnvironment)
|
||||
import Hasura.Prelude
|
||||
import Test.Hspec
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Preamble
|
||||
|
||||
spec :: SpecWith TestEnvironment
|
||||
spec = run (NE.fromList [postgresFixture, citusFixture]) tests
|
||||
|
||||
postgresFixture :: Fixture ()
|
||||
postgresFixture =
|
||||
(fixture $ Backend BackendType.Postgres)
|
||||
{ setupTeardown = \(t, _) ->
|
||||
[ Postgres.setupTablesAction tables t,
|
||||
Postgres.setupPermissionsAction (permissions "postgres") t
|
||||
]
|
||||
}
|
||||
|
||||
citusFixture :: Fixture ()
|
||||
citusFixture =
|
||||
(fixture $ Backend BackendType.Citus)
|
||||
{ setupTeardown = \(t, _) ->
|
||||
[ Citus.setupTablesAction tables t,
|
||||
Citus.setupPermissionsAction (permissions "citus") t
|
||||
]
|
||||
}
|
||||
|
||||
tables :: [Table]
|
||||
tables =
|
||||
[ (table "foo")
|
||||
{ tableColumns =
|
||||
[ column "id" TInt,
|
||||
column "bar" TStr
|
||||
],
|
||||
tablePrimaryKey = ["id"],
|
||||
tableData =
|
||||
[ [VInt 0, VStr "initial"]
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
permissions :: Text -> [Permission]
|
||||
permissions source =
|
||||
[ SelectPermission
|
||||
{ permissionTable = "foo",
|
||||
permissionSource = source,
|
||||
permissionRole = "role-select-only",
|
||||
permissionColumns = ["id", "bar"]
|
||||
},
|
||||
InsertPermission
|
||||
{ permissionTable = "foo",
|
||||
permissionSource = source,
|
||||
permissionRole = "role-insert-only",
|
||||
permissionColumns = ["id", "bar"]
|
||||
}
|
||||
]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Tests
|
||||
|
||||
tests :: Options -> SpecWith TestEnvironment
|
||||
tests _ =
|
||||
-- Tests relating to https://github.com/hasura/graphql-engine/issues/8260
|
||||
describe "The schema for insert mutations with an 'on_conflict' clause" do
|
||||
describe "When no columns are updateable" do
|
||||
it "Is still present with an empty enum" testEmptyUpdateColumns
|
||||
it "Inserts ignoring duplicates" testInsertDoNothing
|
||||
|
||||
testEmptyUpdateColumns :: TestEnvironment -> IO ()
|
||||
testEmptyUpdateColumns env = do
|
||||
introspectTypes env "role-insert-only"
|
||||
>>= (`shouldContain` ["hasura_foo_on_conflict"])
|
||||
|
||||
introspectEnums env "role-insert-only"
|
||||
>>= (`shouldContain` [("hasura_foo_update_column", ["_PLACEHOLDER"])])
|
||||
|
||||
testInsertDoNothing :: TestEnvironment -> IO ()
|
||||
testInsertDoNothing env = do
|
||||
-- We can insert ignoring duplicates
|
||||
GraphqlEngine.postGraphqlWithHeaders
|
||||
env
|
||||
[("X-Hasura-Role", encodeUtf8 "role-insert-only")]
|
||||
[graphql|
|
||||
mutation OnConflictDoNothing {
|
||||
insert_hasura_foo
|
||||
(
|
||||
objects: [
|
||||
{bar: "untouched", id: 0},
|
||||
{bar: "inserted", id: 1}],
|
||||
on_conflict: {constraint: foo_pkey, update_columns: []}
|
||||
)
|
||||
{
|
||||
affected_rows
|
||||
}
|
||||
}
|
||||
|]
|
||||
>>= ( `shouldBe`
|
||||
[yaml|
|
||||
data:
|
||||
insert_hasura_foo:
|
||||
affected_rows: 1
|
||||
|]
|
||||
)
|
||||
|
||||
-- The data actually gets stored
|
||||
GraphqlEngine.postGraphqlWithHeaders
|
||||
env
|
||||
[("X-Hasura-Role", encodeUtf8 "role-select-only")]
|
||||
[graphql|
|
||||
query ActualData {
|
||||
hasura_foo {
|
||||
bar
|
||||
id
|
||||
}
|
||||
}
|
||||
|]
|
||||
>>= ( `shouldBe`
|
||||
[yaml|
|
||||
data:
|
||||
hasura_foo:
|
||||
- bar: "initial"
|
||||
id: 0
|
||||
- bar: "inserted"
|
||||
id: 1
|
||||
|]
|
||||
)
|
159
server/tests-hspec/Test/Regression/InsertOnConflict8260Spec.hs
Normal file
159
server/tests-hspec/Test/Regression/InsertOnConflict8260Spec.hs
Normal file
@ -0,0 +1,159 @@
|
||||
{-# LANGUAGE QuasiQuotes #-}
|
||||
|
||||
-- | Tests of the Postgres-specific upsert feature.
|
||||
-- https://github.com/hasura/graphql-engine/issues/8260
|
||||
module Test.Regression.InsertOnConflict8260Spec (spec) where
|
||||
|
||||
import Data.Aeson (Value)
|
||||
import Data.List.NonEmpty qualified as NE
|
||||
import Data.Text.Encoding (encodeUtf8)
|
||||
import Harness.Backend.Citus qualified as Citus
|
||||
import Harness.Backend.Postgres qualified as Postgres
|
||||
import Harness.GraphqlEngine (postGraphqlWithHeaders)
|
||||
import Harness.Quoter.Graphql (graphql)
|
||||
import Harness.Quoter.Yaml (yaml)
|
||||
import Harness.Test.BackendType qualified as BackendType
|
||||
import Harness.Test.Fixture qualified as Fixture
|
||||
import Harness.Test.Introspection (introspectEnums, introspectTypes)
|
||||
import Harness.Test.Permissions (Permission (..))
|
||||
import Harness.Test.Schema qualified as Schema
|
||||
import Harness.TestEnvironment (TestEnvironment)
|
||||
import Harness.Yaml (shouldReturnYaml)
|
||||
import Hasura.Prelude
|
||||
import Test.Hspec (SpecWith, describe, it, shouldContain)
|
||||
|
||||
spec :: SpecWith TestEnvironment
|
||||
spec = do
|
||||
Fixture.run
|
||||
( NE.fromList
|
||||
[ (Fixture.fixture $ Fixture.Backend BackendType.Postgres)
|
||||
{ Fixture.setupTeardown = \(testEnvironment, _) ->
|
||||
[ Postgres.setupTablesAction schema testEnvironment,
|
||||
Postgres.setupPermissionsAction (permissions "postgres") testEnvironment
|
||||
]
|
||||
},
|
||||
(Fixture.fixture $ Fixture.Backend BackendType.Citus)
|
||||
{ Fixture.setupTeardown = \(testEnvironment, _) ->
|
||||
[ Citus.setupTablesAction schema testEnvironment,
|
||||
Citus.setupPermissionsAction (permissions "citus") testEnvironment
|
||||
]
|
||||
}
|
||||
]
|
||||
)
|
||||
tests
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Schema
|
||||
|
||||
schema :: [Schema.Table]
|
||||
schema =
|
||||
[ (Schema.table "foo")
|
||||
{ Schema.tableColumns =
|
||||
[ Schema.column "id" Schema.TInt,
|
||||
Schema.column "bar" Schema.TStr
|
||||
],
|
||||
Schema.tablePrimaryKey = ["id"],
|
||||
Schema.tableData =
|
||||
[ [ Schema.VInt 0,
|
||||
Schema.VStr "initial"
|
||||
]
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Permissions
|
||||
|
||||
permissions :: Text -> [Permission]
|
||||
permissions source =
|
||||
[ SelectPermission
|
||||
{ permissionTable = "foo",
|
||||
permissionSource = source,
|
||||
permissionRole = "role-select-only",
|
||||
permissionColumns = ["id", "bar"]
|
||||
},
|
||||
InsertPermission
|
||||
{ permissionTable = "foo",
|
||||
permissionSource = source,
|
||||
permissionRole = "role-insert-only",
|
||||
permissionColumns = ["id", "bar"]
|
||||
}
|
||||
]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Tests
|
||||
|
||||
tests :: Fixture.Options -> SpecWith TestEnvironment
|
||||
tests opts = do
|
||||
let shouldBe :: IO Value -> Value -> IO ()
|
||||
shouldBe = shouldReturnYaml opts
|
||||
|
||||
describe "The schema for insert mutations with an 'on_conflict' clause" do
|
||||
describe "When no columns are updateable" do
|
||||
it "Is still present with an empty enum" \testEnvironment -> do
|
||||
types <- introspectTypes testEnvironment "role-insert-only"
|
||||
types `shouldContain` ["hasura_foo_on_conflict"]
|
||||
|
||||
enums <- introspectEnums testEnvironment "role-insert-only"
|
||||
enums `shouldContain` [("hasura_foo_update_column", ["_PLACEHOLDER"])]
|
||||
|
||||
it "Inserts ignoring duplicates" \testEnvironment -> do
|
||||
do
|
||||
let expected :: Value
|
||||
expected =
|
||||
[yaml|
|
||||
data:
|
||||
insert_hasura_foo:
|
||||
affected_rows: 1
|
||||
|]
|
||||
|
||||
actual :: IO Value
|
||||
actual =
|
||||
postGraphqlWithHeaders
|
||||
testEnvironment
|
||||
[ ("X-Hasura-Role", encodeUtf8 "role-insert-only")
|
||||
]
|
||||
[graphql|
|
||||
mutation OnConflictDoNothing {
|
||||
insert_hasura_foo
|
||||
(
|
||||
objects: [
|
||||
{bar: "untouched", id: 0},
|
||||
{bar: "inserted", id: 1}],
|
||||
on_conflict: {constraint: foo_pkey, update_columns: []}
|
||||
)
|
||||
{
|
||||
affected_rows
|
||||
}
|
||||
}
|
||||
|]
|
||||
|
||||
actual `shouldBe` expected
|
||||
|
||||
do
|
||||
let expected :: Value
|
||||
expected =
|
||||
[yaml|
|
||||
data:
|
||||
hasura_foo:
|
||||
- bar: "initial"
|
||||
id: 0
|
||||
- bar: "inserted"
|
||||
id: 1
|
||||
|]
|
||||
|
||||
actual :: IO Value
|
||||
actual =
|
||||
postGraphqlWithHeaders
|
||||
testEnvironment
|
||||
[("X-Hasura-Role", encodeUtf8 "role-select-only")]
|
||||
[graphql|
|
||||
query ActualData {
|
||||
hasura_foo {
|
||||
bar
|
||||
id
|
||||
}
|
||||
}
|
||||
|]
|
||||
|
||||
actual `shouldBe` expected
|
Loading…
Reference in New Issue
Block a user