dbtest

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package dbtest gives one test its own Postgres schema.

It lives outside kit/db so that kit/db exports no DDL door: an application connection can only reach the database through db.Run or db.RunSystem, and the owner connection a test needs for CREATE TABLE is a plain *sql.DB opened here. Nothing in kit/ imports this package; only tests do.

The schema travels in the connection URL, as options=-csearch_path=<schema>, so every connection a pool opens from it lands in the same place, and so does everything db.Migrate creates: the ledger and the tenancy helper functions are private to the test rather than shared through public.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Open

func Open(t *testing.T, rawURL string) *sql.DB

Open is a database/sql pool on rawURL, closed when the test ends.

func OpenFor added in v1.1.0

func OpenFor(t testing.TB, rawURL string) *sql.DB

OpenFor is Open for tests and benchmarks, with the same automatic cleanup.

func RoleOf

func RoleOf(t *testing.T, rawURL string) string

RoleOf is the login role of a connection URL, so a test names the same role the app connects as instead of hard-coding it a second time.

func Schema

func Schema(t *testing.T, extra ...db.MigrationSource) (admin *sql.DB, app *db.Conn)

Schema creates an isolated database schema with the kernel's tables and the named capabilities — a module's exported Migrations source, such as task.Migrations, and the sources of the modules it depends on (auth needs user). It returns an owner handle for assertions and an application connection subject to row-level security. A test that names no capability gets the tenancy, outbox and limits tables and nothing else.

func System

func System(ctx context.Context, conn *db.Conn, fn func(context.Context, db.Tx[db.System]) error) error

System runs fn in a cross-tenant transaction, for a test that has to reach the tables no tenant owns: the tenant registry, and the outbox as the relay sees it.

func SystemToken

func SystemToken() tenancy.SystemToken

SystemToken is that capability, for a test of something the application hands one to: a module's control-plane routes take api.SystemToken(), and a job that has to cross tenants takes it in Module.Routes, so a test of either has to be able to stand where the kernel stands.

func URLs

func URLs(t *testing.T) (adminURL, appURL string)

URLs creates the schema and returns the two connection URLs that reach it: the first owns it and holds the DDL rights, the second is the unprivileged role row-level security binds. The schema is dropped when the test ends.

It fails rather than skips when the environment names no database. A suite that quietly skips the isolation tests proves nothing.

func URLsFor added in v1.1.0

func URLsFor(t testing.TB) (adminURL, appURL string)

URLsFor is URLs for tests and benchmarks, with the same automatic cleanup.

Types

This section is empty.

Jump to

Keyboard shortcuts

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