go_test_pg

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2023 License: Apache-2.0 Imports: 18 Imported by: 0

README

go-test-pg — Helper tool to test go programs with PostgreSQL database

GoDoc

v2 version depends on pgx/v5. If you want support for pgx/v4, use 1.x.x version of this package.

The aim this package is to help test golang programs against PostgreSQL database. It creates an empty database for each test and drops it when test is complete.

As a side effect tool checks that all resources are released when test exits. If any Rows is not closed or Conn is not released to pool, test fails.

go-test-pg uses schema file to initialize database with. It creates template database with this schema. Then each temporary database for every test creates from this template database. If the template database for this schema is exists, it will be reused. The name of the template database is composed of baseName and md5 hashsum of schema file content. If schema file is empty, then use default PostgreSQL empty database template1.

On complete, temporary databases would be dropped, template database will not be dropped and would remain for future reuse.

Template database would be created only on first use. If you call NewPool and do not call With<something> on it, real database would not be touched.

Each method was Std version that returns *sql.DB. For example, default method WithFixtures returns *pgxpool.Pool and WithStdFixtures returns *sql.DB.

Example usage

package main

import (
	"context"
	"testing"

	ptg "github.com/olomix/go-test-pg/v2"
)

var dbpool = &ptg.Pgpool{SchemaFile: "../schema.sql"}

func TestX(t *testing.T) {
    dbPool := dbpool.WithEmpty(t)
    var dbName string
    err := dbPool.
        QueryRow(context.Background(), "SELECT current_database()").
        Scan(&dbName)
    if err != nil {
        t.Fatal(err)
    }

    t.Log(dbName)
}

Connection to database configured using standard PostgreSQL environment variable https://www.postgresql.org/docs/11/libpq-envars.html. User needs permissions to create databases.

If you want to skip all database tests, you need to set Skip field in Pgpool struct to true.

var dbpool = &ptg.Pgpool{Skip: true}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fixture

type Fixture struct {
	Query  string
	Params []interface{}
}

type Pgpool

type Pgpool struct {
	// BaseName is the prefix of template and temporary databases.
	// Default is dbtestpg.
	BaseName string
	// Name of schema file. If empty, create empty database.
	SchemaFile string // schema file name
	// If true, skip all database tests.
	Skip bool
	// contains filtered or unexported fields
}

func (*Pgpool) WithEmpty

func (p *Pgpool) WithEmpty(t testing.TB) *pgxpool.Pool

WithEmpty creates empty database from template database, that was created from `schema` file.

func (*Pgpool) WithFixtures

func (p *Pgpool) WithFixtures(t testing.TB, fixtures []Fixture) *pgxpool.Pool

WithFixtures creates database from template database, and initializes it with fixtures from `fixtures` array

func (*Pgpool) WithSQLs

func (p *Pgpool) WithSQLs(t testing.TB, sqls []string) *pgxpool.Pool

WithSQLs creates database from template database, and initializes it with fixtures from `sqls` array

func (*Pgpool) WithStdEmpty

func (p *Pgpool) WithStdEmpty(t testing.TB) *sql.DB

WithStdEmpty creates empty database from template database, that was created from `schema` file.

func (*Pgpool) WithStdFixtures

func (p *Pgpool) WithStdFixtures(t testing.TB, fixtures []Fixture) *sql.DB

WithStdFixtures creates database from template database, and initializes it with fixtures from `fixtures` array

func (*Pgpool) WithStdSQLs

func (p *Pgpool) WithStdSQLs(t testing.TB, sqls []string) *sql.DB

WithStdSQLs creates database from template database, and initializes it with fixtures from `sqls` array

Jump to

Keyboard shortcuts

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