pgtest

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package pgtest gives a test its own Postgres schema.

The index is the one part of this repository that cannot be tested without a database. Rather than mock it — a mocked index would agree with whatever the code did and prove nothing about idempotency, which is the whole contract — the tests that need Postgres skip when none is configured. A contributor without one still runs everything else, and `just check` stays hermetic.

Index

Constants

View Source
const (
	Writer  = "audit_writer"
	Observe = "audit_observe"
	Purge   = "audit_purge"
)

The roles of the parts, as a deployment's migration job names them.

View Source
const Reader = "audit_reader"

Reader is the role a reading service connects as.

It exists because row-level security does not apply to the role that owns the tables, and the tests all connect as that role. A policy exercised only by its owner is a policy nobody has read: it passes whatever it says. So a test about isolation has to come in as somebody else, and this is the somebody.

View Source
const URLEnv = "AUDIT_POSTGRES_URL"

URLEnv names the database the Postgres tests run against.

Variables

This section is empty.

Functions

func AsReader

func AsReader(t *testing.T, pool *pgxpool.Pool) *pgxpool.Pool

AsReader returns a pool on the same schema connected as a role that row-level security applies to, with select granted and nothing else.

It is deliberately not given insert: a reader that could write the trail it reads is not a reader, and the grant is the place that has to say so.

func AsRole added in v0.7.0

func AsRole(t *testing.T, pool *pgxpool.Pool, role string, grant func(context.Context) error) *pgxpool.Pool

AsRole returns a pool on the same schema connected as a role of its own, one the tables' owner has granted what grant says and nothing else.

func Open

func Open(t *testing.T) *pgxpool.Pool

Open returns a migrated pool with a schema of its own, dropped when the test ends. A schema per test is what lets them run in parallel without undoing each other's rows, and it costs one statement.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL