Help us shape the future of Go! Take the 2026 Go Developer Survey

pgtest

package module
v0.1.0-preview.14 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package pgtest is the PostgreSQL server the Go modules' PostgreSQL tests share (the postgres module's own and the Agent Framework integration's cross-language tests): it starts or finds the server, makes the roles, and creates databases migrated as the .NET library migrates them. It is a module of its own (Go 1.26), with the store's own PostgreSQL tests, so the store's module requires no container library. For tests only; nothing else imports it.

The schema is never reimplemented here. Each test database is migrated by running the .NET migrator's own scripts (src/AgentExperience.Storage.Postgres/Migrations/*.sql) in name order, one transaction per script, as the owner role, which is what ExperienceSchemaMigrator.MigrateAsync does. The application role's privileges are the contract's sql/application-role-privileges.sql then, from contract 1.4, sql/application-role-privileges-1.4.sql (the injection table's), both modes (row-level security on, what a contract 1.x port runs against).

The server comes from PGHOST (with PGPORT, PGUSER, PGPASSWORD: a superuser connection) when set, else from a Testcontainers postgres container (AGENTEXPERIENCE_POSTGRES_MAJOR, default 16). With neither, the tests skip, unless AGENTEXPERIENCE_REQUIRE_POSTGRES=1, when they fail. testcontainers-go applies TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX to every image it starts, the Reaper's (Ryuk) included, and recognises the Reaper by its name's suffix, so a prefix (CI's Docker Hub mirror) needs no workaround here, unlike testcontainers-python (python/tests/_postgres.py).

Index

Constants

View Source
const (
	OwnerRole       = "contract_owner"
	ApplicationRole = "contract_app"
	RolePassword    = "contract-role-password"
)

The roles every test database uses.

View Source
const (
	PrivilegesFile   = "application-role-privileges.sql"
	Privileges14File = "application-role-privileges-1.4.sql"
)

The contract's privileges files: 1.0's, and 1.4's for the injection table.

Variables

This section is empty.

Functions

func ApplicationRolePrivileges

func ApplicationRolePrivileges(t *testing.T, role string) []string

ApplicationRolePrivileges are the statements of the contract's two privileges files for role (a plain identifier), the 1.0 file then the 1.4 file: what the .NET privileges call grants.

func CreateDatabase

func CreateDatabase(t *testing.T, admin Server, label string) string

CreateDatabase is a fresh database, migrated by the .NET scripts and with the application role's privileges, dropped when the test ends (after the stores that use it close). Give each module's labels their own prefix: a server may be shared.

func CreateDatabaseWith

func CreateDatabaseWith(t *testing.T, admin Server, label string, privileges []string) string

CreateDatabaseWith is CreateDatabase with the application role given exactly privileges: PrivilegesOf the 1.0 file alone, say, for a schema whose role was never granted the injection table.

func DatabaseName

func DatabaseName(label string) string

DatabaseName is a unique, valid database name for a label: a readable prefix and a hash of the whole label.

func PrivilegesOf

func PrivilegesOf(t *testing.T, role, file string) []string

PrivilegesOf are the statements of one contract privileges file (under contract/sql/) for role (a plain identifier): those under "-- mode: always", then those under "-- mode: row-level-security".

func Run

func Run(m *testing.M) int

Run is a TestMain's body: it starts or finds the server, prepares the roles, runs the tests, and stops the server. Without a server the tests run and Require skips them, unless AGENTEXPERIENCE_REQUIRE_POSTGRES=1, when Run fails.

Types

type Server

type Server struct {
	Host           string
	Port           int
	User, Password string
}

Server is a PostgreSQL server and its superuser.

func Require

func Require(t *testing.T) Server

Require is the server, or skips the test when there is none.

func (Server) Admin

func (s Server) Admin(database string) string

Admin is a connection string to database as the superuser.

func (Server) Application

func (s Server) Application(database string) string

Application is a connection string to database as the application role.

func (Server) ConnString

func (s Server) ConnString(database, user, password string) string

ConnString is a connection string to database as user.

func (Server) Owner

func (s Server) Owner(database string) string

Owner is a connection string to database as the owner role.

Jump to

Keyboard shortcuts

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