Documentation ¶
Overview ¶
Package sqldbtest provides utilities for testing database-related code.
It offers a set of utility functions to initialize a database pool, load DDL schema, clean it up, and perform other testing-related tasks.
Note: This package is intended to be used only within test files.
Index ¶
- func WithUp(up func(context.Context, *pgx.Conn) error) func(*Config)
- type Config
- type DB
- func (db *DB) Close()
- func (db *DB) DropAllTables(ctx context.Context) error
- func (db *DB) DropTables(ctx context.Context, tables []string) 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 ¶
Types ¶
type Config ¶
type Config struct { // Timeout is the timeout for establishing a connection to the database in milliseconds. Timeout int `env:"DATABASE_CONNECTION_TIMEOUT" envDefault:"5000"` // optional (default: 5000ms) // 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") Up func(context.Context, *pgx.Conn) error }
func MustConfig ¶
MustConfig 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), 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 ¶
MustWithConfig creates a new DB with the given config.
It initializes a new pool with the given config.
It panics if there is an error initializing a connection to the database.
func (*DB) DropAllTables ¶
DropAllTables drops all tables available in the schema.
func (*DB) DropTables ¶
DropTables drops the specified tables from the schema.
func (*DB) TruncateAllTables ¶
TruncateAllTables truncates all tables in the database.
func (*DB) TruncateTable ¶
TruncateTable truncates the given table.