testdb

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 16 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testdb gives tests one shared Postgres and one shared MariaDB server per machine, and isolated databases on them.

`go test ./...` runs every package in its own process, so process-global state can't keep packages from starting their own containers. Instead:

  • In CI (EP_CI=1) the servers are the workflow's service containers (EP_TEST_POSTGRES_HOST/PORT, EP_TEST_MYSQL_HOST/PORT).
  • Locally each server is a testcontainers container with a fixed name and Reuse enabled. Finding or starting it happens under an OS file lock, so concurrent test processes agree on one container instead of racing. The containers keep running between `go test` runs (their data lives in RAM, without fsync); remove them with `docker rm -f etherpad-test-postgres-v2 etherpad-test-mysql-v2`.
  • Tests never share a database: each caller gets its own (NewDatabase), so packages can run in parallel against the same servers.

Set EP_TEST_SKIP_SQL_SERVERS=1 to skip everything that needs a server.

Index

Constants

View Source
const (
	// User and password of both servers (CI services and containers).
	User     = "test_user"
	Password = "test_password"
)

Variables

This section is empty.

Functions

func PgBouncer added in v0.7.0

func PgBouncer() bool

PgBouncer reports that the PostgreSQL server is reached through PgBouncer in transaction mode (EP_TEST_POSTGRES_PGBOUNCER=1): the data stores then avoid named prepared statements, like with dbSettings.pgBouncer.

func SkipIfDisabled added in v0.7.0

func SkipIfDisabled(t testing.TB, kind Kind)

SkipIfDisabled skips the test when servers of this kind are switched off: EP_TEST_SKIP_SQL_SERVERS for all, EP_TEST_SKIP_MYSQL for MySQL (CI's PgBouncer job only has PostgreSQL).

func SkipSQLServers

func SkipSQLServers() bool

SkipSQLServers reports whether tests needing Postgres/MySQL should be skipped.

Types

type Kind

type Kind string

Kind is the database server type.

const (
	Postgres Kind = "postgres"
	MySQL    Kind = "mysql"
)

type Server

type Server struct {
	Kind Kind
	Host string
	Port int
}

Server is a running database server tests can create databases on.

func Acquire

func Acquire(kind Kind) (*Server, error)

Acquire returns the shared server of the given kind, starting or attaching to it on first use in this process.

func Get

func Get(t testing.TB, kind Kind) *Server

Get is Acquire for tests: it skips the test when EP_TEST_SKIP_SQL_SERVERS is set and fails it when the server is unavailable.

func (*Server) AdminUser

func (s *Server) AdminUser() string

AdminUser is the account tests should connect as: it owns the databases created by NewDatabase.

func (*Server) CreateDatabase

func (s *Server) CreateDatabase() (string, error)

CreateDatabase creates a new empty database and returns its name. The caller is responsible for dropping it (see NewDatabase for tests).

func (*Server) DSN

func (s *Server) DSN(database string) string

DSN returns a database/sql DSN for database (driver "pgx" or "mysql").

func (*Server) DriverName

func (s *Server) DriverName() string

DriverName is the database/sql driver for this server.

func (*Server) DropDatabase

func (s *Server) DropDatabase(name string) error

DropDatabase drops a database created by CreateDatabase.

func (*Server) NewDatabase

func (s *Server) NewDatabase(t testing.TB) string

NewDatabase creates a database for this test and drops it when the test and its subtests have finished.

Jump to

Keyboard shortcuts

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