Repository navigation
Expand file tree
/
Copy pathpostgres_test.go
More file actions
462 lines (410 loc) · 11 KB
/
Copy pathpostgres_test.go
File metadata and controls
462 lines (410 loc) · 11 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package postgres_test
import (
"context"
"errors"
"flag"
"fmt"
"math"
"reflect"
"strings"
"testing"
"time"
"github.com/randlabs/go-postgres"
)
// -----------------------------------------------------------------------------
const (
varCharText = "varchar-sample"
charText = "char-sample"
veryLongText = "this is a big text"
)
type TestRowDef struct {
id int
num uint64
sm int16
bi int64
bi2 uint64
dbl float64
va string
chr string
txt string
blob []byte
ts time.Time
dt time.Time
tim time.Time
b bool
js string
}
type TestNullableRowDef struct {
id int
num *uint64
sm *int16
bi *int64
bi2 *uint64
dbl *float64
va *string
chr *string
txt *string
blob *[]byte
ts *time.Time
dt *time.Time
tim *time.Time
b *bool
js *string
}
type TestJSON struct {
Id int `json:"id"`
Text string `json:"text"`
}
// -----------------------------------------------------------------------------
func TestPostgres(t *testing.T) {
var db *postgres.Database
var err error
// Parse and check command-line parameters
flag.Parse()
checkSettings(t)
ctx := context.Background()
// Create database driver
if len(pgUrl) > 0 {
db, err = postgres.NewFromURL(ctx, pgUrl)
} else {
db, err = postgres.New(ctx, postgres.Options{
Host: pgHost,
Port: uint16(pgPort),
User: pgUsername,
Password: pgPassword,
Name: pgDatabaseName,
})
}
if err != nil {
t.Fatalf("%v", err.Error())
}
defer db.Close()
t.Log("Creating test table")
err = createTestTable(ctx, db)
if err != nil {
t.Fatalf("%v", err.Error())
}
t.Log("Inserting test data")
err = insertTestData(ctx, db)
if err != nil {
t.Fatalf("%v", err.Error())
}
t.Log("Reading test data")
err = readTestData(ctx, db)
if err != nil {
t.Fatalf("%v", err.Error())
}
t.Log("Reading test data (multi-row)")
err = readMultiTestData(ctx, db)
if err != nil {
t.Fatalf("%v", err.Error())
}
}
// -----------------------------------------------------------------------------
func createTestTable(ctx context.Context, db *postgres.Database) error {
// Destroy old test table if exists
_, err := db.Exec(ctx, `DROP TABLE IF EXISTS go_postgres_test_table CASCADE`)
if err != nil {
return fmt.Errorf("unable to drop tables [err=%v]", err.Error())
}
// Create the test table
_, err = db.Exec(ctx, `CREATE TABLE go_postgres_test_table (
id INT NOT NULL,
num NUMERIC(24, 0) NULL,
sm SMALLINT NULL,
bi BIGINT NULL,
bi2 BIGINT NULL,
dbl DOUBLE PRECISION NULL,
va VARCHAR(32) NULL,
chr CHAR(32) NULL,
txt TEXT NULL,
blob BYTEA NULL,
ts TIMESTAMP NULL,
dt DATE NULL,
tim TIME NULL,
b BOOLEAN NULL,
js JSONB NULL,
PRIMARY KEY (id)
)`)
if err != nil {
return fmt.Errorf("unable to create test table [err=%v]", err.Error())
}
// Done
return nil
}
func insertTestData(ctx context.Context, db *postgres.Database) error {
return db.WithinTx(ctx, func(ctx context.Context, tx postgres.Tx) error {
for idx := 1; idx <= 2; idx++ {
rd := genTestRowDef(idx, true)
err := insertTestRowDef(ctx, tx, rd)
if err != nil {
return fmt.Errorf("unable to insert test data [id=%v/err=%v]", rd.id, err.Error())
}
nrd := genTestNullableRowDef(idx, true)
err = insertTestNullableRowDef(ctx, tx, nrd)
if err != nil {
return fmt.Errorf("unable to insert test data [id=%v/err=%v]", nrd.id, err.Error())
}
}
// Done
return nil
})
}
func readTestData(ctx context.Context, db *postgres.Database) error {
for idx := 1; idx <= 2; idx++ {
compareRd := genTestRowDef(idx, false)
rd, err := readTestRowDef(ctx, db, compareRd.id)
if err != nil {
return fmt.Errorf("unable to verify test data [id=%v/err=%v]", compareRd.id, err.Error())
}
// Do deep comparison
if !reflect.DeepEqual(compareRd, rd) {
return errors.New("data mismatch")
}
compareNrd := genTestNullableRowDef(idx, false)
nrd, err := readTestNullableRowDef(ctx, db, compareNrd.id)
if err != nil {
return fmt.Errorf("unable to verify test data [id=%v/err=%v]", compareNrd.id, err.Error())
}
// Do deep comparison
if !reflect.DeepEqual(compareNrd, nrd) {
return fmt.Errorf("data mismatch while comparing test data [id=%v]", compareNrd.id)
}
}
// Done
return nil
}
func readMultiTestData(ctx context.Context, db *postgres.Database) error {
compareRd := make([]TestRowDef, 0)
for idx := 1; idx <= 2; idx++ {
compareRd = append(compareRd, genTestRowDef(idx, false))
}
rd, err := readMultiTestRowDef(ctx, db, compareRd)
if err != nil {
return fmt.Errorf("unable to verify test data [err=%v]", err.Error())
}
// Do deep comparison
if len(compareRd) != len(rd) {
return fmt.Errorf("data mismatch while comparing test data [len1=%d/len2=%d]", len(compareRd), len(rd))
}
for idx := 0; idx < len(rd); idx++ {
if !reflect.DeepEqual(compareRd[idx], rd[idx]) {
return fmt.Errorf("data mismatch while comparing test data [id=%v]", compareRd[idx].id)
}
}
// Done
return nil
}
func genTestRowDef(index int, write bool) TestRowDef {
var r TestRowDef
switch index {
case 1:
r = TestRowDef{
id: 1,
num: math.MaxUint64,
sm: math.MaxInt16,
bi: math.MaxInt64,
bi2: math.MaxInt64, // BIGINT is signed so sending an uint64 that does not overflow
dbl: math.MaxFloat64,
va: varCharText,
chr: charText,
txt: veryLongText,
blob: testBLOB,
ts: time.Date(2022, 12, 31, 23, 59, 59, 0, time.UTC),
dt: time.Date(2022, 12, 31, 0, 0, 0, 0, time.UTC),
tim: time.Date(2000, 1, 1, 23, 59, 59, 0, time.UTC),
b: false,
js: string(testJSONBytes),
}
if !write {
r.chr = charText + strings.Repeat(" ", 32-len(charText))
}
case 2:
r = TestRowDef{
id: 2,
num: math.MaxUint64,
sm: math.MinInt16,
bi: math.MinInt64,
bi2: math.MaxInt64, // BIGINT is signed so sending an uint64 that does not overflow
dbl: math.SmallestNonzeroFloat64,
va: varCharText,
chr: charText,
txt: veryLongText,
blob: testBLOB,
ts: time.Date(2022, 12, 31, 23, 59, 59, 0, time.UTC),
dt: time.Date(2022, 12, 31, 0, 0, 0, 0, time.UTC),
tim: time.Date(2000, 1, 1, 23, 59, 59, 0, time.UTC),
b: false,
js: string(testJSONBytes),
}
if !write {
r.chr = charText + strings.Repeat(" ", 32-len(charText))
}
default:
panic("unexpected")
}
return r
}
func genTestNullableRowDef(index int, write bool) TestNullableRowDef {
var r TestNullableRowDef
switch index {
case 1:
r = TestNullableRowDef{
id: 101,
num: addressOf[uint64](math.MaxUint64),
sm: addressOf[int16](math.MaxInt16),
bi: addressOf[int64](math.MaxInt64),
bi2: addressOf[uint64](math.MaxInt64), // BIGINT is signed so sending an uint64 that does not overflow
dbl: addressOf[float64](math.MaxFloat64),
va: addressOf[string](varCharText),
chr: addressOf[string](charText),
txt: nil,
blob: nil,
ts: nil,
dt: nil,
tim: nil,
b: nil,
js: nil,
}
if !write {
r.chr = addressOf[string](charText + strings.Repeat(" ", 32-len(charText)))
}
case 2:
r = TestNullableRowDef{
id: 102,
num: nil,
sm: nil,
bi: nil,
bi2: nil,
dbl: nil,
va: nil,
chr: nil,
txt: addressOf[string](veryLongText),
blob: addressOf[[]byte](testBLOB),
ts: addressOf[time.Time](time.Date(2022, 12, 31, 23, 59, 59, 0, time.UTC)),
dt: addressOf[time.Time](time.Date(2022, 12, 31, 0, 0, 0, 0, time.UTC)),
tim: addressOf[time.Time](time.Date(2000, 1, 1, 23, 59, 59, 0, time.UTC)),
b: addressOf[bool](false),
js: addressOf[string](string(testJSONBytes)),
}
default:
panic("unexpected")
}
return r
}
func insertTestRowDef(ctx context.Context, tx postgres.Tx, rd TestRowDef) error {
_, err := tx.Exec(ctx, `
INSERT INTO go_postgres_test_table (
id, num, sm, bi, bi2, dbl, va, chr, txt, blob, ts, dt, tim, b, js
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
)
`,
rd.id, rd.num, rd.sm, rd.bi, rd.bi2, rd.dbl, rd.va, rd.chr, rd.txt, rd.blob, rd.ts, rd.dt, rd.tim, rd.b, rd.js,
)
return err
}
func readTestRowDef(ctx context.Context, db *postgres.Database, id int) (TestRowDef, error) {
rd := TestRowDef{}
err := db.QueryRow(ctx, `
SELECT
id, num, sm, bi, bi2, dbl, va, chr, txt, blob, ts, dt, tim, b, js
FROM
go_postgres_test_table
WHERE
id = $1
`, id).Scan(
&rd.id, &rd.num, &rd.sm, &rd.bi, &rd.bi2, &rd.dbl, &rd.va, &rd.chr, &rd.txt, &rd.blob, &rd.ts, &rd.dt, &rd.tim,
&rd.b, &rd.js,
)
if err != nil {
return TestRowDef{}, err
}
// JSON data returned by Postgres can contain spaces and other encoding so re-encode the returned string
// for comparison
rd.js, err = jsonReEncode(rd.js)
if err != nil {
return TestRowDef{}, err
}
// Done
return rd, nil
}
func readMultiTestRowDef(ctx context.Context, db *postgres.Database, compareRd []TestRowDef) ([]TestRowDef, error) {
// Populate ids
ids := make([]int, len(compareRd))
for idx := 0; idx < len(compareRd); idx++ {
ids[idx] = compareRd[idx].id
}
rd := make([]TestRowDef, 0)
err := db.QueryRows(ctx, `
SELECT
id, num, sm, bi, bi2, dbl, va, chr, txt, blob, ts, dt, tim, b, js
FROM
go_postgres_test_table
WHERE
id = ANY($1)
`, ids).Do(func(ctx context.Context, row postgres.Row) (bool, error) {
item := TestRowDef{}
err := row.Scan(&item.id, &item.num, &item.sm, &item.bi, &item.bi2, &item.dbl, &item.va, &item.chr, &item.txt,
&item.blob, &item.ts, &item.dt, &item.tim, &item.b, &item.js)
if err == nil {
rd = append(rd, item)
}
return true, err
})
if err != nil {
return nil, err
}
// JSON data returned by Postgres can contain spaces and other encoding so re-encode the returned string
// for comparison
for idx := range rd {
rd[idx].js, err = jsonReEncode(rd[idx].js)
if err != nil {
return nil, err
}
}
// Done
return rd, nil
}
func insertTestNullableRowDef(ctx context.Context, tx postgres.Tx, nrd TestNullableRowDef) error {
_, err := tx.Exec(ctx, `
INSERT INTO go_postgres_test_table (
id, num, sm, bi, bi2, dbl, va, chr, txt, blob, ts, dt, tim, b, js
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
)
`,
nrd.id, nrd.num, nrd.sm, nrd.bi, nrd.bi2, nrd.dbl, nrd.va, nrd.chr, nrd.txt, nrd.blob, nrd.ts, nrd.dt, nrd.tim,
nrd.b, nrd.js,
)
return err
}
func readTestNullableRowDef(ctx context.Context, db *postgres.Database, id int) (TestNullableRowDef, error) {
nrd := TestNullableRowDef{}
err := db.QueryRow(ctx, `
SELECT
id, num, sm, bi, bi2, dbl, va, chr, txt, blob, ts, dt, tim, b, js::text
FROM
go_postgres_test_table
WHERE
id = $1
`, id).Scan(
&nrd.id, &nrd.num, &nrd.sm, &nrd.bi, &nrd.bi2, &nrd.dbl, &nrd.va, &nrd.chr, &nrd.txt, &nrd.blob, &nrd.ts,
&nrd.dt, &nrd.tim, &nrd.b, &nrd.js,
)
if err != nil {
return TestNullableRowDef{}, err
}
// JSON data returned by Postgres can contain spaces and other encoding so re-encode the returned string
// for comparison
if nrd.js != nil {
var js string
js, err = jsonReEncode(*nrd.js)
if err != nil {
return TestNullableRowDef{}, err
}
nrd.js = &js
}
// Done
return nrd, nil
}