Documentation
¶
Overview ¶
Package postgres provides a Gnomock Preset for PostgreSQL database
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func 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
Example ¶
package main
import (
"database/sql"
"fmt"
mockpostgres "github.com/orlangure/gnomock-postgres"
"github.com/orlangure/gnomock/gnomock"
)
func main() {
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 := mockpostgres.Preset(
mockpostgres.WithUser("gnomock", "gnomick"),
mockpostgres.WithDatabase("mydb"),
mockpostgres.WithQueries(queries, query),
)
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
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 ¶
WithDatabase creates a database with the provided name in the container. WithQueries, if provided, runs against the new database
func WithQueries ¶
WithQueries executes the provided queries against the database created with WithDatabase, or against default postgres database
func WithQueriesFile ¶ added in v0.3.1
WithQueriesFile sets a file name to read initial queries from. Queries from this file are executed before any other queries provided in WithQueries
type P ¶ added in v0.3.1
type P struct {
DB string `json:"db"`
Queries []string `json:"queries"`
QueriesFile string `json:"queries_file"`
User string `json:"user"`
Password string `json:"password"`
}
P is a Gnomock Preset implementation of PostgreSQL database
func (*P) Image ¶ added in v0.3.1
Image returns an image that should be pulled to create this container
func (*P) Ports ¶ added in v0.3.1
func (p *P) Ports() gnomock.NamedPorts
Ports returns ports that should be used to access this container