root/trunk/epic/test/test_globals.sql

Revision 23, 2.2 kB (checked in by fumanchu, 4 years ago)

Lots of changes:

  1. Changed global to return the TEMP tablename instead of a record. Use the new test.get function to obtain a record.
  2. Added a 'name' arg to global to re-use the TEMP table.
  3. New test.get(call, rownum).
  4. Moved the few public functions into the test schema.
  5. Moved the under-under attributes to new functions: constructor, len, iter.
  6. Made run_test prepend the test schema to search_path.
Line 
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
4CREATE OR REPLACE FUNCTION test._test_global() RETURNS VOID AS $$
5DECLARE
6  g      text;
7  g2     text;
8  rec    record;
9  trec   record;
10BEGIN
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;
46END;
47$$ LANGUAGE plpgsql;
48
49
50CREATE OR REPLACE FUNCTION test.test_global() RETURNS VOID AS $$
51-- module: test_globals
52DECLARE
53  rec    record;
54BEGIN
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();
67END;
68$$ LANGUAGE plpgsql;
Note: See TracBrowser for help on using the browser.