/usr/share/postgresql/9.6/extension/pgtap--0.94.0--0.95.0.sql is in postgresql-9.6-pgtap 0.97.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | CREATE OR REPLACE FUNCTION pgtap_version()
RETURNS NUMERIC AS 'SELECT 0.95;'
LANGUAGE SQL IMMUTABLE;
CREATE OR REPLACE FUNCTION plan( integer )
RETURNS TEXT AS $$
DECLARE
rcount INTEGER;
BEGIN
BEGIN
EXECUTE '
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;
';
EXCEPTION WHEN duplicate_table THEN
-- Raise an exception if there's already a plan.
EXECUTE 'SELECT TRUE FROM __tcache__ WHERE label = ''plan''';
GET DIAGNOSTICS rcount = ROW_COUNT;
IF rcount > 0 THEN
RAISE EXCEPTION 'You tried to plan twice!';
END IF;
END;
-- Save the plan and return.
PERFORM _set('plan', $1 );
PERFORM _set('failed', 0 );
RETURN '1..' || $1;
END;
$$ LANGUAGE plpgsql strict;
CREATE OR REPLACE FUNCTION add_result ( bool, bool, text, text, text )
RETURNS integer AS $$
BEGIN
IF NOT $1 THEN PERFORM _set('failed', _get('failed') + 1); END IF;
RETURN nextval('__tresults___numb_seq');
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION num_failed ()
RETURNS INTEGER AS $$
SELECT _get('failed');
$$ LANGUAGE SQL strict;
CREATE OR REPLACE FUNCTION _finish (INTEGER, INTEGER, INTEGER)
RETURNS SETOF TEXT AS $$
DECLARE
curr_test ALIAS FOR $1;
exp_tests INTEGER := $2;
num_faild ALIAS FOR $3;
plural CHAR;
BEGIN
plural := CASE exp_tests WHEN 1 THEN '' ELSE 's' END;
IF curr_test IS NULL THEN
RAISE EXCEPTION '# No tests run!';
END IF;
IF exp_tests = 0 OR exp_tests IS NULL THEN
-- No plan. Output one now.
exp_tests = curr_test;
RETURN NEXT '1..' || exp_tests;
END IF;
IF curr_test <> exp_tests THEN
RETURN NEXT diag(
'Looks like you planned ' || exp_tests || ' test' ||
plural || ' but ran ' || curr_test
);
ELSIF num_faild > 0 THEN
RETURN NEXT diag(
'Looks like you failed ' || num_faild || ' test' ||
CASE num_faild WHEN 1 THEN '' ELSE 's' END
|| ' of ' || exp_tests
);
ELSE
END IF;
RETURN;
END;
$$ LANGUAGE plpgsql;
-- is_member_of( role, members[], description )
CREATE OR REPLACE FUNCTION is_member_of( NAME, NAME[], TEXT )
RETURNS TEXT AS $$
DECLARE
missing text[];
BEGIN
IF NOT _has_role($1) THEN
RETURN fail( $3 ) || E'\n' || diag (
' Role ' || quote_ident($1) || ' does not exist'
);
END IF;
SELECT ARRAY(
SELECT quote_ident($2[i])
FROM generate_series(1, array_upper($2, 1)) s(i)
LEFT JOIN pg_catalog.pg_roles r ON rolname = $2[i]
WHERE r.oid IS NULL
OR NOT r.oid = ANY ( _grolist($1) )
ORDER BY s.i
) INTO missing;
IF missing[1] IS NULL THEN
RETURN ok( true, $3 );
END IF;
RETURN ok( false, $3 ) || E'\n' || diag(
' Members missing from the ' || quote_ident($1) || E' role:\n ' ||
array_to_string( missing, E'\n ')
);
END;
$$ LANGUAGE plpgsql;
-- is_member_of( role, members[] )
CREATE OR REPLACE FUNCTION is_member_of( NAME, NAME[] )
RETURNS TEXT AS $$
SELECT is_member_of( $1, $2, 'Should have members of role ' || quote_ident($1) );
$$ LANGUAGE SQL;
-- foreign_tables_are( schema, tables, description )
CREATE OR REPLACE FUNCTION foreign_tables_are ( NAME, NAME[], TEXT )
RETURNS TEXT AS $$
SELECT _are( 'foreign tables', _extras('f', $1, $2), _missing('f', $1, $2), $3);
$$ LANGUAGE SQL;
-- foreign_tables_are( tables, description )
CREATE OR REPLACE FUNCTION foreign_tables_are ( NAME[], TEXT )
RETURNS TEXT AS $$
SELECT _are( 'foreign tables', _extras('f', $1), _missing('f', $1), $2);
$$ LANGUAGE SQL;
-- foreign_tables_are( schema, tables )
CREATE OR REPLACE FUNCTION foreign_tables_are ( NAME, NAME[] )
RETURNS TEXT AS $$
SELECT _are(
'foreign tables', _extras('f', $1, $2), _missing('f', $1, $2),
'Schema ' || quote_ident($1) || ' should have the correct foreign tables'
);
$$ LANGUAGE SQL;
-- foreign_tables_are( tables )
CREATE OR REPLACE FUNCTION foreign_tables_are ( NAME[] )
RETURNS TEXT AS $$
SELECT _are(
'foreign tables', _extras('f', $1), _missing('f', $1),
'Search path ' || pg_catalog.current_setting('search_path') || ' should have the correct foreign tables'
);
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION _fix_enum_has_labels(
) RETURNS VOID LANGUAGE PLPGSQL AS $FIX$
BEGIN
IF pg_version_num() < 90100 THEN RETURN; END IF;
EXECUTE $RUN$
-- enum_has_labels( schema, enum, labels, description )
CREATE OR REPLACE FUNCTION enum_has_labels( NAME, NAME, NAME[], TEXT )
RETURNS TEXT AS $$
SELECT is(
ARRAY(
SELECT e.enumlabel
FROM pg_catalog.pg_type t
JOIN pg_catalog.pg_enum e ON t.oid = e.enumtypid
JOIN pg_catalog.pg_namespace n ON t.typnamespace = n.oid
WHERE t.typisdefined
AND n.nspname = $1
AND t.typname = $2
AND t.typtype = 'e'
ORDER BY e.enumsortorder
),
$3,
$4
);
$$ LANGUAGE sql;
-- enum_has_labels( enum, labels, description )
CREATE OR REPLACE FUNCTION enum_has_labels( NAME, NAME[], TEXT )
RETURNS TEXT AS $$
SELECT is(
ARRAY(
SELECT e.enumlabel
FROM pg_catalog.pg_type t
JOIN pg_catalog.pg_enum e ON t.oid = e.enumtypid
WHERE t.typisdefined
AND pg_catalog.pg_type_is_visible(t.oid)
AND t.typname = $1
AND t.typtype = 'e'
ORDER BY e.enumsortorder
),
$2,
$3
);
$$ LANGUAGE sql;
$RUN$;
END;
$FIX$;
SELECT _fix_enum_has_labels();
DROP FUNCTION _fix_enum_has_labels();
-- isnt_strict( schema, function, args[], description )
CREATE OR REPLACE FUNCTION isnt_strict ( NAME, NAME, NAME[], TEXT )
RETURNS TEXT AS $$
SELECT _func_compare($1, $2, $3, NOT _strict($1, $2, $3), $4 );
$$ LANGUAGE SQL;
-- isnt_strict( schema, function, args[] )
CREATE OR REPLACE FUNCTION isnt_strict( NAME, NAME, NAME[] )
RETURNS TEXT AS $$
SELECT ok(
NOT _strict($1, $2, $3),
'Function ' || quote_ident($1) || '.' || quote_ident($2) || '(' ||
array_to_string($3, ', ') || ') should not be strict'
);
$$ LANGUAGE sql;
-- isnt_strict( schema, function, description )
CREATE OR REPLACE FUNCTION isnt_strict ( NAME, NAME, TEXT )
RETURNS TEXT AS $$
SELECT _func_compare($1, $2, NOT _strict($1, $2), $3 );
$$ LANGUAGE SQL;
-- isnt_strict( schema, function )
CREATE OR REPLACE FUNCTION isnt_strict( NAME, NAME )
RETURNS TEXT AS $$
SELECT ok(
NOT _strict($1, $2),
'Function ' || quote_ident($1) || '.' || quote_ident($2) || '() should not be strict'
);
$$ LANGUAGE sql;
-- isnt_strict( function, args[], description )
CREATE OR REPLACE FUNCTION isnt_strict ( NAME, NAME[], TEXT )
RETURNS TEXT AS $$
SELECT _func_compare(NULL, $1, $2, NOT _strict($1, $2), $3 );
$$ LANGUAGE SQL;
-- isnt_strict( function, args[] )
CREATE OR REPLACE FUNCTION isnt_strict( NAME, NAME[] )
RETURNS TEXT AS $$
SELECT ok(
NOT _strict($1, $2),
'Function ' || quote_ident($1) || '(' ||
array_to_string($2, ', ') || ') should not be strict'
);
$$ LANGUAGE sql;
-- isnt_strict( function, description )
CREATE OR REPLACE FUNCTION isnt_strict( NAME, TEXT )
RETURNS TEXT AS $$
SELECT _func_compare(NULL, $1, NOT _strict($1), $2 );
$$ LANGUAGE sql;
-- isnt_strict( function )
CREATE OR REPLACE FUNCTION isnt_strict( NAME )
RETURNS TEXT AS $$
SELECT ok( NOT _strict($1), 'Function ' || quote_ident($1) || '() should not be strict' );
$$ LANGUAGE sql;
-- col_is_unique( schema, table, column[] )
CREATE OR REPLACE FUNCTION col_is_unique ( NAME, NAME, NAME[] )
RETURNS TEXT AS $$
SELECT col_is_unique( $1, $2, $3, 'Columns ' || quote_ident($2) || '(' || _ident_array_to_string($3, ', ') || ') should have a unique constraint' );
$$ LANGUAGE sql;
-- col_is_unique( scheam, table, column )
CREATE OR REPLACE FUNCTION col_is_unique ( NAME, NAME, NAME )
RETURNS TEXT AS $$
SELECT col_is_unique( $1, $2, ARRAY[$3], 'Column ' || quote_ident($2) || '(' || quote_ident($3) || ') should have a unique constraint' );
$$ LANGUAGE sql;
CREATE OR REPLACE FUNCTION _runner( text[], text[], text[], text[], text[] )
RETURNS SETOF TEXT AS $$
DECLARE
startup ALIAS FOR $1;
shutdown ALIAS FOR $2;
setup ALIAS FOR $3;
teardown ALIAS FOR $4;
tests ALIAS FOR $5;
tap TEXT;
tfaild INTEGER := 0;
ffaild INTEGER := 0;
tnumb INTEGER := 0;
fnumb INTEGER := 0;
tok BOOLEAN := TRUE;
errmsg TEXT;
BEGIN
BEGIN
-- No plan support.
PERFORM * FROM no_plan();
FOR tap IN SELECT * FROM _runem(startup, false) LOOP RETURN NEXT tap; END LOOP;
EXCEPTION
-- Catch all exceptions and simply rethrow custom exceptions. This
-- will roll back everything in the above block.
WHEN raise_exception THEN RAISE EXCEPTION '%', SQLERRM;
END;
-- Record how startup tests have failed.
tfaild := num_failed();
FOR i IN 1..COALESCE(array_upper(tests, 1), 0) LOOP
-- What subtest are we running?
RETURN NEXT ' ' || diag_test_name('Subtest: ' || tests[i]);
-- Reset the results.
tok := TRUE;
tnumb := COALESCE(_get('curr_test'), 0);
IF tnumb > 0 THEN
EXECUTE 'ALTER SEQUENCE __tresults___numb_seq RESTART WITH 1';
PERFORM _set('curr_test', 0);
PERFORM _set('failed', 0);
END IF;
BEGIN
BEGIN
-- Run the setup functions.
FOR tap IN SELECT * FROM _runem(setup, false) LOOP
RETURN NEXT regexp_replace(tap, '^', ' ', 'gn');
END LOOP;
-- Run the actual test function.
FOR tap IN EXECUTE 'SELECT * FROM ' || tests[i] || '()' LOOP
RETURN NEXT regexp_replace(tap, '^', ' ', 'gn');
END LOOP;
-- Run the teardown functions.
FOR tap IN SELECT * FROM _runem(teardown, false) LOOP
RETURN NEXT regexp_replace(tap, '^', ' ', 'gn');
END LOOP;
-- Emit the plan.
fnumb := COALESCE(_get('curr_test'), 0);
RETURN NEXT ' 1..' || fnumb;
-- Emit any error messages.
IF fnumb = 0 THEN
RETURN NEXT ' # No tests run!';
tok = false;
ELSE
-- Report failures.
ffaild := num_failed();
IF ffaild > 0 THEN
tok := FALSE;
RETURN NEXT ' ' || diag(
'Looks like you failed ' || ffaild || ' test' ||
CASE tfaild WHEN 1 THEN '' ELSE 's' END
|| ' of ' || fnumb
);
END IF;
END IF;
EXCEPTION WHEN raise_exception THEN
-- Something went wrong. Record that fact.
errmsg := SQLERRM;
END;
-- Always raise an exception to rollback any changes.
RAISE EXCEPTION '__TAP_ROLLBACK__';
EXCEPTION WHEN raise_exception THEN
IF errmsg IS NOT NULL THEN
-- Something went wrong. Emit the error message.
tok := FALSE;
RETURN NEXT ' ' || diag('Test died: ' || errmsg);
errmsg := NULL;
END IF;
END;
-- Restore the sequence.
EXECUTE 'ALTER SEQUENCE __tresults___numb_seq RESTART WITH ' || tnumb + 1;
PERFORM _set('curr_test', tnumb);
PERFORM _set('failed', tfaild);
-- Record this test.
RETURN NEXT ok(tok, tests[i]);
IF NOT tok THEN tfaild := tfaild + 1; END IF;
END LOOP;
-- Run the shutdown functions.
FOR tap IN SELECT * FROM _runem(shutdown, false) LOOP RETURN NEXT tap; END LOOP;
-- Finish up.
FOR tap IN SELECT * FROM _finish( COALESCE(_get('curr_test'), 0), 0, tfaild ) LOOP
RETURN NEXT tap;
END LOOP;
-- Clean up and return.
PERFORM _cleanup();
RETURN;
END;
$$ LANGUAGE plpgsql;
-- check_test( test_output, pass, name, description, diag, match_diag )
CREATE OR REPLACE FUNCTION check_test( TEXT, BOOLEAN, TEXT, TEXT, TEXT, BOOLEAN )
RETURNS SETOF TEXT AS $$
DECLARE
tnumb INTEGER;
aok BOOLEAN;
adescr TEXT;
res BOOLEAN;
descr TEXT;
adiag TEXT;
have ALIAS FOR $1;
eok ALIAS FOR $2;
name ALIAS FOR $3;
edescr ALIAS FOR $4;
ediag ALIAS FOR $5;
matchit ALIAS FOR $6;
BEGIN
-- What test was it that just ran?
tnumb := currval('__tresults___numb_seq');
-- Fetch the results.
aok := substring(have, 1, 2) = 'ok';
adescr := COALESCE(substring(have FROM E'(?:not )?ok [[:digit:]]+ - ([^\n]+)'), '');
-- Now delete those results.
EXECUTE 'ALTER SEQUENCE __tresults___numb_seq RESTART WITH ' || tnumb;
IF NOT aok THEN PERFORM _set('failed', _get('failed') - 1); END IF;
-- Set up the description.
descr := coalesce( name || ' ', 'Test ' ) || 'should ';
-- So, did the test pass?
RETURN NEXT is(
aok,
eok,
descr || CASE eok WHEN true then 'pass' ELSE 'fail' END
);
-- Was the description as expected?
IF edescr IS NOT NULL THEN
RETURN NEXT is(
adescr,
edescr,
descr || 'have the proper description'
);
END IF;
-- Were the diagnostics as expected?
IF ediag IS NOT NULL THEN
-- Remove ok and the test number.
adiag := substring(
have
FROM CASE WHEN aok THEN 4 ELSE 9 END + char_length(tnumb::text)
);
-- Remove the description, if there is one.
IF adescr <> '' THEN
adiag := substring(
adiag FROM 1 + char_length( ' - ' || substr(diag( adescr ), 3) )
);
END IF;
IF NOT aok THEN
-- Remove failure message from ok().
adiag := substring(adiag FROM 1 + char_length(diag(
'Failed test ' || tnumb ||
CASE adescr WHEN '' THEN '' ELSE COALESCE(': "' || adescr || '"', '') END
)));
END IF;
IF ediag <> '' THEN
-- Remove the space before the diagnostics.
adiag := substring(adiag FROM 2);
END IF;
-- Remove the #s.
adiag := replace( substring(adiag from 3), E'\n# ', E'\n' );
-- Now compare the diagnostics.
IF matchit THEN
RETURN NEXT matches(
adiag,
ediag,
descr || 'have the proper diagnostics'
);
ELSE
RETURN NEXT is(
adiag,
ediag,
descr || 'have the proper diagnostics'
);
END IF;
END IF;
-- And we're done
RETURN;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION _cleanup()
RETURNS boolean AS $$
DROP SEQUENCE __tresults___numb_seq;
DROP TABLE __tcache__;
DROP SEQUENCE __tcache___id_seq;
SELECT TRUE;
$$ LANGUAGE sql;
-- has_sequence( schema, sequence )
CREATE OR REPLACE FUNCTION has_sequence ( NAME, NAME )
RETURNS TEXT AS $$
SELECT ok(
_rexists( 'S', $1, $2 ),
'Sequence ' || quote_ident($1) || '.' || quote_ident($2) || ' should exist'
);
$$ LANGUAGE SQL;
GRANT SELECT ON tap_funky TO PUBLIC;
GRANT SELECT ON pg_all_foreign_keys TO PUBLIC;
|