-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgx_db_test.go
363 lines (320 loc) · 7.96 KB
/
pgx_db_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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package goquery
import (
"context"
"fmt"
"reflect"
"strconv"
"strings"
"testing"
)
type FishingSpot struct {
ID int32 `db:"id"`
Location *string `db:"location"`
}
func pgxsetup(t *testing.T) DataStore {
ctx := context.Background()
store := getPgxStore(t)
err := store.Transaction(func(tx Tx) {
pgxtx := tx.PgxTx()
sql := `create table fishing_spots(
id serial not null primary key,
location text)`
_, err := pgxtx.Exec(ctx, sql)
if err != nil {
panic(err)
}
inserts := []string{
`insert into fishing_spots (location) values ('Alpine Frove')`,
`insert into fishing_spots (location) values ('Rivertown')`,
`insert into fishing_spots (location) values ('Pine Island')`,
`insert into fishing_spots (location) values (null)`,
}
for _, v := range inserts {
_, err := pgxtx.Exec(ctx, v)
if err != nil {
panic(err)
}
}
_, err = tx.PgxTx().Exec(ctx, "create table json_test (id int,json_attr jsonb)")
if err != nil {
panic(err)
}
_, err = tx.PgxTx().Exec(ctx, "create table arrays_test (id int,intlist integer[],boollist boolean[])")
if err != nil {
panic(err)
}
})
if err != nil {
t.Errorf("Setup error:%s\n", err)
}
return store
}
func pgxteardown(store DataStore, t *testing.T) {
//ctx := context.Background()
err := store.Transaction(func(tx Tx) {
store.MustExec(&tx, "drop table fishing_spots")
store.MustExec(&tx, "drop table json_test")
store.MustExec(&tx, "drop table arrays_test")
})
if err != nil {
t.Errorf("Failed to teardown test:%s\n", err)
}
}
func getPgxStore(t *testing.T) DataStore {
config := RdbmsConfigFromEnv()
db, err := NewPgxConnection(config)
if err != nil {
t.Errorf("Failed to connect to store:%s\n", err)
}
store := RdbmsDataStore{&db}
return &store
}
func TestPgxConnection(t *testing.T) {
getPgxStore(t)
}
func TestPgxJsonDepricated(t *testing.T) {
correctResult := `[{"id":1,"location":"Alpine Frove"},{"id":2,"location":"Rivertown"},{"id":3,"location":"Pine Island"},{"id":4,"location":null}]`
store := pgxsetup(t)
defer pgxteardown(store, t)
json, err := store.
Select("select * from fishing_spots").
OmitNull(false).
IsJsonArray(true).
FetchJSON()
if err != nil {
t.Errorf("Failed JSON Test: %s\n", err)
}
jsonstring := string(json)
if jsonstring != correctResult {
t.Errorf("Failed JSON Test: Got %s want %s", jsonstring, correctResult)
}
}
func TestPgxJson(t *testing.T) {
correctResult := `[{"id":1,"location":"Alpine Frove"},{"id":2,"location":"Rivertown"},{"id":3,"location":"Pine Island"},{"id":4,"location":null}]`
store := pgxsetup(t)
defer pgxteardown(store, t)
builder := strings.Builder{}
err := store.
Select("select * from fishing_spots").
OmitNull(false).
IsJsonArray(true).
OutputJson(&builder).
Fetch()
if err != nil {
t.Errorf("Failed JSON Test: %s\n", err)
}
jsonstring := builder.String()
if jsonstring != correctResult {
t.Errorf("Failed JSON Test: Got %s want %s", jsonstring, correctResult)
}
}
func TestPgxSlice(t *testing.T) {
ap := "Alpine Frove"
rt := "Rivertown"
pi := "Pine Island"
correctResult := []FishingSpot{
{1, &ap},
{2, &rt},
{3, &pi},
{4, nil},
}
store := pgxsetup(t)
defer pgxteardown(store, t)
///////////autogenerate select///////////////////
fsTbl := TableDataSet{
Name: "fishing_spots",
}
dest := &[]FishingSpot{}
err := store.Select().
DataSet(&fsTbl).
Dest(dest).
PanicOnErr(true).
Fetch()
if err != nil {
t.Errorf("Failed Slice Test:%s\n", err)
}
if reflect.DeepEqual(dest, correctResult) {
t.Errorf("Failed Slice Test: Got %v want %v", dest, correctResult)
}
/////////////////add select statement///////////
stmts := Statements{
"named-select": `select * from fishing_spots`,
}
fsTbl.Statements = stmts
dest = &[]FishingSpot{}
err = store.Select().
DataSet(&fsTbl).
StatementKey("named-select").
Dest(dest).
Fetch()
if err != nil {
t.Errorf("Failed Slice Test:%s\n", err)
}
if !reflect.DeepEqual(dest, correctResult) {
t.Errorf("Failed Slice Test: Got %v want %v", dest, correctResult)
}
}
func TestPgxRow(t *testing.T) {
store := pgxsetup(t)
defer pgxteardown(store, t)
stmt := "select * from fishing_spots"
dest := FishingSpot{}
rows, err := store.Select(stmt).FetchRows()
if err != nil {
t.Errorf("Failed Rows Test:%s\n", err)
}
defer rows.Close()
for rows.Next() {
rows.ScanStruct(&dest)
fmt.Println(dest)
}
}
func TestPgxRowFunction(t *testing.T) {
store := pgxsetup(t)
defer pgxteardown(store, t)
stmt := "select * from fishing_spots"
dest := FishingSpot{}
builder := strings.Builder{}
err := store.Select(stmt).
ForEachRow(func(row Rows) error {
if dest.Location != nil {
builder.WriteString(fmt.Sprintf("%d:%s\n", dest.ID, *dest.Location))
}
return nil
}).
Dest(&dest).
Fetch()
if err != nil {
t.Errorf("Failed Rows Test:%s\n", err)
}
fmt.Println(builder.String())
}
func TestPgxRowFunction2(t *testing.T) {
store := pgxsetup(t)
defer pgxteardown(store, t)
stmt := "select * from fishing_spots"
dest := FishingSpot{}
builder := strings.Builder{}
err := store.Select(stmt).
ForEachRow(func(row Rows) error {
err := row.ScanStruct(&dest)
if err != nil {
return err
}
if dest.Location != nil {
builder.WriteString(fmt.Sprintf("%d:%s\n", dest.ID, *dest.Location))
}
return nil
}).
Dest(&dest).
Fetch()
if err != nil {
t.Errorf("Failed Rows Test:%s\n", err)
}
fmt.Println(builder.String())
}
func TestPgxInsert(t *testing.T) {
l10 := "New Spot 10"
l11 := "New Spot 11"
fs := []FishingSpot{
{10, &l10},
{11, &l11},
}
//fs := FishingSpot{10, &l10}
fsTbl := TableDataSet{
Name: "fishing_spots",
TableFields: FishingSpot{},
}
store := pgxsetup(t)
defer pgxteardown(store, t)
err := store.Insert(&fsTbl).Records(fs).Execute()
if err != nil {
t.Error(err)
}
}
func TestPgxInsertBatch(t *testing.T) {
/*
l10 := "New Spot 10"
l11 := "New Spot 11"
fs := []FishingSpot{
{10, &l10},
{11, &l11},
}
*/
count := 40000
fs := make([]FishingSpot, count)
for i := 0; i < count; i++ {
val := strconv.Itoa(i)
fs[i] = FishingSpot{int32(i + 10), &val}
}
fsTbl := TableDataSet{
Name: "fishing_spots",
TableFields: FishingSpot{},
}
store := pgxsetup(t)
defer pgxteardown(store, t)
err := store.Insert(&fsTbl).Records(&fs).Batch(true).BatchSize(len(fs)).Execute()
if err != nil {
t.Error(err)
}
}
type ArrayTest struct {
ID int `db:"id"`
IntList []int `db:"intlist"`
Boollist []bool `db:"boollist"`
}
var at TableDataSet = TableDataSet{
Name: "arrays_test",
TableFields: ArrayTest{},
}
func TestPgxArrayInsert(t *testing.T) {
store := pgxsetup(t)
defer pgxteardown(store, t)
il := []int{3, 5, 7, 9}
bl := []bool{false, true, false, true}
store.MustExec(NoTx, `insert into arrays_test (id,intlist,boollist) values ($1,$2,$3)`, 1, il, bl)
atData := ArrayTest{2, []int{100, 101, 102}, []bool{true, false, false, true}}
err := store.Insert(&at).Records(atData).Execute()
if err != nil {
t.Error(err)
}
dest := []ArrayTest{}
err = store.Select("select * from arrays_test").Dest(&dest).Fetch()
if err != nil {
t.Error(err)
}
t.Log(dest)
}
type JsonTest struct {
ID int `db:"id"`
Attributes JsonAttr `db:"json_attr"`
}
type JsonAttr struct {
Name string `json:"name"`
Age int `json:"age"`
}
var fs TableDataSet = TableDataSet{
Name: "json_test",
TableFields: JsonTest{},
}
func TestJson(t *testing.T) {
store := pgxsetup(t)
defer pgxteardown(store, t)
store.MustExec(NoTx, `insert into json_test values (1,'{"name":"jack","age":8}')`)
store.MustExec(NoTx, `insert into json_test values ($1,$2)`, 2, JsonAttr{"Luna", 4})
recs := []JsonTest{
{3, JsonAttr{"John", 20}},
{4, JsonAttr{"Karen", 30}},
}
err := store.Insert(&fs).Records(recs).Execute()
if err != nil {
t.Error(err)
}
//store.MustExec(NoTx, `insert into json_test (id) values (1,'{"n`)
dest := []JsonTest{}
err = store.Select("select * from json_test").Dest(&dest).Fetch()
if err != nil {
t.Error(err)
}
t.Log(dest)
}