-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool_test.go
More file actions
200 lines (165 loc) · 4.59 KB
/
pool_test.go
File metadata and controls
200 lines (165 loc) · 4.59 KB
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
package pg_test
import (
"context"
"encoding/json"
"fmt"
"testing"
// Packages
pg "github.com/mutablelogic/go-pg"
test "github.com/mutablelogic/go-pg/pkg/test"
assert "github.com/stretchr/testify/assert"
)
// Global connection variable
var conn test.Conn
// Start up a container and test the pool
func TestMain(m *testing.M) {
test.Main(m, &conn)
}
func Test_Pool_001(t *testing.T) {
assert := assert.New(t)
conn := conn.Begin(t)
defer conn.Close()
// Ping the database
assert.NoError(conn.Ping(context.Background()))
}
func Test_Pool_002(t *testing.T) {
assert := assert.New(t)
conn := conn.Begin(t)
defer conn.Close()
// Create a table
err := conn.Exec(context.Background(), "CREATE TABLE test (id SERIAL PRIMARY KEY, name TEXT NOT NULL)")
if !assert.NoError(err) {
t.FailNow()
}
// Insert a row
var test Test
assert.NoError(conn.Insert(context.Background(), &test, test))
assert.NotEqual(0, test.Id)
// Update a row
test.Name = "Hello, World"
assert.NoError(conn.Update(context.Background(), &test, test, test))
assert.NotEqual(0, test.Id)
assert.Equal("Hello, World", test.Name)
// Get a row
assert.NoError(conn.Get(context.Background(), &test, test))
assert.NotEqual(0, test.Id)
assert.Equal("Hello, World", test.Name)
// Delete a row
assert.NoError(conn.Delete(context.Background(), &test, test))
assert.NotEqual(0, test.Id)
assert.Equal("Hello, World", test.Name)
// Insert 20 rows
for i := 0; i < 20; i++ {
assert.NoError(conn.Insert(context.Background(), &test, test))
assert.NotEqual(0, test.Id)
}
// List rows
var list TestList
assert.NoError(conn.List(context.Background(), &list, list))
assert.Equal(uint64(20), list.Count)
assert.Equal(20, len(list.Tests))
// Drop the table
assert.NoError(conn.Exec(context.Background(), "DROP TABLE test"))
}
func Test_Pool_003(t *testing.T) {
assert := assert.New(t)
conn := conn.Begin(t)
defer conn.Close()
// Transaction
err := conn.Tx(context.Background(), func(conn pg.Conn) error {
// Create a table
err := conn.Exec(context.Background(), "CREATE TABLE test (id SERIAL PRIMARY KEY, name TEXT NOT NULL)")
if !assert.NoError(err) {
t.FailNow()
}
// Insert a row
var test Test
assert.NoError(conn.Insert(context.Background(), &test, test))
assert.NotEqual(0, test.Id)
// Update a row
test.Name = "Hello, World"
assert.NoError(conn.Update(context.Background(), &test, test, test))
assert.NotEqual(0, test.Id)
assert.Equal("Hello, World", test.Name)
// Get a row
assert.NoError(conn.Get(context.Background(), &test, test))
assert.NotEqual(0, test.Id)
assert.Equal("Hello, World", test.Name)
// Delete a row
assert.NoError(conn.Delete(context.Background(), &test, test))
assert.NotEqual(0, test.Id)
assert.Equal("Hello, World", test.Name)
// Insert 20 rows
for i := 0; i < 20; i++ {
assert.NoError(conn.Insert(context.Background(), &test, test))
assert.NotEqual(0, test.Id)
}
// List rows
var list TestList
assert.NoError(conn.List(context.Background(), &list, list))
assert.Equal(uint64(20), list.Count)
assert.Equal(20, len(list.Tests))
// Commit
return nil
})
assert.NoError(err)
}
////////////////////////////////////////////////////////////////////////////////
type Test struct {
Id int
Name string
}
type TestList struct {
Count uint64
Tests []Test
}
func (t Test) String() string {
data, err := json.MarshalIndent(t, "", " ")
if err != nil {
return err.Error()
}
return string(data)
}
func (t *Test) Scan(row pg.Row) error {
return row.Scan(&t.Id, &t.Name)
}
func (l *TestList) Scan(row pg.Row) error {
var t Test
if err := t.Scan(row); err != nil {
return err
}
l.Tests = append(l.Tests, t)
return nil
}
func (l *TestList) ScanCount(row pg.Row) error {
return row.Scan(&l.Count)
}
func (t Test) Insert(bind *pg.Bind) (string, error) {
bind.Set("name", t.Name)
return "INSERT INTO test (name) VALUES (@name) RETURNING id, name", nil
}
func (t Test) Select(bind *pg.Bind, op pg.Op) (string, error) {
bind.Set("id", t.Id)
switch op {
case pg.Update:
return "UPDATE test SET ${patch} WHERE id=@id RETURNING id, name", nil
case pg.Get:
return "SELECT id, name FROM test WHERE id=@id", nil
case pg.Delete:
return "DELETE FROM test WHERE id=@id RETURNING id, name", nil
default:
return "", fmt.Errorf("Invalid operation %q", op)
}
}
func (t TestList) Select(bind *pg.Bind, op pg.Op) (string, error) {
switch op {
case pg.List:
return "SELECT id, name FROM test", nil
default:
return "", fmt.Errorf("Invalid operation %q", op)
}
}
func (t Test) Update(bind *pg.Bind) error {
bind.Set("patch", `name=`+bind.Set("name", t.Name))
return nil
}