Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 

README.md

go-postgres-test

Circle CI

Go Report Card

A helper library for managing ephemeral test databases in Postgres.

It won't do anything if Postgres is not installed, but if the binaries are available, this library will allow you to create an ephemeral database, use it for the life of a test, and then clean up afterwards.

It was created cos there's currently no 'in memory' postgres clone that can be used for testing.

It's intended to be used on a laptop, or within an ephemeral container, not anyplace where a real production postgres instance is being used.

In a test, set up the following:

    var tempDir string
    var dbPid int
    var dbDir string
    var dbName string

    func TestMain(m *testing.M) {
        setUp()

        code := m.Run()

        tearDown()

        os.Exit(code)
    }

    func setUp() {
        dir, err := ioutil.TempDir("", "testdb")
        if err != nil {
            fmt.Printf("Error creating temp dir %q: %s", tempDir, err)
            os.Exit(1)
        }

        tempDir = dir

        dbName = "fargle"
        dbDir = fmt.Sprintf("%s/%s", tempDir, dbName)

        dbPid, dbPort, err = StartTestDB(dbDir, dbName)
        if err != nil {
            fmt.Printf("Failed to start test db %q: %s", dbName, err)
        }
        
        running, err := PostgresRunning(dbPort)
        if err != nil {
            fmt.Printf("Error Checking to see if postgres is running: %s", err)
            os.Exit(1
        }
        if running {
            fmt.Printf("Postgres is running with pid %d on port %d", dbPid, dbPort)
        }
    }

    func tearDown() {
        err := StopPostgres(dbPid)
        if err != nil {
            fmt.Printf("Failed to stop postgres process %d", dbPid)
        } else {
            fmt.Println("database shut down.")
        }

        if _, err := os.Stat(tempDir); !os.IsNotExist(err) {
            os.Remove(tempDir)
        }

    }

About

Library for managing ephemeral test databases in Postgres

Resources

Packages

No packages published
You can’t perform that action at this time.