-
Notifications
You must be signed in to change notification settings - Fork 14
/
readme_test.go
105 lines (94 loc) · 3.04 KB
/
readme_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// This file contains all of the examples from README.md
package pgtestdb_test
import (
"database/sql"
"testing"
_ "github.com/jackc/pgx/v5/stdlib" // registers the "pgx" driver
_ "github.com/lib/pq" // registers the "postgres" driver
"github.com/peterldowns/testy/assert"
"github.com/peterldowns/testy/check"
"github.com/peterldowns/pgtestdb"
)
func TestMyExample(t *testing.T) {
// pgtestdb is concurrency safe, go crazy, run a lot of tests at once
t.Parallel()
// You should connect as an admin user. Use a dedicated server explicitly
// for tests, do NOT use your production database.
conf := pgtestdb.Config{
DriverName: "pgx",
User: "postgres",
Password: "password",
Host: "localhost",
Port: "5433",
Options: "sslmode=disable",
}
// You'll want to use a real migrator, this is just an example. See
// the rest of the docs for more information.
var migrator pgtestdb.Migrator = pgtestdb.NoopMigrator{}
db := pgtestdb.New(t, conf, migrator)
// If there is any sort of error, the test will have ended with t.Fatal().
// No need to check errors! Go ahead and use the database.
var message string
err := db.QueryRow("select 'hello world'").Scan(&message)
assert.Nil(t, err)
assert.Equal(t, "hello world", message)
}
// NewDB is a helper that returns an open connection to a unique and isolated
// test database, fully migrated and ready for you to query.
func NewDB(t *testing.T) *sql.DB {
t.Helper()
conf := pgtestdb.Config{
DriverName: "pgx",
User: "postgres",
Password: "password",
Host: "localhost",
Port: "5433",
Options: "sslmode=disable",
}
// You'll want to use a real migrator, this is just an example. See the rest
// of the docs for more information.
var migrator pgtestdb.Migrator = pgtestdb.NoopMigrator{}
return pgtestdb.New(t, conf, migrator)
}
func TestAQuery(t *testing.T) {
t.Parallel()
db := NewDB(t) // this is the helper defined above
var result string
err := db.QueryRow("SELECT 'hello world'").Scan(&result)
check.Nil(t, err)
check.Equal(t, "hello world", result)
}
func TestWithLibPqDriver(t *testing.T) {
t.Parallel()
pqConf := pgtestdb.Config{
DriverName: "postgres", // uses the lib/pq driver
User: "postgres",
Password: "password",
Host: "localhost",
Port: "5433",
Options: "sslmode=disable",
}
migrator := pgtestdb.NoopMigrator{}
db := pgtestdb.New(t, pqConf, migrator)
var message string
err := db.QueryRow("select 'hello world'").Scan(&message)
assert.Nil(t, err)
assert.Equal(t, "hello world", message)
}
func TestWithPgxStdlibDriver(t *testing.T) {
t.Parallel()
pgxConf := pgtestdb.Config{
DriverName: "pgx", // uses the pgx/stdlib driver
User: "postgres",
Password: "password",
Host: "localhost",
Port: "5433",
Options: "sslmode=disable",
}
migrator := pgtestdb.NoopMigrator{}
db := pgtestdb.New(t, pgxConf, migrator)
var message string
err := db.QueryRow("select 'hello world'").Scan(&message)
assert.Nil(t, err)
assert.Equal(t, "hello world", message)
}