dbsuite

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 7 Imported by: 0

README

⛁ DB Suites

This library contains a embeddable DB suite that uses testcontainers to spin up a database server and create a fresh isolated database per test.

⬇️ Installation

go get github.com/survivorbat/go-db-suite

📋 Usage

import (
  "testing"

  "github.com/stretchr/testify/suite"
  "github.com/survivorbat/go-db-suite"
)

type CupcakeRepositorySuite struct {
  dbsuite.Postgres
}

func (c *CupcakeRepositorySuite) TestGetAll() {
  // Arrange
  repository := &CupcakeRepository{Db: c.DB}
  // Act
  data, err := repository.GetAll(t.Context())
  // Assert
  // [...]
}

func TestCupcakeRepositorySuite(t *testing.T) {
  t.Parallel()
  suite.Run(t, new(CupcakeRepositorySuite))
}

To add additional setup code, such as running migrations:

import (
  "testing"

  "github.com/stretchr/testify/suite"
  "github.com/survivorbat/go-db-suite"
)

type CupcakeRepositorySuite struct {
  dbsuite.Postgres
}

func (c *CupcakeRepositorySuite) SetupTest() {
  // Important to call the embedded setup method
  c.Postgres.SetupTest()

  // Run migrations here
}

func TestCupcakeRepositorySuite(t *testing.T) {
  t.Parallel()
  suite.Run(t, new(CupcakeRepositorySuite))
}

🔭 Plans

  • More configuration options
  • Examples on how to use it with subtests
  • More databases

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Postgres

type Postgres struct {
	suite.Suite

	// DB is the connection to a test-specific database. After the test the database is dropped.
	DB *sql.DB

	// ContainerImage can be set before a suite is run. Defaults to postgres:alpine.
	ContainerImage string
	// contains filtered or unexported fields
}

func (*Postgres) SetupSuite

func (s *Postgres) SetupSuite()

SetupSuite will spin up a postgres container and set the struct variables

func (*Postgres) SetupTest

func (s *Postgres) SetupTest()

SetupTest will create a new database from the test name and set the cancel function to drop it later

func (*Postgres) TearDownSuite

func (s *Postgres) TearDownSuite()

TearDownSuite will close the top-level connection and call the suite cancel function

func (*Postgres) TearDownTest

func (s *Postgres) TearDownTest()

TearDownTest will close the connection and call the cancel function

Jump to

Keyboard shortcuts

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