From 4686d8f771f8e74c6347f8c62806afc4f7527298 Mon Sep 17 00:00:00 2001 From: jnasbyupgrade Date: Fri, 18 Apr 2025 14:10:45 -0500 Subject: [PATCH] Switch plan(int) to plan(bigint) A useful test pattern is to define a matrix of tests in a table or view by building a cartesian join of multiple dimensions. If you do that, you'll want to do something along the lines of SELECT plan( 0 + + (SELECT count(*) FROM test_view) ); Because count(*) returns bigint doing this with plan(int) forces you to cast the output of count(*) to int. Switching plan() to accept a bigint removes the need for that cast. --- Changes | 3 +++ sql/pgtap--1.3.3--1.3.4.sql | 40 +++++++++++++++++++++++++++++++++++++ sql/pgtap.sql.in | 4 ++-- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Changes b/Changes index 23b1d1b8..fe61db2e 100644 --- a/Changes +++ b/Changes @@ -10,6 +10,9 @@ Revision history for pgTAP to Jim Nasby for reporting the issue (#234) Rodolphe QuiƩdeville for for the pull request (#339). * Added `index_is_partial()`, thanks to Rodolphe QuiƩdeville (#342). +* Change plan() to accept bigint. While only an insane person would need more + than 2^31 tests, the bigint version makes calling plan with the count(*) of + rows in a table of tests easier. 1.3.3 2024-04-08T13:44:11Z -------------------------- diff --git a/sql/pgtap--1.3.3--1.3.4.sql b/sql/pgtap--1.3.3--1.3.4.sql index 7607e95d..3b530a0a 100644 --- a/sql/pgtap--1.3.3--1.3.4.sql +++ b/sql/pgtap--1.3.3--1.3.4.sql @@ -87,3 +87,43 @@ BEGIN ); END; $$ LANGUAGE plpgsql; + +DROP FUNCTION plan( integer ); +CREATE OR REPLACE FUNCTION plan( bigint ) +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::int ); + PERFORM _set('failed', 0 ); + RETURN '1..' || $1; +END; +$$ LANGUAGE plpgsql strict; + diff --git a/sql/pgtap.sql.in b/sql/pgtap.sql.in index c1c7ed92..da1c758c 100644 --- a/sql/pgtap.sql.in +++ b/sql/pgtap.sql.in @@ -22,7 +22,7 @@ CREATE OR REPLACE FUNCTION pgtap_version() RETURNS NUMERIC AS 'SELECT __VERSION__;' LANGUAGE SQL IMMUTABLE; -CREATE OR REPLACE FUNCTION plan( integer ) +CREATE OR REPLACE FUNCTION plan( bigint ) RETURNS TEXT AS $$ DECLARE rcount INTEGER; @@ -54,7 +54,7 @@ BEGIN END; -- Save the plan and return. - PERFORM _set('plan', $1 ); + PERFORM _set('plan', $1::int ); PERFORM _set('failed', 0 ); RETURN '1..' || $1; END;