sqltest

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: BSD-2-Clause Imports: 16 Imported by: 0

Documentation

Overview

Package sqltest provides shared Postgres/MariaDB testcontainer helpers, pre-migrated and ready to use, for repository-level SQL store tests, plus NewServiceForTest (service.go) - the shared "build cfg, call a db package's New(), register Close on cleanup" harness every db.Service's SQL contract test uses.

It lives in its own leaf package, separate from pkg/testsupport itself, matching pkg/testsupport/tracertest and pkg/testsupport/cachetest: those two exist to avoid import cycles through packages that still depend on pkg/model, which this package now also imports (for NewServiceForTest).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MariaDBConfig

func MariaDBConfig(t *testing.T) (*sqlstore.MariaDBConfig, func())

MariaDBConfig spins up a throwaway, unmigrated MariaDB container and returns a *sqlstore.MariaDBConfig pointing at it (for exercising the real sqlstore.Connect/db.New code path, as opposed to StartMariaDB's already-opened *sqlx.DB) and a cleanup function.

func NewServiceForTest

func NewServiceForTest[S Closer](t *testing.T, tracerName string, cfg *model.Cfg, newFn NewServiceFunc[S]) S

NewServiceForTest builds a *logger.Log and *trace.Tracer, calls newFn to exercise a db package's real New()/newSQL() code path end to end (config -> sqlstore.Connect -> sqlstore.ApplySchema -> store wiring) against cfg, and registers svc.Close on t.Cleanup. This is the one piece of setup every db.Service's SQL contract test shares - callers still build cfg themselves (via PostgresConfig/MariaDBConfig below) and still write their own package-specific assertions against the returned service.

func PostgresConfig

func PostgresConfig(t *testing.T) (*sqlstore.PostgresConfig, func())

PostgresConfig spins up a throwaway, unmigrated Postgres container and returns a *sqlstore.PostgresConfig pointing at it (for exercising the real sqlstore.Connect/db.New code path, as opposed to StartPostgres's already-opened *sqlx.DB) and a cleanup function.

func RunPostgresAndMariaDBContract

func RunPostgresAndMariaDBContract[S Closer](t *testing.T, tracerName string, newFn NewServiceFunc[S], checkFn func(*testing.T, S))

RunPostgresAndMariaDBContract runs newFn (a db package's own New()) against both a fresh Postgres and a fresh MariaDB testcontainer, as subtests named "Postgres"/"MariaDB", calling checkFn against each resulting service. This is the shared two-backend contract-test shape every db.Service's SQL test uses; callers keep their own checkFn with package-specific assertions.

func StartMariaDB

func StartMariaDB(t *testing.T) (*sqlx.DB, sqlstore.Dialect, func())

StartMariaDB spins up a throwaway, fully-migrated MariaDB container and returns a connected *sqlx.DB, its Dialect, and a cleanup function. Skips the calling test if Docker is not available.

func StartPostgres

func StartPostgres(t *testing.T) (*sqlx.DB, sqlstore.Dialect, func())

StartPostgres spins up a throwaway, fully-migrated Postgres container and returns a connected *sqlx.DB, its Dialect, and a cleanup function. Skips the calling test if Docker is not available.

The returned cleanup closure always terminates the container via context.Background(), not the ctx used for setup: ctx is t.Context(), which the testing package cancels once the test is done, and the whole point of Terminate is to run reliably during teardown - a caller that registers cleanup via t.Cleanup (rather than a bare defer) could otherwise have it run against an already-canceled context and silently fail to terminate, leaking the container.

Types

type Closer

type Closer interface {
	Close(context.Context) error
}

Closer is satisfied by every db.Service (apigw, verifier, ...): each has its own concrete Service type, but all share this one method shape.

type NewServiceFunc

type NewServiceFunc[S Closer] func(ctx context.Context, cfg *model.Cfg, tracer *trace.Tracer, log *logger.Log) (S, error)

NewServiceFunc matches a db package's own New(ctx, cfg, tracer, log) constructor - every db.Service package has one with this exact signature.

Jump to

Keyboard shortcuts

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