db

package
v0.0.0-...-ff2f52f Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: CC0-1.0 Imports: 13 Imported by: 0

README

Test Database Container

Purpose

To have an idempotent database for each test run.

Implementation Strategy

TestDatabaseContainer is a lightweight wrapper of https://golang.testcontainers.org/modules/postgres/. Because BCDA uses pgx, pgxpool, and sql/db for database connections, this wrapper type will implement methods to utilize any of the aforementioned connection types.

This type also implements methods to apply migrations and seed the database with an initial set of necessary data for the BCDA application to execute database operations.

How To Use

  1. Create the container in the setup of the test suite; this is the longest running step.
  2. Create the database connection in the setup of the test or the setup of the subtest.
  3. (optional) Seed any additional test data with TestDatabaseContainer.ExecuteFile() or TestDatabaseContainer.ExecuteDir(). For details on seed data, please consult the README.md in ./testdata
  4. Restore a snapshot in the test teardown.

Note: Database snapshots cannot be created or restored if a database connection still exists.

type FooTestSuite struct {
	suite.Suite
	db          *sql.DB    // example; pgx or pool can also be used
	dbContainer db.TestDatabaseContainer.  // example; this is optional to be part of the test suite
}

func (s *FooTestSuite) SetupSuite() {
	ctr, err := db.NewTestDatabaseContainer()
	require.NoError(s.T(), err)
	s.dbContainer = ctr
}


func (s *FooTestSuite) SetupTest() {
    db, err := s.dbContainer.NewSqlDbConnection()
	require.NoError(s.T(), err)
	s.db = db
}

func (s *FooTestSuite) TearDownTest() {
	s.db.Close()
	err := s.dbContainer.RestoreSnapshot().   // example, you can restore from another desired snapshot
	require.NoError(s.T(), err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTestDataDir

func GetTestDataDir() (string, error)

GetTestDataDir is a helper function that will return the testdata directory for package in which it is invoked. If the testdata directory has been created in another package or the files exist outside the package, they will not be found.

Types

type TestDatabaseContainer

type TestDatabaseContainer struct {
	Container        *postgres.PostgresContainer
	ConnectionString string
	// contains filtered or unexported fields
}

func NewTestDatabaseContainer

func NewTestDatabaseContainer() (TestDatabaseContainer, error)

Returns a new postgres container with migrations from db/migrations/bcda applied and seed data from db/testdata applied.

func (*TestDatabaseContainer) CreateSnapshot

func (td *TestDatabaseContainer) CreateSnapshot(name string) error

CreateSnapshot will create a snapshot for a given name. Close any active connections to the database before taking a snapshot.

func (*TestDatabaseContainer) ExecuteDir

func (td *TestDatabaseContainer) ExecuteDir(dirpath string) error

ExecuteFile will execute all *.sql files for the provided dirpath. Is it recommended to use the package's testdata/ directory to add test files. A package's testdata/ dir can be retrieved with GetTestDataDir().

func (*TestDatabaseContainer) ExecuteFile

func (td *TestDatabaseContainer) ExecuteFile(path string) (int64, error)

ExecuteFile will execute a *.sql file for a database container. Sql files for testing purposes should be under a package's testdata/ directory.

func (*TestDatabaseContainer) NewPgxConnection

func (td *TestDatabaseContainer) NewPgxConnection() (*pgx.Conn, error)

Return a pgx connection for a given database container.

func (*TestDatabaseContainer) NewPgxPoolConnection

func (td *TestDatabaseContainer) NewPgxPoolConnection() (*pgxpool.Pool, error)

Return a pgx pool for a given database container.

func (*TestDatabaseContainer) NewSqlDbConnection

func (td *TestDatabaseContainer) NewSqlDbConnection() (*sql.DB, error)

Return a sql/db connection for a given database container.

func (*TestDatabaseContainer) RestoreSnapshot

func (td *TestDatabaseContainer) RestoreSnapshot(name string) error

RestoreSnapshot will restore the snapshot that is taken after the database container has had the initial migrations and data seed applied. If no name is provided, it will restore the default snapshot. "Base" will restore the database to it's init state.

Jump to

Keyboard shortcuts

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