-
Notifications
You must be signed in to change notification settings - Fork 1
/
postgres.go
33 lines (27 loc) · 833 Bytes
/
postgres.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package dbtest
import (
"fmt"
"os/exec"
"testing"
_ "github.com/lib/pq"
"github.com/stretchr/testify/require"
)
// Postgres provisions a new, blank database with a random name on the localhost
// of the running process. It assumes that you have postgres running on the
// default port, have the command line postgres tools installed, and that the
// current user has access to the server. It panics on the event of a failure.
func Postgres(t *testing.T) *DB {
var result DB
name := randomName()
result.Dialect = "postgres"
result.DSN = fmt.Sprintf("postgres://localhost/%s?sslmode=disable", name)
result.t = t
// create the db
err := exec.Command("createdb", name).Run()
require.NoError(t, err)
result.closer = func() {
err := exec.Command("dropdb", name).Run()
require.NoError(t, err)
}
return &result
}