brrr

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2025 License: MIT Imports: 22 Imported by: 0

README

BRRR

Brrr is an integration testing thingy for go applications using postgres.

What does it do and what does it try to solve

  1. Launches a single go test container for postgres.
  2. Optionally runs seeding migration scripts.
  3. Marks the database as a template database.
  4. For each test, creates a new database from the template so each test can have its own database in isolation.
  5. Runs isolated integration tests in parallel.

Why The name

It goes fast.

Example

var c *brrr.Container

func TestMain(m *testing.M) {
	cfg := brrr.Config {
		User: "postgres",
		Password: "test",
		Database: "acme",
	}

	var err error
	c, err = brrr.NewContainer(cfg)
	if err != nil {
		log.Fatalf("failed to create test container: %v", err)
	}
	defer c.Close()

	exitCode := m.Run()

	os.Exit(exitCode)
}

func TestSomething(t *testing.T) {
	t.Parallel()

	db, err := c.NewInstance(t.Context())
	if err != nil {
		t.Fatal(err)
	}
	defer c.CloseInstance(t.Context(), db)

	err = db.Connection.Ping(t.Context())
	if err != nil {
		t.Fatalf("failed to ping database: %v", err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Database user for connecting to the database instances created from the template database.
	User string
	// Password for the database user.
	Password string
	// The name of the database which will be used as the template database.
	Database string

	// Image to use for the test container. Defaults to "postgres:17.2"
	Image string

	// MaxConnections to the database. Defaults to 1000.
	MaxConnections int

	// Path to migrations/seeding directory. Will ignore if empty.
	MigrationsPath string

	// Path to seeding directory. Will ignore if empty.
	SeedPath string

	// Seed func to run after migrations. Will ignore if empty.
	SeedFunc func(db *sql.DB, connStr string) error

	// Logger for logging the test container's output. Useful for debugging. Default to testcontainer's noopLogger
	Logger *slog.Logger
	// contains filtered or unexported fields
}

type Container

type Container struct {
	// contains filtered or unexported fields
}

func NewContainer

func NewContainer(cfg Config) (*Container, error)

NewContainer launches a postgres test container and sets up the template database.

func (*Container) Close

func (c *Container) Close() error

Close will terminate the database and delete the test container image

func (*Container) CloseInstance

func (c *Container) CloseInstance(ctx context.Context, di *DatabaseInstance) error

Close will close the connection to the database for the single test instance and drop the database

func (*Container) NewInstance

func (c *Container) NewInstance(ctx context.Context) (*DatabaseInstance, error)

NewInstance clones the template database to setup a database scoped to a single test

type DatabaseInstance

type DatabaseInstance struct {
	// Connection to the database for the single test instance
	Connection *pgx.Conn

	// Name of the database for this single test instance
	Name string
}

type SlogAdapter added in v0.0.3

type SlogAdapter struct {
	// contains filtered or unexported fields
}

func (*SlogAdapter) Printf added in v0.0.3

func (s *SlogAdapter) Printf(format string, v ...any)

Jump to

Keyboard shortcuts

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