postgres

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2021 License: MIT Imports: 7 Imported by: 7

README

Gnomock Postgres

Gnomock Postgres is a Gnomock preset for running tests against a real Postgres database, without mocks.

package postgres_test

import (
	"database/sql"
	"fmt"

	"github.com/orlangure/gnomock"
	"github.com/orlangure/gnomock/preset/postgres"
)

func ExamplePreset() {
	queries := `
		create table t(a int);
		insert into t (a) values (1);
		insert into t (a) values (2);
	`
	query := `insert into t (a) values (3);`
	p := postgres.Preset(
		postgres.WithUser("gnomock", "gnomick"),
		postgres.WithDatabase("mydb"),
		postgres.WithQueries(queries, query),
		postgres.WithTimezone("Europe/Paris"),
	)

	container, err := gnomock.Start(p)
	if err != nil {
		panic(err)
	}

	defer func() { _ = gnomock.Stop(container) }()

	connStr := fmt.Sprintf(
		"host=%s port=%d user=%s password=%s  dbname=%s sslmode=disable",
		container.Host, container.DefaultPort(),
		"gnomock", "gnomick", "mydb",
	)

	db, err := sql.Open("postgres", connStr)
	if err != nil {
		panic(err)
	}

	var max, avg, min, count float64

	rows := db.QueryRow("select max(a), avg(a), min(a), count(a) from t")

	err = rows.Scan(&max, &avg, &min, &count)
	if err != nil {
		panic("can't query the database: " + err.Error())
	}

	fmt.Println("max", 3)
	fmt.Println("avg", 2)
	fmt.Println("min", 1)
	fmt.Println("count", 3)

	// Output:
	// max 3
	// avg 2
	// min 1
	// count 3
}

Documentation

Overview

Package postgres provides a Gnomock Preset for PostgreSQL database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Preset

func Preset(opts ...Option) gnomock.Preset

Preset creates a new Gmomock Postgres preset. This preset includes a Postgres specific healthcheck function, default Postgres image and port, and allows to optionally set up initial state.

By default, this preset uses `postgres` user with `password` password, with default database `postgres`. Default PostgresQL version is 12.5.

Types

type Option

type Option func(*P)

Option is an optional configuration of this Gnomock preset. Use available Options to configure the container.

func WithDatabase

func WithDatabase(db string) Option

WithDatabase creates a database with the provided name in the container. WithQueries, if provided, runs against the new database.

func WithQueries

func WithQueries(queries ...string) Option

WithQueries executes the provided queries against the database created with WithDatabase, or against default postgres database.

func WithQueriesFile

func WithQueriesFile(file string) Option

WithQueriesFile sets a file name to read initial queries from. Queries from this file are executed before any other queries provided in WithQueries.

func WithTimezone added in v0.17.0

func WithTimezone(timezone string) Option

WithTimezone sets the timezone in this container.

func WithUser

func WithUser(user, password string) Option

WithUser creates a new superuser with the provided credentials in the container.

func WithVersion added in v0.9.0

func WithVersion(version string) Option

WithVersion sets image version.

type P

type P struct {
	DB           string   `json:"db"`
	Queries      []string `json:"queries"`
	QueriesFiles []string `json:"queries_files"`
	User         string   `json:"user"`
	Password     string   `json:"password"`
	Timezone     string   `json:"timezone"`
	Version      string   `json:"version"`
}

P is a Gnomock Preset implementation of PostgreSQL database.

func (*P) Image

func (p *P) Image() string

Image returns an image that should be pulled to create this container.

func (*P) Options

func (p *P) Options() []gnomock.Option

Options returns a list of options to configure this container.

func (*P) Ports

func (p *P) Ports() gnomock.NamedPorts

Ports returns ports that should be used to access this container.

Jump to

Keyboard shortcuts

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