forked from rubenv/pgtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgtest_test.go
79 lines (57 loc) · 1.39 KB
/
pgtest_test.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package pgtest_test
import (
"os"
"testing"
"github.com/rubenv/pgtest"
"github.com/stretchr/testify/assert"
)
func TestPostgreSQL(t *testing.T) {
t.Parallel()
assert := assert.New(t)
pg, err := pgtest.Start()
assert.NoError(err)
assert.NotNil(pg)
_, err = pg.DB.Exec("CREATE TABLE test (val text)")
assert.NoError(err)
err = pg.Stop()
assert.NoError(err)
}
func TestPostgreSQLWithConfig(t *testing.T) {
t.Parallel()
assert := assert.New(t)
pg, err := pgtest.New().From("/usr/bin/").Start()
assert.NoError(err)
assert.NotNil(pg)
_, err = pg.DB.Exec("CREATE TABLE test (val text)")
assert.NoError(err)
assert.NotEmpty(pg.Host)
assert.NotEmpty(pg.Name)
err = pg.Stop()
assert.NoError(err)
}
func TestPersistent(t *testing.T) {
t.Parallel()
assert := assert.New(t)
dir, err := os.MkdirTemp("", "pgtest")
assert.NoError(err)
defer os.RemoveAll(dir)
pg, err := pgtest.StartPersistent(dir)
assert.NoError(err)
assert.NotNil(pg)
_, err = pg.DB.Exec("CREATE TABLE test (val text)")
assert.NoError(err)
_, err = pg.DB.Exec("INSERT INTO test VALUES ('foo')")
assert.NoError(err)
err = pg.Stop()
assert.NoError(err)
// Open it again
pg, err = pgtest.StartPersistent(dir)
assert.NoError(err)
assert.NotNil(pg)
var val string
err = pg.DB.QueryRow("SELECT val FROM test").Scan(&val)
assert.NoError(err)
assert.Equal(val, "foo")
err = pg.Stop()
assert.NoError(err)
}