----------------------------------------
|
-- Table creation
|
DROP TABLE IF EXISTS trigger_test_table;
|
CREATE TABLE trigger_test_table (
|
id int,
|
message text
|
);
|
|
----------------------------------------
|
-- Helpful note:
|
--
|
-- Use this to delete all triggers:
|
--
|
-- echo "SELECT
|
-- event_object_table as table,
|
-- trigger_name,
|
-- event_manipulation as event,
|
-- action_timing as timing,
|
-- action_statement
|
-- FROM
|
-- information_schema.triggers;
|
-- "|psql|grep EXEC| awk '{print "drop trigger " $3 " on " $1 ";";}'
|
----------------------------------------
|
|
|
----------------------------------------
|
-- Function creation
|
|
CREATE OR REPLACE FUNCTION trigger_test_function_modify_fsharp() RETURNS TRIGGER AS $$
|
if tg.Arguments.[1] <> "1" then
|
raise (SystemException($"Assertion failed: wrong trigger argument, '{tg.Arguments.[1]}' != '1'"))
|
if (unbox<int> tg.NewRow.[0]) <> 2 then
|
raise (SystemException($"Assertion failed: wrong row value, {tg.NewRow.[1]} != 2"))
|
|
tg.NewRow.[1] <- "MODIFIED Text with F#!!!"
|
ReturnMode.TriggerModify
|
$$ LANGUAGE plfsharp;
|
|
CREATE OR REPLACE FUNCTION trigger_test_skip_fsharp() RETURNS TRIGGER AS $$
|
if tg.Arguments.[1] <> "2" then
|
raise (SystemException($"Assertion failed: wrong trigger argument, '{tg.Arguments.[1]}' != '2'"))
|
|
if (unbox<int> tg.NewRow.[0]) = 5 then
|
ReturnMode.TriggerSkip
|
else
|
ReturnMode.Normal;
|
$$ LANGUAGE plfsharp;
|
|
CREATE OR REPLACE FUNCTION trigger_test_tg_vals_fsharp() RETURNS TRIGGER AS $$
|
if unbox<int> tg.NewRow.[0] = 6 then
|
if tg.TriggerName = "test_trigger_bir_3" &&
|
tg.TriggerWhen = "BEFORE" &&
|
tg.TriggerLevel = "ROW" &&
|
tg.TriggerEvent = "INSERT" &&
|
tg.RelationId > 0 &&
|
tg.TableName = "trigger_test_table" &&
|
tg.TableSchema = "public" &&
|
tg.NewRow.Length = 2 &&
|
unbox<int> tg.NewRow.[0] = 6 &&
|
tg.Arguments.[0] = "BEFORE/INSERT/ROW" &&
|
tg.Arguments.[1] = "3" then
|
tg.NewRow.[1] <- "TG value assertions passed"
|
ReturnMode.TriggerModify
|
else
|
Elog.Error($"failed test, tg values didn't check out: {tg}")
|
ReturnMode.Normal
|
else
|
Elog.Error($"failed test, tg values didn't check out: {tg}")
|
ReturnMode.Normal;
|
$$ LANGUAGE plfsharp;
|
|
-- This test isn't automatically effective, but it can be manually checked.
|
CREATE OR REPLACE FUNCTION trigger_test_exception_fsharp() RETURNS TRIGGER AS $$
|
raise (SystemException(("This is a test of exception handling")))
|
ReturnMode.Normal; // unreached
|
$$ LANGUAGE plfsharp;
|
|
CREATE OR REPLACE FUNCTION trigger_test_update_type_fsharp() RETURNS TRIGGER AS $$
|
tg.NewRow.[1] = 1;
|
ReturnMode.TriggerModify;
|
$$ LANGUAGE plfsharp;
|
|
|
----------------------------------------
|
-- Trigger creation
|
|
-- encoding for test case:
|
-- B/A = BEFORE/AFTER
|
-- I/U/D/T = INSERT/UPDATE/DELETE/TRUNCATE
|
-- R/S = ROW/STATEMENT
|
|
CREATE OR REPLACE TRIGGER test_trigger_BIR_1
|
BEFORE INSERT ON trigger_test_table
|
FOR EACH ROW
|
WHEN (new.id = 2)
|
EXECUTE FUNCTION trigger_test_function_modify_fsharp('BEFORE/INSERT/ROW', 1);
|
|
CREATE OR REPLACE TRIGGER test_trigger_BIR_2
|
BEFORE INSERT ON trigger_test_table
|
FOR EACH ROW
|
WHEN (new.id = 5)
|
EXECUTE FUNCTION trigger_test_skip_fsharp('BEFORE/INSERT/ROW', 2);
|
|
CREATE OR REPLACE TRIGGER test_trigger_BIR_3
|
BEFORE INSERT ON trigger_test_table
|
FOR EACH ROW
|
WHEN (new.id = 6)
|
EXECUTE FUNCTION trigger_test_tg_vals_fsharp('BEFORE/INSERT/ROW', 3);
|
|
CREATE OR REPLACE TRIGGER test_trigger_BIR_4
|
BEFORE INSERT ON trigger_test_table
|
FOR EACH ROW
|
WHEN (new.id = 7)
|
EXECUTE FUNCTION trigger_test_update_type_fsharp('BEFORE/INSERT/ROW', 4);
|
|
CREATE OR REPLACE TRIGGER test_trigger_AUS_4
|
AFTER UPDATE ON trigger_test_table
|
FOR EACH STATEMENT
|
EXECUTE FUNCTION trigger_test_exception_fsharp ('AFTER/UPDATE/STATEMENT', 4);
|
|
----------------------------------------
|
-- Data manipulation
|
|
INSERT INTO trigger_test_table VALUES (1, 'Inserted Text');
|
INSERT INTO trigger_test_table VALUES (2, 'Inserted Second Text');
|
INSERT INTO trigger_test_table VALUES (3, 'Inserted Third Text (for updating)');
|
INSERT INTO trigger_test_table VALUES (4, 'Inserted Fourth Text (for deletion)');
|
INSERT INTO trigger_test_table VALUES (5, 'Inserted Fifth Text (for simple skip)');
|
INSERT INTO trigger_test_table VALUES (6, 'Inserted Sixth Text (for values check modify)');
|
INSERT INTO trigger_test_table VALUES (7, 'This should be a string');
|
UPDATE trigger_test_table SET message ='Updated Text' WHERE id=1;
|
UPDATE trigger_test_table SET id=13, message ='Updated Third Text' WHERE id=3;
|
DELETE FROM trigger_test_table WHERE id=4;
|
|
-- Test for zero rows affected, but per-statement trigger fires
|
UPDATE trigger_test_table SET id=2, message='Updated Text' WHERE id=1234;
|
|
|
-- Test 1: Check if 'id' values are only 1, 2, 6, 7, or 13
|
-- This ensures that the delete worked, and we have no extra values
|
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
|
SELECT 'f#-trigger', 'validRows',
|
NOT EXISTS (SELECT 1 FROM trigger_test_table WHERE id NOT IN (1, 2, 6, 7, 13));
|
|
-- Test 2: Check 'b' value for 'a' = 1
|
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
|
SELECT 'f#-trigger', 'rowUpdated',
|
(SELECT message = 'Updated Text' FROM trigger_test_table WHERE id = 1);
|
|
-- Test 3: Check 'b' value for 'a' = 2
|
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
|
SELECT 'f#-trigger', 'rowModifiedByTrigger',
|
(SELECT message = 'MODIFIED Text with F#!!!' FROM trigger_test_table WHERE id = 2);
|
|
-- Test 4: Check 'b' value for 'a' = 13
|
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
|
SELECT 'f#-trigger', 'rowUpdated-2',
|
(SELECT message = 'Updated Third Text' FROM trigger_test_table WHERE id = 13);
|
|
-- Test 5: Check that skip works
|
-- (already checked in Test 1, but this is specific)
|
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
|
SELECT 'f#-trigger', 'skipWorks',
|
NOT EXISTS (SELECT 1 FROM trigger_test_table WHERE id = 5);
|
|
-- Test 6: Check that tg values are correct
|
-- (already checked in Test 1, but this is specific)
|
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
|
SELECT 'f#-trigger', 'tgValuesCorrect',
|
EXISTS (SELECT 1 FROM trigger_test_table WHERE id = 6 and message = 'TG value assertions passed');
|
|
-- Test 7: Ensure that type-mismatched updates are ignored
|
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
|
SELECT 'f#-trigger', 'tgTypeMismatchHandling',
|
EXISTS (SELECT 1 FROM trigger_test_table WHERE id = 7 and message = 'This should be a string');
|