test

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

The `test` package supports running unit tests with containerized supporting services. This is useful for testing that require external services, such as databases, message brokers, etc. The `test` package provides a simple API to create and manage containers.

For example, in order to test postgres integration tests, use the following boilerplate for your tests:

	// Global variable which will hold the connection
	var conn test.Conn

 // Start up a container and return the connection
	func TestMain(m *testing.M) {
					test.Main(m, &conn)
				}

		     // Run a test which pings the database
				func Test_Pool_001(t *testing.T) {
					assert := assert.New(t)
					conn := conn.Begin(t)
					defer conn.Close()

					// Ping the database
					assert.NoError(conn.Ping(context.Background()))
				}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Main

func Main(m *testing.M, conn *Conn)

Types

type Conn

type Conn struct {
	pg.PoolConn
	// contains filtered or unexported fields
}

Conn is a wrapper around pg.PoolConn which provides a test connection

func (*Conn) Begin

func (c *Conn) Begin(t *testing.T) *Conn

Begin a test

func (*Conn) Close

func (c *Conn) Close()

End a test

type Container

type Container struct {
	testcontainers.Container `json:"-"`
	Env                      map[string]string `json:"env"`
	MappedPorts              map[string]string `json:"mapped_ports"`
}

func NewContainer

func NewContainer(ctx context.Context, name, image string, opt ...Opt) (*Container, error)

Create a new container with the given name and image, and additional options

func NewPgxContainer

func NewPgxContainer(ctx context.Context, name string, verbose bool, tracer pg.TraceFn) (*Container, pg.PoolConn, error)

Create a new postgresql database and pool, using a unique container name

func (*Container) Close

func (c *Container) Close(ctx context.Context) error

func (*Container) GetEnv

func (c *Container) GetEnv(name string) (string, error)

func (*Container) GetPort

func (c *Container) GetPort(name string) (string, error)

func (*Container) String

func (c *Container) String() string

type Opt

type Opt func(*opts) error

func OptCommand

func OptCommand(cmd []string) Opt

func OptEnv

func OptEnv(name, value string) Opt

func OptPorts

func OptPorts(ports ...string) Opt

func OptPostgres

func OptPostgres(user, password, database string) Opt

Jump to

Keyboard shortcuts

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