| 1 | -- Tests for the 'global' function which Epic provides. |
|---|
| 2 | -- To run, execute epic.sql, then this script, then test.run_module('test_globals'). |
|---|
| 3 | |
|---|
| 4 | CREATE OR REPLACE FUNCTION test._test_global() RETURNS VOID AS $$ |
|---|
| 5 | DECLARE |
|---|
| 6 | g text; |
|---|
| 7 | g2 text; |
|---|
| 8 | rec record; |
|---|
| 9 | trec record; |
|---|
| 10 | BEGIN |
|---|
| 11 | g := global('pg_namespace WHERE nspname = ''test'';'); |
|---|
| 12 | |
|---|
| 13 | -- The returned string MUST be the name of the TEMP table. |
|---|
| 14 | PERFORM test.assert(g LIKE E'\_global\_%', g || ' not like _global'); |
|---|
| 15 | |
|---|
| 16 | rec := get(g); |
|---|
| 17 | |
|---|
| 18 | -- The result of get() should be a normal record. |
|---|
| 19 | PERFORM test.assert_equal(rec.nspname, 'test'); |
|---|
| 20 | |
|---|
| 21 | -- The returned record MUST possess a .__name__ attribute. |
|---|
| 22 | PERFORM test.assert(rec.__name__ LIKE E'\_global\_%', rec.__name__ || ' not like _global'); |
|---|
| 23 | |
|---|
| 24 | -- The global MUST reference a temporary table with the same fields. |
|---|
| 25 | EXECUTE 'SELECT * FROM ' || g INTO trec; |
|---|
| 26 | PERFORM test.assert_equal(trec.nspname, 'test'); |
|---|
| 27 | PERFORM test.assert_equal(trec.nspowner, rec.nspowner); |
|---|
| 28 | PERFORM test.assert_equal(trec.nspacl, rec.nspacl); |
|---|
| 29 | |
|---|
| 30 | -- The TEMP table MUST possess its own constructor SQL string in a COMMENT |
|---|
| 31 | PERFORM test.assert_equal(constructor(g), 'SELECT * FROM pg_namespace WHERE nspname = ''test'''); |
|---|
| 32 | |
|---|
| 33 | -- Test the iter() function. |
|---|
| 34 | g2 := global('iter(''' || g || ''')'); |
|---|
| 35 | PERFORM test.assert_not_empty(g2); |
|---|
| 36 | |
|---|
| 37 | -- Test the attributes() function. |
|---|
| 38 | PERFORM test.assert_column('SELECT attname FROM attributes(''' || g || ''')', |
|---|
| 39 | ARRAY['nspname', 'nspowner', 'nspacl']); |
|---|
| 40 | |
|---|
| 41 | -- Test the len() function. |
|---|
| 42 | PERFORM test.assert_equal(len(g), 1); |
|---|
| 43 | |
|---|
| 44 | -- Raise an exception to test deletion of the TEMP table ON COMMIT |
|---|
| 45 | RAISE EXCEPTION '%', g; |
|---|
| 46 | END; |
|---|
| 47 | $$ LANGUAGE plpgsql; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | CREATE OR REPLACE FUNCTION test.test_global() RETURNS VOID AS $$ |
|---|
| 51 | -- module: test_globals |
|---|
| 52 | DECLARE |
|---|
| 53 | rec record; |
|---|
| 54 | BEGIN |
|---|
| 55 | BEGIN |
|---|
| 56 | PERFORM test._test_global(); |
|---|
| 57 | EXCEPTION WHEN raise_exception THEN |
|---|
| 58 | -- The table, since temporary, MUST be DROP'ed on rollback. |
|---|
| 59 | BEGIN |
|---|
| 60 | EXECUTE 'SELECT * FROM ' || SQLERRM INTO rec; |
|---|
| 61 | EXCEPTION WHEN undefined_table THEN |
|---|
| 62 | NULL; |
|---|
| 63 | END; |
|---|
| 64 | END; |
|---|
| 65 | |
|---|
| 66 | PERFORM test.pass(); |
|---|
| 67 | END; |
|---|
| 68 | $$ LANGUAGE plpgsql; |
|---|