Skip to content

🚢 Go package providing lifecycle management for PostgreSQL Docker instances.

License

Notifications You must be signed in to change notification settings

adrianbrad/psqldocker

Repository files navigation

adrianbrad psqldocker

🚢 psqldocker GitHub release

powered by ory/dockertest.

GitHub go.mod Go version of a Go module GoDoc reference example

CodeFactor Go Report Card codecov

lint-test grype codeql gitleaks


Go package providing lifecycle management for PostgreSQL Docker instances.

Here is an article expanding on the usage of this package.

Leverage Docker to run unit and integration tests against a real PostgreSQL database.

Usage

Recommended: In a TestXxx function

package foo_test

import (
	"testing"

	"github.com/adrianbrad/psqldocker"
)

func TestXxx(t *testing.T) {
    const schema = "CREATE TABLE users(user_id UUID PRIMARY KEY);"
	
    c, err := psqldocker.NewContainer(
        "user",
        "password",
        "dbName",
        psqldocker.WithContainerName("test"), 
        // initialize with a schema
        psqldocker.WithSql(schema),
        // you can add other options here
    )
    if err != nil {
        t.Fatalf("cannot start new psql container: %s\n", err)
    }
	
    t.Cleanup(func() {
        err = c.Close()
        if err != nil {
            t.Logf("err while closing conainter: %w", err)
        }
    })
	
    t.Run("Subtest", func(t *testing.T) {
        // execute your test logic here 
    })
}

In a TestMain function

package foo_test

import (
	"log"
	"testing"

	"github.com/adrianbrad/psqldocker"
)

func TestMain(m *testing.M) {
    const schema = "CREATE TABLE users(user_id UUID PRIMARY KEY);"

    c, err := psqldocker.NewContainer(
        "user",
        "password",
        "dbName",
        psqldocker.WithContainerName("test"), 
        // initialize with a schema
        psqldocker.WithSql(schema),
        // you can add other options here
    )
    if err != nil {
        log.Fatalf("cannot start new psql container: %s\n", err)
    }
	
    defer func() {
        err = c.Close()
        if err != nil {
            log.Printf("err while closing conainter: %w", err)
        }
    }() 
	
    m.Run()
}

About

🚢 Go package providing lifecycle management for PostgreSQL Docker instances.

Topics

Resources

License

Stars

Watchers

Forks