| 1 | -- Tests for Epic's exception-raising approach, plus test.results. |
|---|
| 2 | -- To run, execute epic.sql, then this script, then test.run_module('test_results'). |
|---|
| 3 | |
|---|
| 4 | SET search_path = test, public, pg_catalog; |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | CREATE OR REPLACE FUNCTION test.test_pass() RETURNS VOID AS $$ |
|---|
| 8 | -- Assert the correct operation of test.pass. |
|---|
| 9 | -- module: test_results |
|---|
| 10 | BEGIN |
|---|
| 11 | BEGIN |
|---|
| 12 | PERFORM test.pass(); |
|---|
| 13 | EXCEPTION WHEN OTHERS THEN |
|---|
| 14 | IF SQLERRM = '[OK]' THEN |
|---|
| 15 | PERFORM test.pass(); |
|---|
| 16 | ELSE |
|---|
| 17 | RAISE EXCEPTION 'test.pass() did not raise the ''[OK]'' exception. Got % instead.', SQLERRM; |
|---|
| 18 | END IF; |
|---|
| 19 | END; |
|---|
| 20 | PERFORM test.fail('test.pass() did not raise the ''[OK]'' exception.'); |
|---|
| 21 | END; |
|---|
| 22 | $$ LANGUAGE plpgsql IMMUTABLE; |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | CREATE OR REPLACE FUNCTION test.test_fail() RETURNS VOID AS $$ |
|---|
| 26 | -- Assert the correct operation of test.fail. |
|---|
| 27 | -- module: test_results |
|---|
| 28 | BEGIN |
|---|
| 29 | BEGIN |
|---|
| 30 | PERFORM test.fail('This is supposed to fail.'); |
|---|
| 31 | EXCEPTION WHEN OTHERS THEN |
|---|
| 32 | IF SQLERRM = '[FAIL] This is supposed to fail.' THEN |
|---|
| 33 | PERFORM test.pass(); |
|---|
| 34 | ELSE |
|---|
| 35 | RAISE EXCEPTION 'test.fail() did not raise the correct exception. Got % instead.', SQLERRM; |
|---|
| 36 | END IF; |
|---|
| 37 | END; |
|---|
| 38 | RAISE EXCEPTION '[FAIL] test.fail() did not raise an exception.'; |
|---|
| 39 | END; |
|---|
| 40 | $$ LANGUAGE plpgsql; |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | CREATE OR REPLACE FUNCTION test.test_todo() RETURNS VOID AS $$ |
|---|
| 44 | -- Assert the correct operation of test.todo. |
|---|
| 45 | -- module: test_results |
|---|
| 46 | BEGIN |
|---|
| 47 | BEGIN |
|---|
| 48 | PERFORM test.todo('This test isn''t done yet.'); |
|---|
| 49 | EXCEPTION WHEN OTHERS THEN |
|---|
| 50 | IF SQLERRM = '[TODO] This test isn''t done yet.' THEN |
|---|
| 51 | PERFORM test.pass(); |
|---|
| 52 | ELSE |
|---|
| 53 | RAISE EXCEPTION 'test.todo() did not raise the correct exception. Got % instead.', SQLERRM; |
|---|
| 54 | END IF; |
|---|
| 55 | END; |
|---|
| 56 | RAISE EXCEPTION '[FAIL] test.todo() did not raise an exception.'; |
|---|
| 57 | END; |
|---|
| 58 | $$ LANGUAGE plpgsql; |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | CREATE OR REPLACE FUNCTION test.test_skip() RETURNS VOID AS $$ |
|---|
| 62 | -- Assert the correct operation of test.skip. |
|---|
| 63 | -- module: test_results |
|---|
| 64 | BEGIN |
|---|
| 65 | BEGIN |
|---|
| 66 | PERFORM test.skip('This test is too slow.'); |
|---|
| 67 | EXCEPTION WHEN OTHERS THEN |
|---|
| 68 | IF SQLERRM = '[SKIP] This test is too slow.' THEN |
|---|
| 69 | PERFORM test.pass(); |
|---|
| 70 | ELSE |
|---|
| 71 | RAISE EXCEPTION 'test.skip() did not raise the correct exception. Got % instead.', SQLERRM; |
|---|
| 72 | END IF; |
|---|
| 73 | END; |
|---|
| 74 | RAISE EXCEPTION '[FAIL] test.skip() did not raise an exception.'; |
|---|
| 75 | END; |
|---|
| 76 | $$ LANGUAGE plpgsql; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | CREATE OR REPLACE FUNCTION test.demo_fail() RETURNS VOID AS $$ |
|---|
| 80 | -- Demonstration function for test failure. |
|---|
| 81 | BEGIN |
|---|
| 82 | PERFORM test.fail('This demonstrates a failed test.'); |
|---|
| 83 | END; |
|---|
| 84 | $$ LANGUAGE plpgsql; |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | CREATE OR REPLACE FUNCTION test.demo_todo() RETURNS VOID AS $$ |
|---|
| 88 | -- Demonstration function for TODO tests. |
|---|
| 89 | BEGIN |
|---|
| 90 | PERFORM test.todo('This demonstrates a TODO test.'); |
|---|
| 91 | END; |
|---|
| 92 | $$ LANGUAGE plpgsql; |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | CREATE OR REPLACE FUNCTION test.test_run_test() RETURNS VOID AS $$ |
|---|
| 96 | -- Assert the correct operation of test.run_test. |
|---|
| 97 | -- module: test_results |
|---|
| 98 | DECLARE |
|---|
| 99 | result bool; |
|---|
| 100 | BEGIN |
|---|
| 101 | -- Successful tests MUST insert an [OK] record into test.results. |
|---|
| 102 | DELETE FROM test.results WHERE name = 'test_pass'; |
|---|
| 103 | PERFORM test.run_test('test_pass'); |
|---|
| 104 | PERFORM test.assert_rows( |
|---|
| 105 | 'SELECT result, errcode, errmsg FROM test.results WHERE name = ''test_pass''', |
|---|
| 106 | 'SELECT ''[OK]'', '''', '''''); |
|---|
| 107 | |
|---|
| 108 | -- Failed tests MUST insert a [FAIL] record into test.results. |
|---|
| 109 | DELETE FROM test.results WHERE name = 'demo_fail'; |
|---|
| 110 | PERFORM test.run_test('demo_fail'); |
|---|
| 111 | PERFORM test.assert_rows( |
|---|
| 112 | 'SELECT result, errcode, errmsg FROM test.results WHERE name = ''demo_fail''', |
|---|
| 113 | 'SELECT ''[FAIL]'', ''P0001'', ''This demonstrates a failed test.'''); |
|---|
| 114 | |
|---|
| 115 | -- TODO tests MUST insert a [TODO] record into test.results. |
|---|
| 116 | DELETE FROM test.results WHERE name = 'demo_todo'; |
|---|
| 117 | PERFORM test.run_test('demo_todo'); |
|---|
| 118 | PERFORM test.assert_rows( |
|---|
| 119 | 'SELECT result, errcode, errmsg FROM test.results WHERE name = ''demo_todo''', |
|---|
| 120 | 'SELECT ''[TODO]'', '''', ''This demonstrates a TODO test.'''); |
|---|
| 121 | |
|---|
| 122 | PERFORM test.pass(); |
|---|
| 123 | END; |
|---|
| 124 | $$ LANGUAGE plpgsql; |
|---|
| 125 | |
|---|
| 126 | |
|---|