pgtap

Updated at:

pgTAP is a unit testing framework for PolarDB for PostgreSQL (Compatible with Oracle), written in PL/pgSQL and PL/SQL. It provides a comprehensive set of assertion functions that emit Test Anything Protocol (TAP) output, making it easy to verify the structure and behavior of database objects—tables, columns, views, indexes, functions, row-level security (RLS) policies, and more.

Prerequisites

Before you begin, ensure that you have:

  • A PolarDB for PostgreSQL (Compatible with Oracle) cluster running one of the following engine versions:

    • Version 2.0 (revision version 2.0.14.3.0 or later)

    • Version 1.0 (revision version 1.1.30 or later)

  • Superuser permissions on the database

To check the revision version of your cluster, run:

SHOW polar_version;

Install pgTAP

Before creating the extension, disable the polar_enable_empty_string_is_null parameter on your database:

ALTER DATABASE <dbname> SET polar_enable_empty_string_is_null=off;

Creating the pgTAP extension requires superuser permissions. Contact us if you need assistance.

Create the extension:

CREATE EXTENSION pgtap;

To remove it:

DROP EXTENSION pgtap;

How it works

Every pgTAP test script follows a four-step structure:

  1. BEGIN — wrap the test in a transaction so it can be rolled back without affecting your data.

  2. SELECT plan(N) — declare the number of tests to run. If the actual number of tests run differs from N, pgTAP reports an error, protecting against silent early failures.

  3. Assertion functions — each assertion emits one TAP line: ok N - <description> on success or not ok N - <description> on failure. The description string (the last argument to most functions) is optional but strongly recommended—it makes failure output immediately actionable.

  4. SELECT * FROM finish(); ROLLBACK; — print a summary of any failures and roll back the transaction.

Test examples

The sections below demonstrate the most common test categories. All examples follow the same four-step structure above.

Tables, views, and indexes

To check whether a table, index, view, or materialized view exists:

BEGIN;
SELECT plan(6);

-- Check whether the tap_table table exists.
SELECT has_table('tap_table');

-- Check whether the tap_table_non_exist table does not exist.
SELECT hasnt_table('tap_table_non_exist');

-- Check whether the tap_view view exists.
SELECT has_view('tap_view');

-- Check whether the materialized_tap_view materialized view exists.
SELECT has_materialized_view('materialized_tap_view');

-- Check whether the tap_table table has the tap_table_index index.
SELECT has_index('tap_table', 'tap_table_index');

-- Check whether tap_table is a relation.
SELECT has_relation('tap_table');

SELECT * FROM finish();
ROLLBACK;

Assertion functions used:

If the objects do not yet exist, the output looks like this:

has_table
-------------------------------------------------
 not ok 1 - Table tap_table should exist        +
 # Failed test 1: "Table tap_table should exist"
(1 row)
                    hasnt_table
---------------------------------------------------
 ok 2 - Table tap_table_non_exist should not exist
(1 row)
                   has_view
-----------------------------------------------
 not ok 3 - View tap_view should exist        +
 # Failed test 3: "View tap_view should exist"
(1 row)
                          has_materialized_view
-------------------------------------------------------------------------
 not ok 4 - Materialized view materialized_tap_view should exist        +
 # Failed test 4: "Materialized view materialized_tap_view should exist"
(1 row)
                       has_index
-------------------------------------------------------
 not ok 5 - Index tap_table_index should exist        +
 # Failed test 5: "Index tap_table_index should exist"
(1 row)
                    has_relation
----------------------------------------------------
 not ok 6 - Relation tap_table should exist        +
 # Failed test 6: "Relation tap_table should exist"
(1 row)
                finish
--------------------------------------
 # Looks like you failed 5 tests of 6
(1 row)

Create the objects and re-run the test:

CREATE TABLE tap_table(col INT PRIMARY KEY, tap_desc TEXT);
CREATE INDEX tap_table_index on tap_table(col);
CREATE VIEW tap_view AS SELECT * FROM tap_table;
CREATE MATERIALIZED VIEW materialized_tap_view AS SELECT * FROM tap_table;

All assertions now pass:

has_table
-------------------------------------
 ok 1 - Table tap_table should exist
(1 row)
                    hasnt_table
---------------------------------------------------
 ok 2 - Table tap_table_non_exist should not exist
(1 row)
             has_view
-----------------------------------
 ok 3 - View tap_view should exist
(1 row)
                    has_materialized_view
-------------------------------------------------------------
 ok 4 - Materialized view materialized_tap_view should exist
(1 row)
                 has_index
-------------------------------------------
 ok 5 - Index tap_table_index should exist
(1 row)
              has_relation
----------------------------------------
 ok 6 - Relation tap_table should exist
(1 row)
 finish
--------
(0 rows)

RLS policies

To verify that an RLS policy exists, applies to the correct command, and is bound to the right roles:

CREATE USER tap_user_1;
CREATE USER tap_user_2;
CREATE TABLE tap_table(col INT PRIMARY KEY, tap_desc TEXT);
CREATE POLICY tap_policy ON tap_table FOR select TO tap_user_1, tap_user_2;

BEGIN;
SELECT plan(5);
SELECT policy_cmd_is(
    'public',
    'tap_table',
    'tap_policy'::NAME,
    'select'
);
SELECT policy_roles_are(
  'public',
  'tap_table',
  'tap_policy',
  ARRAY [
    'tab_user_1', -- Check whether the tab_user_1 user is restricted by the RLS policy tab_policy.
    'tab_user_2' -- Check whether thetab_user_2 user is restricted by the RLS policy tab_policy.
  ]
);
SELECT policies_are(
  'public',
  'tap_table',
  ARRAY [
    'TAP_POLICE' -- Check whether the tap_policy table contains the RLS policy tab_policy.
  ]
);
SELECT * FROM check_test(
    policy_roles_are(
      'public',
      'tap_table',
      'tap_policy',
      ARRAY [
        'tape_user_1' -- Check whether the tab_user_1 user is the only user restricted by the RLS policy tab_policy.
      ]),
    false,
    'check policy roles',
    'Policy tap_policy for table public.tap_table should have the correct roles');
SELECT * FROM finish();
ROLLBACK;

DROP POLICY tap_policy ON tap_table;
DROP TABLE tap_table;
DROP USER tap_user_1;
DROP USER tap_user_2;

Assertion functions used:

  • policy_cmd_is(): checks whether the specified RLS policy exists and applies to the given command.

  • policy_roles_are(): checks whether the RLS policy applies to exactly the listed roles—returns TRUE only if the role list matches completely.

  • policies_are(): checks whether a table contains the specified RLS policies.

  • check_test(): wraps any assertion and lets you specify whether you expect it to pass (true) or fail (false). Useful for verifying that a test correctly catches an error.

Sample output:

policy_cmd_is
------------------------------------------------------------------------------------
 ok 1 - Policy tap_policy for table public.tap_table should apply to SELECT command
(1 row)
                                 policy_roles_are
-----------------------------------------------------------------------------------
 ok 2 - Policy tap_policy for table public.tap_table should have the correct roles
(1 row)
                          policies_are
----------------------------------------------------------------
 ok 3 - Table public.tap_table should have the correct policies
(1 row)
                          check_test
--------------------------------------------------------------
 ok 4 - check policy roles should fail
 ok 5 - check policy roles should have the proper description
(2 rows)
 finish
--------
(0 rows)

Columns

To check whether a column exists and whether it is a primary key or foreign key:

CREATE TABLE tap_table(col INT PRIMARY KEY, tap_desc TEXT);
CREATE INDEX tap_table_index ON tap_table(col);
CREATE UNIQUE INDEX tap_table_unique_index ON tap_table(col);

BEGIN;
SELECT plan(7);
-- Check whether the col column is the primary key of the tap_table table.
SELECT col_is_pk('tap_table', 'col');
-- Check whether the tap_desc column is not a primary key of the tap_table table.
SELECT col_isnt_pk('tap_table', 'tap_desc');
-- Check whether the col column is a foreign key of the tap_table table.
SELECT * FROM check_test(
    col_is_fk('tap_table', 'col'),
    false,
    'check foreign key of table',
    'Column tap_table(col) should be a foreign key');
-- Check whether the col column is not a foreign key of the tap_table table.
SELECT col_isnt_fk('tap_table', 'col');
-- Check whether the tap_table table contains the col column.
SELECT has_column('tap_table', 'col');
-- Check whether the tap_table table does not contain the col column.
SELECT hasnt_column('tap_table', 'non_col');
SELECT * FROM finish();
ROLLBACK;

DROP TABLE tap_table;

Assertion functions used:

  • col_is_pk(): checks whether a column is the primary key of a table.

  • col_isnt_pk(): checks whether a column is not a primary key.

  • col_isnt_fk(): checks whether a column is not a foreign key.

  • has_column(): checks whether a table contains the specified column.

  • hasnt_column(): checks whether a table does not contain the specified column.

Sample output:

col_is_pk
------------------------------------------------------
 ok 1 - Column tap_table(col) should be a primary key
(1 row)
                          col_isnt_pk
---------------------------------------------------------------
 ok 2 - Column tap_table(tap_desc) should not be a primary key
(1 row)
                              check_test
----------------------------------------------------------------------
 ok 3 - check foreign key of table should fail
 ok 4 - check foreign key of table should have the proper description
(2 rows)
                       col_isnt_fk
----------------------------------------------------------
 ok 5 - Column tap_table(col) should not be a foreign key
(1 row)
                has_column
------------------------------------------
 ok 6 - Column tap_table.col should exist
(1 row)
                   hasnt_column
--------------------------------------------------
 ok 7 - Column tap_table.non_col should not exist
(1 row)
 finish
--------
(0 rows)

Functions

To check a function's return type and whether it is defined as SECURITY DEFINER:

CREATE OR REPLACE FUNCTION tap_function()
RETURNS text
AS $$
BEGIN
    RETURN 'This is tap test function';
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

CREATE OR REPLACE FUNCTION tap_function_bool(arg1 integer, arg2 boolean, arg3 text)
RETURNS boolean
AS $$
BEGIN
    RETURN true;
END;
$$ LANGUAGE plpgsql;

BEGIN;
SELECT plan(6);
-- Check whether the return value type of the tap_function function is TEXT.
SELECT function_returns('tap_function', 'text');
-- Check whether the return value type of the tap_function_bool function whose parameter list is INTEGER, BOOLEAN, and TEXT is BOOLEAN.
SELECT function_returns('tap_function_bool', ARRAY['integer', 'boolean', 'text'], 'boolean');
-- Check whether the tap_function function is SECURITY DEFINER.
SELECT is_definer('tap_function');
-- Check whether the tap_function_bool function is not SECURITY DEFINER.
SELECT isnt_definer('tap_function_bool');
-- Check whether the tap_function_bool function is SECURITY DEFINER.
SELECT * FROM check_test(
    is_definer('tap_function_bool'),
    false,
    'check function security definer',
    'Function tap_function_bool() should be security definer');
SELECT * FROM finish();
ROLLBACK;

DROP FUNCTION tap_function;
DROP FUNCTION tap_function_bool;

Assertion functions used:

  • function_returns(): checks whether a function returns the specified type. Pass an array of parameter types to disambiguate overloaded functions.

  • is_definer(): checks whether a function is SECURITY DEFINER.

  • isnt_definer(): checks whether a function is not SECURITY DEFINER.

Sample output:

function_returns
---------------------------------------------------
 ok 1 - Function tap_function() should return text
(1 row)
                                function_returns
---------------------------------------------------------------------------------
 ok 2 - Function tap_function_bool(integer, boolean, text) should return boolean
(1 row)
                        is_definer
-----------------------------------------------------------
 ok 3 - Function tap_function() should be security definer
(1 row)
                            isnt_definer
--------------------------------------------------------------------
 ok 4 - Function tap_function_bool() should not be security definer
(1 row)
                                check_test
---------------------------------------------------------------------------
 ok 5 - check function security definer should fail
 ok 6 - check function security definer should have the proper description
(2 rows)
 finish
--------
(0 rows)

What's next

pgTAP also supports tests for tablespaces, schemas, classes, sequences, triggers, users, languages, rules, operators, and extensions. The test structure is the same as the examples above. For the full list of assertion functions, see the pgTAP documentation.