2022-05-11 19:00:10 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
|
|
|
-- | Test insert with default values
|
|
|
|
module Test.InsertDefaultsSpec (spec) where
|
|
|
|
|
2022-08-03 17:18:43 +03:00
|
|
|
import Data.List.NonEmpty qualified as NE
|
2022-05-11 19:00:10 +03:00
|
|
|
import Harness.Backend.Citus qualified as Citus
|
|
|
|
import Harness.Backend.Postgres qualified as Postgres
|
|
|
|
import Harness.Backend.Sqlserver qualified as Sqlserver
|
|
|
|
import Harness.GraphqlEngine qualified as GraphqlEngine
|
|
|
|
import Harness.Quoter.Graphql (graphql)
|
2022-08-05 12:34:25 +03:00
|
|
|
import Harness.Quoter.Yaml (yaml)
|
2022-05-11 19:00:10 +03:00
|
|
|
import Harness.Test.Context qualified as Context
|
2022-06-17 11:44:04 +03:00
|
|
|
import Harness.Test.Schema (Table (..), table)
|
2022-05-11 19:00:10 +03:00
|
|
|
import Harness.Test.Schema qualified as Schema
|
|
|
|
import Harness.TestEnvironment (TestEnvironment)
|
2022-08-05 12:34:25 +03:00
|
|
|
import Harness.Yaml (shouldReturnYaml)
|
2022-08-03 17:18:43 +03:00
|
|
|
import Hasura.Prelude
|
2022-05-11 19:00:10 +03:00
|
|
|
import Test.Hspec (SpecWith, it)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- ** Preamble
|
|
|
|
|
|
|
|
spec :: SpecWith TestEnvironment
|
|
|
|
spec = do
|
2022-08-03 17:18:43 +03:00
|
|
|
Context.run (NE.fromList [postgresContext, citusContext, mssqlContext]) commonTests
|
|
|
|
Context.run (NE.fromList [postgresContext, citusContext]) postgresTests
|
|
|
|
Context.run (NE.fromList [mssqlContext]) mssqlTests
|
2022-05-11 19:00:10 +03:00
|
|
|
where
|
|
|
|
postgresContext =
|
|
|
|
Context.Context
|
|
|
|
{ name = Context.Backend Context.Postgres,
|
|
|
|
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
|
|
|
|
setup = Postgres.setup schema,
|
|
|
|
teardown = Postgres.teardown schema,
|
|
|
|
customOptions = Nothing
|
|
|
|
}
|
|
|
|
citusContext =
|
|
|
|
Context.Context
|
|
|
|
{ name = Context.Backend Context.Citus,
|
|
|
|
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
|
|
|
|
setup = Citus.setup schema,
|
|
|
|
teardown = Citus.teardown schema,
|
|
|
|
customOptions = Nothing
|
|
|
|
}
|
|
|
|
mssqlContext =
|
|
|
|
Context.Context
|
|
|
|
{ name = Context.Backend Context.SQLServer,
|
|
|
|
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
|
|
|
|
setup = Sqlserver.setup schema,
|
|
|
|
teardown = Sqlserver.teardown schema,
|
|
|
|
customOptions = Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- ** Schema
|
|
|
|
|
|
|
|
schema :: [Schema.Table]
|
|
|
|
schema =
|
|
|
|
[ alldefaults,
|
|
|
|
somedefaults,
|
|
|
|
withrelationship
|
|
|
|
]
|
|
|
|
|
|
|
|
alldefaults :: Schema.Table
|
|
|
|
alldefaults =
|
2022-06-17 11:44:04 +03:00
|
|
|
(table "alldefaults")
|
|
|
|
{ tableColumns =
|
2022-07-18 18:15:34 +03:00
|
|
|
[ Schema.column "id" Schema.defaultSerialType,
|
2022-05-11 19:00:10 +03:00
|
|
|
Schema.column "dt" defaultDateTimeType
|
|
|
|
],
|
2022-06-17 11:44:04 +03:00
|
|
|
tablePrimaryKey = ["id"]
|
2022-05-11 19:00:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
somedefaults :: Schema.Table
|
|
|
|
somedefaults =
|
2022-06-17 11:44:04 +03:00
|
|
|
(table "somedefaults")
|
|
|
|
{ tableColumns =
|
2022-07-18 18:15:34 +03:00
|
|
|
[ Schema.column "id" Schema.defaultSerialType,
|
2022-05-11 19:00:10 +03:00
|
|
|
Schema.column "dt" defaultDateTimeType,
|
|
|
|
Schema.column "name" Schema.TStr
|
|
|
|
],
|
2022-06-17 11:44:04 +03:00
|
|
|
tablePrimaryKey = ["name"]
|
2022-05-11 19:00:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
withrelationship :: Schema.Table
|
|
|
|
withrelationship =
|
2022-06-17 11:44:04 +03:00
|
|
|
(table "withrelationship")
|
|
|
|
{ tableColumns =
|
2022-07-18 18:15:34 +03:00
|
|
|
[ Schema.column "id" Schema.defaultSerialType,
|
2022-05-11 19:00:10 +03:00
|
|
|
Schema.column "nickname" Schema.TStr,
|
|
|
|
Schema.column "time_id" Schema.TInt
|
|
|
|
],
|
|
|
|
tablePrimaryKey = ["nickname"],
|
2022-06-17 11:44:04 +03:00
|
|
|
tableReferences = [Schema.Reference "time_id" "alldefaults" "id"]
|
2022-05-11 19:00:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
defaultDateTimeType :: Schema.ScalarType
|
|
|
|
defaultDateTimeType =
|
|
|
|
Schema.TCustomType $
|
|
|
|
Schema.defaultBackendScalarType
|
|
|
|
{ Schema.bstMysql = Nothing,
|
|
|
|
Schema.bstMssql = Just "DATETIME DEFAULT GETDATE()",
|
|
|
|
Schema.bstCitus = Just "TIMESTAMP DEFAULT NOW()",
|
|
|
|
Schema.bstPostgres = Just "TIMESTAMP DEFAULT NOW()",
|
|
|
|
Schema.bstBigQuery = Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- * Tests
|
|
|
|
|
|
|
|
commonTests :: Context.Options -> SpecWith TestEnvironment
|
|
|
|
commonTests opts = do
|
|
|
|
it "Insert empty object with default values" $ \testEnvironment ->
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphql
|
|
|
|
testEnvironment
|
|
|
|
[graphql|
|
|
|
|
mutation {
|
|
|
|
insert_hasura_alldefaults(
|
|
|
|
objects:[{}]
|
|
|
|
){
|
|
|
|
affected_rows
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
insert_hasura_alldefaults:
|
|
|
|
affected_rows: 1
|
|
|
|
|]
|
|
|
|
|
2022-05-30 15:17:37 +03:00
|
|
|
it "Insert multiple empty objects with default values" $ \testEnvironment ->
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphql
|
|
|
|
testEnvironment
|
|
|
|
[graphql|
|
|
|
|
mutation {
|
|
|
|
insert_hasura_alldefaults(
|
|
|
|
objects:[{}, {}, {}]
|
|
|
|
){
|
|
|
|
affected_rows
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
insert_hasura_alldefaults:
|
|
|
|
affected_rows: 3
|
|
|
|
|]
|
|
|
|
|
2022-05-11 19:00:10 +03:00
|
|
|
it "Insert simple object with default values" $ \testEnvironment ->
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphql
|
|
|
|
testEnvironment
|
|
|
|
[graphql|
|
|
|
|
mutation {
|
|
|
|
insert_hasura_somedefaults(
|
|
|
|
objects:[{ name: "a" }]
|
|
|
|
){
|
|
|
|
affected_rows
|
|
|
|
returning {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
insert_hasura_somedefaults:
|
|
|
|
affected_rows: 1
|
|
|
|
returning:
|
|
|
|
- id: 1
|
|
|
|
name: "a"
|
|
|
|
|]
|
|
|
|
|
|
|
|
postgresTests :: Context.Options -> SpecWith TestEnvironment
|
|
|
|
postgresTests opts = do
|
|
|
|
it "Upsert simple object with default values - check empty constraints" $ \testEnvironment ->
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphql
|
|
|
|
testEnvironment
|
|
|
|
[graphql|
|
|
|
|
mutation {
|
|
|
|
insert_hasura_somedefaults(
|
|
|
|
objects: [{ name: "a" }]
|
|
|
|
on_conflict: {
|
|
|
|
constraint: somedefaults_pkey,
|
|
|
|
update_columns: []
|
|
|
|
}
|
|
|
|
){
|
|
|
|
affected_rows
|
|
|
|
returning {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
insert_hasura_somedefaults:
|
|
|
|
affected_rows: 1
|
|
|
|
returning:
|
|
|
|
- id: 1
|
|
|
|
|]
|
|
|
|
|
|
|
|
it "Upsert simple object with default values - check conflict doesn't update" $ \testEnvironment ->
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphql
|
|
|
|
testEnvironment
|
|
|
|
[graphql|
|
|
|
|
mutation {
|
|
|
|
insert_hasura_somedefaults(
|
|
|
|
objects: [{ name: "a" }]
|
|
|
|
on_conflict: {
|
|
|
|
constraint: somedefaults_pkey,
|
|
|
|
update_columns: []
|
|
|
|
}
|
|
|
|
){
|
|
|
|
affected_rows
|
|
|
|
returning {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
insert_hasura_somedefaults:
|
|
|
|
affected_rows: 0
|
|
|
|
returning: []
|
|
|
|
|]
|
|
|
|
|
|
|
|
it "Nested insert with empty object" $ \testEnvironment ->
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphql
|
|
|
|
testEnvironment
|
|
|
|
[graphql|
|
|
|
|
mutation {
|
|
|
|
insert_hasura_withrelationship(
|
2022-06-27 17:32:31 +03:00
|
|
|
objects: [{ nickname: "the a", alldefaults_by_time_id_to_id: {data: {} } }]
|
2022-05-11 19:00:10 +03:00
|
|
|
on_conflict: {
|
|
|
|
constraint: withrelationship_pkey,
|
|
|
|
update_columns: []
|
|
|
|
}
|
|
|
|
){
|
|
|
|
affected_rows
|
|
|
|
returning {
|
|
|
|
id
|
|
|
|
nickname
|
2022-06-27 17:32:31 +03:00
|
|
|
alldefaults_by_time_id_to_id {
|
2022-05-11 19:00:10 +03:00
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
insert_hasura_withrelationship:
|
|
|
|
affected_rows: 2
|
|
|
|
returning:
|
|
|
|
- id: 1
|
|
|
|
nickname: "the a"
|
2022-06-27 17:32:31 +03:00
|
|
|
alldefaults_by_time_id_to_id:
|
2022-05-11 19:00:10 +03:00
|
|
|
id: 1
|
|
|
|
|]
|
|
|
|
|
|
|
|
mssqlTests :: Context.Options -> SpecWith TestEnvironment
|
|
|
|
mssqlTests opts = do
|
|
|
|
it "Upsert simple object with default values - check empty if_matched" $ \testEnvironment ->
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphql
|
|
|
|
testEnvironment
|
|
|
|
[graphql|
|
|
|
|
mutation {
|
|
|
|
insert_hasura_somedefaults(
|
|
|
|
objects: [{ name: "a" }]
|
|
|
|
if_matched: {
|
|
|
|
match_columns: [],
|
|
|
|
update_columns: []
|
|
|
|
}
|
|
|
|
){
|
|
|
|
affected_rows
|
|
|
|
returning {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
insert_hasura_somedefaults:
|
|
|
|
affected_rows: 1
|
|
|
|
returning:
|
|
|
|
- id: 1
|
|
|
|
|]
|
|
|
|
|
|
|
|
it "Upsert simple object with default values - check conflict doesn't update" $ \testEnvironment ->
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphql
|
|
|
|
testEnvironment
|
|
|
|
[graphql|
|
|
|
|
mutation {
|
|
|
|
insert_hasura_somedefaults(
|
|
|
|
objects: [{ name: "a" }]
|
|
|
|
if_matched: {
|
|
|
|
match_columns: name,
|
|
|
|
update_columns: []
|
|
|
|
}
|
|
|
|
){
|
|
|
|
affected_rows
|
|
|
|
returning {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
insert_hasura_somedefaults:
|
|
|
|
affected_rows: 0
|
|
|
|
returning: []
|
|
|
|
|]
|