Skip to content

Running the Test Suite

SMTA has 35 test files containing 449 total assertions. All tests target the live database and exercise SQL directly via pgTap.

Terminal window
npm test

This executes scripts/run_tests.sh, which:

  1. Loads all fixture files from tests/fixtures/
  2. Runs pg_prove against all 35 test files in the order listed below
  3. Calls SELECT test_helpers.cleanup_test_data() to remove fixture data

Pass the file path directly to pg_prove:

Terminal window
pg_prove --dbname=postgres tests/functions/01_organization_functions.sql

To override connection details:

Terminal window
pg_prove \
--host=localhost \
--port=54322 \
--dbname=postgres \
--username=postgres \
tests/rls/01_orgs.sql

Tests are organized by concern. Each directory corresponds to a layer of the SMTA schema.

Verifies the database structure exists as expected.

FileWhat it tests
schemas_existRequired schemas are present
core_tablesCore schema tables and columns
platform_tablesPlatform schema tables and columns
indexesExpected indexes exist
triggersExpected triggers are registered

Tests organization and unit membership management.

FileWhat it tests
roles_existMembership role enum values
super_admin_protectionSuper admin cannot be demoted or removed
transferOwnership transfer logic
orgOrganization-level membership operations
unitUnit-level membership operations
FileWhat it tests
updated_atupdated_at timestamp is set on row updates
auto_createAuto-creation of related records on insert

Tests platform-admin functionality.

FileWhat it tests
rolesPlatform role assignments
usersPlatform user management
settingsPlatform-wide settings
feature_flagsFeature flag evaluation

Tests the public-facing SQL functions called by application code.

FileWhat it tests
organizationspublic.create_organization and related functions
unitsUnit CRUD functions
membershipMembership management functions
userUser profile functions
FileWhat it tests
createInvitation creation and validation
acceptAccepting an invitation
manageRevoking and listing invitations

Verifies row-level security policies enforce access control correctly. Each file tests a specific table.

FileTable covered
orgscore.organizations
unitscore.units
membershipscore.memberships
unit_membershipscore.unit_memberships
users_metacore.users_meta
audit_logscore.audit_logs
invitationscore.invitations
organizations_metacore.organizations_meta
FileWhat it tests
multi_org_userA user belonging to multiple organizations
cascading_deletesSoft-delete and cascade behavior
concurrent_accessBehavior under concurrent modification
role_scenariosEdge cases in role assignment and escalation

Tests set the current user context using the test helper — not by writing to app.current_user_id directly:

-- Correct
SELECT test_helpers.set_auth_user(test_helpers.get_test_user_id('maria@test.bellaitalia.com'));
-- Do NOT use this form
-- SET app.current_user_id = '...';

set_auth_user calls core.get_current_user_id() internally, which is the same function all production SQL uses to resolve the acting user.

Every test file follows the same structure:

-- 01_organization_functions.sql
-- Purpose: Test public organization management functions
BEGIN;
SELECT plan(12);
-- Set auth context
SELECT test_helpers.set_auth_user(test_helpers.get_test_user_id('maria@test.bellaitalia.com'));
SELECT lives_ok(
$$SELECT public.create_organization('Test Restaurant', 'A test organization')$$,
'create_organization should succeed'
);
SELECT ok(
EXISTS (SELECT 1 FROM core.organizations WHERE name = 'Test Restaurant'),
'Organization should be created with correct name'
);
SELECT finish();
ROLLBACK;

Key points:

  • Wrap everything in BEGIN / ROLLBACK so no test state persists
  • Declare the exact assertion count in SELECT plan(N) — it must match the number of test calls
  • Call SELECT finish() before ROLLBACK
  • Set auth context before any operation that depends on the current user