Documentation
¶
Index ¶
Constants ¶
const ( CgoSqliteSuffix = "with_CGO" DBTypeSqliteCgo = "SqliteWithCgo" )
const ( LocalSqliteEnv = "LOCAL_SQLITE" RunAllDBsEnv = "TESTDBS_ALL" )
const (
DBTypeMysql = "mysql"
)
const (
DBTypePostgres = "postgres"
)
const (
DBTypeSqliteNOCgo = "SqliteNoCgo"
)
Variables ¶
This section is empty.
Functions ¶
func InitCustomDbs ¶ added in v0.2.0
func InitCustomDbs(fastDbs, longDBs []TargetDb)
InitCustomDbs initializes the given DBs and makes them available through DBs(). fastDbs always run; longDBs run only when the -alldbs flag or the TESTDBS_ALL env var is set. Call it once, from TestMain.
Types ¶
type ConnParams ¶ added in v0.4.0
type ConnParams struct {
Host string
Port int
User string
Password string
DbName string
// SslMode is libpq's sslmode; "disable" for Postgres. Empty for MySQL,
// which has no such setting.
SslMode string
}
ConnParams holds what a caller needs to open its own connection to a test database, e.g. to test code that builds its own DSN or gorm.Config.
type ConnParamsProvider ¶ added in v0.4.0
type ConnParamsProvider interface {
// ConnParams returns the parameters of the default database, the one
// Conn() uses.
ConnParams() ConnParams
// ConnParamsDbName returns the parameters of the database ConnDbName(name)
// uses, creating it if needed.
ConnParamsDbName(name string) ConnParams
}
ConnParamsProvider is implemented by the targets that run a database server (Postgres and MySQL), not by SQLite. Type-assert a TargetDb from DBs() to get it:
if p, ok := dbt.(testdbs.ConnParamsProvider); ok {
params := p.ConnParamsDbName(t.Name())
}
It is a separate interface so that custom TargetDb implementations passed to InitCustomDbs do not have to implement it.
type SqliteCgo ¶ added in v0.2.0
type SqliteCgo struct {
// contains filtered or unexported fields
}
SqliteCgo is safe for concurrent use: mu guards pool, and ConnDbName holds it while creating a DB so parallel callers asking for one name share one pool.
type SqliteNoCgo ¶ added in v0.2.0
type SqliteNoCgo struct {
// contains filtered or unexported fields
}
SqliteNoCgo is safe for concurrent use: mu guards pool, and ConnDbName holds it while creating a DB so parallel callers asking for one name share one pool.
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
CloseAll closes every DB and, outside local mode, deletes the temp dir.
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
DBs returns the DBs selected by InitDBS or InitCustomDbs. The list is empty when only slow DBs were registered and neither -alldbs nor TESTDBS_ALL is set, so tests should skip in that case. DBs panics if no init function ran.
func NewPostgres ¶ added in v0.3.0
NewPostgres returns a Postgres target that runs image instead of the default postgres:18, e.g. "pgvector/pgvector:pg18" for the vector extension. An empty image keeps the default. The image must start like the official postgres image: same env vars, and "database system is ready to accept connections" logged twice (initdb's temporary server, then the real one).