sqldbtest

package
v0.0.0-...-56d8f82 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2024 License: MIT Imports: 13 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithUp

func WithUp(up func(context.Context, *pgx.Conn) error) func(*Config)

WithUp sets the database initialization function.

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

func MustConfig(paths []string, opts ...func(*Config)) *Config

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 Must

func Must(ctx context.Context, tb testing.TB, opts ...func(*Config)) *DB

Must loads the configuration from the environment and creates a new DB.

func MustWithConfig

func MustWithConfig(ctx context.Context, tb testing.TB, config *Config) *DB

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) Close

func (db *DB) Close()

Close closes the DB connection.

func (*DB) DropAllTables

func (db *DB) DropAllTables(ctx context.Context) error

DropAllTables drops all tables available in the schema.

func (*DB) DropTables

func (db *DB) DropTables(ctx context.Context, tables []string) error

DropTables drops the specified tables from the schema.

func (*DB) Init

func (db *DB) Init(ctx context.Context) error

func (*DB) Pool

func (db *DB) Pool() *pgxpool.Pool

Pool returns the underlying connection pool.

func (*DB) Reset

func (db *DB) Reset(ctx context.Context) error

Reset resets the database by dropping all tables and re-creating them.

func (*DB) TruncateAllTables

func (db *DB) TruncateAllTables(ctx context.Context) error

TruncateAllTables truncates all tables in the database.

func (*DB) TruncateTable

func (db *DB) TruncateTable(ctx context.Context, table string) error

TruncateTable truncates the given table.

func (*DB) TruncateTables

func (db *DB) TruncateTables(ctx context.Context, tables []string) error

TruncateTables truncates the given tables.

Jump to

Keyboard shortcuts

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