2022-07-19 14:39:44 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
{-# LANGUAGE ViewPatterns #-}
|
|
|
|
|
2022-07-27 10:56:53 +03:00
|
|
|
-- | Test that when a table is untracked, SQL triggers that were created on the
|
|
|
|
-- tables is also removed
|
2022-08-23 11:49:51 +03:00
|
|
|
module Test.EventTrigger.MSSQL.EventTriggersUntrackTableCleanupSpec (spec) where
|
2022-07-19 14:39:44 +03:00
|
|
|
|
|
|
|
import Control.Concurrent.Chan qualified as Chan
|
2022-08-11 18:03:04 +03:00
|
|
|
import Data.List.NonEmpty qualified as NE
|
2022-07-19 14:39:44 +03:00
|
|
|
import Harness.Backend.Sqlserver qualified as Sqlserver
|
|
|
|
import Harness.GraphqlEngine qualified as GraphqlEngine
|
|
|
|
import Harness.Quoter.Yaml
|
2022-08-11 13:11:43 +03:00
|
|
|
import Harness.Test.Fixture qualified as Fixture
|
2022-07-19 14:39:44 +03:00
|
|
|
import Harness.Test.Schema (Table (..), table)
|
|
|
|
import Harness.Test.Schema qualified as Schema
|
|
|
|
import Harness.TestEnvironment (TestEnvironment, stopServer)
|
|
|
|
import Harness.Webhook qualified as Webhook
|
2022-08-05 12:34:25 +03:00
|
|
|
import Harness.Yaml (shouldBeYaml, shouldReturnYaml)
|
2022-07-19 14:39:44 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import System.Timeout (timeout)
|
|
|
|
import Test.HUnit.Base (assertFailure)
|
|
|
|
import Test.Hspec (SpecWith, describe, it)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Preamble
|
|
|
|
|
|
|
|
spec :: SpecWith TestEnvironment
|
|
|
|
spec =
|
2022-08-11 13:11:43 +03:00
|
|
|
Fixture.runWithLocalTestEnvironment
|
2022-08-11 18:03:04 +03:00
|
|
|
( NE.fromList
|
|
|
|
[ (Fixture.fixture $ Fixture.Backend Fixture.SQLServer)
|
|
|
|
{ -- setup the webhook server as the local test environment,
|
|
|
|
-- so that the server can be referenced while testing
|
|
|
|
Fixture.mkLocalTestEnvironment = webhookServerMkLocalTestEnvironment,
|
|
|
|
Fixture.setupTeardown = \testEnv ->
|
|
|
|
[ Fixture.SetupAction
|
|
|
|
{ Fixture.setupAction = mssqlSetup testEnv,
|
|
|
|
Fixture.teardownAction = \_ -> mssqlTeardown testEnv
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2022-08-03 17:18:43 +03:00
|
|
|
)
|
2022-07-19 14:39:44 +03:00
|
|
|
tests
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- * Backend
|
|
|
|
|
|
|
|
-- ** Schema
|
|
|
|
|
|
|
|
schema :: Text -> [Schema.Table]
|
|
|
|
schema authorTableName = [authorsTable authorTableName]
|
|
|
|
|
|
|
|
authorsTable :: Text -> Schema.Table
|
|
|
|
authorsTable tableName =
|
|
|
|
(table tableName)
|
|
|
|
{ tableColumns =
|
|
|
|
[ Schema.column "id" Schema.TInt,
|
|
|
|
Schema.column "name" Schema.TStr
|
|
|
|
],
|
|
|
|
tablePrimaryKey = ["id"],
|
|
|
|
tableData =
|
|
|
|
[ [Schema.VInt 1, Schema.VStr "Author 1"],
|
|
|
|
[Schema.VInt 2, Schema.VStr "Author 2"]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Tests
|
|
|
|
|
2022-08-11 13:11:43 +03:00
|
|
|
tests :: Fixture.Options -> SpecWith (TestEnvironment, (GraphqlEngine.Server, Webhook.EventsQueue))
|
2022-07-19 14:39:44 +03:00
|
|
|
tests opts = do
|
2022-07-27 10:56:53 +03:00
|
|
|
cleanupEventTriggersWhenTableUntracked opts
|
2022-07-19 14:39:44 +03:00
|
|
|
|
2022-08-11 13:11:43 +03:00
|
|
|
cleanupEventTriggersWhenTableUntracked :: Fixture.Options -> SpecWith (TestEnvironment, (GraphqlEngine.Server, Webhook.EventsQueue))
|
2022-07-27 10:56:53 +03:00
|
|
|
cleanupEventTriggersWhenTableUntracked opts =
|
|
|
|
describe "untrack a table with event triggers should remove the SQL triggers created on the table" do
|
2022-07-19 14:39:44 +03:00
|
|
|
it "check: inserting a new row invokes a event trigger" $
|
|
|
|
\(testEnvironment, (_, (Webhook.EventsQueue eventsQueue))) -> do
|
|
|
|
let insertQuery =
|
|
|
|
[yaml|
|
|
|
|
type: mssql_run_sql
|
|
|
|
args:
|
|
|
|
source: mssql
|
|
|
|
sql: "INSERT INTO authors (id, name) values (3, N'john')"
|
|
|
|
|]
|
|
|
|
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
|
|
|
result_type: CommandOk
|
|
|
|
result: null
|
|
|
|
|]
|
|
|
|
|
|
|
|
expectedEventPayload =
|
|
|
|
[yaml|
|
|
|
|
old: null
|
|
|
|
new:
|
|
|
|
name: john
|
|
|
|
id: 3
|
|
|
|
|]
|
|
|
|
|
|
|
|
-- Insert a row into the table with event trigger
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
(GraphqlEngine.postV2Query 200 testEnvironment insertQuery)
|
|
|
|
expectedResponse
|
|
|
|
|
|
|
|
-- Check if there was a payload generated due to the insert statement
|
|
|
|
eventPayload <-
|
|
|
|
-- wait for the event for a maximum of 5 seconds
|
|
|
|
timeout (5 * 1000000) (Chan.readChan eventsQueue)
|
|
|
|
>>= (`onNothing` (assertFailure "Event expected, but not fired"))
|
|
|
|
|
|
|
|
eventPayload `shouldBeYaml` expectedEventPayload
|
|
|
|
|
2022-07-27 10:56:53 +03:00
|
|
|
it "untrack table, check the SQL triggers are deleted from the table" $
|
2022-07-19 14:39:44 +03:00
|
|
|
\(testEnvironment, _) -> do
|
2022-07-27 10:56:53 +03:00
|
|
|
let untrackTableQuery =
|
2022-07-19 14:39:44 +03:00
|
|
|
[yaml|
|
2022-07-27 10:56:53 +03:00
|
|
|
type: mssql_untrack_table
|
2022-07-19 14:39:44 +03:00
|
|
|
args:
|
2022-07-27 10:56:53 +03:00
|
|
|
source: mssql
|
|
|
|
table:
|
|
|
|
schema: hasura
|
|
|
|
name: authors
|
2022-07-19 14:39:44 +03:00
|
|
|
|]
|
|
|
|
|
2022-07-27 10:56:53 +03:00
|
|
|
untrackTableQueryExpectedResponse = [yaml| message: success |]
|
2022-07-19 14:39:44 +03:00
|
|
|
|
2022-07-27 10:56:53 +03:00
|
|
|
-- Untracking the table should remove the SQL triggers on the table
|
2022-07-19 14:39:44 +03:00
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-07-27 10:56:53 +03:00
|
|
|
(GraphqlEngine.postMetadata testEnvironment untrackTableQuery)
|
|
|
|
untrackTableQueryExpectedResponse
|
2022-07-19 14:39:44 +03:00
|
|
|
|
2022-07-27 10:56:53 +03:00
|
|
|
let checkIfTriggerExists =
|
2022-07-19 14:39:44 +03:00
|
|
|
[yaml|
|
|
|
|
type: mssql_run_sql
|
|
|
|
args:
|
|
|
|
source: mssql
|
2022-07-27 10:56:53 +03:00
|
|
|
sql: "SELECT
|
|
|
|
(CASE WHEN EXISTS
|
|
|
|
( SELECT 1
|
|
|
|
FROM sys.triggers tr
|
|
|
|
INNER join sys.tables tb on tr.parent_id = tb.object_id
|
|
|
|
INNER join sys.schemas s on tb.schema_id = s.schema_id
|
|
|
|
WHERE tb.name = 'authors' AND tr.name = 'notify_hasura_insert_author_INSERT' AND s.name = 'hasura'
|
|
|
|
)
|
|
|
|
THEN CAST(1 AS BIT)
|
|
|
|
ELSE CAST(0 AS BIT)
|
|
|
|
END) AS 'exists';"
|
2022-07-19 14:39:44 +03:00
|
|
|
|]
|
|
|
|
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
2022-07-27 10:56:53 +03:00
|
|
|
result_type: TuplesOk
|
|
|
|
result:
|
|
|
|
- - exists
|
|
|
|
- - false
|
2022-07-19 14:39:44 +03:00
|
|
|
|]
|
|
|
|
|
2022-07-27 10:56:53 +03:00
|
|
|
-- If the clean-up did not happen properly this insert query would add
|
|
|
|
-- entries to the 'hdb_catalog.event_log' table
|
2022-07-19 14:39:44 +03:00
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-07-27 10:56:53 +03:00
|
|
|
(GraphqlEngine.postV2Query 200 testEnvironment checkIfTriggerExists)
|
2022-07-19 14:39:44 +03:00
|
|
|
expectedResponse
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- ** Setup and teardown override
|
|
|
|
|
2022-07-27 10:56:53 +03:00
|
|
|
mssqlSetup :: (TestEnvironment, (GraphqlEngine.Server, Webhook.EventsQueue)) -> IO ()
|
|
|
|
mssqlSetup (testEnvironment, (webhookServer, _)) = do
|
2022-07-19 14:39:44 +03:00
|
|
|
Sqlserver.setup (schema "authors") (testEnvironment, ())
|
|
|
|
let webhookServerEchoEndpoint = GraphqlEngine.serverUrl webhookServer ++ "/echo"
|
|
|
|
GraphqlEngine.postMetadata_ testEnvironment $
|
|
|
|
[yaml|
|
|
|
|
type: bulk
|
|
|
|
args:
|
|
|
|
- type: mssql_create_event_trigger
|
|
|
|
args:
|
|
|
|
name: authors_all
|
|
|
|
source: mssql
|
|
|
|
table:
|
|
|
|
name: authors
|
|
|
|
schema: hasura
|
|
|
|
webhook: *webhookServerEchoEndpoint
|
|
|
|
insert:
|
|
|
|
columns: "*"
|
2022-07-27 10:56:53 +03:00
|
|
|
|]
|
2022-07-19 14:39:44 +03:00
|
|
|
|
|
|
|
mssqlTeardown :: (TestEnvironment, (GraphqlEngine.Server, Webhook.EventsQueue)) -> IO ()
|
|
|
|
mssqlTeardown (_, (server, _)) = do
|
|
|
|
stopServer server
|
|
|
|
Sqlserver.dropTable (authorsTable "authors")
|
|
|
|
|
|
|
|
webhookServerMkLocalTestEnvironment ::
|
|
|
|
TestEnvironment -> IO (GraphqlEngine.Server, Webhook.EventsQueue)
|
|
|
|
webhookServerMkLocalTestEnvironment _ = do
|
|
|
|
Webhook.run
|