pgtest

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2022 License: MIT Imports: 15 Imported by: 0

README

go-pgtest

Import github.com/charlieparkes/go-pgtest.

Example

For a full example, see examples/.

    p, err := pgtest.NewPostgres(ctx)
    pool, err := p.Connect(ctx)
    p.TearDown(ctx)

Documentation

Index

Constants

View Source
const (
	DEFAULT_POSTGRES_REPO    = "postgres"
	DEFAULT_POSTGRES_VERSION = "13-alpine"
)

Variables

This section is empty.

Functions

func Columns added in v1.0.2

func Columns(i interface{}) []string

Given a struct, return the expected column names.

func ValidateModel added in v1.0.2

func ValidateModel(ctx context.Context, f *Postgres, databaseName string, i interface{}) error

ValidateModel checks that a given struct is a valid representation of a database table.

  1. Validate table name (using gorm-style TableName() or name-to-snake)
  2. Validate columns exist.

func ValidateModels added in v1.0.2

func ValidateModels(ctx context.Context, f *Postgres, databaseName string, i ...interface{}) error

ValidateModels checks that the given structs are valid representations of a database tables.

  1. Validate table name (using gorm-style TableName() or name-to-snake)
  2. Validate columns exist.

Types

type ConnOpt

type ConnOpt func(*connConfig)

func ConnOptCreateCopy added in v1.0.4

func ConnOptCreateCopy() ConnOpt

func ConnOptDatabase added in v1.0.4

func ConnOptDatabase(database string) ConnOpt

func ConnOptRole added in v1.0.4

func ConnOptRole(role string) ConnOpt

type ConnectionSettings

type ConnectionSettings struct {
	Host         string
	Port         string
	User         string
	Password     string
	Database     string
	DisableSSL   bool
	MaxOpenConns int
}

Since you shouldn't create a pgx.ConnConfig from scratch, ConnectionSettings exists to hold all the parameters we'll need to work with throughout the lifecycle of a postgres database throughout tests.

func (*ConnectionSettings) Config

func (cs *ConnectionSettings) Config() (*pgx.ConnConfig, error)

func (*ConnectionSettings) Connect

func (cs *ConnectionSettings) Connect(ctx context.Context) (*pgx.Conn, error)

func (*ConnectionSettings) Copy

func (*ConnectionSettings) DSN added in v1.0.3

func (cs *ConnectionSettings) DSN() string

func (*ConnectionSettings) PoolConfig

func (cs *ConnectionSettings) PoolConfig() (*pgxpool.Config, error)

func (*ConnectionSettings) String

func (cs *ConnectionSettings) String() string

func (*ConnectionSettings) URL added in v1.0.3

func (cs *ConnectionSettings) URL() string

type Opt

type Opt func(*Postgres)

func OptExpireAfter

func OptExpireAfter(expireAfter uint) Opt

Tell docker to kill the container after an unreasonable amount of test time to prevent orphans. Defaults to 600 seconds.

func OptLogger

func OptLogger(logger *zap.Logger) Opt

func OptMounts

func OptMounts(mounts []string) Opt

func OptName

func OptName(name string) Opt

func OptNetworkName

func OptNetworkName(networkName string) Opt

func OptRepo added in v1.0.3

func OptRepo(repo string) Opt

func OptSettings

func OptSettings(settings *ConnectionSettings) Opt

func OptSkipTearDown

func OptSkipTearDown() Opt

func OptTimeoutAfter

func OptTimeoutAfter(timeoutAfter uint) Opt

Wait this long for operations to execute. Defaults to 30 seconds.

func OptVersion added in v1.0.3

func OptVersion(version string) Opt

type Postgres

type Postgres struct {
	// contains filtered or unexported fields
}

func Must

func Must(p *Postgres, err error) *Postgres

func NewPostgres

func NewPostgres(ctx context.Context, opts ...Opt) (*Postgres, error)

func (*Postgres) Connect

func (f *Postgres) Connect(ctx context.Context, opts ...ConnOpt) (*pgxpool.Pool, error)

func (*Postgres) CopyDatabase

func (f *Postgres) CopyDatabase(ctx context.Context, source string, target string) error

CopyDatabase creates a copy of an existing postgres database using `createdb --template={source} {target}` source will default to the primary database

func (*Postgres) CreateDatabase

func (f *Postgres) CreateDatabase(ctx context.Context, name string) error

func (*Postgres) DropDatabase

func (f *Postgres) DropDatabase(ctx context.Context, name string) error

func (*Postgres) Dump

func (f *Postgres) Dump(ctx context.Context, dir string, filename string) error

func (*Postgres) HostName

func (f *Postgres) HostName() string

func (*Postgres) LoadSql

func (f *Postgres) LoadSql(ctx context.Context, path string) error

LoadSql runs a file or directory of *.sql files against the default postgres database.

func (*Postgres) LoadSqlPattern

func (f *Postgres) LoadSqlPattern(ctx context.Context, pattern string) error

LoadSqlPattern finds files matching a custom pattern and runs them against the default database.

func (*Postgres) MustConnect

func (f *Postgres) MustConnect(ctx context.Context, opts ...ConnOpt) *pgxpool.Pool

func (*Postgres) Ping

func (f *Postgres) Ping(ctx context.Context) error

func (*Postgres) PingPsql

func (f *Postgres) PingPsql(ctx context.Context) error

func (*Postgres) Psql

func (f *Postgres) Psql(ctx context.Context, cmd []string, mounts []string, quiet bool) (int, error)

func (*Postgres) RecoverTearDown

func (p *Postgres) RecoverTearDown(ctx context.Context)

func (*Postgres) Restore

func (f *Postgres) Restore(ctx context.Context, dir string, filename string) error

func (*Postgres) SetUp

func (f *Postgres) SetUp(ctx context.Context) error

func (*Postgres) Settings

func (f *Postgres) Settings() *ConnectionSettings

func (*Postgres) TableColumns

func (f *Postgres) TableColumns(ctx context.Context, database, schema, table string) ([]string, error)

func (*Postgres) TableExists

func (f *Postgres) TableExists(ctx context.Context, database, schema, table string) (bool, error)

func (*Postgres) Tables

func (f *Postgres) Tables(ctx context.Context, database string) ([]string, error)

func (*Postgres) TearDown

func (p *Postgres) TearDown(ctx context.Context) error

Jump to

Keyboard shortcuts

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