Documentation ¶
Overview ¶
Package drydock implements a way to run unit tests against a PostgreSQL database if you have docker installed.
import ( "github.com/borud/drydock" "github.com/stretchr/testify/assert" ) func TestSomething(t *testing.T) { // This fires up a Docker container with postgres. You can // run multiple of these concurrently since this creates a // new container, listening to a unique port. New will wait // until the database responds or the operation times out // before responding. dd, err := drydock.New("postgres:13") assert.Nil(t, err) // Ask the unit test framework to clean up once the test // is done. If the test crashes this might end up not // running, so there may be docker containers still running. // These will have names that start with "drydock-". t.Cleanup(func() { dd.Terminate() }) // Start the container err = dd.Start() assert.Nil(t, err) // This creates a new database inside the postgres instance // and returns a connection to it. Or rather, a *sqlx.DB. // The idea being that every time you ask for a new DB and // connection, you want to have a clean database so you can // know the state. db, err := dd.NewDBConn() assert.Nil(t, err) // We can then do our database things. _, err = db.Exec("CREATE TABLE foo (id INTEGER NOT NULL)") assert.Nil(t, err) stmt, err := db.Preparex("INSERT INTO foo (id) VALUES ($1)") assert.Nil(t, err) for i := 0; i < 10; i++ { _, err := stmt.Exec(i) assert.Nil(t, err) } // We don't bother cleaning up after ourselves since // the container gets nuked anyway. }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Drydock ¶
type Drydock struct { // Image is the docker image given by repository and tag, eg. "postgres:13" Image string // DataDir is the directory where the data files live during the test DataDir string // Port is a randomly allocated free port that is used by the database instance Port int // Password of the PostgreSQL database Password string // contains filtered or unexported fields }
Drydock represents a database drydock
func New ¶
New creates a new Drydock instance. Returns a *Drydock and a nil error if successful, otherwise a nil *Drydock and an error
Click to show internal directories.
Click to hide internal directories.