-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
230 lines (187 loc) · 4.78 KB
/
main_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package pg_test
import (
"bytes"
"database/sql/driver"
"net"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/go-pg/pg"
. "github.com/onsi/ginkgo"
. "gopkg.in/check.v1"
)
func TestUnixSocket(t *testing.T) {
opt := pgOptions()
opt.Network = "unix"
opt.Addr = "/var/run/postgresql/.s.PGSQL.5432"
opt.TLSConfig = nil
db := pg.Connect(opt)
defer db.Close()
_, err := db.Exec("SELECT 'test_unix_socket'")
if err != nil {
t.Fatal(err)
}
}
func TestGocheck(t *testing.T) { TestingT(t) }
var _ = Suite(&DBTest{})
type DBTest struct {
db *pg.DB
}
func (t *DBTest) SetUpTest(c *C) {
t.db = pg.Connect(pgOptions())
}
func (t *DBTest) TearDownTest(c *C) {
c.Assert(t.db.Close(), IsNil)
}
func (t *DBTest) TestQueryZeroRows(c *C) {
res, err := t.db.Query(pg.Discard, "SELECT 1 WHERE 1 != 1")
c.Assert(err, IsNil)
c.Assert(res.RowsAffected(), Equals, 0)
}
func (t *DBTest) TestQueryOneErrNoRows(c *C) {
_, err := t.db.QueryOne(pg.Discard, "SELECT 1 WHERE 1 != 1")
c.Assert(err, Equals, pg.ErrNoRows)
}
func (t *DBTest) TestQueryOneErrMultiRows(c *C) {
_, err := t.db.QueryOne(pg.Discard, "SELECT generate_series(0, 1)")
c.Assert(err, Equals, pg.ErrMultiRows)
}
func (t *DBTest) TestExecOne(c *C) {
res, err := t.db.ExecOne("SELECT 'test_exec_one'")
c.Assert(err, IsNil)
c.Assert(res.RowsAffected(), Equals, 1)
}
func (t *DBTest) TestExecOneErrNoRows(c *C) {
_, err := t.db.ExecOne("SELECT 1 WHERE 1 != 1")
c.Assert(err, Equals, pg.ErrNoRows)
}
func (t *DBTest) TestExecOneErrMultiRows(c *C) {
_, err := t.db.ExecOne("SELECT generate_series(0, 1)")
c.Assert(err, Equals, pg.ErrMultiRows)
}
func (t *DBTest) TestScan(c *C) {
var dst int
_, err := t.db.QueryOne(pg.Scan(&dst), "SELECT 1")
c.Assert(err, IsNil)
c.Assert(dst, Equals, 1)
}
func (t *DBTest) TestExec(c *C) {
res, err := t.db.Exec("CREATE TEMP TABLE test(id serial PRIMARY KEY)")
c.Assert(err, IsNil)
c.Assert(res.RowsAffected(), Equals, -1)
res, err = t.db.Exec("INSERT INTO test VALUES (1)")
c.Assert(err, IsNil)
c.Assert(res.RowsAffected(), Equals, 1)
}
func (t *DBTest) TestStatementExec(c *C) {
res, err := t.db.Exec("CREATE TEMP TABLE test(id serial PRIMARY KEY)")
c.Assert(err, IsNil)
c.Assert(res.RowsAffected(), Equals, -1)
stmt, err := t.db.Prepare("INSERT INTO test VALUES($1)")
c.Assert(err, IsNil)
defer stmt.Close()
res, err = stmt.Exec(1)
c.Assert(err, IsNil)
c.Assert(res.RowsAffected(), Equals, 1)
}
func (t *DBTest) TestLargeWriteRead(c *C) {
src := bytes.Repeat([]byte{0x1}, 1e6)
var dst []byte
_, err := t.db.QueryOne(pg.Scan(&dst), "SELECT ?", src)
c.Assert(err, IsNil)
c.Assert(dst, DeepEquals, src)
}
func (t *DBTest) TestIntegrityError(c *C) {
_, err := t.db.Exec("DO $$BEGIN RAISE unique_violation USING MESSAGE='foo'; END$$;")
c.Assert(err.(pg.Error).IntegrityViolation(), Equals, true)
}
type customStrSlice []string
func (s customStrSlice) Value() (driver.Value, error) {
return strings.Join(s, "\n"), nil
}
func (s *customStrSlice) Scan(v interface{}) error {
if v == nil {
*s = nil
return nil
}
b := v.([]byte)
if len(b) == 0 {
*s = []string{}
return nil
}
*s = strings.Split(string(b), "\n")
return nil
}
func (t *DBTest) TestScannerValueOnStruct(c *C) {
src := customStrSlice{"foo", "bar"}
dst := struct{ Dst customStrSlice }{}
_, err := t.db.QueryOne(&dst, "SELECT ? AS dst", src)
c.Assert(err, IsNil)
c.Assert(dst.Dst, DeepEquals, src)
}
//------------------------------------------------------------------------------
type badConnError string
func (e badConnError) Error() string { return string(e) }
func (e badConnError) Timeout() bool { return false }
func (e badConnError) Temporary() bool { return false }
type badConn struct {
net.TCPConn
readDelay, writeDelay time.Duration
readErr, writeErr error
}
var _ net.Conn = &badConn{}
func (cn *badConn) Read([]byte) (int, error) {
if cn.readDelay != 0 {
time.Sleep(cn.readDelay)
}
if cn.readErr != nil {
return 0, cn.readErr
}
return 0, badConnError("bad connection")
}
func (cn *badConn) Write([]byte) (int, error) {
if cn.writeDelay != 0 {
time.Sleep(cn.writeDelay)
}
if cn.writeErr != nil {
return 0, cn.writeErr
}
return 0, badConnError("bad connection")
}
func perform(n int, cbs ...func(int)) {
var wg sync.WaitGroup
for _, cb := range cbs {
for i := 0; i < n; i++ {
wg.Add(1)
go func(cb func(int), i int) {
defer GinkgoRecover()
defer wg.Done()
cb(i)
}(cb, i)
}
}
wg.Wait()
}
func eventually(fn func() error, timeout time.Duration) (err error) {
done := make(chan struct{})
var exit int32
go func() {
for atomic.LoadInt32(&exit) == 0 {
err = fn()
if err == nil {
close(done)
return
}
time.Sleep(timeout / 100)
}
}()
select {
case <-done:
return nil
case <-time.After(timeout):
atomic.StoreInt32(&exit, 1)
return err
}
}