2022-03-18 13:04:52 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
|
|
|
-- | Testing custom field names.
|
|
|
|
-- See the main hasura documentation for more information.
|
|
|
|
--
|
|
|
|
-- - Postgres: https://hasura.io/docs/latest/graphql/core/databases/postgres/schema/custom-field-names.html
|
|
|
|
-- - MSSQL: https://hasura.io/docs/latest/graphql/core/databases/ms-sql-server/schema/custom-field-names.html
|
|
|
|
module Test.CustomFieldNamesSpec (spec) where
|
|
|
|
|
2022-08-03 17:18:43 +03:00
|
|
|
import Data.List.NonEmpty qualified as NE
|
2022-03-18 13:04:52 +03:00
|
|
|
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)
|
|
|
|
import Harness.Quoter.Yaml (shouldReturnYaml, yaml)
|
|
|
|
import Harness.Test.Context qualified as Context
|
2022-06-17 11:44:04 +03:00
|
|
|
import Harness.Test.Schema (Table (..), table)
|
2022-03-18 13:04:52 +03:00
|
|
|
import Harness.Test.Schema qualified as Schema
|
2022-04-20 20:15:42 +03:00
|
|
|
import Harness.TestEnvironment (TestEnvironment)
|
2022-08-03 17:18:43 +03:00
|
|
|
import Hasura.Prelude
|
2022-03-18 13:04:52 +03:00
|
|
|
import Test.Hspec (SpecWith, it)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- * Preamble
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
spec :: SpecWith TestEnvironment
|
2022-03-18 13:04:52 +03:00
|
|
|
spec =
|
|
|
|
Context.run
|
2022-08-03 17:18:43 +03:00
|
|
|
( NE.fromList
|
|
|
|
[ Context.Context
|
|
|
|
{ name = Context.Backend Context.SQLServer,
|
|
|
|
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
|
|
|
|
setup = sqlserverSetup,
|
|
|
|
teardown = Sqlserver.teardown schema,
|
|
|
|
customOptions = Nothing
|
|
|
|
},
|
|
|
|
Context.Context
|
|
|
|
{ name = Context.Backend Context.Postgres,
|
|
|
|
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
|
|
|
|
setup = postgresSetup,
|
|
|
|
teardown = Postgres.teardown schema,
|
|
|
|
customOptions = Nothing
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
2022-03-18 13:04:52 +03:00
|
|
|
tests
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- * Tests
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
tests :: Context.Options -> SpecWith TestEnvironment
|
2022-03-18 13:04:52 +03:00
|
|
|
tests opts = do
|
2022-04-20 20:15:42 +03:00
|
|
|
it "Delete respects custom names" $ \testEnvironment ->
|
2022-03-18 13:04:52 +03:00
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphql
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-18 13:04:52 +03:00
|
|
|
[graphql|
|
|
|
|
mutation author {
|
|
|
|
delete_hasura_author(
|
|
|
|
where: {Id: {_eq: 1}}
|
|
|
|
) {
|
|
|
|
returning {
|
|
|
|
Id
|
|
|
|
Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
delete_hasura_author:
|
|
|
|
returning:
|
|
|
|
- Id: 1
|
|
|
|
Name: 'Mercer'
|
|
|
|
|]
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
it "Update respects custom names" $ \testEnvironment ->
|
2022-03-18 13:04:52 +03:00
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphql
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-18 13:04:52 +03:00
|
|
|
[graphql|
|
|
|
|
mutation author {
|
|
|
|
update_hasura_author(
|
|
|
|
where: {Id: {_eq: 2}}
|
|
|
|
_set: { Name: "Johnson" }
|
|
|
|
) {
|
|
|
|
returning {
|
|
|
|
Id
|
|
|
|
Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
update_hasura_author:
|
|
|
|
returning:
|
|
|
|
- Id: 2
|
|
|
|
Name: 'Johnson'
|
|
|
|
|]
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
it "Insert respects custom names" $ \testEnvironment ->
|
2022-03-18 13:04:52 +03:00
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphql
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-18 13:04:52 +03:00
|
|
|
[graphql|
|
|
|
|
mutation author {
|
|
|
|
insert_hasura_author(objects:
|
|
|
|
{ Id: 3
|
|
|
|
Name: "Jaffe"
|
|
|
|
}) {
|
|
|
|
returning {
|
|
|
|
Id
|
|
|
|
Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
insert_hasura_author:
|
|
|
|
returning:
|
|
|
|
- Id: 3
|
|
|
|
Name: 'Jaffe'
|
|
|
|
|]
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- * Backend
|
|
|
|
|
|
|
|
-- ** Schema
|
|
|
|
|
|
|
|
schema :: [Schema.Table]
|
|
|
|
schema =
|
2022-06-17 11:44:04 +03:00
|
|
|
[ (table "author")
|
|
|
|
{ tableColumns =
|
2022-03-18 13:04:52 +03:00
|
|
|
[ Schema.column "AuthorId" Schema.TInt,
|
|
|
|
Schema.column "AuthorName" Schema.TStr
|
|
|
|
],
|
|
|
|
tablePrimaryKey = ["AuthorId"],
|
|
|
|
tableData =
|
|
|
|
[ [Schema.VInt 1, Schema.VStr "Mercer"],
|
|
|
|
[Schema.VInt 2, Schema.VStr "Ray"]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- ** Postgres backend
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
postgresSetup :: (TestEnvironment, ()) -> IO ()
|
|
|
|
postgresSetup (testEnvironment, localTestEnvironment) = do
|
|
|
|
Postgres.setup schema (testEnvironment, localTestEnvironment)
|
|
|
|
postgresCreateCustomNames testEnvironment
|
2022-03-18 13:04:52 +03:00
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
postgresCreateCustomNames :: TestEnvironment -> IO ()
|
|
|
|
postgresCreateCustomNames testEnvironment = do
|
2022-03-18 13:04:52 +03:00
|
|
|
let source = Context.defaultBackendTypeString Context.Postgres
|
|
|
|
in GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-18 13:04:52 +03:00
|
|
|
[yaml|
|
|
|
|
type: pg_set_table_customization
|
|
|
|
args:
|
|
|
|
source: *source
|
|
|
|
table:
|
|
|
|
schema: hasura
|
|
|
|
name: author
|
|
|
|
configuration:
|
|
|
|
custom_column_names:
|
|
|
|
"AuthorId": "Id"
|
|
|
|
"AuthorName": "Name"
|
|
|
|
|]
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- ** SQL Server backend
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
sqlserverSetup :: (TestEnvironment, ()) -> IO ()
|
|
|
|
sqlserverSetup (testEnvironment, localTestEnvironment) = do
|
|
|
|
Sqlserver.setup schema (testEnvironment, localTestEnvironment)
|
|
|
|
sqlserverCreateCustomNames testEnvironment
|
2022-03-18 13:04:52 +03:00
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
sqlserverCreateCustomNames :: TestEnvironment -> IO ()
|
|
|
|
sqlserverCreateCustomNames testEnvironment = do
|
2022-03-18 13:04:52 +03:00
|
|
|
let source = Context.defaultBackendTypeString Context.SQLServer
|
|
|
|
in GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-18 13:04:52 +03:00
|
|
|
[yaml|
|
|
|
|
type: mssql_set_table_customization
|
|
|
|
args:
|
|
|
|
source: *source
|
|
|
|
table:
|
|
|
|
schema: hasura
|
|
|
|
name: author
|
|
|
|
configuration:
|
|
|
|
custom_column_names:
|
|
|
|
"AuthorId": "Id"
|
|
|
|
"AuthorName": "Name"
|
|
|
|
|]
|