This library contains a embeddable DB suite that uses testcontainers to spin up a database server and create a fresh isolated database per test.
go get github.com/survivorbat/go-db-suiteimport (
"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))
}- More configuration options
- Examples on how to use it with subtests
- More databases