-
Notifications
You must be signed in to change notification settings - Fork 913
/
connector_test.go
87 lines (80 loc) · 1.76 KB
/
connector_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
//go:build go1.10
// +build go1.10
package pq
import (
"context"
"database/sql"
"database/sql/driver"
"os"
"testing"
)
func TestNewConnector_WorksWithOpenDB(t *testing.T) {
name := ""
c, err := NewConnector(name)
if err != nil {
t.Fatal(err)
}
db := sql.OpenDB(c)
defer db.Close()
// database/sql might not call our Open at all unless we do something with
// the connection
txn, err := db.Begin()
if err != nil {
t.Fatal(err)
}
txn.Rollback()
}
func TestNewConnector_Connect(t *testing.T) {
name := ""
c, err := NewConnector(name)
if err != nil {
t.Fatal(err)
}
db, err := c.Connect(context.Background())
if err != nil {
t.Fatal(err)
}
defer db.Close()
// database/sql might not call our Open at all unless we do something with
// the connection
txn, err := db.(driver.ConnBeginTx).BeginTx(context.Background(), driver.TxOptions{})
if err != nil {
t.Fatal(err)
}
txn.Rollback()
}
func TestNewConnector_Driver(t *testing.T) {
name := ""
c, err := NewConnector(name)
if err != nil {
t.Fatal(err)
}
db, err := c.Driver().Open(name)
if err != nil {
t.Fatal(err)
}
defer db.Close()
// database/sql might not call our Open at all unless we do something with
// the connection
txn, err := db.(driver.ConnBeginTx).BeginTx(context.Background(), driver.TxOptions{})
if err != nil {
t.Fatal(err)
}
txn.Rollback()
}
func TestNewConnector_Environ(t *testing.T) {
name := ""
os.Setenv("PGPASSFILE", "/tmp/.pgpass")
defer os.Unsetenv("PGPASSFILE")
c, err := NewConnector(name)
if err != nil {
t.Fatal(err)
}
for key, expected := range map[string]string{
"passfile": "/tmp/.pgpass",
} {
if got := c.opts[key]; got != expected {
t.Fatalf("Getting values from environment variables, for %v expected %s got %s", key, expected, got)
}
}
}