Documentation ¶
Overview ¶
Package dbtesting providers utilities for testing database related code.
It provides a set of utility functions to initialize a database pool, load DLL schema, clean it up, etc.
Note the package is intended to be used only within testing files.
Index ¶
- type Config
- type DB
- func (db *DB) Close()
- func (db *DB) DropAllTables(ctx context.Context) error
- func (db *DB) Init(ctx context.Context) error
- func (db *DB) Pool() *pgxpool.Pool
- func (db *DB) Reset(ctx context.Context) error
- func (db *DB) TruncateAllTables(ctx context.Context) error
- func (db *DB) TruncateTable(ctx context.Context, table string) error
- func (db *DB) TruncateTables(ctx context.Context, tables []string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // TimeoutSecond is the timeout for establishing a connection to the database. TimeoutSecond int `env:"DB_CONN_TIMEOUT" envDefault:"5"` // optional (default: 5) // DatabaseURI is the connection string for the database. DatabaseURI string `env:"DATABASE_URI"` // Schema is the schema to use for the database. Schema string `env:"DB_SCHEMA" envDefault:"public"` // optional (default: "public") // FilePath is the path to the DDL file. FilePath []string `env:"DB_SCHEMA_PATH" envSeparator:","` AfterConnect func(context.Context, *pgx.Conn) error `env:"-"` }
func MustLoadConfig ¶
MustLoadConfig loads the configuration from the environment.
If no paths are provided, it defaults to ".test.env" in the current working directory (which for tests is the directory in which they are located at), and in the root of the project.
It panics if there is an error loading the configuration.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a wrapper around pgxpool.Pool with useful utilities for DB management in tests.
func MustWithConfig ¶
Must creates a new DB.
It initializes a new pool with the given config.
It panics if there is an error initializing a connection to the database.
func (*DB) Init ¶
Init initializes tables in the database by creating a schema provided by the config.
func (*DB) TruncateAllTables ¶
TruncateAllTables truncates all tables in the database.
func (*DB) TruncateTable ¶
TruncateTable truncates the given table.