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
module Test.EventTrigger.EventTriggersPGUntrackTableCleanupSpec ( spec ) where
2022-07-19 14:39:44 +03:00
import Control.Concurrent.Chan qualified as Chan
2022-07-27 10:56:53 +03:00
import Harness.Backend.Postgres qualified as Postgres
2022-07-19 14:39:44 +03:00
import Harness.GraphqlEngine qualified as GraphqlEngine
import Harness.Quoter.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 , stopServer )
import Harness.Webhook qualified as Webhook
import Hasura.Prelude
import System.Timeout ( timeout )
import Test.HUnit.Base ( assertFailure )
import Test.Hspec ( SpecWith , describe , it )
--------------------------------------------------------------------------------
-- Preamble
spec :: SpecWith TestEnvironment
spec =
Context . runWithLocalTestEnvironment
[ Context . Context
2022-07-27 10:56:53 +03:00
{ name = Context . Backend Context . Postgres ,
2022-07-19 14:39:44 +03:00
-- setup the webhook server as the local test environment,
-- so that the server can be referenced while testing
mkLocalTestEnvironment = webhookServerMkLocalTestEnvironment ,
2022-07-27 10:56:53 +03:00
setup = postgresSetup ,
teardown = postgresTeardown ,
2022-07-19 14:39:44 +03:00
customOptions = Nothing
}
]
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
tests :: Context . Options -> SpecWith ( TestEnvironment , ( GraphqlEngine . Server , Webhook . EventsQueue ) )
tests opts = do
2022-07-27 10:56:53 +03:00
cleanupEventTriggersWhenTableUntracked opts
2022-07-19 14:39:44 +03:00
2022-07-27 10:56:53 +03:00
cleanupEventTriggersWhenTableUntracked :: Context . Options -> SpecWith ( TestEnvironment , ( GraphqlEngine . Server , Webhook . EventsQueue ) )
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 |
2022-07-27 10:56:53 +03:00
type : run_sql
2022-07-19 14:39:44 +03:00
args :
2022-07-27 10:56:53 +03:00
source : postgres
sql : " INSERT INTO authors (id, name) values (3, 'john') "
2022-07-19 14:39:44 +03:00
| ]
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 : pg_untrack_table
2022-07-19 14:39:44 +03:00
args :
2022-07-27 10:56:53 +03:00
source : postgres
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
-- Query the database and see if the trigger exists
let checkIfTriggerExists =
2022-07-19 14:39:44 +03:00
[ yaml |
2022-07-27 10:56:53 +03:00
type : run_sql
2022-07-19 14:39:44 +03:00
args :
2022-07-27 10:56:53 +03:00
source : postgres
sql : " SELECT EXISTS (SELECT 1 FROM pg_trigger WHERE NOT tgisinternal AND tgname = 'notify_hasura_insert_author_INSERT' AND tgrelid = 'hasura.authors'::regclass); "
2022-07-19 14:39:44 +03:00
| ]
expectedResponse =
[ yaml |
2022-07-27 10:56:53 +03:00
result_type : TuplesOk
result :
- - exists
- - f
2022-07-19 14:39:44 +03:00
| ]
2022-07-27 10:56:53 +03:00
-- If the cleanup happened then the hasura SQL trigger should not exists in the 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
postgresSetup :: ( TestEnvironment , ( GraphqlEngine . Server , Webhook . EventsQueue ) ) -> IO ()
postgresSetup ( testEnvironment , ( webhookServer , _ ) ) = do
Postgres . setup ( schema " authors " ) ( testEnvironment , () )
2022-07-19 14:39:44 +03:00
let webhookServerEchoEndpoint = GraphqlEngine . serverUrl webhookServer ++ " /echo "
GraphqlEngine . postMetadata_ testEnvironment $
[ yaml |
type : bulk
args :
2022-07-27 10:56:53 +03:00
- type : pg_create_event_trigger
2022-07-19 14:39:44 +03:00
args :
name : authors_all
2022-07-27 10:56:53 +03:00
source : postgres
2022-07-19 14:39:44 +03:00
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
2022-07-27 10:56:53 +03:00
postgresTeardown :: ( TestEnvironment , ( GraphqlEngine . Server , Webhook . EventsQueue ) ) -> IO ()
postgresTeardown ( _ , ( server , _ ) ) = do
2022-07-19 14:39:44 +03:00
stopServer server
2022-07-27 10:56:53 +03:00
Postgres . dropTable ( authorsTable " authors " )
2022-07-19 14:39:44 +03:00
webhookServerMkLocalTestEnvironment ::
TestEnvironment -> IO ( GraphqlEngine . Server , Webhook . EventsQueue )
webhookServerMkLocalTestEnvironment _ = do
Webhook . run