dbtest

package
v0.0.0-...-3a75ae7 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2024 License: MIT Imports: 17 Imported by: 0

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

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

func MustLoadConfig(paths ...string) *Config

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 Must

func Must(ctx context.Context, tb testing.TB) *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

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

func (db *DB) Close()

Close closes the DB.

func (*DB) DropAllTables

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

func (*DB) Init

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

Init initializes tables in the database by creating a schema provided by the config.

func (*DB) Pool

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

Pool returns the underlying pool.

func (*DB) Reset

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

Reset resets the database by truncating 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