testdbs

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2025 License: LGPL-3.0 Imports: 21 Imported by: 0

README

testdbs

Utility package to start and stop different DBs in tests

Usage

Modify your tests

Modify the Main test function to initialize the DBs before the tests are run and to delete after

func TestMain(m *testing.M) {
	testdbs.InitDBS()
	// main block that runs tests
	code := m.Run()
	err := testdbs.Clean()
	if err != nil {
		os.Exit(1)
	}
	os.Exit(code)
}

And then in your tests you can iterate over the DBs

func TestMyFunction(t *testing.T) {
    for _, db := range testdbs.DBs() {
        t.Run(dbt.DbType(), func(t *testing.T) {
            db := dbt.Conn()
            // db is *gorm.DB
		})
	}
}

if you need to isolate DBs, e.g. run multiple tests on the same db/table you can use ConnDbName("custom") to crete a new database wit the passed name.

note: connections will be reused cross tests for every db name

func TestMyFunction(t *testing.T) {
    for _, db := range testdbs.DBs() {
        t.Run(dbt.DbType(), func(t *testing.T) {
            db := dbt.ConnDbName("custom")
            // db is *gorm.DB
		})
	}
}

running tests

As a default calling go test will only start an embedded sqlite on a temp directory, to run the tests with all the supported DBs you need to call go test -alldbs

Documentation

Index

Constants

View Source
const (
	CgoSqliteSuffix = "with_CGO"
	DBTypeSqliteCgo = "SqliteWithCgo"
)
View Source
const (
	LocalSqliteEnv = "LOCAL_SQLITE"
	RunAllDBsEnv   = "TESTDBS_ALL"
)
View Source
const (
	DBTypeMysql = "mysql"
)
View Source
const (
	DBTypePostgres = "postgres"
)
View Source
const (
	DBTypeSqliteNOCgo = "SqliteNoCgo"
)

Variables

This section is empty.

Functions

func Clean

func Clean() error

Clean closes all db connections and deletes related test files

func InitCustomDbs added in v0.2.0

func InitCustomDbs(fastDbs, longDBs []TargetDb)

func InitDBS

func InitDBS()

Types

type SqliteCgo added in v0.2.0

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

func (*SqliteCgo) Close added in v0.2.0

func (c *SqliteCgo) Close(name string) error

func (*SqliteCgo) CloseAll added in v0.2.0

func (c *SqliteCgo) CloseAll() error

func (*SqliteCgo) Conn added in v0.2.0

func (c *SqliteCgo) Conn() *gorm.DB

func (*SqliteCgo) ConnDbName added in v0.2.0

func (c *SqliteCgo) ConnDbName(name string) *gorm.DB

func (*SqliteCgo) DbType added in v0.2.0

func (c *SqliteCgo) DbType() string

func (*SqliteCgo) Init added in v0.2.0

func (c *SqliteCgo) Init(logger logger.Interface)

type SqliteNoCgo added in v0.2.0

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

func (*SqliteNoCgo) Close added in v0.2.0

func (c *SqliteNoCgo) Close(name string) error

func (*SqliteNoCgo) CloseAll added in v0.2.0

func (c *SqliteNoCgo) CloseAll() error

func (*SqliteNoCgo) Conn added in v0.2.0

func (c *SqliteNoCgo) Conn() *gorm.DB

func (*SqliteNoCgo) ConnDbName added in v0.2.0

func (c *SqliteNoCgo) ConnDbName(name string) *gorm.DB

func (*SqliteNoCgo) DbType added in v0.2.0

func (c *SqliteNoCgo) DbType() string

func (*SqliteNoCgo) Init added in v0.2.0

func (c *SqliteNoCgo) Init(logger logger.Interface)

type TargetDb added in v0.2.0

type TargetDb interface {
	DbType() string
	Init(logger logger.Interface)
	Conn() *gorm.DB
	ConnDbName(name string) *gorm.DB
	Close(name string) error
	CloseAll() error
}

func DBs added in v0.2.0

func DBs() []TargetDb

Jump to

Keyboard shortcuts

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