Hi, I don't see anything about this from a google search, so I hope this hasn't been discussed to death before.
I'm trying to use pgtap calls insde a plpgsql DO block and it's failing in an interesting way. The docs don't say I can't do this, then again, I don't see any examples of it either, so I can't tell if this should work or not. Here's a simple example, which assumes 2 schemas and 1 table (define any way you want).
---------------------------------------
BEGIN;
SELECT plan(4);
DO $$
DECLARE
v_schema text;
BEGIN
FOREACH v_schema IN ARRAY ARRAY[ 'logging', 'archive' ]
LOOP
PERFORM has_table(v_schema, 'busy_log', 'has busy_log');
/* lots more tests and what I really care about */
END LOOP;
END;
$$ LANGUAGE plpgsql;
/* test that pgtap is working normally with these 2 tests */
select has_table('logging', 'busy_log', 'has busy_log');
select has_table('archive', 'busy_log', 'has busy_log');
SELECT * FROM finish();
ROLLBACK;
---------------------------------------
The reason to do this is because we have multiple pairs of tables that should be the same except that they live in separate schemas, so why have to duplicate a bunch of code when a loop with a variable would do nicely? :) The output shows:
---------------------------------------
$ pg_prove -v
busy.pg
busy.pg ..
1..4
ok 3 - has busy_log
ok 4 - has busy_log
Failed 2/4 subtests
Test Summary Report
-------------------
busy.pg (Wstat: 0 Tests: 2 Failed: 0)
Parse errors: Tests out of sequence. Found (3) but expected (1)
Tests out of sequence. Found (4) but expected (2)
Bad plan. You planned 4 tests but ran 2.
Files=1, Tests=2, 1 wallclock secs ( 0.01 usr + 0.00 sys = 0.01 CPU)
Result: FAIL
---------------------------------------
At the top, it's says I ran 4 tests, but at the bottom it says I ran 2 tests … which is it? :)
I also tried changing PERFORM... to do:
SELECT has_table(...) INTO myvar ;
RAISE NOTICE '%', myvar
I could tell has_table() ran successfully, but I still get general failure overall, so that is a no-go.
For more info, while running that, our pg_log shows:
---------------------------------------
2017-10-30 18:09:27 CDT [4609]: [3-1] user=nroot,db=nms,app=psql DEBUG:
building index "pg_toast_364971_index" on table "pg_toast_364971"
2017-10-30 18:09:27 CDT [4609]: [4-1] user=nroot,db=nms,app=psql
CONTEXT: SQL statement "
CREATE TEMP SEQUENCE __tcache___id_seq;
CREATE TEMP TABLE __tcache__ (
id INTEGER NOT NULL DEFAULT nextval('__tcache___id_seq'),
label TEXT NOT NULL,
value INTEGER NOT NULL,
note TEXT NOT NULL DEFAULT ''
);
CREATE UNIQUE INDEX __tcache___key ON __tcache__(id);
GRANT ALL ON TABLE __tcache__ TO PUBLIC;
GRANT ALL ON TABLE __tcache___id_seq TO PUBLIC;
CREATE TEMP SEQUENCE __tresults___numb_seq;
GRANT ALL ON TABLE __tresults___numb_seq TO PUBLIC;
"
PL/pgSQL function plan(integer) line 6 at EXECUTE
2017-10-30 18:09:27 CDT [4609]: [5-1] user=nroot,db=nms,app=psql STATEMENT: SELECT plan(4);
2017-10-30 18:09:27 CDT [4609]: [6-1] user=nroot,db=nms,app=psql DEBUG: building index "__tcache___key" on table "__tcache__"
2017-10-30 18:09:27 CDT [4609]: [7-1] user=nroot,db=nms,app=psql CONTEXT: SQL statement "
CREATE TEMP SEQUENCE __tcache___id_seq;
CREATE TEMP TABLE __tcache__ (
id INTEGER NOT NULL DEFAULT nextval('__tcache___id_seq'),
label TEXT NOT NULL,
value INTEGER NOT NULL,
note TEXT NOT NULL DEFAULT ''
);
CREATE UNIQUE INDEX __tcache___key ON __tcache__(id);
GRANT ALL ON TABLE __tcache__ TO PUBLIC;
GRANT ALL ON TABLE __tcache___id_seq TO PUBLIC;
CREATE TEMP SEQUENCE __tresults___numb_seq;
GRANT ALL ON TABLE __tresults___numb_seq TO PUBLIC;
"
PL/pgSQL function plan(integer) line 6 at EXECUTE
2017-10-30 18:09:27 CDT [4609]: [8-1] user=nroot,db=nms,app=psql
STATEMENT: SELECT plan(4);
---------------------------------------
So, is what I'm trying to do even possible? If not, is there a way to accomplish the goal, or am I just going to have to duplicate about 40 tests for each pair of tables or maybe do some sort of shell script to control the schema via a variable and dynamically create the test code on the fly?
If it matters:
Pg : 9.5.1
PtTap: 0.95 (upgrade to 0.97 is planned)
OS : Centos 6.7
Thanks,
Kevin